循环链表基本操作的实现

#include<stdio.h>
#include<stdlib.h>

#define OK 1;
#define ERROR 0;

typedef int ElemType, Status;

typedef struct LNode
{
	ElemType data;
	struct LNode* next;
}LNode, *LinkList;

typedef struct CircListInfo    //循环链表定义
{
	LinkList head;             //头结点指针
	LNode *pCurNode;           //当前节点指针位置
	int length;                //循环链表长度
}CircListInfo;

Status InitList(CircListInfo& L)    //循环链表初始化
{
	L.head = (LNode*)malloc(sizeof(LNode));
	if (L.head == NULL) exit(0);
	L.head->next = NULL;
	L.pCurNode = L.head;
	L.length = 0;
	return OK;
}

Status DestroyList(CircListInfo& L)    //循环链表销毁
{
	LNode* p = L.head;
	int i = 0;
	LNode* q;
	while (i <= L.length)
	{
		q = p;
		p = p->next;
		free(q);
		i++;
	}
	L.head = NULL;
	L.length = 0;
	L.pCurNode = NULL;
	return OK;
}

Status ClearList(CircListInfo& L)    //循环链表清空
{
	LNode* p = L.head->next;
	int i = 1;
	LNode* q;
	while (i <= L.length)
	{
		q = p;
		p = p->next;
		i++;
		free(q);
	}
	L.head->next = NULL;
	L.length = 0;
	L.pCurNode = L.head;
	return OK;
}

Status ListEmpty(CircListInfo L)    //循环链表判空
{
	if (L.length == 0) return OK;
	return ERROR;
}

Status GetElem(CircListInfo L, int i, ElemType& e)    //循环链表获取元素
{
	if (L.length == 0) return ERROR;
	if (i < 1 || i > L.length) return ERROR;
	int j = 0;
	LNode* p = L.head;
	while (j < i)
	{
		p = p->next;
		j++;
	}
	e = p->data;
	return OK;
}

Status LocateElem(CircListInfo L, ElemType e)    //循环链表元素查找
{
	if (L.length == 0) return ERROR;
	int i = 1;
	LNode* p = L.head->next;
	while (i <= L.length)
	{
		if (p->data == e) return i;
		p = p->next;
		i++;
	}
	return ERROR;
}

Status PriorElem(CircListInfo L, ElemType cur_e, ElemType& pre_e)    //循环链表元素的前驱
{
	if (L.length == 0) return ERROR;
	if (L.head->next->data == cur_e) return ERROR;
	int i = 1;
	LNode* p = L.head->next;
	while (i < L.length)
	{
		if (p->next->data == cur_e)
		{
			pre_e = p->data;
			return OK;
		}
		p = p->next;
		i++;
	}
	return ERROR;
}

Status NextElem(CircListInfo& L, ElemType cur_e, ElemType& next_e)    //循环链表元素的后继
{
	if (L.length <= 1) return ERROR;
	LNode* p = L.head->next;
	int i = 1;
	while (i < L.length)
	{
		if (p->data == cur_e)
		{
			next_e = p->next->data;
			return OK;
		}
		p = p->next;
		i++;
	}
	return ERROR;
}

Status ListTraverse(CircListInfo L)    //循环链表遍历
{
	if (L.length == 0) return ERROR;
	LNode* p = L.head->next;
	int i = 1;
	while (i <= L.length)
	{
		printf("%d ", p->data);
		p = p->next;
		i++;
	}
	printf("\n");
	return OK;
}

Status SetElem(CircListInfo& L, int i, ElemType& e)    //循环链表元素替换
{
	if (L.length == 0) return ERROR;
	if (i < 1 || i > L.length) return ERROR;
	LNode* p = L.head->next;
	int j = 1;
	while (j < i)
	{
		p = p->next;
		j++;
	}
	ElemType temp = e;
	e = p->data;
	p->data = temp;
	return OK;
}

Status InsertElem(CircListInfo& L, int i, ElemType e)    //循环链表插入元素
{
	if (i < 1 || i > L.length + 1) return ERROR;
	LNode* p = (LNode*)malloc(sizeof(LNode));
	if (p == NULL) exit(0);
	p->data = e;
	LNode* q = L.head;
	int j = 0;
	while (j < i - 1)
	{
		q = q->next;
		j++;
	}
	if (i == L.length + 1)
	{
		q->next = p;
		p->next = L.head;
		L.length = L.length + 1;
		return OK;
	}
	p->next = q->next;
	q->next = p;
	L.length = L.length + 1;
	return OK;
}

Status DeleteElem(CircListInfo& L, int i, ElemType& e)    //循环链表元素删除
{
	if (L.length == 0) return ERROR;
	if (i < 1 || i > L.length) return ERROR;
	LNode* p = L.head;
	LNode* q = p;
	int j = 0;
	while (j < i)
	{
		q = p;
		p = p->next;
		j++;
	}
	e = p->data;
	q->next = p->next;
	free(p);
	L.length = L.length - 1;
	return OK;
}

Status CreateList(CircListInfo& L)    //创建循环链表
{
	L.head = (LNode*)malloc(sizeof(LNode));
	if (L.head == NULL) exit(0);
	L.head->next = NULL;
	L.length = 0;
	L.pCurNode = L.head;
	int n;
	printf("循环链表长度:");
	scanf_s("%d", &n);
	int i = 0;
	printf("链表元素:");
	while (i < n)
	{
		LNode* q = L.pCurNode;
		LNode* p = (LNode*)malloc(sizeof(LNode));
		if (p == NULL) exit(0);
		ElemType e;
		scanf_s("%d", &e);
		p->data = e;
		p->next = NULL;
		L.pCurNode->next = p;
		L.pCurNode = p;
		L.length = L.length + 1;
		i++;
	}
	L.pCurNode->next = L.head;
	return OK;
}

int main()
{
	CircListInfo L;
	CreateList(L);
	ListTraverse(L);
	InsertElem(L, 1, 0);
	ListTraverse(L);
	InsertElem(L, 7, 6);
	ListTraverse(L);
	ElemType e;
	DeleteElem(L, 7, e);
	ListTraverse(L);
	PriorElem(L, 1, e);
	printf("1的前驱元素:%d\n", e);
	NextElem(L, 1, e);
	printf("1的后继元素:%d\n", e);
	e = 0;
	SetElem(L, 2, e);
	ListTraverse(L);
	return 0;
}

运行结果如下:
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值