|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本链表为非循环链表,代码成功完成了穿件链表,输出所创建链表,判断是否为空以及计算链表长度这几个功能可以成功完成,但是运行到链表排序功能就不能实现,系统报错。
- [code]#include<stdio.h>
- #include<malloc.h>
- typedef struct Grade //创建一个GRADE非循环链表
- {
- int data;
- struct Grade * pNext;
- }GRADE,* PGRADE;
- PGRADE create_list(void); //创建链表函数
- void traverse_list(PGRADE pHead); //遍历链表函数
- bool is_empty(PGRADE pHead); //检测链表是否为空函数
- int length_list(PGRADE pHead); //求链表长度函数
- void sort_list(PGRADE); //将链表排序函数
- int main(void)
- {
- PGRADE pHead=NULL;
- pHead=create_list();
- if(is_empty(pHead))
- printf("The list is emoty!\n");
- else
- traverse_list(pHead);
- int len = length_list(pHead);
- printf("链表长度是:%d \n",len); //函数只能实现到这一步,排序函数无法工作!
-
- sort_list(pHead);
- traverse_list(pHead);
- return 0;
- }
- PGRADE create_list(void)
- {
- int i;
- int len;
- int val;
- PGRADE pHead=(PGRADE)malloc(sizeof(GRADE));
- printf("please enter the number of elements in your list: ");
- scanf("%d",&len);
- PGRADE pTail=pHead;
- pTail->pNext=NULL;
- for(i=0;i<len;i++)
- {
- PGRADE pNew=(PGRADE)malloc(sizeof(GRADE));
- printf("The %d th number is : \n",i+1);
- scanf("%d",&val);
- pNew->data=val;
- pNew->pNext=NULL;
- pTail->pNext=pNew;
- pTail=pNew;
- }
- return pHead;
- }
- void traverse_list(PGRADE pHead)
- {
- PGRADE p=pHead->pNext;
- while(NULL!=p)
- {
- printf("%d ",p->data);
- p=p->pNext;
- }
- printf("\n");
- }
- bool is_empty(PGRADE pHead)
- {
- if(pHead->pNext==NULL)
- return true;
- else
- return false;
- }
- int length_list(PGRADE pHead)
- {
- PGRADE p=pHead->pNext;
- int len=0;
- while(NULL!=p) //*********不是p->pNext
- {
- ++len;
- p=p->pNext;
- }
- return len;
- }
- void sort_list(PGRADE pHead)
- {
- int i,j,t;
- PGRADE p,q;
- int len=length_list(pHead);
- for(i=0,p=pHead->pNext;i<len-1;++i,p=p->pNext)
- {
- for(j=j+1,q=p->pNext;j<len;++j,q=q->pNext)
- {
- if(p->data>q->data)
- {
- t=p->data;
- p->data=q->data;
- q->data=t;
- }
- }
- }
- return;
- }
复制代码
希望帮忙找一下问题在哪里? 谢谢 |
|