标签:
1.定义
在单链表中,如果将终端结点的指针域有空指针指向头结点,则整个链表称为一个环,这种头尾相接的单链表称为循环单链表,简称循环链表。
2.代码
#include <iostream>
using namespace std;
struct Node
{
int data;
Node *next;
};
class CLL
{
public:
CLL(int a[], int n); //建立有n个元素的循环单单链表
int Length();
void PrintList(); //遍历单链表,按序号依次输出各元素
void Insert(int i, int x);
int Get(int i);
int Delete(int i);
private:
Node *first; //循环单链表的头指针
};
CLL::CLL(int a[], int n)
{
first = new Node;
first->next = first; //初始化一个空循环链表
for (int i = 0; i<n; i++)
{
Node *s;
s = new Node; //为每个数组元素建立一个结点
s->data = a[n-i-1];
s->next = first->next;
first->next = s; //s插入到循环链表中
}
}
void CLL::PrintList()
{
Node *p;
p = first->next;
while (p != first)
{
cout << p->data << " ";
p = p->next;
}
cout << endl;
}
void main()
{
int r[5] = { 1, 9, 7, 2, 5 };
CLL L(r, 5);
cout << "线性表的数据为:" << endl;
L.PrintList();
}
标签:
原文地址:http://www.cnblogs.com/jfl-xx/p/4905763.html