双方向チェーンテーブルの逆転
20129 ワード
/*(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");
}