创建带头结点的循环双链表。
函数接口定义:
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;
}