单链表的建立、排序、打印
来源:互联网 发布:五大贵重宝石知乎 编辑:程序博客网 时间:2024/06/02 02:53
#include<cstdio>
#include<cstdlib>
#include <cstring>
using namespace std;
struct node
{
int num;
char name[10];
struct node *next;
};
struct node *createlink(int n)
{
int i;
struct node *head,*p,*q;
head=(struct node*)malloc(sizeof(struct node));
head->next=NULL;
q=head;
for(i=0;i<n;i++)
{
p=(struct node*)malloc(sizeof(struct node));
scanf("%d",&p->num);
scanf("%s",p->name);
q->next=p;
q=p;
p->next=NULL;
}
return head;
}
void sort(struct node *head)
{
struct node *p,*q,t,*m;
p=head->next;
if(p!=NULL)
{
while(p->next!=NULL)
{
q=p->next;
while(q!=NULL)
{
if(q->num>p->num)
{
t=*p;
*p=*q;
*q=t;
m=p->next;
p->next=q->next;
q->next=m;
}
q=q->next;
}
p=p->next;
}
}
}
void disp(struct node *head)
{
struct node *p;
p=head->next;
while(p!=NULL)
{
printf("%d\n",p->num);
printf("%s\n",p->name);
p=p->next;
}
printf("\n\n");
}
int main()
{
struct node *head;
head=createlink(3);
disp(head);
sort(head);
disp(head);
return 0;
}
#include<cstdlib>
#include <cstring>
using namespace std;
struct node
{
int num;
char name[10];
struct node *next;
};
struct node *createlink(int n)
{
int i;
struct node *head,*p,*q;
head=(struct node*)malloc(sizeof(struct node));
head->next=NULL;
q=head;
for(i=0;i<n;i++)
{
p=(struct node*)malloc(sizeof(struct node));
scanf("%d",&p->num);
scanf("%s",p->name);
q->next=p;
q=p;
p->next=NULL;
}
return head;
}
void sort(struct node *head)
{
struct node *p,*q,t,*m;
p=head->next;
if(p!=NULL)
{
while(p->next!=NULL)
{
q=p->next;
while(q!=NULL)
{
if(q->num>p->num)
{
t=*p;
*p=*q;
*q=t;
m=p->next;
p->next=q->next;
q->next=m;
}
q=q->next;
}
p=p->next;
}
}
}
void disp(struct node *head)
{
struct node *p;
p=head->next;
while(p!=NULL)
{
printf("%d\n",p->num);
printf("%s\n",p->name);
p=p->next;
}
printf("\n\n");
}
int main()
{
struct node *head;
head=createlink(3);
disp(head);
sort(head);
disp(head);
return 0;
}
0 0
- 单链表的建立、排序、打印
- 单链表的建立,测长,打印,删除,插入,排序,逆置
- 单链表的建立,测长,打印,删除,插入,排序,逆置
- 单链表的建立,测长,打印,删除,插入,排序,逆置
- 单链表的建立和打印
- 建立排序的单链表
- 使用JAVA实现单链表的建立、测长、打印、删除、插入、排序、逆置
- 单链表的建立、测长、打印、删除节点、插入节点、排序、逆转
- 【数据结构】单链表的建立,测长,打印,删除,插入, 排序, 逆置
- 实现一个单链表的建立、测长、打印,插入,排序,逆置
- 单链表的建立、测长、打印、删除节点、插入节点、排序、逆置操作。
- 单链表的建立、测长、删除、插入、排序、逆置及打印(数据结构)
- 单链表的建立,求长度,打印
- 单链表的建立、测长和打印
- 单链表的建立/测长/打印
- 单链表的建立/测长/打印
- 单链表的建立、测长和打印
- 单链表,建立,完成固定数组的排序
- cxf添加身份认证
- Android NestedScroll笔记
- difference between dmesg and /var/log/kern.log
- 单链表的拆分
- FLV文件格式详解
- 单链表的建立、排序、打印
- 查看MYSQL的数据库表空间
- Redis对于key的操作命令(二)
- 大数据常见算法题
- leetcode No116. Populating Next Right Pointers in Each Node
- 分布式文件存储 搭建FastDFS-5.0.5集群
- QML在动画执行完做其它事
- 全透明UINavigationBar的实现
- Failed to unlink socket file /tmp/mongodb-27017.sock errno:1 Operation not permitted