c++单向链表排序的有关问题,在输出新链表时,结果一直出不来,是函数定义有死循环?还是代码太不简洁?求解?
c++单向链表排序的问题,在输出新链表时,结果一直出不来,是函数定义有死循环?还是代码太不简洁?求解????????
#include<iostream>
using namespace std;
#define n 2
struct node
{
int no;
char name[20];
char sex[6];
float math;
float physics;
float english;
node*next;
};
node*creat();
node* rank(node*head);
void print(node *head);
int main()
{
node*head;
head=creat();
print(head);
head=rank(head);
print(head);
return 0;
}
node*creat()
{
int i=0;
node*head,*s,*p;
head=NULL;
s=new node;
while (i<n)
{
cout<<"产生链表,请输入学生学号:"<<endl;
cin>>s->no;
gets(s->name);
cout<<"请输入学生姓名:"<<endl;
gets(s->name);
cout<<"请输入学生性别:"<<endl;
gets(s->sex);
cout<<"请输入学生数学,物理,英语成绩:"<<endl;
cin>>s->math>>s->physics>>s->english;
if(head==NULL)
{
head=s;
}
else
{
p->next=s;
}
p=s;
s=new node;
i++;
}
p->next=NULL;
delete s;
return (head);
}
void print(node *head)
{
node *p;
p=head;
cout<<"输出链表中各结点值:"<<endl;
while (p!=NULL)
{
cout<<p->no<<'\t'<<p->name<<'\t'<<p->sex<<'\t'<<p->math<<'\t'<<p->physics<<'\t'<<p->english<<endl;
p=p->next;
}
}
node*rank(node*head)
{
node*pa,*pc,*newhead,*pn,*px;
px=pc=newhead=pn=head;
pa=head->next;
for(;pa!=NULL;pa=pa->next)
{
if(pa->english>=newhead->english)
{
px=pa;
px->next=newhead;
newhead=px;
}
else
{
while (pc->next!=NULL && pa->english<pc->english) //若链表非空,则按成绩查找插入点
{
pn=pc;
pc=pc->next;
}
if(pa->english<pc->english)
{
px=pa;
pc->next=px;
px->next=NULL;
}
else
{
px=pa;
pn->next=px;
px->next=pc;
}
}
}
return newhead;
}
运行时光标一直在闪烁,等了好久都没出结果(ps:在函数定义里刚开始我没有用node*px,后来lz觉得不能改变pa循环的指向,所以自以为是的加了个px来替代pa,可结果还是出不来,求高手解答,十分感谢!!!)
------解决方案--------------------
![c++单向链表排序的有关问题,在输出新链表时,结果一直出不来,是函数定义有死循环?还是代码太不简洁?求解? c++单向链表排序的有关问题,在输出新链表时,结果一直出不来,是函数定义有死循环?还是代码太不简洁?求解?](/default/index/img?u=aHR0cDovL3d3dy5teWV4Y2VwdGlvbnMubmV0L2ltZy8yMDE0LzA2LzI4LzE2MzM0NzE0NS5qcGc=)
我这里没问题啊
------解决方案--------------------
仅供参考
#include<iostream>
using namespace std;
#define n 2
struct node
{
int no;
char name[20];
char sex[6];
float math;
float physics;
float english;
node*next;
};
node*creat();
node* rank(node*head);
void print(node *head);
int main()
{
node*head;
head=creat();
print(head);
head=rank(head);
print(head);
return 0;
}
node*creat()
{
int i=0;
node*head,*s,*p;
head=NULL;
s=new node;
while (i<n)
{
cout<<"产生链表,请输入学生学号:"<<endl;
cin>>s->no;
gets(s->name);
cout<<"请输入学生姓名:"<<endl;
gets(s->name);
cout<<"请输入学生性别:"<<endl;
gets(s->sex);
cout<<"请输入学生数学,物理,英语成绩:"<<endl;
cin>>s->math>>s->physics>>s->english;
if(head==NULL)
{
head=s;
}
else
{
p->next=s;
}
p=s;
s=new node;
i++;
}
p->next=NULL;
delete s;
return (head);
}
void print(node *head)
{
node *p;
p=head;
cout<<"输出链表中各结点值:"<<endl;
while (p!=NULL)
{
cout<<p->no<<'\t'<<p->name<<'\t'<<p->sex<<'\t'<<p->math<<'\t'<<p->physics<<'\t'<<p->english<<endl;
p=p->next;
}
}
node*rank(node*head)
{
node*pa,*pc,*newhead,*pn,*px;
px=pc=newhead=pn=head;
pa=head->next;
for(;pa!=NULL;pa=pa->next)
{
if(pa->english>=newhead->english)
{
px=pa;
px->next=newhead;
newhead=px;
}
else
{
while (pc->next!=NULL && pa->english<pc->english) //若链表非空,则按成绩查找插入点
{
pn=pc;
pc=pc->next;
}
if(pa->english<pc->english)
{
px=pa;
pc->next=px;
px->next=NULL;
}
else
{
px=pa;
pn->next=px;
px->next=pc;
}
}
}
return newhead;
}
运行时光标一直在闪烁,等了好久都没出结果(ps:在函数定义里刚开始我没有用node*px,后来lz觉得不能改变pa循环的指向,所以自以为是的加了个px来替代pa,可结果还是出不来,求高手解答,十分感谢!!!)
------解决方案--------------------
我这里没问题啊
------解决方案--------------------
仅供参考
//假设带表头结点的单向链表头指针为head,试编写一个算法将值为5的结点插入到连接表的第k个结点前,删除第k个节点,并对该链表进行排序。
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <time.h>
struct NODE {
int data;
struct NODE *next;
} H,*head,*p,*q,*s1,*s2,*s3,*s4,*s;
int i,j,k,n,t,m;
int main() {
srand(time(NULL));
//填写头节点数据
H.data=-1;
H.next=NULL;
head=&H;
//创建10个节点的单链表
p=head;
for (i=0;i<10;i++) {
q=(struct NODE *)malloc(sizeof(struct NODE));
if (NULL==q) return 1;
q->data=rand()%100;//填写0..99的随机值
q->next=NULL;
p->next=q;
p=q;
}
//输出整个单链表
s=head->next;
while (1) {
if (NULL==s) {
printf("\n");
break;
}
printf("%02d->",s->data);
s=s->next;