双向链表的逆转

/*(4)设计算法使链表内所有链接方向原地逆转,要求仅使用原表存储空间
换句话说,要求算法空间复杂度为o(1)
(5)设计算法删除递增表中大于mink小于maxk的元素*/
//来一波双向链表的逆转挑战一下
#include
#include
#include
#include//包含exit头文件
#include
#include
#define OK 1
#define ERROR 0//宏
#define MINK 1
#define MAXK 10
typedef int Status;
typedef struct DuLNode
{
     
   int data;
   DuLNode *next;//后继
   DuLNode *prior;//前驱
}*DuLinkList,DuLNode;

Status CreateList_Dul(DuLinkList &L)
{
     //老规矩初始化
   L=(DuLNode *)malloc(sizeof(DuLNode));
   if(!L) return ERROR;
   L->next=L;
   L->prior=L;
   L->data=-9999;
   return OK;
}
Status EnterData_Dul(DuLinkList &L)
{
     
   DuLNode *r;//尾指针r
   r=L;//L是首元结点可以吗?
   int length;
   cout<<"いくら?"<<endl;
   cin>>length;
   cout<<"dataを"<<endl;
   for(int i=0;i<length;i++)
   {
     
       if(L->data==-9999)
       {
     
           cin>>L->data;
       }else
       {
     
           DuLNode *s;
           s=(DuLNode *)malloc(sizeof(DuLNode));
           cin>>s->data;
           r->next=s;
           s->prior=r;
           r=s;r->next=L;
       }
   }
   L->prior=r;
   return OK;
}
Status DeleteList_Dul(DuLinkList &L)
{
     
   DuLNode *p,*r;
   p=L->next;
   if(L->data>MINK)
   {
     
       L->next->prior=L->prior;
       L->prior->next=L->next;
       r=L;
       delete r;
       //删除首元结点
       while(p->data<MAXK&&p->next!=p)
       {
     //删除L以后所有小于MAXK的结点
           p->next->prior=p->prior;
           p->prior->next=p->next;
           r=p;
           p=p->next;
           delete r;
       }
       if(p->data<MAXK&&p==p->next)
       {
     
           delete p;//表消失
           L=NULL;//首元结点重设空表
       }else
       {
     
           L=p;//首元结点重新设置
       }
   }else
       {
     
           while(p!=L)
           {
     
               if(p->data>MINK&&p->data<MAXK)
               {
             
                   p->next->prior=p->prior;
                   p->prior->next=p->next;
                   r=p;
                   p=p->next;
                   delete r;
               }else if(p->data>=MAXK)
               {
     
                   break;
               
               }else
               {
     
                   p=p->next;
               }
           }
       }
   return OK;
}
Status SetBack_Dul(DuLinkList &L)
{
     
   DuLNode *s,*p;//过渡指针,存储后继的位置
   s=L->next;
   L->next=L->prior;
   L->prior=s;
   p=s;
   s=s->next;
   while(p!=L)
   {
     
       p->next=p->prior;
       p->prior=s;
       p=s;
       s=s->next;
   }
   return OK;
}
void ShowList(DuLinkList L)
{
     
   DuLNode *p;
   if(!L)
   {
     
       cout<<"空表!"<<endl;
   }else
   {
     
       p=L->next;
       cout<<L->data<<endl;
       while(p!=L)
       {
     
           cout<<p->data<<endl;
           p=p->next;
       }
   }
}
void main()
{
     
   DuLinkList L;
   CreateList_Dul(L);
   EnterData_Dul(L);
   cout<<"Lのdata以下"<<endl;
   ShowList(L);
   DeleteList_Dul(L);
   cout<<"删除後L:"<<endl;
   ShowList(L);
   SetBack_Dul(L);
   cout<<"逆转後L:"<<endl;
   ShowList(L);
   system("pause");
}

你可能感兴趣的:(数据结构)