|
|
#2
诸葛修勤2011-05-09 20:53
程序代码:/*带头结点的循环链表表示队列FIFO*/ #include <iostream> using namespace std; class Node { public: Node(Node *node=NULL, int data=0) { data = 0; next = node; } public: int data; Node *next; }; class Link_Q { public: Link_Q();//构造函数 bool EmptyQueue();//判断队列是否为空 bool EnQueue(Node *node);//进队列 Node * DeQueue();//出队列 void PrintQueue();//打印输出 void ResetQueue();//重置 private: Node *rear; }; Link_Q::Link_Q() {//构造队列 rear = new Node(); rear->next = rear; } bool Link_Q::EmptyQueue() {//判断队列是否为空 若为空则返回true 否则返回false if (rear == rear->next) { return true; } return false; } bool Link_Q::EnQueue(Node *node) {//进队列 成功返回true 失败返回false Node *temp = rear->next; while (temp->next != rear) { temp = temp->next; } node->next = temp->next; temp->next = node; return true; } Node * Link_Q::DeQueue() {//摘掉第一个有效数据结点 Node *temp = rear->next; rear->next = temp->next; return temp; } void Link_Q::PrintQueue() {//打印输出队列信息 Node *temp = rear->next; while (temp != rear) { cout << temp->data << " "; temp = temp->next; } cout << endl; } void Link_Q::ResetQueue() { Node *temp = rear->next; while (temp != rear) { rear->next = temp->next; delete temp; temp = rear->next; } } void Menu() { cout << "\t1. 建立空队列" << endl; cout << "\t2. 判断队列是否为空" << endl; cout << "\t3. 入队列" << endl; cout << "\t4. 出队列" << endl; cout << "\t5. 队列置空" << endl; cout << "\t6. 打印队列" << endl; cout << "\t7. 退出" << endl; } void deal(Link_Q &queue, int choice) { switch (choice) { case 1: break; case 2: if (queue.EmptyQueue()) { cout << "是空队列" << endl; } else { cout << "不是空队列" << endl; } break; case 3: Node *te; te = new Node(); cout << "输入数据:" ; cin >> te->data; queue.EnQueue(te); break; case 4: Node *temp; temp = queue.DeQueue(); cout << "出队列的元素为" << temp->data << endl; delete temp; break; case 5: queue.ResetQueue(); break; case 6: queue.PrintQueue(); break; case 7: return; break; default: cout << "未定义" << endl; } } void factory() { int choice; Link_Q queue; while (true) { Menu(); cin >> choice; deal(queue, choice); if (7 == choice) { return; } } } int main() { factory(); return 0; } |
带头结点的循环链表表示队列 只设一个队尾指针,写置空,判队空,入队,出队,初始化队
#include <stdio.h>
#include<stdio.h>
#include<malloc.h>
typedef struct node{
int data;
struct node *next;
}Linklist;
typedef struct{
Linklist *rear;
}LinkQ;
LinkQ *SetQueue(){
LinkQ *Q;
Q=(LinkQ *)malloc(sizeof(LinkQ));
Q->rear->next=Q->rear;
return Q;
}
int EmptyQueue(LinkQ *Q){
if(!Q->rear)
return 1;
else
return 0;
}
LinkQ *Add(LinkQ *Q,int x){
LinkQ *p;
p=(LinkQ *)malloc(sizeof(LinkQ));
p->rear->data=x;
p->next=Q->rear->next;
Q->rear->next=p;
Q->rear=p;
return Q;
}
LinkQ *Delete(LinkQ *Q){
LinkQ *p;
if(!EmptyQueue){
p=Q->rear->next;
Q->rear->next=p->next;
free(p);
}
return Q;
}
LinkQ InitQueue(LinkQ *Q){
Q->rear=Q->rear->next;
return Q;
}
void main(){
int n,choice;
LinkQ *S;
do{
printf("1.建空队\n");
printf("2.判空队\n");
printf("3.入队\n");
printf("4.出队\n");
printf("5.置空队\n");
printf("6.退出\n");
printf("请选择:");
scanf("%d",&choice);
switch(choice){
case 1:SetQueue(); break;
case 2:EmptyQueue(S);break;
case 3:printf("输入元素,以-1结束:");
scanf("%d",&n);
while(n!=-1){
Add(S,n);
scanf("%d",&n);
};break;
case 4:Delete(S);break;
case 5:InitQueue(S);break;
}
}while(choice!=6);
}
程序代码: