PTA 6-21 创建循环双链表

创建带头结点的循环双链表。

函数接口定义:

void CreateDuList(DuLinkList &L,int n);// 创建带头结点含n个结点的循环双链表

裁判测试程序样例:

#include<iostream>
using namespace std;
typedef int ElemType;

typedef struct DuLNode {
    ElemType data;
    struct DuLNode *prior; 
    struct DuLNode *next; 
} DuLNode, *DuLinkList;

void CreateDuList(DuLinkList &L,int n) ;

void print(DuLinkList &L)
{
    DuLinkList p;
    int flag=1;
    p = L->prior;
    while (p!=L) {
        if(flag)
            cout << p->data;
        else
            cout << " "<< p->data;
        flag=0;
        p = p->prior;
    }
}

int main() {
    int n;
    cin >> n;
    DuLinkList L;
    CreateDuList(L,n);
    print(L);
    return 0;
} 
/* 请在这里填写答案 */

输入格式:

输入第一行中给出1个整数n(0<n),为双链表结点数。第二行依次输入n个数。

输出格式:

逆序输出双链表结点值。

输入样例:

8
1 3 5 7 9 11 13 15 

输出样例:

15 13 11 9 7 5 3 1 

代码实现:

好的又水一篇

void CreateDuList(DuLinkList &L,int n){
    DuLNode *h,*q;
    h = (DuLNode*)malloc(sizeof(DuLNode)); 
    int i=0,num=0;
    //先建立空链表(不是NULL是首尾相连)
    h->prior=h;
    h->next=h;
    while(i<n){
        q = (DuLNode*)malloc(sizeof(DuLNode)); 
        i++;
        scanf("%d",&num);
        q->data=num;
        q->prior=h->prior;//以下为插入结点操作 背记就完了
        q->next=h;
        h->prior->next=q;
        h->prior=q;
    }
    L=h;
}

  • 6
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值