循环单链表

#include<bits/stdc++.h>
using namespace std;
# define  Status  int
# define  error   0;
typedef struct LNode{
	int  data;
	LNode* next;
}LNode,*Linklist;
//初始化
void Initlist(Linklist& L)
{
	L = new LNode;
	L->next = L;
}
//摧毁链表
void deslist(Linklist& L)
{
	LNode* p, * q = L->next;
	while (q != L)
	{
		p = q;
		q = q->next;
		free(p);
	}
	free(L);//释放头结点;
}
//按值查找111
Status  locatelem(Linklist L, int e)//返回位置
{
	LNode* p = L->next;
	int count = 1;
	while (p != L&&p->data!=e)
	{
		p = p->next;
		count++;
	}
	if (p == L)//说明链表中没有此元素
	{
		return error;
	}
	else {
		return count;
	}
}
//按值查找222
LNode* Locatelem(Linklist L, int e)//返回结点即地址
{
	LNode* p = L->next;
	while (p != L)
	{
		if (p->data == e)
			return p;
		p = p->next;
	}
	if (p == L)
	{
		return NULL;
	}
}
//求链表长度
Status lengthlist(Linklist L)
{
	LNode* p = L->next;
	int count = 0;
	while (p != L)
	{
		count++;
		p = p->next;
	}
	return count;
}
// 按位查找
LNode* Getelem(Linklist L, int i)
{
	if (i < 1)
	{
		return NULL;
	}
	LNode* p = L->next;
	int count = 1;
	while (p != L && count < i)
	{
		count++;
		p = p->next;
	}
	if (p == L)return NULL;
	else {
		return p;
	}
}
//插入
bool  Insertlist(Linklist& L, int e,int i)
{
	LNode* p = L, * q;
	int count = 1;
	if (i < 1)
	{
		printf("i所处位置太小\n");
		return false;
	}
	if (p->next == L || i == 1)//注意这里要对空表和i==1进行特判因为他们根本不会进入while循环会导致结果出现问题
	{
		q = new LNode;
		q->data = e;
		q->next = p->next;
		p->next = q;
	}
	else {
		p = p->next;
		while (p != L && count < i - 1)
		{
			count++;
			p = p->next;
		}
		if (p == L && count < i - 1)
		{
			printf("i所处位置太大\n");
			return false;
		}
		else {
			q = new LNode;
			q->data = e;
			q->next = p->next;
			p->next = q;
			return true;
		}
	}
	
}

//删除
bool deletelist(Linklist& L, int &e, int i)
{
	if (i < 1)
	{
		printf("i的位置太小了\n");
		return false;
	}
	LNode* p = L;
	int count = 0;
	while (p->next!= L && count < i-1)
	{
		count++;
		p = p->next;
	}
	if (p->next == L)
	{
		return false;
		printf("i的位置太大\n");
	}
	e = p->next->data;
	p->next = p->next->next;
	return true;
}

//头插法建立链表
void headlist(Linklist& L, int a[],int n)
{
	LNode* p = L,*q;
	for (int i =n-1;i>=0 ; i--)//因为是头插法所以要保证数组的顺序的话就要倒序插入
	{
		q = new LNode;//每次生成一个新结点
		q->data = a[i];
		q->next = p->next;
		p->next = q;
	}
}

//尾插法建立链表
void rearlist(Linklist& L, int a[], int n)
{
	LNode* p = L, * q;
	for (int i = 0; i < n; i++)
	{
		q = new LNode;
		q->data = a[i];
		p->next = q;
		p = q;
	}
}

//遍历链表
void show(Linklist& L)
{
	LNode* p = L->next;
	while (p != L)
	{
		printf("%d ", p->data);
		p = p->next;
	}
	printf("\n");
}
int a[10000000];
int main()
{
	Linklist L;
	Initlist(L);
	int n,b;
	printf("请输入数组的长度:");
	scanf("%d", &n);
	printf("请输入其中的数字:");
	for (int i = 0; i < n; i++)
	{
		scanf("%d", &a[i]);
	}
	headlist(L, a, n);
	show(L);
	printf("链表的长度为:%d\n", lengthlist(L));
	Insertlist(L,10,5);
	deletelist(L,b,5);
	printf("删除元素为:%d\n", b);
	show(L);
	printf("链表的长度为:%d\n", lengthlist(L));
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值