2011-12-26 5 views
0

나는 엔티티 중 검색 할 수있는 전화 번호부를 작성했습니다. 이제는 삭제 기능을 작성했습니다. 하지만 검색을 호출하고 삭제하는 방법은 좋은 방법으로 사용하는 방법을 모르겠습니다. 검색 기능에 플래그를 지정해야합니까? 나는 사용자가 어떤 번호, 이메일을 입력하면 원하는 ... 내가 링크 된 목록이 비어 있지 처음으로 가정 삭제가 올바른지 내 기능을 알거나하지 하지 않는 을 삭제가 존재하는 경우linkedlist에서 노드 삭제

#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 person\n****"); 
       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 later\n"); 
} 
//************************************************************************* 

int main() 
{ 
    printf("please choose your option:\n"); 
printf("1.search\n2.add\n3.delete\n4.Sort\n5.Edit\n6.show the list\n7.Exit\n"); 

    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("SEARCH\n"); 
    printf("in which group you want to search?\n"); 
printf("1.Number\n2.FirstName\n3.LastName\n4.FatherName\n5.Email\n6.Site\n"); 
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; 
} 
+0

게시물 당 하나의 질문 만 다른 게시물에 두 번째 질문 (목록의 노드 정렬)을 게시하고 첫 번째 게시물을 편집하지 마십시오 (목록에서 노드 삭제)! –

+0

-1 같은 질문에 대한 기존 게시물 사용 중! –

+0

새 질문이있는 경우 * 새 질문으로 * 질문 *을 편집하지 말고 * 새 질문을 추가하십시오. 이렇게하면 이전에 게시 된 모든 대답이 무효화되어 전체 내용이 무의미 해집니다. 나중에 다시 사용하면 운영자가 추가로 행동하게 될 수 있습니다. – casperOne

답변

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로 설정해야합니다. 프로그램에서 버그를 찾는 것이 좋습니다.

0

this을 참조하십시오. 링크 된 목록의 항목 삭제에 대한 설명입니다.

기본적으로 연결된 목록에서 노드를 삭제할 때 다음 시나리오를 고려해야합니다!

(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..