又是单链表的排序问题,看看我错那儿了!

seaman117 2004-09-24 12:59:22
下面是一个建表加排序的函数,main里用createstacklinknode(&head)调用,head是头节点,已经指向NULL了。调用是,能正常输出链表,但是在冒泡排序那里就死了!
void createstacklinknode(struct linknode **headp)
{
int numbers;
register int tmp,tcurrent;
struct linknode *p,*order;
printf("\nInput Number:");
scanf("%ld",&numbers);
while(numbers)
{
p=(struct linknode *)malloc(sizeof(struct linknode));
p->data=numbers;
p->next=(*headp)->next;
(*headp)->next=p;
scanf("%ld",&numbers);
}

printf("\nThe create stack linknode is following:\n");
printf("------------------------------------------------\n");

p=(*headp)->next;
while (p!=NULL)
{
printf("%2ld *",p->data);
printf(" -> ");
p=p->next;
}
printf("NULL\n");

p=(*headp)->next;
tcurrent=p->data;

for(;p!=NULL;p=p->next)
{
for(;p!=NULL;p=p->next)
{
if(tcurrent<p->data)
{ tmp=tcurrent ;
tcurrent=p->data;
p->data=tmp;
}
}
p=(*headp)->next;

}
p=(*headp)->next;
while (p!=NULL)
{
printf("%2ld *",p->data);
printf(" -> ");
p=p->next;
}
printf("NULL\n");

}

另外我想把排序单独写成一个函数,参数怎么传递啊?我写的是这这样
申明:struct linknode *orderbyincrease(struct linknode **) //指向头节点指针的指针
调用:orderbyincrease( (*headp)->next );总是提示类型不匹配!
分不够再加啊!
...全文
127 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
seaman117 2004-09-24
  • 打赏
  • 举报
回复
include file:

struct linknode
{
long int data;
struct linknode *next;
};
seaman117 2004-09-24
  • 打赏
  • 举报
回复
昨天就这样试过了,今天改这样还是类型匹配错误,干脆把整个贴你看看!
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <ctype.h>
#define menuitem 7

#include "f:\temp\linknode\struct.h"


void main()
{
void showmenus(void);

void createstacklinknode(struct linknode **);
void createqueuelinknode(struct linknode **);
struct linknode *orderbyincrease(struct linknode **);
void insertrecoder(struct linknode**,int);
void deleterecoder(struct linknode **,int);
void jointwolinknode(struct linknode** ,struct linknode**);

int chooise;
struct linknode *head;
head=(struct linknode *)malloc(sizeof(struct linknode));
head->next=NULL;

textbackground(1);
textcolor(2);
clrscr();
showmenus();
printf("\nInput your chooise [0-6]:");
scanf("%d",&chooise);
while (chooise)
{
switch (chooise)
{
case 1: createstacklinknode(&head);break;
case 2: createqueuelinknode(&head);break;
case 3: printf ("Insert recode oprate\n"); break;
case 4: break;
case 5: break;
case 0: break;

}
showmenus();
printf("\nInput your chooise [0-6]:");
scanf("%d",&chooise);
}
}

void createstacklinknode(struct linknode **headp)
{
int numbers,max,min,sum=0,i=0;
register int tmp,tcurrent;
struct linknode *p,*order;
printf("\nInput Number:");
scanf("%ld",&numbers);
while(numbers)
{
p=(struct linknode *)malloc(sizeof(struct linknode));
p->data=numbers;
p->next=(*headp)->next;
(*headp)->next=p;
scanf("%ld",&numbers);
}

printf("\nThe create stack linknode is following:\n");
printf("------------------------------------------------\n");

p=(*headp)->next;
while (p!=NULL)
{
printf("%2ld *",p->data);
printf(" -> ");
p=p->next;
}
printf("NULL\n");
orderbyincrease( &(*headp)->next );
/*p=(*headp)->next;
tcurrent=p->data;
printf("\n p->data=%d",tcurrent);
for(;p!=NULL;p=p->next)
{
for(;p!=NULL;p=p->next)
{
if(tcurrent<p->data)
{ tmp=tcurrent ;
tcurrent=p->data;
p->data=tmp;
}
}
p=(*headp)->next;

} */
p=(*headp)->next;
while (p!=NULL)
{
printf("%2ld *",p->data);
printf(" -> ");
p=p->next;
}
printf("NULL\n");

}



void createqueuelinknode(struct linknode **headp)
{
int numbers;
struct linknode *p,*ini;
ini=*headp;
printf("\nInput Number:");
scanf("%ld",&numbers);
while(numbers)
{
p=(struct linknode *)malloc(sizeof(struct linknode));
p->data=numbers;
(*headp)->next=p;
p->next=NULL;
(*headp)=p;
scanf("%ld",&numbers);
}
printf("\nThe create queue linknode is following:");
printf("\n------------------------------------------------\n");
p=ini->next;
while (p!=NULL)
{

printf("%2ld *",p->data);
printf(" -> ");
p=p->next;
}
printf("NULL");
}


struct linknode *orderbyincrease(struct linknode **headp)
{
register int tmp,tcurrent;
struct linknode *p;
p=*headp;
tcurrent=p->data;

for(;p!=NULL;p=p->next)
{
for(;p!=NULL;p=p->next)
{
if(tcurrent<p->data)
{ tmp=tcurrent ;
tcurrent=p->data;
p->data=tmp;
}
}
p=(*headp)->next;

}
return *headp;
}

void showmenus()
{
int i;
char *menu[]={
"\n\n\n\t\t\tSingle Linkeonde Opration\n",
"\t\t==========================================\n",
"\t\t 1.Create stack type linknode.\n",
"\t\t 2.Create queue type linknode.\n",
"\t\t 3.Insert a recoder to linknode.\n",
"\t\t 4.Delete a recoder from linknode.\n",
"\t\t 5.Jion two linknode into one.\n",
"\t\t 0.Exit.\n",
"\t\t=========================================\n",
};
for (i=0;i<menuitem;i++)
{
printf("%s",menu[i]);
}
}
BlueGenie 2004-09-24
  • 打赏
  • 举报
回复
p=(*headp)->next;
tcurrent=p->data; // No test for empty list

for(;p!=NULL;p=p->next)
{
for(;p!=NULL;p=p->next)
{
if(tcurrent<p->data)
{ tmp=tcurrent ;
tcurrent=p->data;
p->data=tmp;
}
}
p=(*headp)->next; // p∈{ (*headp)->next, (*headp)->next->next }

}

申明:struct linknode *orderbyincrease(struct linknode **) //指向头节点指针的指针
orderbyincrease( & (*headp)->next ) ;
seaman117 2004-09-24
  • 打赏
  • 举报
回复
调用orderbyincrease后要求返回头指针!

69,381

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