#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef int Elemtype;
typedef struct binode
{
Elemtype data;
struct binode *next;
struct binode *prev;
int freq;
}Binode;
typedef struct bihead
{
Binode *first;
Binode *last;
int num;
}Bihead;
Bihead *Create_DouLinklistHead(void)
{
Bihead *bihead = malloc(sizeof(Bihead));
bihead->first = bihead->last = NULL;
bihead->num = 0;
Elemtype d;
printf("请输入数据:\n");
while(1)
{
scanf("%d",&d);
if(d == 0)
{
break;
}
Binode *pnew = malloc(sizeof(Binode));
pnew->data = d;
pnew->freq = 0;
pnew->next = pnew->prev = NULL;
if(bihead->num == 0)
{
bihead->first = bihead->last = pnew;
}
else
{
pnew->prev = bihead->last;
bihead->last->next = pnew;
bihead->last = pnew;
}
bihead->num++;
}
return bihead;
}
void Print_DouLisklistHead1(Bihead *bihead)
{
printf("此链表中节点个数为%d个,顺序打印:\n",bihead->num);
Binode *p = bihead->first;
while(p)
{
printf("%d\t",p->data);
p = p->next;
}
putchar('\n');
p = bihead->first;
while(p)
{
printf("%d\t",p->freq);
p = p->next;
}
putchar('\n');
}
void Print_DouLisklistHead2(Bihead *bihead)
{
printf("此链表中节点个数为%d个,逆序打印:\n",bihead->num);
Binode *p = bihead->last;
while(p)
{
printf("%d\t",p->data);
p = p->prev;
}
putchar('\n');
p = bihead->last;
while(p)
{
printf("%d\t",p->freq);
p = p->prev;
}
putchar('\n');
}
int main()
{
Bihead *bihead = Create_DouLinklistHead();
Print_DouLisklistHead1(bihead);
Print_DouLisklistHead2(bihead);
}