2013-06-06 47 views
1
#include<stdio.h> 
#include<stdlib.h> 

struct node 
{ 
    int data; 
    struct node *next; 
}*first=NULL; 

void insert() 
{ 
struct node *temp; 
struct node *nn=(struct node*)malloc(sizeof(struct node)); 
    printf("enter the data\n"); 
    scanf("%d",&nn->data); 
    temp=first; 
    while(temp->next!=first) 
    temp=temp->next; 
    temp->next=nn; 
    nn->next=NULL; 
} 

void display() 
{ 
    struct node *temp; 
    temp=first; 
    if(temp==NULL) 
    { 
    printf("no elements\n"); 
    return; 
    } 
    printf("elements in linked list are\n"); 
    while(temp!=NULL) 
    { 
    printf("%d\n",temp->data); 
    temp=temp->next; 
    } 
} 

void deletion() 
{ 
    struct node *temp; 
    temp=first; 
    first=first->next; 
    temp->next=NULL; 
    free(temp); 
} 

int main() 
{ 
    int op; 
    do 
    { 
    printf("1.insertion\n2.deletion\n3.display\n4.exi\n"); 
    printf("enter option\n"); 
    scanf("%d",&op); 
    switch(op) 
    { 
     case 1:insert(); 
      break; 
     case 2:deletion(); 
      break; 
     case 3:display(); 
      break; 
    } 
    }while(op!=6); 
} 

这是一个为单链表编写的程序。并执行获取错误分段错误。请建议解决这个问题。 我得到下面的输出 ./out链接列表程序插入和删除节点

1.insertion 
2.deletion 
3.display 
4.exit 

输入选项

1 

输入数据

23 

Segmentation fault 
+0

参考http://tinyit.cc/letslearncs/插入链接列表/在链接列表中插入节点,以及http://tinyit.cc/letslearncs/deletion-in-linked-list在链接中删除名单。 – 2014-06-27 18:16:08

回答

1

insert包含代码

​​3210

(其中first是一个全局变量,最初为NULL)。第一次运行时,while循环立即取消引用NULL指针。这具有未定义的后果,但段错误并不奇怪。

为了解决这个问题,你可以改变insert看起来像

void insert() 
{ 
    struct node *nn=malloc(sizeof(*nn)); 
    if (nn == NULL) { 
     /* handle oom */ 
    } 
    printf("enter the data\n"); 
    scanf("%d",&nn->data); 
    nn->next=NULL; 
    if (first == NULL) { 
     first = nn; 
    } 
    else { 
     struct node *temp=first; 
     while(temp->next!=NULL) 
      temp=temp->next; 
     temp->next=nn; 
    } 
} 

注意,这包括一些不同的变化

  • 需要把first==NULL作为特殊情况
  • 需要设置nn->next = NULL在所有情况下
  • 您不应该从malloc在C
  • 分配sizeof(*nn))略微更长远的保障(它仍然会工作,如果你以后更改的nn类型)
  • malloc可以在低内存
1

返回NULL在你的第一个Insert变量firstNULL ...

您需要检查变量是否为空,循环未执行。

更改您的插入代码:

void insert() 
{ 
struct node *temp; 
struct node *nn=(struct node*)malloc(sizeof(struct node)); 
    printf("enter the data\n"); 
    scanf("%d",&nn->data); 
    if(first!=NULL){ 
     temp=first; 
     while(temp->next!=first) 
     temp=temp->next; 
     temp->next=nn; 
    }else{ 
     first=nn; 
    } 
    nn->next=NULL; 
} 
2
temp=first; /* first is null */ 
while(temp->next!=first) 
    temp=temp->next; 

...所以你试图访问NULL->next,显然你会得到一个赛格故障,使用调试器:

Program received signal SIGSEGV, Segmentation fault. 
0x00000000004006af in insert() at demo.c:17 
17 while(temp->next!=first) 
1

您的insert()函数包含temp=first;first为空,您正在作为temp->next!=first这是null->next!=first这是不正确的。 为了解决这个检查,如果first为空或不是,

struct node *temp; 
struct node *nn=(struct node*)malloc(sizeof(struct node)); 
printf("enter the data\n"); 
scanf("%d",&nn->data); 
nn->next=null; 
if(first==null) 
{ 
first=nn; 
} 
else 
{ 
temp=first; 
    while(temp->next!=null) 
    temp=temp->next; 
    temp->next=nn; 
} 
1

使用

while(temp!=NULL) 

代替

while(temp->next!=NULL) 
0
// insert element using recursion 

Node *insetlinklist(Node *head,Node*mhead,int data,int position) 
    { 
    Node *newNode=NULL; 
    // printf("%d,%d %d %x\n",position,mhead->data,data,mhead); 
    if(position==0 && mhead->next==NULL && head->next==NULL){ 
     newNode = (Node*)malloc(sizeof(Node)); 
     newNode->next=NULL; 
     newNode->data=data; 
     newNode->next=head; 
     head=newNode; 
     return head; 

    } 
    if(position ==0){ 
     newNode = (Node*)malloc(sizeof(Node)); 
     newNode->next=NULL; 
     newNode->data=data; 
     newNode->next=head->next; 
     head->next =newNode; 
     return head; 
    } 

    if(mhead->next!=NULL){ 
     head =mhead; 
     insetlinklist(head,mhead->next,data,position-1); 
    } 
    else 
     insetlinklist(head,mhead,data,0); 
    // printf("%d,%d\n",position,head->data); 
    return head; 

} 
void printL(Node*head){ 
    Node *temp =head; 
    while(temp!=NULL){ 
     printf("Data =%d\n",temp->data); 
     temp = temp->next; 
    } 
} 
Node* InsertNth(Node *head, int data, int position) 
{ 
    Node *temp =head; 
// printL(head); 
    head= insetlinklist(head,temp,data,position); 
    // printL(head); 
    // printf("END\n"); 
    return head; 
    // Complete this method only 
    // Do not write main function. 
}