队列
// 队列操作类.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<iostream>
using namespace std;
struct list
{
int data;
list *next;
};
class Queue
{
public:
Queue()
{
ptrf = ptrb = 0;
}
void enqueue(int x);//入队函数
int dequeue();//出队函数
private:
list *ptrf;//队首指针
list *ptrb;//队尾指针
};
void Queue ::enqueue(int x)
{
list *newnode = new list;//动态内存分配
newnode->data = x;
newnode->next = NULL;
if (ptrb == NULL )//队空时入队情况
ptrf = ptrb = newnode;
else //队非空时入队
{
ptrb->next = newnode;
ptrb = newnode;
}
}
int Queue ::dequeue()
{
list *tmp;
int value;
value = ptrf->data;
tmp = ptrf;
ptrf = ptrf->next;
delete tmp;
return value;
}
int main()
{
Queue A;
int arr[] = { 2,3,4,5,6 };
cout << "入队:" ;
for (int i = 0; i < 5; i++)
{
cout << arr[i] << " ";
A.enqueue(arr[i]);
}
cout << endl << "出队:";
for (int i = 0; i < 5; i++)
cout << A.dequeue() << " ";
cout << endl;
system( "pause");
return 0;
}