链表(二)循环链表

1.概念
循环链表最后一个节点的指针域指向头结点,整个链表形成一个环。

注 :图为单循环链表,类似的还有双向循环链表和多重链的循环链表。

2.区分
单链表遍历判别条件为p!=NULL循环链表为p!=L
3.存储结构

typedef struct node{

 int data; //数据域
 struct node*next; //指向直接后继的指针

}node,*Lnode;

4.实例

#include <iostream>
#include <string.h>
#define OK 1
#define ERROR 0
using namespace std;

typedef struct node{

 int data;
 struct node*next;

}node,*Lnode;

void init(Lnode &L)
{
    L=new node;
    L->next = NULL;
}

void add(Lnode &L,Lnode &R,int d)
{
    node *n=new node;
    n->data=d;
    n->next=NULL;
    R->next=n;
    R=n;
}
void display(Lnode L,Lnode R)
{
    node *p=NULL;
    p=L->next;
    while(p!=R)
    {
        cout<<"==>"<<p->data;
        p=p->next;
    }
    cout<<"==>"<<R->data;
}
void newlist(Lnode &L,Lnode &R)
{
    int n=1;
    while(n)
    {
    int t;
    cout<<"InPut.";cin>>t;
    add(L,R,t);
    cout<<" Do you want new a node[1/0] agin?"<<endl;
    cin>>n;
    }
    R->next=L->next;
}
int main()
{
    Lnode L,R;
    init(L);
    R=L;
    newlist(L,R);
    display(L,R);
}

5.运行结果

原文地址:https://www.cnblogs.com/hugboy/p/xhlb.html