キューのチェーンストレージ(c++実装)
16227 ワード
キューのチェーンストレージは、単一のチェーンテーブルとして見られますが、エンドヘッダのみで、チェーンキューと略称されます.
#pragma once
class Node
{
public:
Node();
~Node();
friend class Queue;
int data;
Node *next;
};
class Queue
{
public:
Queue();
~Queue();
void Push_Queue();//
void Pop_Queue();//
bool IsEmpty();//
void Show_Queue();//
Node *front;//
Node *rear;//
};
#include" .h"
#include
using namespace std;
Node::Node()
{
}
Node::~Node()
{
}
Queue::Queue()
{
Node *pnew = new Node;//
this->front = pnew;
this->rear = pnew;
this->rear->next = NULL;
}
Queue::~Queue()
{
}
void Queue::Push_Queue()//
{
int x = 0;
cout << " " << endl;
cin >> x;
Node *ptemp = new Node;
ptemp->data = x;
ptemp->next = NULL;
this->rear->next = ptemp;
this->rear = ptemp;
cout << " " << endl;
}
void Queue::Pop_Queue()//
{
if (this->IsEmpty())
{
cout << " !" << endl;
}
else
{
int x = 0;
Node* p = this->front->next;
x = p->data;
this->front->next = p->next;
if (p == this->rear)
{
this->rear = this->front;
}
delete p;
cout << " !" << " :" << x << endl;
}
}
bool Queue::IsEmpty()//
{
return this->front == this->rear ? true : false;
}
void Queue::Show_Queue()//
{
Node* p = this->front->next;
while (p != NULL)
{
cout << p->data << " ";
p = p->next;
}
cout << endl;
}
#include" .h"
#include
using namespace std;
int main()
{
Queue q;
int i = 7;
while (i != 0)
{
cout << "0. 1. 2. 3. " << endl;
cin >> i;
switch (i)
{
case 0:
break;
case 1:
q.Push_Queue();
break;
case 2:
q.Pop_Queue();
break;
case 3:
q.Show_Queue();
break;
}
}
}