【数据结构】循环链表的基本操作c/c++代码实现

 

       最近一直在在学习数据结构,也根据学习的内容实现了一些基本数据结构代码的实现,所以想分享出来,也是学习过程的乐趣所在,也成为继续学习的动力。

本程序算法主要是根据小甲鱼的课程“数据结构与算法编写的”,单循环链表主要实现的以下的功能:

1、建立一个空的循环单链表。

2、获得循环单链表的最后一个结点的位置。

3、输出循环单链表中各结点的值。

4、在循环单链表中查找值为i的结点。

5、在循环单链表中第i个结点后插入值为i的新结点。

6、在循环单链表中删除值为i的结点。

 

 

 

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

using namespace std;

/*链表存储结构的定义*/
typedef struct CLinkList
{
	int data;
	struct CLinkList *next;
}node;

/************************************************************************/
/* 操作                                                                  */
/************************************************************************/

/*初始化循环链表*/
void ds_init(node **pNode)
{
	int item;
	node *temp;
	node *target;

	printf("输入结点的值,输入0完成初始化\n");

	while (1)
	{
		cin>>item;
		fflush(stdin);

		if (item == 0)
			return;

		if ((*pNode) == NULL)
		{ /*循环链表中只有一个结点*/
			*pNode = (node*)malloc(sizeof(struct CLinkList));
			if (!(*pNode))
				exit(0);
			(*pNode)->data = item;
			(*pNode)->next = *pNode;
		}
		else
		{
			/*找到next指向第一个结点的结点*/
			for (target = (*pNode); target->next != (*pNode); target = target->next)
				;

			/*生成一个新的结点*/
			temp = (node *)malloc(sizeof(struct CLinkList));

			if (!temp)
				exit(0);

			temp->data = item;
			temp->next = *pNode;
			target->next = temp;
		}
	}
}

/*插入结点*/
/*参数:链表的第一个结点,插入的位置*/
void ds_insert(node **pNode, int i)
{
	node *temp;
	node *target;
	node *p;
	int item;
	int j = 1;

	printf("输入要插入结点的值:");
	cin >> item;

	if (i == 1)
	{ //新插入的结点作为第一个结点
		temp = (node *)malloc(sizeof(struct CLinkList));

		if (!temp)
			exit(0);

		temp->data = item;

		/*寻找到最后一个结点*/
		for (target = (*pNode); target->next != (*pNode); target = target->next)
			;

		temp->next = (*pNode);
		target->next = temp;
		*pNode = temp;
	}
	else
	{
		target = *pNode;

		for (; j < (i - 1); ++j)
		{
			target = target->next;
		}

		temp = (node *)malloc(sizeof(struct CLinkList));

		if (!temp)
			exit(0);

		temp->data = item;
		p = target->next;
		target->next = temp;
		temp->next = p;
	}
}

/*删除结点*/
void ds_delete(node **pNode, int i)
{
	node *target;
	node *temp;
	int j = 1;

	if (i == 1)
	{ //删除的是第一个结点
		/*找到最后一个结点*/
		for (target = *pNode; target->next != *pNode; target = target->next)
			;

		temp = *pNode;
		*pNode = (*pNode)->next;
		target->next = *pNode;
		free(temp);
	}
	else
	{
		target = *pNode;

		for (; j < i - 1; ++j)
		{
			target = target->next;
		}

		temp = target->next;
		target->next = temp->next;
		free(temp);
	}
}

/*返回结点所在位置*/
int ds_search(node *pNode, int elem)
{
	node *target;
	int i = 1;

	for (target = pNode; target->data != elem && target->next != pNode; ++i)
	{
		target = target->next;
	}

	if (target->next == pNode) /*表中不存在该元素*/
		return 0;
	else
		return i;
}

/*遍历*/
void ds_traverse(node *pNode)
{
	node *temp;
	temp = pNode;
	printf("***********链表中的元素******************\n");

	do
	{
		printf("%4d ", temp->data);
	} while ((temp = temp->next) != pNode);

	printf("\n");
}

int main()
{
	node *pHead = NULL;
	char opp='1';
	int find;

	printf("1.初始化链表 \n\n2.插入结点 \n\n3.删除结点 \n\n4.返回结点位置 \n\n5.遍历链表  \n\n0.退出 \n\n请选择你的操作:");
	while (opp != '0')
	{
		cin >> opp;
		switch (opp)
		{
		case '1':
			ds_init(&pHead);
			printf("\n");
			ds_traverse(pHead);
			break;

		case '2':
			printf("输入需要插入结点的位置?");
			cin >> find;
			ds_insert(&pHead, find);
			printf("在位置%d插入值后:\n", find);
			ds_traverse(pHead);
			printf("\n");
			break;

		case '3':
			printf("输入需要删除的结点位置?");
			cin >> find;
			ds_delete(&pHead, find);
			printf("删除第%d个结点后:\n", find);
			ds_traverse(pHead);
			printf("\n");
			break;

		case '4':
			printf("你要查找倒数第几个结点的值?");
			cin >> find;
			printf("元素%d所在位置:%d\n", find, ds_search(pHead, find));
			//ListTraverse(L);
			printf("\n");
			break;

		case '5':
			ds_traverse(pHead);
			printf("\n");
			break;

		case '0':
			exit(0);
		}
	}

	return 0;
}

 

代码测试结果:

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值