简体   繁体   English

链表程序,用于插入和删除节点

[英]linked list program to insert and delete nodes

#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.exit\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);
}

This is a program written for single linked list and while executing getting the error segmentation fault. 这是为单个链表编写的程序,并且在执行时遇到错误分段错误。 please suggest idea to solve this. 请提出解决方案。 I am getting the following output: 我得到以下输出:

./out

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

enter option
1
enter  the data
23
Segmentation fault
temp=first; /* first is null */
while(temp->next!=first)
   temp=temp->next;

... so you are trying to access to NULL->next , obviously you get a SEG FAULT, use a debugger: ...因此,您尝试访问NULL->next ,显然您遇到了SEG FAULT,请使用调试器:

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

insert contains the code insert包含代码

temp=first;
while(temp->next!=first)

(where first is a global variable which is initially NULL ). (其中first是一个全局变量,最初是NULL )。 The first time this is run, the while loop immediately dereferences a NULL pointer. 第一次运行时, while循环立即取消引用NULL指针。 This has undefined consequences but a segfault isn't surprising. 这带来了不确定的后果,但段错误不足为奇。

To fix this, you could change insert to look like 要解决此问题,您可以将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;
    }
}

Note that this includes a few different changes 请注意,这包括一些不同的更改

  • Need to treat first==NULL as a special case 需要将first==NULL视为特例
  • Need to set nn->next = NULL in all cases 在所有情况下都需要设置nn->next = NULL
  • You shouldn't cast the return from malloc in C 您不应该在C中malloc的返回值
  • allocating sizeof(*nn)) is slightly more future proof (it'll still work if you later change the type of nn ) 分配sizeof(*nn))稍有前途(如果以后更改nn的类型,它将仍然有效)
  • malloc may return NULL in low memory malloc可能在内存不足时返回NULL

In your first Insert the variable first is NULL ... 在您的第一个Insert变量中, firstNULL ...

You need to check if the variable is null, the loop is not executed. 您需要检查变量是否为null,是否不执行循环。

Change your insert code to: 将您的插入代码更改为:

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;
}

Your insert() functions contans temp=first; 您的insert()函数包含temp=first; But first is null and you are doing as temp->next!=first which is null->next!=first which is not correct. 但是first是null,您正在执行temp->next!=first ,这是null->next!=first ,这是不正确的。 To solve this check if first is null or not , 要解决此检查first是否为null,

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;
}   

use 采用

while(temp!=NULL)

in place of 代替

while(temp->next!=NULL)
    // 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. 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM