头文件
#ifndef __HEAD_H__
#define __HEAD_H__
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
typedef int data_type;
typedef struct node{
data_type data;
struct node *next;
}*linklist;
linklist creat_node();
linklist toucha(linklist head,data_type element);
int output(linklist head);
linklist weicha(linklist head,data_type element);
linklist toushan(linklist head);
linklist weishan(linklist head);
int length(linklist head);
linklist charu_pos(linklist head,int pos,data_type element);
linklist shanchu_pos(linklist head,int pos);
linklist xiugai_pos(linklist head,int pos,data_type element);
int chazhao_pos(linklist head,int pos);
int chazhao_ele(linklist head,data_type element );
linklist xiugai_ele(linklist head,data_type key,data_type element);
linklist shanchu_ele(linklist head,data_type element);
linklist nizhi(linklist head);
#endif
2函数部分
#include "head.h"
linklist creat_node() //创建节点
{
linklist s=(linklist)malloc(sizeof(struct node));
if(s==NULL)
return NULL;
s->data=0;
s->next=NULL;
return s;
}
linklist toucha(linklist head,data_type element)
{
linklist s=creat_node();
if(NULL==s)
return head;
s->data=element;
if(NULL==head)
head=s;
else
{
s->next=head;
head=s;
}
return head;
}
int output(linklist head)
{
if(NULL==head)
return -1;
linklist p=head;
while(p!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
printf("\n");
return 0;
}
linklist weicha(linklist head,data_type element)
{
linklist s=creat_node();
if(NULL==s)
return head;
s->data=element;
if(NULL==head)
head=s;
else
{
linklist p=head;
while(p->next!=NULL)
p=p->next;
p->next=s;
}
return head;
}
linklist toushan(linklist head)
{
if(head==NULL)
return head;
else
{
linklist del=head;
head=head->next;
free(del);
del=NULL;
}
return head;
}
linklist weishan(linklist head)
{
if(head==NULL)
return head;
if(head->next==NULL)
{
free(head);
head=NULL;
return head;
}
linklist del=head;
while(del->next->next!=NULL)
{
del=del->next;
}
free(del->next);
del->next=NULL;
return head;
}
int length(linklist head)
{
int count=0;
linklist p=head;
while(p!=NULL)
{
count++;
p=p->next;
}
return count;
}
linklist charu_pos(linklist head,int pos,data_type element)
{
if(pos<1||pos>length(head)+1)
return head;
if(head==NULL||pos==1)
head=toucha(head,element);
else
{
linklist p=head;
int i;
for(i=1;i<pos-1;i++)
p=p->next;
linklist s=creat_node();
if(s==NULL)
return head;
s->data=element;
s->next=p->next;
p->next=s;
}
return head;
}
linklist shanchu_pos(linklist head,int pos)
{
if(pos<1||pos>length(head)||head==NULL)
return head;
if(head->next==NULL)
{
head=toushan(head);
return head;
}
else
{
linklist p=head;int i;
for(i=1;i<pos-1;i++)
p=p->next;
linklist q=p->next;
p->next=q->next;
free(q);
q=NULL;
}
return head;
}
linklist xiugai_pos(linklist head,int pos,data_type element)
{
if(head==NULL||pos<1||pos>length(head))
return head;
linklist p=head;
int i=1;
for(i=1;i<pos;i++)
{
p=p->next;
}
p->data=element;
return head;
}
int chazhao_pos(linklist head,int pos)
{
if(head==NULL||pos<1||pos>length(head))
return -1;
int i=1;
linklist p=head;
for(i=1;i<pos;i++)
p=p->next;
printf(" %d \n",p->data);
}
int chazhao_ele(linklist head,data_type element )
{
if(head==NULL)
return -1;
linklist p=head;
int i=1;
int index=1;
for(i=1;i<length(head)+1;i++)
{
if(p->data==element)
break;
index++;
p=p->next;
}
return index;
}
linklist xiugai_ele(linklist head,data_type key,data_type element)
{
if(head==NULL)
return head;
int pos=chazhao_ele(head,key);
head=xiugai_pos(head,pos,element);
return head;
}
linklist shanchu_ele(linklist head,data_type element)
{
if(head==NULL)
return head;
int pos=chazhao_ele(head,element);
head=shanchu_pos(head,pos);
return head;
}
linklist nizhi(linklist head)
{
if(head==NULL||head->next==NULL)
return head;
linklist p=head->next;
head->next=NULL;
while(p!=NULL)
{
linklist t=p;
p=p->next;
t->next=head;
head=t;
}
return head;
}
3主函数
#include "head.h"
int main()
{
//linklist p=creat_node();
linklist head=NULL;
int i,n;
data_type element;
#if 0 //输入
printf("please input n:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("please input %d element:",i+1);
scanf("%d",&element);
head=toucha(head,element);
}
int op_flag=output(head);
if(op_flag==-1)
printf("output error\n");
#endif
#if 0 // 尾插
printf("please input n:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("please input %d element:",i+1);
scanf("%d",&element);
head=weicha(head,element);
}
int op_flag=output(head);
if(op_flag==-1)
printf("output error\n");
#endif
#if 0 // 头删
printf("please input n:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("please input %d element:",i+1);
scanf("%d",&element);
head=weicha(head,element);
}
head=toushan(head);
head=toushan(head);
if(head==NULL)
printf("已删完\n");
int op_flag=output(head);
if(op_flag==-1)
printf("output empty\n");
#endif
#if 0 // 尾删
printf("please input n:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("please input %d element:",i+1);
scanf("%d",&element);
head=weicha(head,element);
}
head=weishan(head);
head=weishan(head);
if(head==NULL)
printf("已删完\n");
int op_flag=output(head);
if(op_flag==-1)
printf("output empty\n");
#endif
#if 0 // 插入bypos 有错
printf("please input n:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("please input %d element:",i+1);
scanf("%d",&element);
head=weicha(head,element);
}
int pos;
printf("input pos:");
scanf("%d",&pos);
printf("插入值:");
scanf("%d",&element);
head=charu_pos(head,pos,element);
int op_flag=output(head);
if(op_flag==-1)
printf("output empty\n");
#endif
#if 0 // 按位置删除
printf("please input n:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("please input %d element:",i+1);
scanf("%d",&element);
head=weicha(head,element);
}
int pos;
printf("input pos:");
scanf("%d",&pos);
head=shanchu_pos(head,pos);
int op_flag=output(head);
if(op_flag==-1)
printf("output empty\n");
#endif
#if 0 // 按位置修改
printf("please input n:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("please input %d element:",i+1);
scanf("%d",&element);
head=weicha(head,element);
}
int pos;
printf("input pos:");
scanf("%d",&pos);
printf("input element:");
scanf("%d",&element);
head=xiugai_pos(head,pos,element);
int op_flag=output(head);
if(op_flag==-1)
printf("output empty\n");
#endif
#if 0 // 按位置查找
printf("please input n:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("please input %d element:",i+1);
scanf("%d",&element);
head=weicha(head,element);
}
int pos;
printf("input pos:");
scanf("%d",&pos);
int flag=chazhao_pos(head,pos);
if(flag==-1)
printf("head==NULL or pos error\n");
// int op_flag=output(head);
// if(op_flag==-1)
// printf("output empty\n");
#endif
#if 0 // 按元素查找返回pos
printf("please input n:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("please input %d element:",i+1);
scanf("%d",&element);
head=weicha(head,element);
}
printf("input element:");
scanf("%d",&element);
int flag=chazhao_ele(head,element);
if(flag==-1)
printf("head NULL\n");
printf("%d位置是%d\n",element,flag);
// int op_flag=output(head);
// if(op_flag==-1)
// printf("output empty\n");
#endif
#if 0 // 按值修改 先找位置,再修改
printf("please input n:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("please input %d element:",i+1);
scanf("%d",&element);
head=weicha(head,element);
}
data_type key;
printf("input 被改值:");
scanf("%d",&key);
printf("input 修改为:");
scanf("%d",&element);
head=xiugai_ele(head,key,element);
int op_flag=output(head);
if(op_flag==-1)
printf("output empty\n");
#endif
#if 0 // 按值删除 先找位置,再删除
printf("please input n:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("please input %d element:",i+1);
scanf("%d",&element);
head=weicha(head,element);
}
printf("input 删除值:");
scanf("%d",&element);
head=shanchu_ele(head,element);
int op_flag=output(head);
if(op_flag==-1)
printf("output empty\n");
#endif
#if 1 // 逆置
printf("please input n:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("please input %d element:",i+1);
scanf("%d",&element);
head=weicha(head,element);
}
head=nizhi(head);
int op_flag=output(head);
if(op_flag==-1)
printf("output empty\n");
#endif
return 0;
}
4 按位置删除
5按位置查找
6查找元素位置并返回
7按元素修改
8按元素删除
9逆置