#include<stdlib.h>
//创建一个节点,data为value,指向NULL
Node*Create(intvalue){
Node*head=(Node*)malloc(sizeof(Node));
head->data=value;
head->next=NULL;
returnhead;
}
//销毁链表
boolDestroy_List(Node*head){
Node*temp;
while(head){
temp=head->next;
free(head);
head=temp;
}
head=NULL;
returntrue;
}
//表后添加一个节点,Create(value)
boolAppend(Node*head,intvalue){
Node*n=Create(value);
Node*temp=head;
while(temp->next){
temp=temp->next;
}
temp->next=n;
return0;
}
//打印链表
voidPrint_List(Node*head){
Node*temp=head->next;
while(temp){
printf("%d->",temp->data);
temp=temp->next;
}
printf("\n");
}
//在链表的第locate个节点后(头节点为0)插入创建的节点Create(value)
boolInsert_List(Node*head,intlocate,intvalue){
Node*temp=head;
Node*p;
Node*n=Create(value);
if(locate<0)
returnfalse;
while(locate--){
if(temp->next==NULL){
temp->next=Create(value);
returntrue;
}
temp=temp->next;
}
p=temp->next;
temp->next=n;
n->next=p;
returntrue;
}
//删除第locate个节点后(头节点为0)的节点
boolDelete_List(Node*head,intlocate){
Node*temp=head;
Node*p;
if(locate<0)
returnfalse;
while(locate--){
if(temp==NULL){
returnfalse;
}
temp=temp->next;
}
p=temp->next->next;
free(temp->next);
temp->next=NULL;
temp->next=p;
returntrue;
}
//获取链表长度(不包括头节点)
intSize_List(Node*head){
Node*temp=head;
intsize=0;
while(temp->next){
temp=temp->next;
size++;
}
returnsize;
}
//链表的三种排序(选择,插入,冒泡)
boolSort_List(Node*head){
intt=0;
intsize=Size_List(head);
//选择排序
/*for(Node*temp=head->next;temp!=NULL;temp=temp->next){
for(Node*p=temp;p!=NULL;p=p->next){
if(temp->data>p->data){
printf("换%d和%d\n",temp->data,p->data);
t=temp->data;
temp->data=p->data;
p->data=t;
}
}
}*/
//插入排序
/*for(Node*temp=head->next->next;temp!=NULL;temp=temp->next){
for(Node*p=head;p->next!=NULL;p=p->next){
if(p->next->data>temp->data)
{
printf("换%d和%d\n",temp->data,p->next->data);
t=temp->data;
temp->data=p->next->data;
p->next->data=t;
}
}
}*/
//冒泡排序
for(Node*temp=head->next;temp->next!=NULL;temp=temp->next){
for(Node*p=head->next;p->next!=NULL;p=p->next){
if(p->data>p->next->data){
t=p->data;
p->data=p->next->data;
p->next->data=t;
}
}
}
return0;
}
扩展资料:
return表示把程序流程从被调函数转向主调函数并把表达式的值带回主调函数,实现函数值的返回,返回时可附带一个返回值,由return后面的参数指定。
return通常是必要的,因为函数调用的时候计算结果通常是通过返回值带出的。如果函数执行不需要返回计算结果,也经常需要返回一个状态码来表示函数执行的顺利与否(-1和0就是最常用的状态码),主调函数可以通过返回值判断被调函数的执行情况。
同学,给你一段代码,里面涵盖了链表的冒泡排序!#include<stdio.h>
#include<malloc.h>
typedef
struct
node
{
int
data/*data代表成绩分数*/
struct
node
*next
}LNode,*LinkList
LinkList
Creat(void)/*创建链表,结束标志为当输入的数据为0!*/
{
LinkList
H,p1,p2
int
n
n=0
p1=p2=(LinkList)malloc(sizeof(LNode))
printf("输入数据:")
scanf("%d",&p1->data)
H=NULL
while(p1->data!=0)
{
n=n+1
if(n==1)
H=p1
else
p2->next=p1
p2=p1
p1=(LinkList)malloc(sizeof(LNode))
scanf("%d",&p1->data)
}
p2->next=NULL
return(H)
}
LinkList
Sort(LinkList
SL)/*递增排序函数:入口参数:链表的头指针,此为链表中的排序函数*/
{
LinkList
p,q
int
temp
for(p=SLp!=NULLp=p->next)
{
for(q=p->nextq!=NULLq=q->next)
{
if(p->data>q->data)
{
temp=q->data
q->data=p->data
p->data=temp
}
}
}
return
SL
}
int
main()
{
LinkList
L,S,K
L=Creat()
printf("初始化的单链表数据序列为:\n")
for(S=LS!=NULLS=S->next)
printf("%d
",S->data)
Sort(L)
printf("\n按递增顺序排序后的序列为:\n")
for(K=LK!=NULLK=K->next)
printf("%d==>",K->data)
return
0
}