简体   繁体   English

删除链表中的随机节点

[英]deletion of random node in a linked list

I wrote this code to delete any item from the linked list, 我编写这段代码是为了删除链接列表中的任何项目,

this is the output after insertion
 16 -->  15 -->  14 -->  13 -->  12 -->  11 --> 

when i delete 11 the output is like this
16 -->  15 -->  14 -->  13 -->  12 -->  0 --> 

how to remove that zero at the last? 最后如何删除该零?

typedef struct node
{
int data;
struct node *next;
}node;

node *getnode()
{
node *x;
x = (node*)malloc(sizeof(node));
if (x==NULL)
    {
        printf("no memory \n");
        exit(1);
    }
return x;
}

node *insert_front(int item , node *first)
{

node *temp;
temp = first;

temp = getnode();
temp -> data = item;
temp -> next = first;
return temp;
}

void *display(node *first)
{
node *temp;
temp = first;
if (temp == NULL)
    {
        printf("list is empty \n");
        return ;
    }
while (temp != NULL)

    {
        printf(" %d --> ",temp -> data);
        temp = temp -> next;

    }
}


void delete_middle(int item, node *first)
{
node *temp;
node *store_addr;
int value;
temp = first;
if (temp == NULL)
{
   printf("list is empty \n");
   return ;
}

while(temp!=NULL)
{
if (temp->data == item)
{
    if(temp->next != NULL)
    {
    temp->data =  temp->next->data;
            temp->next = temp->next->next;
    }
    else
     free(temp); 
    break;
}
temp = temp->next;
}


}


main()
{

node *first;
int item = 11,ch,i;
first = NULL;
while(1)
{
    printf("\n 1.insert front \n 2.display \n 3. delete middle \n 4.quit\n");
            scanf ("%d",&ch);
    switch(ch)
    {

    case 1:  // printf("\nenter the item to be inserted \n");
          //scanf("%d",&item);
           for(i=0 ;i<6;i++)
          first = insert_front (item++ , first);
          break;

    case 2:   display(first);
          break;


    case 3 :  printf("delete in middle \n");
          scanf("%d",&item);
          delete_middle(item, first);
          break;

    case 4:   exit(0);
          break;
}
}
}

While deleting, you must store the previous node in the list and modify it. 删除时,必须将前一个节点存储在列表中并对其进行修改。 For example, you can handle the first node separately and loop like this 例如,您可以单独处理第一个节点并像这样循环

struct node* tmp;    
while(temp->next!=NULL)
{
    if (temp->next->data == item)
    {
        tmp = temp->next->next;
        free(temp->next); 
        temp->next = tmp;
    }
    temp = temp->next;
}

In your code, you free(temp) if it is the last one, but you don't remove it from the list. 在您的代码中,如果它是最后一个,则将其释放(临时),但不要将其从列表中删除。 This makes the list corrupted and can lead to many bad behaviour of your software. 这会使列表损坏,并可能导致软件的许多不良行为。 NEVER keep a reference to a freed pointer. 永远不要保留对释放指针的引用。

In your code try this, 在您的代码中尝试此操作,

while(temp!=NULL)
{
   if (temp->data == item)
   {
        if(temp->next != NULL)
        {
            prev->next = temp->next;
        }

        free(temp);
        break; 
   } 
   prev = temp;
   temp = temp->next; 
 }  

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

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