循环双链表

最近在学数据结构,在参照书的过程中,再结合自己的一些理解,实现了一些基本操作,写得不好,其中肯定存在着不少错误,如果您发现了其中的错误,欢迎指出。

个人对于循环双链表的理解:

在双链表的基础上,使得头结点的前驱指向尾结点的后驱,尾结点的后驱指向头结点的前驱:

即:head->prior = rear->next,rear->next = head->next;

 

好了 ,下面是代码:

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

//

 

#include "stdafx.h"

#include

using namespace std;

struct Node

{

         Node*prior,*next;

         int data;

};

 

int getLength(Node *head)//获得表的长度

{

         Node *s = head->next;

         int count = 0;

         while( s != head->prior)

         {

                   count++;

                   s= s->next;

         }

         return count;

}

 

Node* getPointer(Node *head,int position)//获得第position个位置的指针,位置是从1开始计算

{

         if(position < 1 )//位置大于链表的长度

         {

                   cout<<"非法获取"<<endl;

                   return NULL;

         }

         int count = 1;

         if(position > getLength(head))position = position % getLength(head);

         Node *s = head->next;

         while(count < position)

         {

                   s = s->next;

                   count++;

                   if(s == head->prior)s = head;//否则head->prior->next不知道指向哪里

         }

         //cout<<s->data<<endl;

         return  s;

}

 

void insert(Node *head,int position ,int data)

{

         if(position <0 )

         {

                   cout<<"非法插入!"<<endl;

                   return;

         }

         Node *s = new Node();

         s->data = data;

         Node *p;

                   if(position == 0)//如果是位置为0,即数要插在position=1的数的前面

         {

                   p = getPointer(head,1);

                   //cout<<"ddd"<<p->prior<<endl;

                   s->next = p;

                   p->prior =s;

                   head->next = s;

                   s->prior = head;

         }

                   else if(position == getLength(head))//如果在最后一个点插入

                   {

                            p = getPointer(head,position);

                            p->next = s;

                            s->prior = p;

                            s->next = head->prior;

                            head->prior = s->next;

                   }

                   else //如果1<=position<10

         {

                   p = getPointer(head,position);

                   s->next = p->next;

                   if(p->next != NULL)p->next->prior=s;//如果获得的指针next指向不为空,则令p->next->priro指向s

                   s->prior = p;

                   p->next = s;

         }

}

 

void deleteData(Node *head,int position)

{

         if(position <= 0)//position范围为[1,getlength(head)]

         {

                   cout<<"删除失败!"<<endl;

                   return;

         }

         Node *s;

         if(getLength(head) < position)

         {

                   if(position % getLength(head) != 0)//如果不是最后一个数

                   {

                            position = position % getLength(head);

                   }

                   else//如果是最后一个数

                   {

                            position = getLength(head);

                   }

         }

         s = getPointer(head,position);

         if(position == 1)//如果是第一个位置上的数

         {

                   head->next = s->next;

                   s->next->prior = head;

                   free(s);

         }

         else//如果不是删除第一个位置上的数

         {

             Node *p = getPointer(head,position - 1);        

                   if(position < getLength(head))//如果不是最后一个数

                   {

                            p->next = s->next;

                       s->next->prior = p;

                   }

                   else //如果是最后一个数

                   {

                            p->next = head->prior;

                            head->prior = p->next;

                   }

                   free(s);

         }

}

 

Node* create()//尾插法

{

         Node *head = new Node;

         Node *rear;

         head->next = head->prior = NULL;

         rear = head;

         Node *s;

         for (int i = 0; i < 10; i++)

         {

                   s = new Node;

                   s->data = i + 1;

                   s->prior = rear;//新结点的prior指针指向尾结点

                   rear->next = s;//尾结点的next指针指向新结点

                   rear = s;//新结点成为新的尾结点

         }

         //rear->next = NULL;

         head->prior = rear->next;//头结点的prior指针 指向尾结点的next指针

         rear->next = head->prior;//尾结点的next指针指向头结点的prior指针

         return head;

}

 

void showNode(Node *head)

{

         Node *s = head->next;

         while(s != head->prior)//当尾结点的next指针指向头结点的prior指针认为已经遍历完成

         {

                   cout<<s->data<<"   ";

                   s = s->next;

         }

         cout<<endl;

}

 

int _tmain(int argc, _TCHAR* argv[])

{

         Node *head = create();

         //insert(head,10,20);

         deleteData(head,30);

         showNode(head);

         //cout<<getLength(head)<<endl;

         //getPointer(head,88);

         return 0;

}

 其中绝大部分代码都有注释了,如果您发现了其中的错误,欢迎指出。 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值