为什么我的程序突然终止?

时间:2013-12-30 18:53:05

标签: c data-structures linked-list singly-linked-list

我有一个用于创建单链表的程序,在列表的开头和结尾添加一个节点。程序正在产生预期的输出,但在产生最后一个输出后它会崩溃。我在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",&current->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",&current->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);

  }

}

为什么会这样?

3 个答案:

答案 0 :(得分:2)

代码遗漏给#include <stdlib.h>。至少在64位系统上调用malloc()最有可能是致命的。

如果在64位系统上并且缺少malloc()的原型,则编译器会将int视为malloc()的返回值。 int肯定是32位,其中地址malloc()尝试返回的是64位宽,因此返回的地址值很可能会在返回时被切断一半。


也是这一行

void addnode_end(struct node *first)

应该是

void addnode_end(struct book *first)

<强>更新

此外,代码错过了初始化新malloc()个节点的成员。通常,其next成员应明确设置为NULL

答案 1 :(得分:2)

addnodebeginning函数中,first参数按值传递,这意味着值已复制,您可以修改副本功能。如果要修改传递的值,则必须通过引用传递

这是通过传递指向值的指针来完成的,在这种情况下是指向指针的指针。

答案 2 :(得分:1)

首先,请注意警告。他们是有原因的。其次,您只需在 if test-

的末尾包含此内容
newnode->next=NULL;
 break;

您的程序将没有运行时崩溃。