?? duilie.cpp
字號:
#include<iostream.h>
struct QNode
{int data;
QNode *next;
};
struct LinkQueue
{QNode *front,*rear;
};
void endqueue(LinkQueue *Q)//將元素插入到隊列的未尾
{ int x;
cout<<"請輸入要加入的隊列元素:"<<endl;cin>>x;
QNode *p;
p=new QNode;
p->data=x ; p->next=NULL ; /*形成新結點*/
Q->rear->next=p ; Q->rear=p ;
}
void front(LinkQueue *Q)
//輸出隊列的第一個元素
{ if (Q->front->next==NULL)cout<<"隊列為空"<<endl;
else cout<<"第一個元素為:"<<Q->front->next->data<<endl;
}
/*void show(LinkQueue *Q)
{LinkQueue *p;
p=Q;
while(p->front!=NULL)
{cout<<p->front->data<<endl;
p->front=p->front->next;
}
delete p;
}*/
void delqueue(LinkQueue *Q)
{QNode *p;
if(Q->front==Q->rear)
cout<<"隊列為空"<<endl;
else { p=Q->front->next ;
Q->front->next=p->next ;
if (p==Q->rear) Q->rear=Q->front ;
/*當隊列只有一個結點時應防止丟失隊尾指針*/
delete p;
}
}
void makenull(LinkQueue *Q)
{while(Q->front!=Q->rear)
delqueue(Q);
cout<<"隊列已置空"<<endl;
}
void main()
{LinkQueue *Q ;
QNode *p ;
p=new QNode; //開辟頭結點
p->next=NULL ;
Q=new LinkQueue; //開辟鏈隊的指針結點,頭結點不參與運算
Q->front=Q->rear=p;
char ans;
while(ans!='0')
{cout<<" 請選擇操作 "<<endl;
cout<<"1.加入元素 2.輸出隊列的第一個元素 "<<endl;
cout<<"3.將隊列置空 4.刪除隊列的第一個元素 "<<endl;
cout<<"0.退出 "<<endl;
cin>>ans;
switch(ans)
{
case '1':endqueue(Q);break;
case '2':front(Q);break;
case '3':makenull(Q);break;
case '4':delqueue(Q);break;
case '0':break;
default:cout<<"輸入錯,請重新輸入:"<<endl;
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -