循环列表及输出
#include<iostream>
#include<stdlib.h>struct s
{
int date;
struct s *next;
};//定义结点
int main()
{
struct s *p,*head,*q;
head=(struct s *)malloc(sizeof(struct s));
head->date=11;//定义头节点并给予赋值
q=(struct s*)malloc(sizeof(struct s));
q->date=12;//定义尾节点并给予赋值
head->next=q;
q->next=NULL;//连接头尾节点
for(int i=0;i<10;i++)
{
p=(struct s *)malloc(sizeof(struct s));
p->date=i;
p->next=head->next;
head->next=p;
}
q->next=head;//尾节点指向头结点从而实现循环列表
for(int a=0;a<20;a++)
{
std::cout<<q->date<<" ";
q=q->next;
}//输出20个数
return 0;
}
本文介绍了一种使用C++实现循环列表的方法,并演示了如何通过不断遍历该列表来输出预设数量的数据。通过创建头节点和尾节点,并将新元素插入到头节点之后,最终实现了循环列表结构。
2880

被折叠的 条评论
为什么被折叠?



