📄 queue.cpp
字号:
#include "iostream.h"
#include "stdlib.h"
typedef int ElemType;
typedef struct dulnode {
ElemType data;
struct dulnode *prior;
struct dulnode *next;
}DNode;
// 双向循环链表的初始化;(只有头结点)
DNode *dlinkinit()
{DNode *p;
p=new DNode;
p->prior=p;
p->next=p;
return p;
}
//建立双向循环链表
void dlinkcreate(DNode *H)
{
DNode *p,*q;
ElemType x;
cout<<"endter 0 to end"<<endl;
cin>>x;
while(x)
{p=new DNode;
p->data=x;p->next=H;
if(H->next==H) {H->next=p;p->prior=H;}
else
{p->prior=q;q->next=p;}
q=p;H->prior=p;
cin>>x;
}
}
// 双向循环链表的输出
void dprint(DNode *H)
{
DNode *p;
p=H->next;
while(p!=H)
{cout<<p->data<<" ";
p=p->next;
}
cout<<endl;
}
//上机题:删除双向循环链表中所有数据域值为x0的结点。
void deleteall(DNode *H)
{ElemType x;
cout<<"请输入要删除的结点x0";cin>>x;
DNode *p,*q;
p=H->next;
while(p!=H)
{if(p->data==x)
{ q=p;
p->prior->next=p->next;
p->next->prior=p->prior;
p=p->next;
delete q;
}
else p=p->next;
}
}
//上机题:设计一算法,判断该双向链表是否是对称的,即从前往后和从后往前的输出序列是相同的
//若对称返回1,否则返回0。
int dlistsym(DNode *H)
{
DNode *p,*q;
int k=0;
p=H->prior;
q=H->next;
while(q!=p&&p->next!=q)
{ if(p->data!=q->data) return 0;
p=p->prior;
q=q->next;
k++;
}
return 1;
}
void main()
{
DNode *h1,*h2;
h1=dlinkinit();
// h2=dlinkinit();
dlinkcreate(h1);
dprint(h1);
//删除测试
deleteall(h1);
dprint(h1);
//是否对称测试
//if(dlistsym(h1)) cout<<"对称"<<endl;
// else cout<<"不对称!"<<endl;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -