双向循环链表

双向循环链表中有一些比较明显的BUG,没有改正,但是我主要是通过程序深入理解双向循环链表,最近比较忙,抽出一小点时间,刚写的,调试过,能用。主要理解思想,BUG这个当中BUG不用过多理会。

// 双向链表.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

struct link
{
	int data;
	struct link *next;
	struct link *poir;
};

link *createLink()
{
	link *head=new link;
	head->data=-1;
	link *node=new link;
	cin>>node->data;
	head->next=node;
	node->poir=head;
	link *l=node;
	while(node->data!=0)
	{
		node=new link;
		cin>>node->data;
		l->next=node;
		node->poir=l;
		l=node;
	}
	node->next=head;
	head->poir=node;
	return head;
}
void printNext(link *head)/*向前打印链表*/
{
	head=head->next;
	while(head->data!=-1)
	{
		cout<<head->data;
		head=head->next;
	}
	cout<<endl;
}
void printPoir(link *head)/*向后打印链表*/
{
	
	head=head->poir;
	while(head->data!=-1)
	{
		cout<<head->data;
		head=head->poir;
	}
	cout<<endl;
}
void insert(link *l,int data,int pos)/*向链表插入节点*/
{
	l=l->next;
	for(int i=0;i<pos-2;i++)
		l=l->next;
	 link *node=new link;
	 node->data=data;
	 node->next=l->next;
	 node->poir=l;
	 l->next=node;
	 node->next->poir=node;
}
void deleteNode(link *l,int pos)/*删除第pos个节点*/
{
	l=l->next;
	for(int i=0;i<pos-1;i++)
		l=l->next;
	l->poir->next=l->next;
	l->next->poir=l->poir;
}
int _tmain(int argc, _TCHAR* argv[])
{
	link  *head=createLink();
	printNext(head);
    printPoir(head);
	cout<<"-------------------------"<<endl;
	insert(head,1,2);
	printNext(head);
    printPoir(head);
		cout<<"-------------------------"<<endl;
	deleteNode(head,2);
	printNext(head);
    printPoir(head);
	return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值