数据结构上机实验(编程)(单链表的基本操作)

来源:百度知道 编辑:UC知道 时间:2024/05/22 23:19:07
实验要求:链表的显示要作为函数被调用
把自己使用的单链表结构明确的表达出来
基本上实现每个实验题目的要求。

题目(输入,排序,插入,显示):
1、由键盘输入的方式给出一组整数,把这些整数存到一个首地址为AH的单链表中。
2、把链表中的元素按照降序排列。
3、把链表中的元素按照先后顺序显示(要求在显示器可见)。
4、插入一个新的整数,使得链表插入后仍然为有序排列。

#include<stdio.h>
#include<stdlib.h>
typedef struct node{
int data;
struct node *next;
}*Linklist,Node;
Linklist creat(int n)
{Linklist head,r,p;
int x,i;
head=(Node*)malloc(sizeof(Node));
r=head;
printf("输入数字:\n");
for(i=n;i>0;i--)
{scanf("%d",&x);
p=(Node*)malloc(sizeof(Node));
p->data=x;
r->next=p;
r=p;}
r->next=NULL;
return head;
} void output(Linklist head)
{Linklist p;
p=head->next;
do{
printf("%3d",p->data);p=p->next;
}while(p);
printf("\n");
} void paixu(Linklist head)
{Linklist p,q,small;int temp;

for(p=head->next;p->next!=NULL;p=p->next)
{small=p;
for(q=p->next;q;q=q->next)
if(q->data<small->data)
small=q;
if(small!=p)
{temp=p->data;
p->data=smal