循环列表的建立

来源:互联网 发布:sem优化 编辑:程序博客网 时间:2024/06/05 07:19

                                                                                                                                 循环列表及输出

#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;
}
1 0
原创粉丝点击