2014-04-12 61 views
0

我需要對列表中輸入的數字進行排序,但是我做了一些錯誤的處理,它將所有的數據排序,除了第一個數字。任何想法如何解決這個問題?對列表中的數字進行排序/ C

這裏是我的代碼:

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

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

    void Add (struct node* p, int d) 
    { 
    struct node* q; 
    q=malloc(sizeof(struct node)); 
    if (q==NULL) 
     printf("Not enaugh memory!"); 
    else 
    { 
     q->data=d; 
     if(List==NULL) 
     { 
      q->next=NULL; 
      List=q; 
     } 
     else 
     { 
      struct node *ptr=List; 
      while((ptr->next!=NULL)&&(ptr->next->data>d)) 
      { 
       ptr=ptr->next; 
      } 
      q->next=ptr->next; 
      ptr->next=q; 

     } 
    } 
} 


    int main() 
    { 
     int n,i,a; 
     printf("How much numbers are you going to enter? "); 
     scanf("%d",&n); 
     for (i=1; i<=n; i++) 
     { 
      printf("\nEnter a number: "); 
      scanf("%d",&a); 
      Add(List,a); 
     } 
     printf("\nThe sorted numbers are: "); 
     struct node *ptr=List; 
     while(ptr!=NULL) 
     { 
      printf("%d\t",ptr->data); 
      ptr=ptr->next; 
     } 
     printf("\n\n"); 
     system("PAUSE"); 
     return 0; 
    } 

感謝提前的幫助:-)

+0

這裏做的事情」'而((PTR!= NULL)&&(ptr->下一步 - >數據> d))'當有** **一個列表中的元素? – WhozCraig

回答

0

在add()函數,

if(List==p) 

這種說法是對你插入的所有元素真列出,因爲要添加的電話號碼是

Add(List,a); 

so p=List。因此else部分寫入的排序代碼不會執行。
還添加語句來檢查空的初始列表。
您可以使用類似下面的代碼,

void Add (int d) 
{ 
    struct node* q; 
    q=malloc(sizeof(struct node)); 
    if (q==NULL) 
     printf("Not enaugh memory!"); 
    else 
    { 
     q->data=d; 
     if(List==NULL) 
     { 
      q->next=NULL; 
      List=q; 
     } 
     else 
     { 
      struct node *ptr=List; 
      while((ptr->next!=NULL)&&(ptr->next->data>d)) 
      { 
       ptr=ptr->next; 
      } 
      q->next=ptr->next; 
      ptr->next=q; 

     } 
    } 
} 

由於名單是你不需要將它傳遞給Add()功能的全局變量。改變函數調用

Add(a); 
+0

好的,我應該改變什麼?我不擅長編程(如你所知):/ – Monster

+0

@Monster ok。我添加了代碼。你可以嘗試類似的代碼 – LearningC

+0

非常感謝你!我編輯了主帖,以便在更改代碼時看到代碼。除了我輸入的第一個數字之外,它正在對所有內容進行排序。任何其他建議? – Monster

0

您隨時撥打AddList作爲第一個參數,所以它的送花兒給人真正的內部Add(List==p)。因此,每個新項目只是插在列表的前面,根本沒有排序。

編輯1

一個很好的做法將被髮送列表中Add程序作爲參數。或者,如果你想保持它的外部,就是不給它Add在所有和試驗if(List == NULL)

void Add(int d) 
{ 
    // ... alloc 'q' and fill q->d here, then: 
    if(List == NULL) 
    { 
     q->next = NULL; 
     List = q; 
    } 
    else 
    { 
     struct node *b; // put q after b 
     for(b = List; b->next != NULL; b = b->next) 
      if(b->next->data >= d) 
       break; 
     q->next = b->next; 
     b->next = q; 
    } 
} 

EDIT 2

通過參數

列表轉移到功能的示例
void Add(struct node **n, int d) 
{ 
    // ... alloc 'q' and fill q->d here, then: 

    while(*n != NULL && (*n)->data < d) 
     n = & (*n)->next; 

    q->next = *n; 
    *n = q; 
} 

int main() 
{ 
    // ... 
    Add(&List, a); 
    // ... 
} 
+0

好的,我應該改變什麼?我並不擅長編程(如你所見):/ – Monster

+0

@Monster編輯 – CiaPan

0
void Add (struct node* p, int d){ 
    struct node* q; 
    q=malloc(sizeof(struct node)); 
    if (q==NULL) 
     printf("Not enaugh memory!"); 
    else{ 
     q->data=d; 
     if(List==NULL || List->data < d){//modify this line 
      q->next= List;//and change NULL to List 
      List=q; 
     } else { 
      struct node *ptr=List; 
      while((ptr->next!=NULL)&&(ptr->next->data>d)){ 
       ptr=ptr->next; 
      } 
      q->next=ptr->next; 
      ptr->next=q; 
     } 
    } 
} 
+0

非常感謝!它正在工作! – Monster