单链表的插入排序

_阿尔法 学生  2017-11-02 10:13:12
这是单链表的插入排序,不懂为什么p->next一定要为空?
void sortlist()
{
node * newhead, * s, * pre ,* p;
p=head->next;
newhead=p->next;
p->next=NULL;//这里p为什么一定要为空?
while(newhead)
{
s=newhead;
newhead=newhead->next;
pre=head;
p=head->next;
while(p!=NULL && p->a < s->a)
{
pre=p;
p=p->next;
}
s->next=p;
pre->next=s;
}
}
...全文
250 5 点赞 打赏 收藏 举报
写回复
5 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
destory27 2017-11-05
#include <stdio.h> #include <windows.h> #include <malloc.h> #include <string.h> typedef struct LoopList{ int num; struct LoopList *next; }list; void cursor(void) { system("color 0A"); system("title Circular points list"); system("mode con lines=20 cols=40"); //HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); //DWORD dword = 18; //BOOL SetConsoleFont(handle, dword); return ; } void chooseMenu(void) { printf("----------------------------------------"); printf("\tLoop singly linked list \t"); printf("----------------------------------------"); printf("\t************************\t"); printf("\t*1.初始化\t2.添加*\t\t"); printf("\t*3.元素个数\t4.插入*\t\t"); printf("\t*5.显示\t\t6.排序*\t\t"); printf("\t*7.查找\t\t8.取出*\t\t"); printf("\t*\t 0.退出 \t\t"); printf("\t************************\t"); printf("----------------------------------------"); return ; } list * init(list **head) { *head = (list *)malloc(sizeof(list)); (list *)memset(*head, 0x00, sizeof(list)); (*head)->next = *head; printf("初始化成功。\012"); Sleep(3000); return *head; } list *add(list **head) { list *s = *head; int n; printf("你要添加的元素个数:"); while((scanf("%d", &n)) == 0){ fflush(stdin); printf("Enter again:"); } while(n--){ list *p = (list *)malloc(sizeof(list)); (list *)memset(p, 0x00, sizeof(list)); printf("input data:"); while((scanf("%d", &p->num)) == 0){ fflush(stdin); printf("Enter again:"); } while(s->next != *head){ s = s->next; } s->next = p; p->next = *head; } printf("添加完成。\012"); Sleep(3000); return *head; } void size(list **head) { int size = 0; list *p = *head; while(p->next != *head){ p = p->next; ++size; } printf("元素个数:%d\012", size); Sleep(3000); return ; } void insert(list **head) { list *s = *head; list *p = (list *)malloc(sizeof(list)); (list *)memset(p, 0x00, sizeof(list)); printf("insert num:"); while((scanf("%d", &p->num)) == 0){ fflush(stdin); printf("Enter again:"); } while(s->next != *head) s = s->next; s->next = p; p->next = *head; printf("插入完成.\012"); Sleep(3000); return ; } void sort(list **head) { list *p = *head; if(p->next == *head || p->next->next == *head){ printf("数据为空或只有一个无法进行排序.\012"); Sleep(3000); } else{ int i = 0; int arr[100]; while(p->next != *head){ arr[i++] = p->next->num; p = p->next; } int t = i; for(i = 0; i < t-1; ++i){ int j = i; int temp = arr[j+1]; while(j > -1 && temp < arr[j]){ arr[j+1] = arr[j]; --j; } arr[j+1] = temp; } p = *head; i = 0; while(p->next != *head && i < t){ p->next->num = arr[i++]; p = p->next; } printf("排序完成.\012"); Sleep(3000); } return ; } void show(list **head) { list *p = *head; if(p->next == *head){ printf("要显示的内容为空.\012"); Sleep(3000); } else{ while(p->next != *head){ printf("%d ", p->next->num); p = p->next; } printf("\012"); printf("已显示完."); printf("\012"); Sleep(3000); } return; } void seek(list **head) { list *p = *head; if(p->next == *head){ printf("数据内容为空.\012"); Sleep(3000); } else{ int n; printf("输入你要查找的数:"); while((scanf("%d", &n)) == 0){ fflush(stdin); printf("Enter again:"); } int i = 1; int count = 0; while(p->next != *head){ if(p->next->num == n){ printf("第%d个数是%d.\012", i, n); ++i; ++count; p = p->next; } else { ++i; p = p->next; } } if(count == 0) printf("没有此数据.\n"); Sleep(3000); } return ; } void delete(list **head) { list *p = (*head)->next; if(p == *head){ printf("数据内容为空.\012"); Sleep(3000); } else{ list *s = *head; list *temp = NULL; int n; printf("输入你要查找的数:"); while((scanf("%d", &n)) == 0){ fflush(stdin); printf("Enter again:"); } while(p != *head){ if(p->num != n){ s = p; p = p->next; } else{ temp = p; s->next = p->next; p = p->next; } } if(temp == NULL) printf("未找到要删除的数据.\012"); else printf("已取出 %d \012", temp->num); Sleep(3000); } return; } int main(void) { cursor(); chooseMenu(); list *head; int flag = 1; while(1){ int choose; system("cls"); chooseMenu(); printf("please choose:"); while((scanf("%d", &choose)) == 0){ fflush(stdin); system("cls"); chooseMenu(); printf("Enter again:"); } while(flag){ if(choose == 1) flag = 0; else if(choose == 0) exit(0); else { printf("必须先初始化.\012"); printf("please choose:"); while((scanf("%d", &choose)) == 0){ fflush(stdin); system("cls"); chooseMenu(); printf("Enter again:"); } } } switch(choose){ case 1: head = init(&head);break; case 2: head = add(&head);break; case 3: size(&head);break; case 4: insert(&head);break; case 5: show(&head);break; case 6: sort(&head);break; case 7: seek(&head);break; case 8: delete(&head);break; case 0: exit(0); default: printf("error!\012");Sleep(2000);break; } } return 0; }
  • 打赏
  • 举报
回复
CT8100 2017-11-03
你看下面有一句判断 while(p!=NULL && p->a < s->a);如果不赋NULL初值的化这句判断就无效过了!
  • 打赏
  • 举报
回复
li374456042 2017-11-03
代码有问题,输入空的或者单节点的链表,会崩溃。所以换一个答案参考。
  • 打赏
  • 举报
回复
Davidsdu 2017-11-03
结束下面的while()循环,p->next=NULL后相当于先建了另一条新链表,该链表只包含头结点和原链表的第一个数据结点。 然后newHead指向原链表的第二个数据结点,然后开始依次通过比较方法插入新链表中,当新链表中的结点数据均小于newHead指向的旧链表数据结点时,此时需要结束while循环,此时p==NULL退出while循环,然后将newHead指向的数据结点插入新链表的末尾。
  • 打赏
  • 举报
回复
das白 2017-11-02
初始化 方便下面循环判断
  • 打赏
  • 举报
回复
相关推荐
发帖
C++ 语言
加入

6.0w+

社区成员

C++ 语言相关问题讨论,技术干货分享,前沿动态等
申请成为版主
帖子事件
创建了帖子
2017-11-02 10:13
社区公告
暂无公告