假设一个单循环链表,其结点含有三个域 pre、data、link。其中 data 为数据域;pre 为指针域,它的值为空指针(NIL);link 为指针域,它指向后继结点。请设计算法,将此表改成双向

#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;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值