单向循环链表改为双向循环链表

/*
单项循环链表改双向循环链表
data数据域,next后继结点指针域,prior前驱结点指针域

说明:
这里的链表带头结点
*/
#include <iostream> 
#include <cstdlib>
using namespace std;
const int flag = -1;
typedef struct SiLinkCirList{
	int data;
	struct SiLinkCirList *next, *prior;
}*List;
//创建单向循环链表
void creSiLinkCirList(List &L){//system("pause");
	int x;
	L = (List)malloc(sizeof(SiLinkCirList));
	List r = L;
	while(cin>>x && x!=flag){
		List s = (List)malloc(sizeof(SiLinkCirList));
		s->data = x;
		r->next = s;
		r = s;
	}
	r->next = L;
}
//输出单向循环链表
void printSiCirList(List L){
	List p = L->next;
	cout << "当前的单向循环链表为:";
	while(p != L){
		cout << p->data << " ";
		p = p->next;
	}
	cout << endl;
}
//转换成双向循环链表
void trDouCirList(List &L) {
	List p = L;
	List s;
	while(1){
		s = p->next;
		s->prior = p;
		if(p->next==L)
			break;
		p = p->next;
	}
	L->prior = p;	
}
//根据prior输出链表
void printByPrior(List L) {
	List p = L->prior;
	cout << "反向输出的循环链表为:" ;
	while(p != L){
		cout << p->data << " ";
		p = p->prior;
	}
	cout << endl;
}
int main(){
	List L;
	cout << "输入单向循环链表元素:";
	creSiLinkCirList(L);
	printSiCirList(L);
	trDouCirList(L);
	printByPrior(L);
	return 0;
}
  • 3
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值