1.循环链表
回顾单链表,单链表只能索引后继节点,不能索引前驱节点。而且单链表只能从头节点出发。
1.1循环链表定义
示意图:
注意:
- 循环链表不一定要有头结点
- 循环链表和单链表的主要差异就在于:空链表的判断上
- 如果单链表为空链表,则可以判断 head->next == NULL;
- 如果循环链表为空链表,则可以判断 hea->next == head;
代码
// 循环链表-单向.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
#define ElemType int
class Node {
public:
Node* next;
ElemType data;
};
class cirList {
public:
Node* head;
int length;
cirList();
~cirList();
void CreatecirList(int n);
bool TravaList();
bool GetElem(int posi, ElemType *value);
bool InsertNode(int posi, ElemType e);
bool DeleteNode(int posi);
int cirListLength();
bool InsertTail(ElemType e);
bool DeleteTail();
};
cirList* L = new cirList();
cirList::cirList()
{
head = new Node;
head->data = 0;
head->next = head; //创建一个空的循环链表
length = 0;
}
cirList::~cirList()
{
delete head;
}
void cirList::CreatecirList(int n)
{
Node* p;
Node* targe;
Node* temp;
for (int i = 1; i <= n; i++)
{
p = new Node;
if (head->next == head) { //只有头结点
p->next = head;
p->data = i;
head->next = p;
}
else {
temp = new Node;
for (targe = head; targe->next != head; targe = targe->next);
temp->next = head;
temp->data = i;
targe->next = temp;
}
}
}
bool cirList::TravaList()
{
Node* p;
p = head->next;
cout << "循环链表的元素为:";
while (p!=head)
{
cout << p->data << " ";
p = p->next;
}
cout << endl;
return true;
}
bool cirList::GetElem(int posi, ElemType* value)
{
Node* p;
p = head;
if (posi<1 || posi>(L->cirListLength()))
{
return false;
}
for (int k = 0; k < posi ; k++)
{
p = p->next;
}
*value = p->data;
return true;
}
int cirList::cirListLength()
{
Node* p;
int length = 1;
p = head->next;
while (p->next != head)
{
p = p->next;
length++;
}
return length;
}
bool cirList::InsertNode(int posi, ElemType e)
{
Node* p, * s;
p = head;
if (posi < 1 || (posi > L->cirListLength()))
{
cout << "插入位置不合法" << endl;
return false;
}
for (int i = 0; i < posi-1; i++)
{
p = p->next;
}
s = new Node;
s->data = e;
s->next = p->next;
p->next = s;
return true;
}
bool cirList::InsertTail(ElemType e)
{
Node* p, * s;
for (p = head->next; p->next != head; p = p->next)
;
printf("这是一个尾结点值:%d,", p->data);
s = new Node;
s->data = e;
s->next = p->next;
p->next = s;
return true;
}
bool cirList::DeleteNode(int posi)
{
Node* p, * q;
p = head;
if (posi < 1 || (posi > L->cirListLength()))
{
cout << "删除位置不合法" << endl;
return false;
}
if (head->next == head)
{
cout << "无节点可删除" << endl;
return false;
}
for (int k = 0; k < posi-1; k++)
{
p = p->next;
}
q = p->next;
p->next = q->next;
free(q);
return true;
}
bool cirList::DeleteTail()
{
Node* p, * q;
if (head->next == head)
{
cout << "无节点可删除" << endl;
return false;
}
for (p = head; p->next->next != head; p = p->next); //找到最后一个节点的前一个节点
q = p->next;
p->next = q->next;
delete q;
return true;
}
int main()
{
bool k;
ElemType value;
int posi;
L->CreatecirList(10);
k = L->TravaList();
cout << "length = " << L->cirListLength() << endl;
cout << "输入chaxun元素位置:";
cin >> posi;
k = L->GetElem(posi, &value);
printf("第%d个元素位置的值为:%d\n", posi, value);
cout << "输入要插入的位置:"; cin >> posi;
cout << "输入要插入的值:"; cin >> value;
L->InsertNode(posi, value);
k = L->TravaList();
cout << "输入尾结点值:"; cin >> value;
L->InsertTail(value);
k = L->TravaList();
while (1)
{
cout << "输入要删除的位置:"; cin >> posi;
L->DeleteNode(posi);
k = L->TravaList();
cout << "输入尾结点后:" ;
L->DeleteTail();
k = L->TravaList();
}
}