您的位置:首页 > 其它

双向循环链表

2012-10-15 00:29 218 查看
双向循环链表中有一些比较明显的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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: