双向链表和单向循环链表的增删改查

head.h

#ifndef __HEAD_H__
#define __HEAD_H__
#include 
#include 
#include 
typedef char datatype;
typedef struct Node
{
	union
	{
		int len;
		datatype data;
	};
	struct Node *next;
	struct Node *prev;
}* Doublelink;

int insert_head(Doublelink L,datatype e);
Doublelink Create_node();
Doublelink Create_head();
void Output(Doublelink L);
int insert_rear(Doublelink L,datatype e);
int delete_head(Doublelink L);
int delete_rear(Doublelink L);
int search_pos(Doublelink L,int pos);
int insert_pos(Doublelink L,int pos,datatype e);
int delete_by_pos(Doublelink L,int pos);
int rev_by_pos(Doublelink L,int pos,datatype e);



#endif

test.c

#include "head4.h"
//申请堆区头部空间
Doublelink Create_head()
{
	Doublelink L=(Doublelink)malloc(sizeof(struct Node));
	if (L==NULL)
		return NULL;
	L->len=0;
	L->next=NULL;
	L->prev=NULL;
	return L;
}
//申请堆区非头空间
Doublelink Create_node()
{
	Doublelink p=(Doublelink)malloc(sizeof(struct Node));
	if (p==NULL)
		return NULL;
	p->next=NULL;
	p->prev=NULL;
	return p;
}
//头插
int insert_head(Doublelink L,datatype e)
{
	if(L==NULL)
		return -1;
	Doublelink p=Create_node();
	if (p==NULL)
		return -1;
	p->data=e;  //p的数据域赋值
	p->next=L->next;//p的指针域指向下一个
	p->prev=L;//p的指针域指向头部
	if(L->next!=NULL)
	{
		L->next->prev=p;
	}
	L->next=p;
	L->len++;//链表的长度增加
}
//链表输出
void Output(Doublelink L)
{
	//判断链表是否存在
	if(L==NULL||L->len==0)
		return;
	Doublelink p=L;
#if 0
	for(int i=0;ilen;i++)
	{
		p=p->next;
		printf("%c\t",p->data);
	}
#endif
	while(p->next)
	{
		p=p->next;
		printf("%c\t",p->data);
	}
	printf("\n");
}
//尾插
int insert_rear (Doublelink L,datatype e)
{
	if(L==NULL)
		return -1;
	Doublelink p=L;
	while(p->next)
	{
		p=p->next;
	}
	Doublelink s=Create_node();
	if(s==NULL)
		return -1;
	s->next=p->next;
	s->data=e;
	s->prev=p;
	p->next=s;
	L->len++;
	return 0; 
}
//头删除
int delete_head(Doublelink L)
{
	if(L==NULL||L->len==0)
	{
		printf("删除失败");
		return -1;
	}
	Doublelink p=L->next;
	L->next=p->next;
	if(p->next!=NULL)
	{
    	p->next->prev=L;
	}
	free(p);
	p=NULL;
	L->len--;
	return 0;
}
//尾删除
int delete_rear(Doublelink L)
{
	if(L==NULL||L->len==0)
	{
		printf("尾删除失败");
		return -1;
	}
	Doublelink p=L;
	while(p->next)
	{
		p=p->next;
	}
	p->prev->next=NULL;
	free(p);
	p=NULL;
	L->len--;
	return 0; 
}
//在任意位置查找
int search_pos(Doublelink L,int pos)
{
	if(L==NULL||pos<1||pos>L->len)
	{
		printf("查找失败");
		return -1;
	}
	Doublelink p=L;
	for(int i=0;inext;
	}
	printf("查找的值为%c\n",p->data);
	return 0;
}
//在任意位置插入
int insert_pos(Doublelink L,int pos,datatype e)
{
	if(L==NULL||pos<1||pos>L->len+1)
	{
		printf("无法插入");
		return -1;
	}
	Doublelink p=L;
	for(int i=0;inext;
	}
	Doublelink s=Create_node();
	if(s==NULL)
		return -1;
	//s的数据域赋值
	s->data=e;
	//s的指针域
	s->prev=p;
	s->next=p->next;
	if(p->next!=NULL)
	{
		p->next->prev=s;
		p->next=s;
	}
	L->len++;
	return 0;

}
//按位置删除
int delete_by_pos(Doublelink L,int pos)
{
	if(L==NULL||pos<1||pos>L->len)
	{
		printf("无法删除");
		return -1;
	}
	Doublelink p=L;
	for(int i=0;inext;
	}
	p->prev->next=p->next;
	p->next->prev=p->prev;
	free(p);
	p=NULL;
	L->len--; 
	return 0;
}
//按位置修改
int rev_by_pos(Doublelink L,int pos,datatype e)
{
	if(L==NULL||pos<1||pos>L->len)
	{
		printf("无法修改");
		return -1;
	}
	Doublelink p=L;
	for(int i=0;inext;
	}
	p->data=e;
	return 0;

}
		

main.c

#include "head4.h"
int main(int argc, const char *argv[])
{
	Doublelink L=Create_head();
	int n;
	datatype e;
#if 0
	printf("请输入需要输入元素的个数");
	scanf("%d",&n);
	for(int i=0;i

head.h

#ifndef __HEAD_H__
#define __HEAD_H__
#include 
#include 
#include 
typedef float datatype;
typedef struct Node
{
	union
	{
		int len;
		datatype data;
	};
	struct Node *next;
}* Looplink;


Looplink Create_head();
Looplink Create_Node();
int insert_rear(Looplink L,datatype e);
int delete_rear(Looplink L);
int delete_head(Looplink L);
void Output(Looplink L);

#endif

test.c

#include "head5.h"
Looplink Create_head()
{
	Looplink L=(Looplink)malloc(sizeof(struct Node));
	if(L==NULL)
		return NULL;
	L->len=0;
	L->next=L;
	return L;
}
Looplink Create_Node()
{
	Looplink p=(Looplink)malloc(sizeof(struct Node));
	if(p==NULL)
		return NULL;
	p->data=0;
	p->next=NULL;
	return p;
}
//尾插
int insert_rear(Looplink L,datatype e)
{
	if(L==NULL)
		return -1;
	Looplink p=L;
	//找到尾部结点
	while(p->next!=L)
	{
		p=p->next;
	}
	//创建新的结点
	Looplink s=Create_Node();
	if(s==NULL)
		return -1;
	//s的数据域
	s->data=e;
	//s的指针域
	p->next=s;
	s->next=L;
	L->len++;
	return 0;
}
//输出遍历
void Output(Looplink L)
{
	if(L==NULL||L->len==0)
		return;
	Looplink p=L;
	while(p->next!=L)
	{
		p=p->next;
		printf("%.2f\t",p->data);
	}
	printf("\n");   
}
//尾删
int delete_rear(Looplink L)
{
	if(L==NULL||L->len==0)
		return -1;
	Looplink p=L;
	for(int i=0;ilen-1;i++)
	{
		p=p->next;
	}
	Looplink q=p;
	q=p->next;
	p->next=L;
	free(q);
	q=NULL;
	L->len--;
	return 0;

}
//头删
int delete_head(Looplink L)
{
	if(L==NULL||L->len==0)
		return -1;
	Looplink p=L->next;
	L->next=p->next;
	free(p);
	p=NULL;
	L->len--;
	return 0;
}

main.c

#include "head5.h"
int main(int argc, const char *argv[])
{
	//在堆区申请头空间
	Looplink L=Create_head();
	int n;
	datatype e;
	//尾插
	printf("请输入要输入值的个数:");
	scanf("%d",&n);
	for(int i=0;i

你可能感兴趣的:(链表,数据结构,c++)