test.c(链表自定义函数)
#include "head.h"
//创建节点函数
//1创建头节点,0创建普通节点
linklist creat_node(int flag)
{
linklist s=(linklist)malloc(sizeof(struct node));
if(NULL==s)
{
return NULL;
}
if(flag==1)
{
s->len=0;
}
else if(flag==0)
{
s->data=0;
}
//指针域初始化
s->next=NULL;
return s;
}
//头插函数
int insert_head(datatype element,linklist head)
{
//1判断头节点是否存在
if(head==NULL)
{
return FAULSE;
}
//2头插入
//2.1创建新节点
linklist s=creat_node(0);
//2.2判断是否为空
if(s==NULL)
return FAULSE;
//2.3新节点赋值
s->data=element;
//2.4插入新节点
s->next=head->next;
head->next=s;
//2.5链表长度加1
head->len++;
return SUCCESS;
}
//输出函数
int output(linklist head)
{
//判断头结点是否为空
if(head==NULL)
return FAULSE;
linklist p=head->next;
while(p!=NULL)
{
printf("%-3d",p->data);
p=p->next;
}
return SUCCESS;
}
//尾插函数
int insert_rear(datatype element,linklist head)
{
//1判断是否为空
if(head==NULL)
return FAULSE;
//2尾部插入
//创建新节点
linklist s=creat_node(0);
//判断新节点空否
if(s==NULL)
return FAULSE;
//找到最后一个节点
linklist p=head;
while(p->next!=NULL)
{
p=p->next;
}
//在后面插入
p->next=s;
//链表长度加1
return SUCCESS;
}
//头删函数
int delete_first(linklist head)
{
if(head==NULL)
return FAULSE;
if(head->next==NULL)
return FAULSE;
linklist del=head->next;
head->next=del->next;
free(del);
del=NULL;
head->len--;
return SUCCESS;
}
//尾删函数
int delete_tail(linklist head)
{
//判断头结点,元素是否为空
if(head==NULL||head->next==NULL)
return FAULSE;
//p的next的next是否为空
linklist p=head;
while(p->next->next!=NULL)
{
p=p->next;
}
//释放p的next
free(p->next);
//置空
p->next=NULL;
//链表长度减1
head->len--;
}
//按位置插入
int pos_insert(int pos,datatype element,linklist head)
{
//1判断头节点是否为空,插入pos是否合法
if(head==NULL||pos<1||pos>head->len+1)
return FAULSE;
//2创建新节点,并赋值
linklist s=creat_node(0);
s->data=element;//插入的值
//3链接,插入新节点
//3.1遍历到需插入节点的前一位
linklist p=head;
for(int i=0;i<pos-1;i++)
{
p=p->next;
}
//3.2开始插入
s->next=p->next;
p->next=s;
//3.3链表长度加1
head->len++;
}
int pos_delete(int pos,linklist head)
{
//判断头节点是否存在,其他节点是否存在,位置是否合法
if(head==NULL||head->next==NULL||pos<1||pos>head->len)
return FAULSE;
//删除
//1找到pos-1,p
linklist p=head;
for(int i=0;i<pos-1;i++)
{
p=p->next;
}
//2保存pos节点到del,p指向pos的后一个
linklist del=p->next;
p->next=p->next->next;
//3释放del,指针置空
free(del);
//4链表减1
head->len--;
return SUCCESS;
}
int pos_change(int pos,datatype element,linklist head)
{
//判断头节点是否存在,其他节点是否存在,位置是否合法
if(head==NULL||head->next==NULL||pos<1||pos>head->len)
return FAULSE;
//便利找到pos位置,替换值为element
linklist p=head;
for(int i=0;i<pos;i++)
{
p=p->next;
}
p->data=element;
return SUCCESS;
}
int pos_find(int pos,linklist head)
{
//判断头节点是否存在,其他节点是否存在,位置是否合法
if(head==NULL||head->next==NULL||pos<1||pos>head->len)
return FAULSE;
//便利找到pos位置,返回data
linklist p=head;
for(int i=0;i<pos;i++)
{
p=p->next;
}
printf("%d\n",p->data);
return SUCCESS;
}
int key_find(datatype key,linklist head)
{
//判断链表是否为空
if(head==NULL||head->next==NULL)
return FAULSE;
//从第一个节点(头后)遍历链表,对key进行匹配
linklist p=head->next;
for(int i=1;i<p->len;i++)//i与链表标号重合
{
if(p->data==key)
{ printf("位置为:%d",i);
return i;
}
p=p->next;
}
return FAULSE;
}
int key_change(datatype key,datatype element,linklist head)
{
//判断链表是否为空
if(head==NULL||head->next==NULL)
return FAULSE;
//第一个节点遍历,查找key,然后修改值
linklist p=head->next;
for(int i=1;i<p->len;i++)//i与链表标号重合
{
if(p->data==key)
{ p->data=element;
printf("修改后的值为:%d",p->data);
return SUCCESS;
}
p=p->next;
}
return FAULSE;
}
int key_delete(datatype key,linklist head)
{
//判断链表是否为空
if(head==NULL||head->next==NULL)
return FAULSE;
//p的next遍历到key,再删除节点
linklist p=head;
while(p->next!=NULL)
{
if(p->next->data==key)
{
linklist del=p->next;
p->next=del->next;
free(del);
return SUCCESS;
}
p=p->next;
}
return FAULSE;
}
int nizhi(linklist head)
{
//判断链表是否为空
//判断是否为一个节点或没有节点
if(head==NULL||head->len<=1)
return FAULSE;
//逆置
}