C语言 删除链接列表中的节点



我写了一个电话簿,它可以在实体之间搜索,现在我写了一个删除函数但我不知道如何使用它,如果我调用搜索,然后删除它?我应该给搜索功能的标志?我想当用户输入任何号码,电子邮件,…如果存在,则删除我不知道我在delete中的函数是否正确假设链表第一次不是空的

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
struct node {
char Number[10];
char FirstName[10];
char LastName[10];
char FatherName[10]; 
char Email[20];
char SiteName[30];
struct node *next;
struct node *pre;
};
void print(struct node* list)
{
    printf("FIRSTNAME: "); 
       printf(list->FirstName);
    printf("n");
    printf("LASTNAME: ");      
    printf(list->LastName);
    printf("n");
    printf("FATHERNAME: ");    
    printf(list->FatherName);
    printf("n");
    printf("EMAIL: ");      
    printf(list->Email);
    printf("n");
    printf("SITENAME: ");   
    printf(list->SiteName);
    printf("n");
    printf("NUMBER: ");   
    printf(list->Number);
}
void search(struct node* list,char* sr,int option)
{
   struct node *current =list;
    current=current->next;
    switch(option)
    {
    case 1:         
        while(current!=0)
        {       int flag=strcmp(current->Number,sr);
        if(flag==0)
        {           printf("you searched This person!n");
                    print(current);
                    current=current->next;
        }
    else{   
            current=current->next;
                }
        }           
        break;
case 2:
while(current!=0)
        {
        int flag=strcmp(current->FirstName,sr);
        if(flag==0)
        {           printf("you searched This person!n");
                    print(current);
                    current=current->next;
        }
    else{   
            current=current->next;
                }
        }                       break;
    case 3:
while(current!=0)
        {
        int flag=strcmp(current->LastName,sr);
        if(flag==0)
        {           printf("you searched This person!n");
                    print(current);
                    current=current->next;
        }
    else{   
            current=current->next;
                }
        }           
                break;
    case 4:
while(current!=0)
        {
        int flag=strcmp(current->FatherName,sr);
if(flag==0)
        {           printf("you searched This person!n");
                    print(current);
                    current=current->next;
        }
    else{   
            current=current->next;
                }
        }                   
                break;
    case 5:
while(current!=0)
        {
        int flag=strcmp(current->Email,sr);
    if(flag==0)
        {           printf("you searched This person!n");
                    print(current);
                    current=current->next;
        }
    else{   
            current=current->next;
                }
        }           
                break;
    case 6: 
while(current!=0)
        {
        int flag=strcmp(current->SiteName,sr);
    if(flag==0)
        {           printf("you searched This person!n");
                    print(current);
                    current=current->next;
        }
    else{   
            current=current->next;
                }
        }           
                break;
    }

}
void deleteNode(struct node* node)
{
    if(node->next==0 && node->pre==0)//we have just 1 node
    {
    free(node);
    }
    if(node->next!=0 && node->pre!=0)//node is among nodes
      {
    struct node * temp1 = node->next;
    struct node * temp2 = node->prev;
    temp2->next=node->next;
    temp1->pre=temp2;
        free(node);
    }
    if(node->next!=0 && node->pre==0)//begining of the list
    {
    struct node * temp1 = node->next;
    temp1->pre=0;
     free(node);
    } 
    if(node->next=0 && node->pre!=0)//end of the list
    {
    struct node* temp2=node->pre;
        temp2->next=0;
        free(node);
    } 
}
void addNode(struct node *head)
{
        struct node *current = head;
struct  node *newNode = malloc(sizeof(struct node));
puts("*******Now you can insert a new personn****"); 
                printf("FIRSTNAME: ");     
                gets(newNode->FirstName);
                printf("LASTNAME: ");      
                gets(newNode->LastName);
               printf("FATHERNAME: ");    
                gets(newNode->FatherName);
               printf("EMAIL: ");      
                gets(newNode->Email);
               printf("SITENAME: ");   
              gets(newNode->SiteName);
             printf("NUMBER: ");   
              gets(newNode->Number);
              //create new node
           newNode->next = 0;  // Change 1
 if(current->next==0)
{
current->next=newNode;
newNode->pre=current;
    }        
else    {
        while (current->next != 0)
        {
        current = current->next;
        }
    current->next = newNode;    
    newNode->pre=current;
    }
     //      printf("added latern");
 }
//*************************************************************************
int main()
{
    printf("please choose your option:n");
printf("1.searchn2.addn3.deleten4.Sortn5.Editn6.show the listn7.Exitn");
    struct node *root;   
    struct node *conductor;
    root = malloc( sizeof(struct node) );
        root->next = 0;   
      //  addNode(root);
//  char c=getchar();
        //*********************************
     strcpy(root->FirstName, "root");
      strcpy(root->LastName, "last");   
    //    print(root);    
addNode(root);
addNode(root);
printf("SEARCHn");
    printf("in which group you want to search?n");
printf("1.Numbern2.FirstNamen3.LastNamen4.FatherNamen5.Emailn6.Siten");
int num;
scanf("%d", &num);
    switch(num)
    {
      char s[20];
    case 1:             
    printf("please enter your Number to search!");
    scanf("%s",s);
    search(root,s,num);
    break;
    case 2:
    printf("please enter your FirstName to search!n");
scanf("%s", s);
    search(root,s,num);
    break;
    case 3:
    printf("please enter your LastName to search!n");
    scanf("%s", s);
    search(root,s,num);
    break;
    case 4: 
      printf("please enter your FatherName to search!n");
    scanf("%s", s);
    search(root,s,num);
       break;
    case 5: 
       printf("please enter your Email to search!n");
       scanf("%s", s);
    search(root,s,num);
      break;
    case 6:
    printf("please enter your Site to search!n");
    scanf("%s", s);
    search(root,s,num);
    break;  
        }
    return 0;
}
    case 3:
    while(current!=0)
    {
      int flag=strcmp(current->LastName,sr);
      if(flag==0)
      {           
         printf("you searched This person!n");
         print(current);
         deletenode(current)
      }
      else{   
        current=current->next;
      }
     }           
     break;

您可能想要更改deletenode函数。我认为你不需要满足四个条件。你可以传递节点并删除它。而不是0,你应该让它为NULL。最好是找到程序中的错误

请看这个。这解释了链表中的delete项。

基本上,在删除链表中的节点时,您应该考虑以下场景!

(1) deleting head 
    delete element and adjust head pointer
(2) deleting tail
    delete element and adjust tail pointer
(3) one element list
    if data is the data for the only element
    then delete the list and set head and tail
    pointers to NULL
(4) all the other cases
    traverse through the list and hold the
    previous pointer. delete element and adjust
    the next pointers. 
(5) if not the above cases, then element not
    present.. do nothing..

相关内容

  • 没有找到相关文章

最新更新