求一个C语言链表源程序代码

求一个C语言链表源程序代码,第1张

#include <stdio.h>

#include <stdlib.h>

struct node

{

int num

struct node *next

}

/*建立链表*/

struct node *creat(int n)

{

int x, i

struct node *head, *p, *r

head=(struct node*)malloc(sizeof(struct node))

r=head

printf("请输入数字\r\n")

for(i=0i<ni++)

{

scanf("%d", &x)

p=(struct node*)malloc(sizeof(struct node))

p->num=x

r->next=p

r=p

}

r->next=NULL

return(head)

}

/*删除重复结点*/

void delet(struct node *head)

{

struct node *p, *q, *r

p=head->next

while(p!=NULL)

{

q=p

while(q->next!=NULL)

{

r=q->next

if(r->num==p->num)

{

if(r->next!=NULL)

{

q->next=r->next

free(r)

}

else

{

q->next=NULL

free(r)

}

}

else

{

q=r

}

}

p=p->next

}

}

/*排序*/

void sort(struct node *head)

{

struct node *p, *q, *small

int temp

for(p=head->nextp->next!=NULLp=p->next)

{

small=p

for(q=p->nextq!=NULL q=q->next)

{

if(q->num<small->num)

small=q

}

if(small!=p)

{

temp=small->num

small->num=p->num

p->num=temp

}

}

}

/*输出*/

void output(struct node *head)

{

struct node *pt

pt=head->next

while(pt!=NULL)

{

printf("%d\r\n", pt->num)

pt=pt->next

}

}

main()

{

int n

struct node *head

printf("输入数字的个数n\r\n")

scanf("%d", &n)

head=creat(n)

printf("输入的数字\r\n")

output(head)

delet(head)

printf("删除重复结点后输出数字\r\n")

output(head)

sort(head)

printf("排序后输出数字\r\n")

output(head)

free(head)

}

希望能对你有帮助,俺也学C不到两个月,共同进步啊!

如果是用简单C编译器的话,编译链接运行以后,应该会生成一个.exe文件吧。把这个文件发给别人之间双击就可以用了。但是你得告诉别人这个程序的使用流程或者方法,或者你把提示界面做得足够人性化也行。

如果是VC那种IDE的话,方法也类似,但是应该会复杂一些。比如你的C代码调用了一些库函数或者自定义的资源,需要把相应的被调资源跟.exe文件放在同一个文件夹下。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/11407014.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-15
下一篇2023-05-15

发表评论

登录后才能评论

评论列表(0条)

    保存