头文件
#ifndef __HEAD_H_
#define __HEAD_H_
#include
#include
#include
typedef int type;
enum A{SUCCESS,FLASE=-1};
typedef struct Node
{
type data;
struct Node * next;
}*Llist;
Llist create();
Llist insert_head(Llist head,type element);
Llist delete_head(Llist head);
void output(Llist head);
Llist insert_tail(Llist head,type element);
Llist delete_tail(Llist head);
Llist insert(Llist head,type sub,type element);
Llist delete(Llist head,type sub);
Llist update(Llist head,type sub,type element);
type search(Llist head,type sub);
type search_element(Llist head,type element);
Llist delete_element(Llist head,type element);
Llist update_element(Llist head,type element1,type element);
type d_search(Llist head,type sub);
Llist sort(Llist head);
Llist inverse(Llist head);
Llist free1(Llist head);
#endif
主函数
#include "head.h"
int main(int argc, const char *argv[])
{
Llist head=NULL;
type n,element,sub,element1;
//头插
printf("输入从头插入链表元素的数量:");
scanf("%d",&n);
for(int i=0;i
封装函数
#include "head.h"
//输出链表数据
void output(Llist head)
{
if(head==NULL)
return;
Llist p=head;
while(p!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
putchar(10);
}
//创建链表
Llist create()
{
Llist p=(Llist) malloc(sizeof(struct Node));
if(p==NULL)
return NULL;
p->data=0;
p->next=NULL;
return p;
}
//头插
Llist insert_head(Llist head,type element)
{
Llist p=create();
p->data=element;
//若无表头,则成为表头
if(head==NULL)
{
head=p;
}
//新数据链接原表头并成为表头
else
{
p->next=head;
head=p;
}
return head;//返回形参给实参
}
//头删
Llist delete_head(Llist head)
{
if(head==NULL)
return NULL;
//用del记录链表头
Llist del=head;
//记录新的链表头
head=head->next;
//删除原列表头
free(del);
del=NULL;
return head;
}
//尾插
Llist insert_tail(Llist head,type element)
{
if(head==NULL)
{
Llist s=create();
s->data=element;
head=s;
return head;
}
Llist p=head;
Llist q;
//指向链表尾并创建新结构体
while(p!=NULL)
{
q=p;
p=p->next;
}
p=create();
//将链表最后一项指向新结构体
q->next=p;
p->data=element;
return head;
}
//尾删
Llist delete_tail(Llist head)
{
if(head==NULL)
return NULL;
Llist p=head;
Llist q;
type n=0;
//找到最后一项
while(p!=NULL)
{
n++;
p=p->next;
}
//如果只剩一个链表,引用头删
if(n==1)
{
head=delete_head(head);
return head;
}
//释放最后一项并将前一项指针指向NULL
p=head;
q=p;
for(int i=1;inext;
}
free(p);
q->next=NULL;
return head;
}
//按位插入
Llist insert(Llist head,type sub,type element)
{
Llist p=head;
type n=0;
//计数链表数量
while(p!=NULL)
{
p=p->next;
n++;
}
//判断是否为空或插入位置为1,直接调用头插函数
if(sub==1 || head==NULL)
{
head=insert_head(head,element);
return head;
}
//判断位置为链表尾,调用尾插函数
if(sub==n+1)
{
head=insert_tail(head,element);
return head;
}
//判断位置太大,报错
if(sub>n+1)
{
printf("位置输入过大\n");
return NULL;
}
//插入新节点
p=head;
for(int i=1;inext;
}
Llist s=create();
s->data=element;
s->next=p->next;
p->next=s;
return head;
}
//按位删除
Llist delete(Llist head,type sub)
{
Llist p=head;
type n=0;
//计数链表数量
while(p!=NULL)
{
p=p->next;
n++;
}
//判断是否删除位置为1,直接调用头删函数
if(sub==1)
{
head=delete_head(head);
return head;
}
//判断位置为链表尾,调用尾删函数
if(sub==n)
{
head=delete_tail(head);
return head;
}
//判断位置太大,报错
if(sub>n)
{
printf("位置输入过大\n");
return NULL;
}
//删除节点
p=head;
for(int i=1;inext;
}
p->data=p->next->data;
Llist del=p->next;
p->next=del->next;
free(del);
del=NULL;
return head;
}
//按位修改
Llist update(Llist head,type sub,type element)
{
type n=0;
Llist p=head;
//判断链表是否为空
if(head==NULL)
{
return NULL;
}
//链表长度
while(p!=NULL)
{
p=p->next;
n++;
}
//判断sub输入是否合法
if(sub<0 || sub>n)
{
printf("位输入有误\n");
}
//找到链表位置并修改data
p=head;
for(int i=1;inext;
}
p->data=element;
return head;
}
//按位查找
type search(Llist head,type sub)
{
type n=0;
Llist p=head;
//判断链表是否为空
if(head==NULL)
{
return FLASE;
}
//链表长度
while(p!=NULL)
{
p=p->next;
n++;
}
//判断sub输入是否合法
if(sub<0 || sub>n)
{
printf("位输入有误\n");
}
//查找
p=head;
for(int i=0;inext;
}
return p->data;
}
//按元素查找
type search_element(Llist head,type element)
{
type n=0;
if(head==NULL)
{
return FLASE;
}
Llist p=head;
while(p!=NULL)
{
p=p->next;
n++;
}
//循环查找元素
p=head;
for(int i=0;idata==element)
{
return i+1;
}
p=p->next;
}
printf("未找到\n");
return SUCCESS;
}
//按元素删除
Llist delete_element(Llist head,type element)
{
type n=search_element(head,element);
head=delete(head,n);
return head;
}
//按元素修改
Llist update_element(Llist head,type element1,type element)
{
type n=search_element(head,element1);
head=update(head,n,element);
return head;
}
//查询倒数第n个元素
type d_search(Llist head,type sub)
{
type n=0;
Llist p=head;
//判断链表是否为空
if(head==NULL)
{
return FLASE;
}
//链表长度
while(p!=NULL)
{
p=p->next;
n++;
}
//判断sub输入是否合法
if(sub<0 || sub>n)
{
printf("位输入有误\n");
}
p=head;
for(int i=0;inext;
}
return p->data;
}
//单链表排序
Llist sort(Llist head)
{
type n=0;
Llist p=head;
//判断链表是否为空
if(head==NULL)
{
return NULL;
}
//链表长度
while(p!=NULL)
{
p=p->next;
n++;
}
for(int i=0;idata>p->next->data)
{
int t=p->data;
p->data=p->next->data;
p->next->data=t;
}
p=p->next;
}
}
return head;
}
//单链表逆置
Llist inverse(Llist head)
{
type n=0;
Llist p=head;
//判断链表是否为空
if(head==NULL)
{
return NULL;
}
p=head->next;
head->next=NULL;
while(p!=NULL)
{
Llist t=p;
p=p->next;
t->next=head;
head=t;
}
return head;
}
//释放内存
Llist free1(Llist head)
{
type n=0;
Llist p=head;
//判断链表是否为空
if(head==NULL)
{
return NULL;
}
//链表长度
while(p!=NULL)
{
p=p->next;
n++;
}
for(int i=0;i