链表(二)循环链表

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.运行结果

posted @   HUGBOY  阅读(383)  评论(0编辑  收藏  举报
编辑推荐:
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· .NET 适配 HarmonyOS 进展
· .NET 进程 stackoverflow异常后,还可以接收 TCP 连接请求吗?
阅读排行:
· 本地部署 DeepSeek:小白也能轻松搞定!
· 基于DeepSeek R1 满血版大模型的个人知识库,回答都源自对你专属文件的深度学习。
· 在缓慢中沉淀,在挑战中重生!2024个人总结!
· 大人,时代变了! 赶快把自有业务的本地AI“模型”训练起来!
· Tinyfox 简易教程-1:Hello World!
点击右上角即可分享
微信分享提示