单循环链表练习

8 篇文章 0 订阅

主要内容

单循环链表学习,包含以下内容:
1、创建循环链表
2、 插入 结点
3、 删除 结点
4、显示链表元素
5、返回元素位置
6、清空链表元素

CircleList.h 文件

#pragma once
#include <iostream>

using namespace std;

typedef struct CLinkList
{
	int data;
	struct CLinkList* next;
}node;

//主菜单
void showMenu()
{
	cout << "*******************************************************" << endl;
	cout << "---------------循环链表操作练习------------------------" << endl;
	cout << "---------------1、创建循环链表 ------------------------" << endl;
	cout << "---------------2、 插入  结点  ------------------------" << endl;
	cout << "---------------3、 删除  结点  ------------------------" << endl;
	cout << "---------------4、显示链表元素 ------------------------" << endl;
	cout << "---------------5、返回元素位置 ------------------------" << endl;
	cout << "---------------6、清空链表元素 ------------------------" << endl;
	cout << "******************************************************" << endl;
}

//创建链表
void createCircleList(node** pNode)
{
	int item;
	node* temp;
	node* target;

	while (1)
	{
		cout << "请输入要创建结点的值,输入0完成结点创建:" ;

		cin >> item;
		cout << endl;
		fflush(stdin); //清楚缓冲区

		if (item == 0)
		{
			return;
		}

		if ((*pNode) == nullptr)
		{
			//循环链表中创建第一个节点
			*pNode = (node*)malloc(sizeof(node));
			if (!(*pNode))
				exit(0);

			(*pNode)->data = item;
			(*pNode)->next = *pNode;
		}
		else
		{
			//找到尾结点
			for (target = (*pNode); target->next != (*pNode); target = target->next)
				;

			temp = (node*)malloc(sizeof(node));
			if (!temp)
				exit(0);
			temp->data = item;
			
			/*temp->next = *pNode;
			target->next = temp;*/

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

//插入结点
//需要定位到第i个位置的前驱结点
void CLinkListInsert(node** pNode, int i,int elem)
{
	node* temp;
	node* target;

	if (i == 1)
	{
		temp = (node*)malloc(sizeof(node));
				
		//找到尾结点
		for (target = (*pNode); target->next != (*pNode); target = target->next)
		{
		};

		temp->data = elem;
		temp->next = (*pNode);
		target->next = temp;

		*pNode = temp;   //重新赋值头结点
	}
	else
	{
		target = (*pNode);
		int j = 1;
		while ( j < i - 1)
		{
			target = target -> next;
			j++;
		}

		temp = (node*)malloc(sizeof(node));
		temp->data = elem;
		temp->next = target->next;
		
		target->next = temp;
	}
}


//删除节点
//仍需要定位到第i个位置的前驱结点
void CLinkListDelete(node** pNode, int i)
{
	node* temp;
	node* target;
	
	if (i == 1)
	{
		//定位尾结点
		for (target = (*pNode); target->next != (*pNode); target = target->next)
			;
			
		temp = *pNode;
		(*pNode) = (*pNode)->next;
		target->next = (*pNode)->next;

		free(temp);
	}
	else
	{
		int j = 1;
		target = *pNode;
		
		for (; j < i - 1; j++)
		{
			target = target->next;
		}
		
		temp = target->next;
		target->next = temp->next;
		free(temp);  //释放内存
	}
}


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

	for (temp = (*pNode);  temp->data != elem && temp->next != (*pNode) ; temp = temp->next , i++)
		;

	if (temp->next == (*pNode))
	{
		cout << "所查找元素不在链表中,错误!" << endl;
		return 0;
	}
	else
		return i;
}


//遍历链表所有元素
void CLinkListShow(node *pNode)
{
	if ((pNode) == nullptr)
	{
		cout << "链表为空链表,请新键链表!" << endl;
		return;
	}

	node* target = pNode;

	cout << "链表元素为:";
	//do   while  先执行 后判断
	do
	{
		cout << target->data << "  " ;
		target = target->next;
	} 	while (target != pNode);

	cout << endl;
}


void clarCLinkList(node** pNode)
{
	//从逻辑上把链表清除 ,内存上尚未完成
	(* pNode) = nullptr;
	
}

CircleList.cpp 文件

#include "CircleList.h"

int main()
{
	
	node* CL = nullptr;

	int select;
	int num;
	int elem;
	
	while (true)
	{
		showMenu();
		CLinkListShow(CL);
		cout << "请输入您的操作:";
		cin >> select;

		switch (select)
		{
		case 1:
			//1、创建链表结点
			createCircleList(&CL);
			CLinkListShow(CL);

			system("pause");
			system("cls");  //清屏
			break;
		case 2:
			//2、插入新节点
			cout << "请输入要插入的结点位置:";			
			cin >> num;
			
			cout << "请输入要插入的元素值:";
			cin >> elem;
			
			CLinkListInsert(&CL, num,elem);

			CLinkListShow(CL);

			system("pause");
			system("cls");
			break;
		case 3:
			//3、删除节点
			cout << "请输入要删除的结点位置:";
			cin >> num;
			
			CLinkListDelete(&CL, num);
			CLinkListShow(CL);

			system("pause");
			system("cls");
			break;
		case 4:
			//4、显示元素值
			CLinkListShow(CL);

			system("pause");
			system("cls");
			break;
		case 5:
			//5、返回某元素值的结点位置
			
			cout << "请输入要返回位置的元素:" << endl;
			cin >> elem;
			
			CLinkListGetPos(&CL, elem);
			system("pause");
			system("cls");
			break;
		case 6:
			//6、清空链表元素
			clarCLinkList(&CL);

			system("pause");
			system("cls");
		default:
			system("cls");
			break;
		}
	}

	return 0;
}

总结

本文主要用于练习单循环链表的相关操作,既有C的内容,也有部分C++的内容,是二者的混合使用,在清空链表操作上,本文仅实现了逻辑上清空,并未完成内存的清理,这是待解决的问题。

参考

小甲鱼

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值