【link.h】
#ifndef __LINK_H__
#define __LINK_H__
#include <stdlib.h>
typedef struct link
{
struct link *next;
int data;
}link_t;
link_t* linkcreate();//创建链表
int headinsert(link_t *header,int indata);//头插法
int print(link_t *header);//打印
int tailinsert(link_t *header,int indata);//尾插法
int poseinsert(link_t *header,int pose,int indata);//按位置插入
int delhead(link_t *header);//头删法
int deltail(link_t *header);//尾删法
int delpose(link_t *header,int pose);//位置删除
int modpose(link_t *header,int pose,int indata);//按位置修改
int findpose(link_t *header,int pose);//按位置查找
int datafind(link_t *header,int indata);//按值查找
int datapose(link_t *header,int indata,int data);//按值修改
int print(link_t *header);//打印
int free_link(link_t *p);//清除链表
#endif
【main.c】
#include <stdio.h>
#include "link.h"
int main(int argc, const char *argv[])
{
link_t *header=linkcreate();
headinsert(header,10);
headinsert(header,20);
headinsert(header,30);
headinsert(header,40);
headinsert(header,50);
tailinsert(header,60);
poseinsert(header,7,2);
delhead(header);
deltail(header);
delpose(header,1);
modpose(header,1,99);
findpose(header,1);
datafind(header,20);
datapose(header,10,124);
free_link(header);
print(header);
return 0;
}
【link.c】
#include <stdio.h>
#include "link.h"
link_t* linkcreate()//创建链表
{
link_t *header=NULL;
header=(link_t *)malloc(sizeof(link_t));
if(header==NULL)
{
printf("申请失败\n");
return NULL;
}
else
{
printf("申请成功\n");
}
header->data=0;
header->next=NULL;
return header;
}
int headinsert(link_t *header,int indata)//头插法
{
link_t *node=(link_t *)malloc(sizeof(link_t));
if(node==NULL)
{
return -1;
}
node->data=indata;
node->next=header->next;
header->next=node;
}
int tailinsert(link_t *header,int indata)//尾插法
{
link_t *p=header;
link_t *pr=(link_t *)malloc(sizeof(link_t));
if(pr==NULL)
{
return -1;
}
if(p->next==NULL)
{
pr->data=indata;
p->next=pr;
pr->next=NULL;
}
else
{
while(p->next!=NULL)
{
p=p->next;
}
pr->data=indata;
p->next=pr;
pr->next=NULL;
}
}
int poseinsert(link_t *header,int pose,int indata)//按位置插入
{
int count=0;
link_t *p=header;
link_t *pr=(link_t *)malloc(sizeof(link_t));
if(pr==NULL)
{
return -1;
}
while(p->next!=NULL)
{
count++;
p=p->next;
}
if(pose>count+1||pose<1)
{
printf("位置不合理\n");
}
p=header;
while(p->next!=NULL)
{
if(pose!=1)
{
pose--;
p=p->next;
}
else
{
pr->data=indata;
pr->next=p->next;
p->next=pr;
break;
}
}
if(p->next==NULL)
{
tailinsert(header,indata);
}
}
int delhead(link_t *header)//头删法
{
link_t *p=header->next;
header->next=p->next;
}
int deltail(link_t *header)//尾删法
{
link_t *p=header;
int count=0;
while(p->next!=NULL)
{
count++;
p=p->next;
}
p=header;
while(p->next!=NULL)
{
if(count!=1)
{
p=p->next;
count--;
}
else
{
p->next=NULL;
break;
}
}
}
int delpose(link_t *header,int pose)//位置删除
{
link_t *p=header;
while(p->next!=0)
{
if(pose!=1)
{
p=p->next;
pose--;
}
else
{
p->next=p->next->next;
break;
}
}
}
int modpose(link_t *header,int pose,int indata)//按位置修改
{
link_t *p=header;
while(p->next!=0)
{
if(pose!=0)
{
p=p->next;
pose--;
}
else
{
p->data=indata;
break;
}
}
}
int findpose(link_t *header,int pose)//按位置查找
{
link_t *p=header;
while(p->next!=NULL)
{
if(pose!=0)
{
p=p->next;
pose--;
}
else
{
printf("该下标的值为%d\n",p->data);
break;
}
}
}
int datafind(link_t *header,int indata)//按值查找
{
link_t *p=header->next;
int count=1;
while(p->data!=indata)
{
p=p->next;
count++;
}
printf("该数值的下标为%d\n",count);
}
int datapose(link_t *header,int indata,int data)//按值修改
{
link_t *p=header->next;
int count=1;
while(p->data!=indata)
{
p=p->next;
count++;
}
modpose(header,count,data);
}
int print(link_t *header)//打印
{
link_t *pcurrent=(link_t *)malloc(sizeof(link_t));
if(pcurrent==NULL)
{
return -1;
}
pcurrent=header->next;
while(pcurrent!=NULL)
{
printf("%d\n",pcurrent->data);
pcurrent=pcurrent->next;
}
}
int free_link(link_t *p)//清除链表
{
link_t *q;
while(p->next)
{
q=p->next;
p->next=q->next;
free(q);
}
}