#include<stdio.h>
#include<stdlib.h>
struct Lain
{
int data;
struct Lain *pre,*link;
};
struct Lain *Create();
void Shu(struct Lain *head);
void SShu(struct Lain *head);
struct Lain *Shuang(struct Lain *head);
int main()
{
struct Lain *head;
printf("请输入链表结点元素:\n");
head=Create(); //创建符合题目的初始链表
printf("链表创建结果如下:\n");
Shu(head); //输出链表
head=Shuang(head);
printf("双向循环链表创建结果如下:\n");
SShu(head); //输出链表(稍微改动了一下,因为它是循环链表,不存在下一个为空的状态)
return 0;
}
struct Lain *Create()
{
struct Lain *head=NULL,*p,*p1;
p=(struct Lain *)malloc(sizeof(struct Lain));
p->pre =NULL;
p->link =NULL;
scanf("%d",&p->data );
while(p->data !=-1)
{
if(head==NULL)
{
head=p;
p1=p;
}
else
{
p1->link =p;
p1=p;
}
p=(struct Lain *)malloc(sizeof(struct Lain));
p->pre =NULL;
p->link =NULL;
scanf("%d",&p->data );
}
return head;
}
void Shu(struct Lain *head)
{
struct Lain *p;
p=head;
while(p!=NULL )
{
printf("%-3d",p->data );
p=p->link ;
}
putchar('\n');
}
void SShu(struct Lain *head)
{
struct Lain *p;
p=head;
while(p->link !=head )
{
printf("%-3d",p->data );
p=p->link ;
}
printf("%-3d",p->data );
putchar('\n'); //这里主要是测试循环成功没有
printf(":%d\n",p->link->data );
putchar('\n');
printf(":%d\n",head->pre ->data );
putchar('\n');
}
struct Lain *Shuang(struct Lain *head)
{
struct Lain *p,*l;
p=head;
l=head->link ;
while(l!=NULL)
{
l->pre =p;
p=p->link ;
l=l->link ;
}
p->link =head;
head->pre =p;
return head;
}
假设一个单循环链表,其结点含有三个域 pre、data、link。其中 data 为数据域;pre 为指针域,它的值为空指针(NIL);link 为指针域,它指向后继结点。请设计算法,将此表改成双向
于 2022-11-08 16:59:38 首次发布