/*
10.设计一个算法,以后进先出的方式
创建一个带头结点的双向链表,并输出 。
*/
# include <iostream>
# include <stdio.h>
# include <stdlib.h>
using namespace std;
typedef struct Node
{
int elem;
struct Node* prior;
struct Node* next;
}*Linklist;
void inlinklist(Linklist &l, int n)
{
Linklist p;
l = (Linklist) malloc (sizeof(Node));
l->next = NULL;
l->prior = NULL;
p = (Linklist) malloc (sizeof(Node));
scanf("%d", &p->elem);
p->next = l->next;//头插
l->next = p;
printf("请输入链表元素:\n");
for(int i = 0; i<n; i++)
{
p = (Linklist) malloc (sizeof(Node));
scanf("%d", &p->elem);
p->next = l->next;
l->next->prior = p;
l->next = p;
p->prior = l;
}
}
void outlinklist(Linklist l)
{
printf("链表中的元素为:\n");
Linklist p;
p = l->next;
while(p != NULL)
{
printf("%d", p->elem);
p = p->next;
}
printf("\n");
}
int main()
{
Linklist l;
int n;
printf("请输入链表中的元素个数:\n");
scanf("%d", &n);
inlinklist(l, n);
outlinklist(l);
return 0;
}