微博上如何做网站推广,学校网站功能描述,瑞安哪里有做百度的网站,myeclipse怎么做网页题目描述
请根据输入数据构造一个带头结点的单链表#xff0c;链表结点的数据结构为struct node {int data; struct node *next;}#xff0c;试设计算法#xff1a;按递增次序输出单链表中各结点的数据元素#xff0c;并释放结点所占用的存储空间。
要求#xff1a;不允…题目描述
请根据输入数据构造一个带头结点的单链表链表结点的数据结构为struct node {int data; struct node *next;}试设计算法按递增次序输出单链表中各结点的数据元素并释放结点所占用的存储空间。
要求不允许使用数组作为辅助存储空间。
输入
每组测试数据包括两行第一行为单链表中的元素个数n第二行为n个元素的值。
输出
排序后的元素值每组测试数据输出占一行行末无空格。
样例输入
6 4 5 9 3 2 1
样例输出
1 2 3 4 5 9
#includestdio.h
#includestdlib.h
struct node {int data;struct node *next;
};
void Insert(struct node*p,int i,int a)//插入新元素尾插
{while(p-next){pp-next;}struct node ss;struct node *s;//每新加一个元素就要申请一块新的内存来存储新元素s(struct node*)malloc(sizeof(ss));//然后再把它连接到原先的链表当中s-dataa;s-nextNULL;p-nexts;
}
int main()
{int n;scanf(%d,n);struct node *p;struct node nn;p(struct node*)malloc(sizeof(nn));if(pNULL)return -1;p-nextNULL;//初始化头指针for(int i0;in;i){int a;scanf(%d,a);Insert(p,i,a);//每输入一个元素插入一次}struct node*tailp;while(tail!NULL)tailtail-next;for(int i0;in;i)//冒泡排序{struct node*currp-next;struct node*prep;for(int j0;jn-1;j){if(curr-datacurr-next-data){struct node*qcurr-next-next;pre-nextcurr-next;curr-next-nextcurr;curr-nextq;if(curr-nexttail)tailcurr;//尾指针特判}prepre-next;currpre-next;}}pp-next;while(p-next){printf(%d ,p-data);pp-next;}printf(%d,p-data);//这里是怕行末无空格所以最后一个元素分开输出了free(p);
}