
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct people
{
char name[10]
int age
struct people * next
}
int main()
{
struct people * head=NULL
struct people * prev , * current
int flag=1
while(flag!=0)
{
printf("请输入学生姓名,年宏凯龄:(年龄输入0结束所有输入工作)\n")
current=(struct people *)malloc(sizeof(struct people))
if(head==NULL)
head=current
else
prev->next=current
前裂 current->next=NULL
scanf("%s",&current->name)
scanf("%d",&current->age)
prev=current
flag=current->age
}
printf("Output:\n")
if(head==NULL)
printf("无资料慧绝闭。\n")
else
{
current=head
while(current->next!=NULL)
{
printf("姓名:%s\n年龄:%d\n\n",current->name,current->age)
current=current->next
}
}
}
至于排序,断开旧链表,将前后指针链接到新的节点就好
如果还有问题欢迎再问哈
#include<stdio.h>#include<stdlib.h>
# define LEN sizeof(struct biao)
struct biao
{
int num
char name[20]
struct biao *next
}
int n
struct biao *create()
{
struct biao *head,*p1,*p2
n=0
p1=p2=(struct biao *)malloc(LEN)
scanf("%d%s",&p1->num,p1->name)
head=NULL
while(p1->num!=0)
{
n=n+1
if(n==1)head=p1
else p2->next=p1
p2=p1
p1=(struct biao *)malloc(LEN)
scanf("%d%s",&p1->num,p1->name)
}
p2->next=NULL
return(head)
}
void print(struct biao *head)
{
struct biao *p
p=head
if(p!=NULL)
do
{
printf("num:%d\tname:%s\t\n",p->闭掘茄num,p->name)
p=p->next
}while(p!=NULL)
else printf("链表为空!\n")
}
struct biao *sort(struct biao *head)/散灶*此函轿察数为按逆序排列函数*/
{
struct biao *p1,*p2,*p3
p1=head
if(p1==NULL)goto end
else
{
if(p1->next==NULL)goto end
else
{
p2=p1->next
while(p2->next!=NULL)
{
if(p1==head){p3=p2->nextp1->next=NULLp2->next=p1p1=p2p2=p3p3=p3->next}
else
{
p2->next=p1
p1=p2p2=p3p3=p3->next
}
}
}
}
if(p2->next==NULL){head=p2p2->next=p1}
end:
return head
}
int main()
{
struct biao *p
printf("说明:\n链表成员包括“编号”和“名称”。\n请对链表赋值:")
p=create()
print(p)
p=sort(p)
printf("逆序排列完毕!\n")
print(p)
return 0
}
//MergeSort.cpp#include <iostream.h>
#include <conio.h>
#define MAXSIZE 20
#define LENGTH 7
typedef int RedType
typedef struct //SqList structure
{ RedType r[MAXSIZE+1] //Records Type
int length
}SqList
typedef SqList RcdType
void Merge(RcdType SR,RcdType &TR,int i,int m,int n) //Merge() function
{ int j,k
for(j=m+1,k=ii<=m&&j<=n++k)
{ if(SR.r[i]<=SR.r[j])
TR.r[k]=SR.r[i++]
else
TR.r[k]=SR.r[j++]
}
while(i<=m)
TR.r[k++]=SR.r[i++]
while(j<=n)
TR.r[k++]=SR.r[j++]
}//end of Merge() function
void MSort(RcdType SR,RcdType &TR1,int s, int t) //MSort() function
{ int m
RcdType TR2//[LENGTH]
if(s==t)
TR1.r[s]=SR.r[t]
else
{ m=(s+t)/2
MSort(SR,TR2,s,m)
MSort(SR,TR2,m+1,t)
Merge(TR2,TR1,s,m,t)
}//end of else
}//end of MSort() function
void MergeSort(SqList &L) //MergeSort() function
{
MSort(L,L,1,L.length)
}//end of MergeSort() function
void main() //main function
{ int i
SqList L//={{0,49,38,65,97,76,13,27,},LENGTH}
cout<<"MergeSort.cpp"磨拆<<endl<<"瞎姿枣============="<<endl<<endl
cout<<"Please input the length of SqList L: <eg. 7>"
cin>>L.length
cout<<"Please input the disordered array L.r: <eg. {49,38,65,97,76,13,27,...}>"<<endl
for(i=1i<=L.lengthi++)
cin>>L.r[i]
MergeSort(L)
cout<<endl<<"The sorted array L.r: "
for(i=1i<=L.lengthi++)
cout<<L.r[i]<<" "
cout<<endl
cout<<"...OK!..."<<endl
getch()
}//end of main() function我以前的,可以调试的
应该符合你要求,只册胡是很少部分你自己改一下,比如数的个数
输入改为 rand()随即输入,刚才粘贴错了
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)