单向循环链表创建、遍历、插入、删除、查找(按位置,按元素值)、清空、销毁

时间:2021-10-10 21:41:56

SinglyLinkedCirculayList.h

#include "stdafx.h"
#include "iostream"
using namespace std;
typedef struct LNode {
	int data;
	struct LNode *next;
}LNode, *LinkedList;
//创建链表
bool createLinkedList(LinkedList &L, int i) {
	if (i < 1)
	{
		cout << "输入不合法!" << endl;
		exit(EXIT_FAILURE);
	}
	L = (LinkedList)malloc(sizeof(LNode));
	if (!L)
	{
		cout << "内存分配失败!" << endl;
		exit(EXIT_FAILURE);
	}
	LinkedList p, q;
	L->next = L;
	p = L;
	int j;
	for (j = 0; j < i; j++)
	{
		q = (LinkedList)malloc(sizeof(LNode));
		if (!q)
		{
			cout << "内存分配失败!" << endl;
			exit(EXIT_FAILURE);
		}
		cout << "请输入第" << j + 1 << "个元素:";
		cin >> q->data;
		p->next = q;
		q->next = L;
		p = q;
	}
	return true;
}
//获取链表的长度
int getListLength(LinkedList &L) {
	if (L->next == L)
	{
		cout << "链表为空" << endl;
		return 0;
	}
	LinkedList p;
	p = L;
	int j = 0;
	while (p->next != L)
	{
		j++;
		p = p->next;
	}
	return j;
}
//打印链表
void printLinkedList(LinkedList &L) {
	LinkedList p;
	p = L;
	if (L->next == L)
	{
		cout << "链表为空!" << endl;
		exit(EXIT_FAILURE);
	}
	for (int i = 0; i < getListLength(L); i++)
	{
		p = p->next;
		cout << p->data << "  ";
	}
	cout << endl;
}
//在第i个位置之前插入元素
bool insertListElem(LinkedList &L, int i, int num) {
	if (i<1 || i>getListLength(L))
	{
		cout << "输入不合法!" << endl;
		exit(EXIT_FAILURE);
	}
	LinkedList p, q;
	p = L;
	for (int j = 0; j < i - 1; j++)   //定位到要插入结点的前一个
	{
		p = p->next;
	}
	q = (LinkedList)malloc(sizeof(LNode));
	if (!q)
	{
		cout << "内存分配失败!" << endl;
		exit(EXIT_FAILURE);
	}
	q->data = num;
	q->next = p->next;
	p->next = q;
	return true;
}
//删除第i个元素
bool deleteListElem(LinkedList &L, int i) {
	if (i<1 || i>getListLength(L))
	{
		cout << "输入不合法!" << endl;
		exit(EXIT_FAILURE);
	}
	LinkedList p,q;
	p = L;
	int j;
	for (j = 0; j < i - 1; j++)   //定位到要删除的结点的前一个
	{
		p = p->next;
	}
	q = p->next;
	p->next = p->next->next;
	free(q);
	return true;
}
//查找并返回第i个位置的元素
int getElem(LinkedList &L, int i) {
	if (i<1 || i>getListLength(L))
	{
		cout << "输入不合法!" << endl;
		exit(EXIT_FAILURE);
	}
	LinkedList p;
	p = L;
	int j;
	for (j = 0; j < i; j++)
	{
		p = p->next;
	}
	return p->data;
}
//在链表中查找节点值等于num的位置,并返回
int searchElem(LinkedList &L, int num) {
	LinkedList p;
	p = L->next;
	int j = 1;
	while ((p->data != num) && (j <= getListLength(L)))
	{
		p = p->next;
		j++;
	}
	if (j <= getListLength(L))
	{
		return j;
	}
	else
	{
		cout << "查找无该值!" << endl;
		return false;
	}
}
//销毁链表
bool destroylinkedlist(LinkedList &L) {
	LinkedList p, q;
	p = q = L->next;
	while (p != L)
	{
		p = p->next;
		free(q);
		q = p;
	}
	free(L);
	return true;
}
//清空链表
bool clearList(LinkedList &L) {
	LinkedList p, q;
	p =q= L->next;
	if (L == NULL)
	{
		return false;
	}
	while (p != L)
	{
		p = p->next;
		free(q);
		q = p;
	}
	L->next = L;
	return true;
}

main.cpp

#include "stdafx.h"
#include "SinglyLinkedCirculayList.h"
int main()
{
	LinkedList list1;
	int length;
	cout << "请输入要创建的链表的长度:";
	cin >> length;
	createLinkedList(list1, length);
	cout << "当前链表为:";
	printLinkedList(list1);
	int i, elem;
	cout << "要插入的位置为:";
	cin >> i;
	cout << "要插入的元素的大小为:";
	cin >> elem;
	insertListElem(list1,i,elem);
	cout << "当前链表为:";
	printLinkedList(list1);
	cout << "要删除的元素的位置为:";
	cin >> i;
	deleteListElem(list1,i);
	cout << "当前链表为:";
	printLinkedList(list1);
	cout << "要查找的元素位置为:";
	cin >> i;
	cout << "该位置元素的值为:";
	cout<<getElem(list1,i)<<endl;
	cout << "要查找的元素大小为:";
	cin >> elem;
	cout << "该元素所在的位置为:";
	cout << searchElem(list1,elem)<<endl;
	cout << "清空链表";
	clearList(list1);
	cout << "打印当前链表:";
	printLinkedList(list1);
	system("pause");
	return 0;
}
单向循环链表创建、遍历、插入、删除、查找(按位置,按元素值)、清空、销毁