2013-11-02 201 views
1

以下代碼是c中的單個鏈接列表實現。每次調用函數addtoqueue時,都會創建一個節點並將該節點追加到列表的末尾。指針列表指向鏈表的第一個節點,但每次我使用輸入更新節點的值(這些值都是從客戶端連接讀取的)時,鏈表中的所有前面的節點都會獲取輸入的最後一個文件名。即:C中的鏈接列表實現

第一個節點創建後(abc.txt作爲輸入):鏈表具有一個值爲abc.txt的節點;

第2節點後(xyz.txt將該輸入):鏈表具有相同的文件名xyz.txt將該兩個節點(而不是與ABC一個節點和XYZ一個節點)

有我下面的實施,邏輯失敗在哪裏結束?

struct listdata 
{ 
    char *filename; 
    struct listdata *next; 
}*list;       

void addtoqueue(int client,char *value) 
{ 
    char buffer[512]; 
    char filepath[100]; 
    struct listdata *temp,*input; 

    input=(struct listdata *)malloc(sizeof(struct listdata)); 

    read(client,buffer,sizeof(buffer)); 
    d = sscanf(buffer,"%s",filepath); 

    input->filename=&filepath; 

    if(list == NULL) 
    { 
     list=input; 
     list->next=NULL; 
    } 
    else if((list->next)==NULL) 
    { 
     list->next=input; 
     input->next=NULL; 
    } 
    else 
    { 
     temp=list->next; 
     while((temp->next)!=NULL) 
     { 
     temp=temp->next; 
     } 
     temp->next=input; 
     input->next=NULL; 
    } 
    //list points to the first node 
} 

回答

1

這是簡單

void addtoqueue(int client,char *value) 
{ 
    char buffer[512]; 

    char filepath[100]; 

    struct listdata *temp=NULL,*input=NULL; 

    input=(struct listdata *)malloc(sizeof(struct listdata)); 

    read(client,buffer,sizeof(buffer)); 
    d=sscanf(buffer,"%s",filepath); 

    input->filename=&filepath; 
    input->next = NULL; 

    if(list == NULL) 
    { 
    list=input; 
    } 
    else 
    { 
    temp=list; 

    while(temp->next != NULL) 
    { 
     temp=temp->next; 
    } 
    temp->next=input; 
    } 
} 
-1

這裏是鏈表的完整代碼.. 這一定會幫助你
訪問http://codingloverlavi.blogspot.in/2013/12/singly-linked-list.html更多細節

//Single LinkList 
#include<stdio.h> 
#include<stdlib.h> 

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

typedef struct Node Node; 
Node*sort2(Node*,Node**); 
Node*sort(Node*start,Node**); 
Node*reverseList(Node*,Node**); 
void insertAfter(Node*,Node**,int,int); 
Node* insertBefore(Node*,int,int); 
void printList(Node*); 
Node*insertLast(Node*,Node**,Node*); 
Node*createNode(int); 
int search(Node*,int); 
Node*Delete(Node*,Node**,int); 
Node*deleteLast(Node*,Node**); 

int main() 
{ 
    int temp,ch,num; 
    Node *newNode,*start,*last; 
    start=last=NULL; 
    while(1) 
    { 
     printf("\n_________________menu__________________\n"); 
     printf("1. insert at the end of list...\n"); 
     printf("2. print the list...\n"); 
     printf("3. search a speific item...\n"); 
     printf("4. insert after a specific node...\n"); 
     printf("5. insert a node before a specific node...\n"); 
     printf("6. delete a specific node...\n"); 
     printf("7. delete a last node...\n"); 
     printf("8. reverse the link list...\n"); 
     printf("9. sort the link list...\n"); 
     printf("10. sort the link list(another method)...\n"); 
     printf("11. exit...\n"); 

     printf("\nenter your choice : "); 
     scanf("%d",&ch); 

     switch(ch) 
     { 
      case 1: 
       printf("enter the data part of the node that you want to insert :\n"); 
       scanf("%d",&temp); 
       newNode=createNode(temp); 
       start=insertLast(start,&last,newNode); 
       break; 
      case 2: 
       printList(start); 
       break; 
      case 3: 
       printf("enter the data that you want to search : "); 
       scanf("%d",&temp); 
       if(search(start,temp)) 
        printf("the number you entered was in the list"); 
       else 
        printf("the number you entered was not in the list"); 
       break; 
      case 4: 
       printf("enter the data of the node after which you want to insert new node : "); 
       scanf("%d",&temp); 
       printf("enter the data part of the node that you want to insert : "); 
       scanf("%d",&num); 
       insertAfter(start,&last,temp,num); 
       break; 
      case 5: 
       printf("enter the data of the node before which you want to insert new node : "); 
       scanf("%d",&temp); 
       printf("enter the data part of the node that you want to insert : "); 
       scanf("%d",&num); 
       start=insertBefore(start,temp,num); 
       break; 
      case 6: 
       printf("enter the data part of the node that you want to delete : "); 
       scanf("%d",&temp); 
       start=Delete(start,&last,temp); 
       break; 
      case 7: 
       if(last==NULL) 
       { 
        printf("the list is empty...you can't delete any node..."); 
        break; 
       } 
       start=deleteLast(start,&last); 
       break; 
      case 8: 
       start=reverseList(start,&last); 
       break; 
      case 9: 
       start=sort(start,&last); 
       break; 
      case 10: 
       start=sort2(start,&last); 
       break; 
      case 11: 
       exit(1); 
      default: 
       printf("you have entered a wrong choice...enter a valid choice"); 
     } 
    } 
} 

