【发布时间】:2013-12-30 18:53:04
【问题描述】:
我有一个创建单链表的程序,在列表的开头和结尾添加一个节点。该程序正在产生预期的输出,但在产生最后一个输出后崩溃。我在 main() 中创建了链表,我有 2 个函数分别在开头和结尾添加一个节点。
#include<stdio.h>
struct book
{
char bname[20];
char aname[20];
int pages;
struct book *next;
};
struct book *first;
struct book *current;
struct book *previous;
int main()
{
int count=0,temp=0;
for(int i=1;i<4;i++)
{
current=(struct book*)malloc(sizeof(struct book));//memory assigned to only the current structure
if(current==NULL)
{
break;
}
if(first==NULL)
{
first=current; //if the first node value is null then it hadn't yet been processed,so current node is now the first;.
}
if(previous!=NULL)
{
previous->next=current;//stores the current structure address to the next member(pointer) of the previous structure address
}
printf("\nBook Name:: ");
scanf("%s",current->bname);
printf("\nAuthor Name:: ");
scanf("%s",current->aname);
printf("\nPages::");
scanf("%d",¤t->pages);
current->next=NULL;//if this is the last node
//will again be filled up if there is a next structure
previous=current;//the current node is the previous node for the next iteration
}
current=first;
while(current!=NULL)
{
count++;
printf("\nBook Name:: %s Author Name:: %s Pages:: %d",current->bname,current->aname,current->pages);
previous=current;//store to previous only because of freeing the current structure
current=current->next;//next structure address of the current node is the current node of the next iteration.Now if this next address doesn't exist,then the current pointer does not exist and the loop terminates
}
addnodebeginning(first);
}
/*****************************inserting at the BEGINNING*********************************/
void addnodebeginning(struct book *first)
{
int count=0,temp=0;
current=(struct book*)malloc(sizeof(struct book));
current->next=first;
first=current;
printf("\nBook Name:: ");
scanf("%s",current->bname);
printf("\nAuthor Name:: ");
scanf("%s",current->aname);
printf("\nPages::");
scanf("%d",¤t->pages);
current=first;
while(current!=NULL)
{
count++;
printf("\nBook Name:: %s Author Name:: %s Pages:: %d",current->bname,current->aname,current->pages);
previous=current;//store to previous only because of freeing the current structure
current=current->next;//next structure address of the current node is the current node of the next iteration.Now if this next address doesn't exist,then the current pointer does not exist and the loop terminates
}
addnode_end(first);
}
/*****************************inserting at the end*********************************/
void addnode_end(struct node *first)
{
current=first;
while(1)
{
if(current->next==NULL)
{
struct book *newnode=(struct book*)malloc(sizeof(struct book));
printf("\nBook Name:: ");
scanf("%s",newnode->bname);
printf("\nAuthor Name:: ");
scanf("%s",newnode->aname);
printf("\nPages::");
scanf("%d",&newnode->pages);
current->next=newnode;
//newnode->next=NULL;
break;
}
current=current->next;
}
current=first;
while(current!=NULL)
{
printf("\nBook Name:: %s Author Name:: %s Pages:: %d",current->bname,current->aname,current->pages);
previous=current;//store to previous only because of freeing the current structure
current=current->next;
free(previous);
}
}
为什么会这样?
【问题讨论】:
-
我想知道这个编译器是否正确!
-
你的函数原型丢失了。
-
是的,我可以向你保证它编译得非常好!已经测试了不止一次。
-
@Digital_Reality 它在 gcc 上以 c99 模式编译,但有 17 个警告(有些严重)
标签: c data-structures linked-list singly-linked-list