2013-04-16 113 views
2

经过近3年,我开始重新学习C创建排序的链接列表

我创建了一个Linked list,并且希望将其扩展为创建排序链接列表。这里是我的代码:

typedef struct node{ 
int data; 
struct node *ptr; 
}node; 

node* insert(node* head, int num){ 
node *temp,*prev,*next; 
temp = (node*)malloc(sizeof(node)); 
temp->data = num; 
temp->ptr = '\0'; 
if(head=='\0'){ 
    head=temp; 
}else{ 
    next = head; 
    prev = next; 
    while(next->data<=num){ 
     prev = next; 
     next = next->ptr; 
    } 
    if(next==NULL){ 
     prev->ptr = temp; 
    }else{ 
     temp->ptr = prev->ptr; 
     prev-> ptr = temp; 
    } 

} 
return head; 
} 

void main(){ 
int num; 
node *head, *p; 
head = '\0'; 
do{ 
    printf("Enter a number"); 
    scanf("%d",&num); 
    if(num!=0) 
     head = insert(head,num); 
}while(num!=0); 
p = head; 
printf("\nThe numbers are:\n"); 
while(p!='\0'){ 
    printf("%d ",p->data); 
    p = p->ptr; 
} 
} 

这是我的想法。我遍历列表,直到找到一个数字>=为止。我将前一个节点存储在prevnext节点中包含当前值。如果接下来是null,那么列表结束并且列表中的数字是最高的,因此它将被插入到最后的位置,如果该数字是中间的某个位置,则prev节点的地址部分被存储在临时节点中地址部分现在临时节点指针保存下一个节点的地址。

编辑:我的代码问题是如果我输入1,2我得到错误信息为a.exe has stopped working。我正在使用MinGW进行编译。我打破了循环时,用户输入0

+2

''\ 0''与NULL不相同。 http://stackoverflow.com/questions/1296843/what-is-the-difference-between-null-0-and-0 – mohit

+0

@mohit,''\ 0''将和'NULL'完全一样。它在语义上并不真正有意义,但它应该没问题。 –

回答

3

你必须改变线路

while(next->data<=num) 

while(next!='\0' && next->data<=num) 

当您插入第二个元素next'\0'在第二迭代试图让datanext->data一起导致分段错误。

随着改变的状况中,而如果next!='\0'将为假(因此next=='\0')的同时被中止并且由于&&的短路next->data不计算。


编辑

你在你的代码更多的问题。

如果你看看输入2 1 0那么具有正确工作的程序的输出应该是1 2,但它的代替是2 1。问题在于,在你的insert函数中,你不会考虑插入当前最小元素的情况,它将成为新的头部。

另一个问题是你最终没有释放内存,导致内存泄漏。

我改变你的代码正确行为:

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

typedef struct node{ 
    int data; 
    struct node *ptr; 
} node; 

node* insert(node* head, int num) { 
    node *temp, *prev, *next; 
    temp = (node*)malloc(sizeof(node)); 
    temp->data = num; 
    temp->ptr = NULL; 
    if(!head){ 
     head=temp; 
    } else{ 
     prev = NULL; 
     next = head; 
     while(next && next->data<=num){ 
      prev = next; 
      next = next->ptr; 
     } 
     if(!next){ 
      prev->ptr = temp; 
     } else{ 
      if(prev) { 
       temp->ptr = prev->ptr; 
       prev-> ptr = temp; 
      } else { 
       temp->ptr = head; 
       head = temp; 
      }    
     } 
    } 
    return head; 
} 

void free_list(node *head) { 
    node *prev = head; 
    node *cur = head; 
    while(cur) { 
     prev = cur; 
     cur = prev->ptr; 
     free(prev); 
    }  
} 

int main(){ 
    int num; 
    node *head, *p; 
    head = NULL; 
    do { 
     printf("Enter a number"); 
     scanf("%d",&num); 
     if(num) { 
      head = insert(head, num); 
     } 
    } while(num); 
    p = head; 
    printf("\nThe numbers are:\n"); 
    while(p) { 
     printf("%d ", p->data); 
     p = p->ptr; 
    } 
    free_list(head); 
    return 0; 
} 

我的代码中看到https://ideone.com/wT5iQ8在testinput连同正确的输出。

+1

为什么要比较一个字符的指针?无论如何,只要'while(next && next-> data <= num)'就足够了。 –

+1

@FredLarson你说得对,OP应该使用'NULL'而不是''\ 0'',但我想尽可能少地改变他的代码:)。它也适用于''\ 0''。 – halex

+0

是的,它会工作,但... blech。 –