2011-03-31 5 views
0

많은 기능을 가진 프로그램을 작성했지만 링크 된 목록의 2 노드 2 개 요소를 바꿀 수는 없습니다. 실제로 링크를 변경하여 노드 2 개를 바꿀 수는 있지만 두 개의 요소를 바꿀 수는 없습니다. 사용자가 2 요소 교환을 요청했습니다. 여기에 스왑 연산이없는 코드가 있습니다. 노드 링크를 변경하지 않고 2 노드 요소를 교체하여이 스왑 연산을 수행하고 싶습니다.이 문제를 제거하려면 어떻게해야합니까? 감사하겠습니다.LinkedList 요소 스왑 문제 C

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

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

void insert(int ,struct node **); 
void display(struct node *); 
void search(int, struct node *); 
void delete(int, struct node **); 
int main(void){ 

    nodetype *p; 
    p=NULL; 
    int x=0,choice; 
    int s_no,k,r_no; 


    while(x!=1){ 
     printf("enter 1 for insert\n"); 
     printf("enter 2 for display\n"); 
     printf("enter 3 for search\n"); 
     printf("enter 4 for delete\n"); 

     printf("enter 0 for exit\n"); 
     fflush(stdout); 
     scanf("%d",&choice); 
     if(choice==1){ 
      printf("enter inserted no\n"); 
      fflush(stdout); 
      scanf("%d",&k); 
      insert(k,&p); 
     } 

     else if(choice==2) 
      display(p); 
     else if(choice==3){ 
      printf("enter searched no\n"); 
      scanf("%d",&s_no); 
      search(s_no, p); 
     } 
     else if(choice==4){ 
      printf("enter deleted no\n"); 
      scanf("%d",&r_no); 
      delete(r_no,&p); 
       } 
     else 
      printf("invalid choice\n"); 
    } 
    return 0; 
} 
void display (struct node *p) 
{ 
    printf("the content is:\n"); 
    if(p==NULL) 
     printf("the link is empty\n"); 
    while (p != NULL) 
    { 
     printf ("%d ", p -> data) ; 
     p = p -> next ; 
    } 
printf ("\n") ; 
} 

void search(int no, struct node *p){ 
    nodetype * loc; 
    for(loc=p;loc!=NULL;loc=loc->next) 
    { 
    if(no==loc->data){ 
     printf("\nthe number exist in the list\n"); 
     return; 
    } 

    } 
    printf("\nthe number is not exist in the \n"); 
} 
void insert(int x,struct node **p) 
{ 
    struct node *r,*temp=*p; 
    r = (struct node *)malloc (sizeof (struct node)) ; 
    r ->data = x ; 
    r->next=NULL; 
    if (*p == NULL) 
    { 
     *p = r ; 
    } 
    else 
    { 
     while(temp->next!= NULL) 
     { 
        temp=temp->next; 
     } 

    temp->next=r; 
    } 
} 
void delete(int num, struct node **p){ 
    struct node *temp,*x; 
    temp=*p; 
    x= NULL; 
    while (temp->next !=NULL){ 
    if(temp->data == num) 
    { 
     if (x==NULL) 
     { 
     *p = temp->next; 
     free(temp); 
     return; 
     } 
     else 
     { 
     x->next = temp->next; 
     free(temp); 
     return; 
     } 
    } 
    x=temp; 
    temp=temp->next; 
    } 
    printf(" No such entry to delete "); 
} 
+1

"다음은 스왑 작업이없는 코드입니다."- 스왑 작업을 위해 시도한 작업은 무엇입니까? 이를 출발점으로 사용하는 것이 좋습니다! – corsiKa

+2

이 숙제가 있습니까? 적절한 경우 태그를 지정하십시오. – jv42

+0

문제를 간단하게 재현하려면 코드를 자르십시오. – pajton

답변

1

나는, numbers.For 인스턴스를 입력 사용자를 사용하여 교체 작업을 시도 내가 그렇게 할 수있는 사용자는 linkedlist.How에 스왑하기 위해 12, 45을 입력?

나는 당신이 사용자가 입력 한 두 값에 대한 검색을 부르는 그 후 (단지 아이디어를 테스트) 반환 값,

nodetype * search(int no, struct node *p){ 
    nodetype * loc; 
    for(loc=p;loc!=NULL;loc=loc->next) 
    { 
    if(no==loc->data){ 
     printf("\nthe number exist in the list\n"); 
     return loc; 
    } 

    } 
    printf("\nthe number is not exist in the \n"); 
    return NULL; 
} 

으로 기존의 기능을 확장 건의 할 것입니다. 그런 다음 새 값을 지정하기 만하면 포인터 참조를 변경하지 않고 값을 바꿀 수 있습니다. 물론 노드가 실제로 발견되었는지 (NULL이 아님) 확인해야합니다.

nodetype *node1; 
nodetype *node2; 

node1 = search(userInput1, &p); 
node2 = search(userInput2, &p); 

int tmp_data = node1->data; 
node1->data = node2->data; 
node2->data = tmp;