2014-07-16 2 views
0

이 함수는 사용자에게 검색 할 값을 묻고 목록에서 일치하는 값을 모두 삭제합니다.내 연결된 목록 "검색 및 삭제"기능을

프로그램이 완벽하게 실행되지만 '첫 번째 노드', '마지막 노드'및 '중간 노드'에 대한 사례를 구분하는 간단하고 우아한 방법을 이해할 수 없습니다.

좀 더 우아하고 읽기 쉽게 만들어주세요.

관련 코드 블록을 게시하고 전체 프로그램을 게시합니다.

관련 함수에서 다음 코드 블록은 내가 사용하고있는 switch-case 구문의 일부입니다 (사용자가 'v'를 입력하면 'value'를 검색하고 'delete' 첫 번째 노드 '또는' '마지막 노드를 삭제)

case 'v': 
      printf("\n\tEnter value to delete:\t"); 
      scanf("%d",&value); 
      r=q; 
      if(!q)//If linked list is empty 
      { 
       printf("\n\t\tLinked list is EMPTY\n"); 
       return; 
      } 
      else if(!(q->next))//If linked list consists of only one node 
      { 
       if((q->data)==value) 
       { 
        free(q); 
        printf("\nValue found and deleted in position 1\n"); 
        count=1; 
       } 
      } 
      else//If linked list consists of more than one node 
      { 
       for(i=1;q;i++) 
       { 
        if(q->data==value) 
        { 
         if(i==1)//for first node 
         { 
          q=q->next; 
          free(r); 
          *pp=q; 
          printf("\nValue found and deleted in position 1\n"); 
          count=1; 
         } 
         else//for the rest of the nodes 
         { 
          printf("\nValue found and deleted in position %d\n",i); 
          r->next=q->next; 
          temp=q; 
          q=q->next; 
          free(temp); 
          count++; 
         } 
        } 
        else 
        { 
         r=q; 
         q=q->next; 
        } 
       } 
      } 
      if(count==0)//If no matches are found 
       printf("Value not found"); 
      break; 

전체 프로그램 :

#include <stdio.h> 
#include <stdlib.h> 

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

int count(struct node *); 
void traverse(struct node *); 
void insert(struct node **); 
void delete(struct node **); 
void deletelist(struct node **); 

int main() 
{ char choice; 
    struct node *head, **tohead; 
    head=(struct node *)malloc (sizeof(struct node)); 
    head=NULL; 
    tohead=&head; 
    do 
    { 
     printf("\nChoose operation to perform:\n"); 
     printf("\tCount number of nodes(c):\n"); 
     printf("\tInsert a node(i)\n"); 
     printf("\tDelete a node(d)\n"); 
     printf("\tDelete the list(D)\n"); 
     printf("\tShow currenct linked list(s)\n"); 
     printf("\tQuit without saving changes(q):\t"); 
     scanf(" %c",&choice); 
     switch(choice) 
     { 
      case 'i': 
       insert(tohead); 
       break; 
      case 'c': 
       printf("\nList count is %d\n",count(head)); 
       break; 
      case 's': 
       traverse(head); 
       break; 
      case 'q': 
       printf("QUITTING\n"); 
       break; 
      case 'd': 
       delete(tohead); 
       break; 
      case 'D': 
       deletelist(tohead); 
       break; 
      default: 
       printf("\n\tINVALID CHOICE\n"); 
     } 
    }while(choice!='q'); 
} 

int count(struct node *p) 
{ 
    int i; 
    for(i=0;p;i++) 
     p=p->next; 
    return i; 
} 

void traverse(struct node *p) 
{ 
    printf("\nLinked list looks like: "); 

    if(!p) 
    { 
     printf("nothing. It's EMPTY"); 
    } 
    else 
    { while(p) 
     { 
      printf("%d\t",p->data); 
      p=p->next; 
     } 
    } 
    printf("\n"); 
} 

void insert(struct node **pp) 
{ 
    int value,position; 
    struct node *q; 
    q=*pp; 
    struct node *newnode=(struct node *)malloc(sizeof(struct node)); 
    printf("Insert what number?:\t"); 
    scanf("%d",&value); 
    printf("In what position? Push '0' for last,'1' for first"); 
    printf("\n\t\tOR\nenter position no.:\t"); 
    scanf("%d",&position); 
    newnode->data=value; 
    if(position==1) 
    { newnode->next=q; 
     *pp=newnode; 
    } 
    else if(position==0) 
    { 
     if(!q) 
     { 
      newnode->next=q; 
      *pp=newnode; 
     } 
     else 
     { 
      while(q->next) 
       q=q->next; 
      q->next=newnode; 
      newnode->next=NULL; 
     } 
    } 
    else if((position>1)&&(position<=count(q)+1)) 
    { 
     int i; 
     for(i=1;i<position-1;i++) 
      q=q->next; 
     newnode->next=q->next; 
     q->next=newnode; 
    } 
    else 
     printf("\n\t\tINVALID POSITION\n"); 
} 

void delete(struct node **pp) 
{ 
    struct node *q,*r,*temp; 
    q=*pp; 
    int i,count=0,value,position; 
    char choice; 
    printf("\n\tPush 'v' to delete a specific value"); 
    printf("\n\t\t\tOR"); 
    printf("\n\t'1' to delete the first value"); 
    printf("\n\t'0' to delete the last value:\t"); 
    scanf(" %c",&choice); 
    if(q==NULL) 
    { printf("List is EMPTY\n"); 
     return; 
    } 
    switch(choice) 
    { 
     case '1': 
      *pp=q->next; 
      free(q); 
      break; 
     case '0': 
      while(q->next!=NULL) 
      { 
       r=q; 
       q=q->next; 
      } 
      r->next=NULL; 
      free(q); 
      break; 
     case 'v': 
      printf("\n\tEnter value to delete:\t"); 
      scanf("%d",&value); 
      r=q; 
      if(!q)//If linked list is empty 
      { 
       printf("\n\t\tLinked list is EMPTY\n"); 
       return; 
      } 
      else if(!(q->next))//If linked list consists of only one node 
      { 
       if((q->data)==value) 
       { 
        free(q); 
        printf("\nValue found and deleted in position 1\n"); 
        count=1; 
       } 
      } 
      else//If linked list consists of more than one node 
      { 
       for(i=1;q;i++) 
       { 
        if(q->data==value) 
        { 
         if(i==1)//for first node 
         { 
          q=q->next; 
          free(r); 
          *pp=q; 
          printf("\nValue found and deleted in position 1\n"); 
          count=1; 
         } 
         else//for the rest of the nodes 
         { 
          printf("\nValue found and deleted in position %d\n",i); 
          r->next=q->next; 
          temp=q; 
          q=q->next; 
          free(temp); 
          count++; 
         } 
        } 
        else 
        { 
         r=q; 
         q=q->next; 
        } 
       } 
      } 
      if(count==0)//If no matches are found 
       printf("Value not found"); 
      break; 
     default: 
      printf("\nBad choice"); 
    }return; 
} 

void deletelist(struct node **pp) 
{ 
    struct node *p,*temp; 
    p=*pp; 
    while(p) 
    { 
     temp=p; 
     p=p->next; 
     free(temp); 
    } 
    *pp=NULL; 
    printf("\nLIST DELETED\n"); 
} 
이 질문은 것으로 보인다
+4

주제에서 벗어난가 [코드 검토 (에 요청해야하기 때문에 HTTP : //codereview.stackexchange.com/) –

+0

@ YuHao 어떻게 Code Review에 플래그를 지정합니까? – drum

+0

@drum 닫기 투표는 주제 외적인 사용자 정의 이유를 지원합니다. [그것에 액세스하려면 3K의 평판이 필요합니다.] (http://stackoverflow.com/help/privileges/close-questions). –

답변

0
void filter(struct node **pp, int value){ 
    struct node *curr, *prev=NULL, *tmp; 
    curr = *pp; 
    if(!curr) 
     return ; 
    tmp = curr; 
    while(tmp){ 
     if(tmp->data != value){ 
      curr->data = tmp->data; 
      prev = curr; 
      curr = curr->next; 
     } 
     tmp = tmp->next; 
    } 
    deletelist(&curr); 
    if(prev){ 
     prev->next = NULL; 
    } else { 
     *pp = NULL; 
    } 
}