Node* createNode(int data) 
{ 
    Node*newNode; 
    newNode=(Node*)malloc(sizeof(Node)); 
    newNode->data=data; 
    newNode->next=NULL; 
    return newNode; 
} 

Node*insertLast(Node*start,Node**p2last,Node*newNode) 
{ 
    if(*p2last==NULL) 
    { 
     *p2last=newNode; 
     return newNode; 
    } 
    (*p2last)->next=newNode; 
    *p2last=newNode; 
    return start; 
} 

void printList(Node*start) 
{ 
    printf("your list is as follows : \n"); 
    while(start) 
    { 
     printf("%d\t",start->data); 
     start=start->next; 
    } 
} 

int search(Node*start,int data) 
{ 
    while(start) 
    { 
     if(start->data==data) 
      return 1; 
     start=start->next; 
    } 
    return 0; 
} 

void insertAfter(Node*start,Node**p2last,int data,int num) 
{ 
    Node*newNode,*tmp; 
    newNode=createNode(num); 
    if(*p2last==NULL) 
     printf("the list is empty..."); 
    tmp=start; 
    while(tmp) 
    { 
     if(tmp->data==data) 
      break; 
     tmp=tmp->next; 
    } 
    if(!tmp) 
     printf("the number you enter was not in the list\n"); 
    else 
    { 
     newNode->next=tmp->next; 
     tmp->next=newNode; 
     if(tmp==*p2last) 
      *p2last=newNode; 
    } 
} 

Node*insertBefore(Node*start,int data,int num) 
{ 
    Node *newNode,*prev,*tmp; 
    prev=NULL; 
    newNode=createNode(num); 
    if(start==NULL) 
    { 
     printf("the list is empty..."); 
     return start; 
    } 
    tmp=start; 
    while(tmp) 
    { 
     if(tmp->data==data) 
      break; 
     prev=tmp; 
     tmp=tmp->next; 
    } 
    if(!tmp) 
     printf("the number you enter was not in the list\n"); 
    else if(prev==NULL) 
    { 
     newNode->next=start; 
     start=newNode; 
    } 
    else 
    { 
     newNode->next=prev->next; 
     prev->next=newNode; 
    } 
    return start; 
} 

Node*Delete(Node*start,Node**p2last,int data) 
{ 
    Node*prev,*tmp; 
    tmp=start; 
    prev=NULL; 
    while(tmp) 
    { 
     if(tmp->data==data) 
      break; 
     prev=tmp; 
     tmp=tmp->next; 
    } 

    if(!tmp) 
    { 
     printf("the item you entered was not in the list...\n"); 
     return start; 
    } 
    if(tmp==start) 
    { 
     if((*p2last)==start) 
      *p2last=NULL; 
     return NULL; 
    } 
    prev->next=tmp->next; 
    if(tmp==(*p2last)) 
     *p2last=prev; 
    free(tmp); 

    return start; 
} 

Node*deleteLast(Node*start,Node**p2last) 
{ 
    return Delete(start,p2last,(*p2last)->data); 
} 

Node*reverseList(Node*start,Node**p2last) 
{ 
    Node*ptr,*tmp,*prev; 
    (*p2last)=start; 
    prev=NULL; 
    for(ptr=start;ptr;) 
    { 
     tmp=ptr->next; 
     ptr->next=prev; 
     prev=ptr; 
     ptr=tmp; 
    } 
    return prev; 
} 

Node*sort(Node*start,Node**p2last) 
{ 
    Node*ptr,*newNode,*tmp,*start1,*last1; 
    start1=last1=NULL; 
    while(start!=NULL) 
    { 
     tmp=ptr=start; 
     while(ptr) 
     { 
      if(tmp->data > ptr->data) 
       tmp=ptr; 
      ptr=ptr->next; 
     } 
     newNode=createNode(tmp->data); 
     start1=insertLast(start1,&last1,newNode); 
     start=Delete(start,p2last,tmp->data); 
    } 
    *p2last=last1; 
    return start1; 
} 

Node*sort2(Node*start,Node**p2last) 
{ 
    int *arr,count=0,i,tmp,j; 
    Node*ptr,*start1,*last1,*newNode; 
    ptr=start; 
    start1=last1=NULL; 
    while(ptr) 
    { 
     count++; 
     ptr=ptr->next; 
    } 

    arr=(int*)malloc(sizeof(int)*count); 

    ptr=start; 
    for(i=0;i<count;i++) 
    { 
     arr[i]=ptr->data;  
     ptr=ptr->next; 
    } 

    /* sorting the array bubble */ 
    for(i=1;i<count;i++) 
     for(j=0;j<count-i;j++) 
      if(arr[j]>arr[j+1]) 
      { 
       tmp=arr[j]; 
       arr[j]=arr[j+1]; 
       arr[j+1]=tmp; 
      } 

    for(i=0;i<count;i++) 
     start1=insertLast(start1,&last1,createNode(arr[i])); 
    *p2last=last1; 
    return start1; 
}