单向链表

#include"list.h"
#include<stdio.h>

#include<stdlib.h>

typedef struct node

{

int data;

struct node *next;

}

link create_Link()//创建链表
{
link head,cur;
int input;
head=(link)malloc(sizeof(node));//申请头结点
if(head==NULL)
{
printf("malloc error\n");
exit(0);
}
head->next=NULL;
printf("please input data:\n");
scanf("%d",&input);
while(input)
{
cur=(link)malloc(sizeof(node));//为新的节点申请空间
cur->data=input;
//头插法,让新来的节点有所指向
cur->next=head->next;
head->next=cur;
scanf("%d",&input);
}
return head;
}


void print_Link(link head)//打印链表
{
head=head->next;
while(head!=NULL)
{
printf("%d ",head->data);
head=head->next;
}
printf("\n");
}
link insert_Link(link head)//插入节点
{
link cur;
int input;
printf("please input what you want to insert :\n");
scanf("%d",&input);
while(input)
{
cur=(link)malloc(sizeof(node));
cur->data=input;
cur->next=head->next;
head->next=cur;
scanf("%d",&input);
}
return head;
}
link search(link head)//查找节点
{
int find;
printf("please input what you want to find:\n");
scanf("%d",&find);
head=head->next;
while(head!=NULL)
{
if(head->data==find)
return head;
head=head->next;
}
return NULL;
}


link del_link(link head,link pfind)//删除节点
{
link p;
p=head;
while(p->next!=pfind)//前一个节点的后继等于pfind,把pfind 的后继赋给起一个节点的后继
{
p=p->next;
}
p->next=pfind->next;
free(pfind);
return head;
}
int count_link(link head)//统计链表长度
{
int count=0;
while(head->next!=NULL)
{
count++;
head=head->next;
}
return count ;
}
link sort_link(link head,int size)//冒泡排序,每趟都从首节点开始
{
int j,i;
link p,q,list,temp;


for(i=0;i<size-1;i++)
{
list=head;
p=list->next;
q=p->next;


for(j=0;j<size-i-1;j++)
{
if(p->data>q->data)
{
list->next=q;
p->next=q->next;
q->next=p;
//交换指针所指,P,Q 的位置发生变化,利用TEMP交换,保持原有的顺序
temp=p;
p=q;
q=temp;
}
list=list->next;
p=p->next;
q=q->next;
}
}
return head;


}
void destory_Link(link head)//销毁链表,使两个指针指向链表,一个后移,一个销毁
{
link p;
while(head!=NULL)
{
p=head;
head=head->next;
free(p);
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值