queue
链表实现队列
#include <iostream>
#include "stdio.h"
#include "queue.cpp"
typedef int datatype;
using namespace std;
int main()
{
queue a;
a.push(3);
a.push(6);
cout<<a.gethead()<<endl;
cout<<a.getlength()<<endl;
cout<<a.isempty()<<endl;
a.pop();
cout<<a.gethead()<<endl;
}
#ifndef QUEUE_H_INCLUDED
#define QUEUE_H_INCLUDED
const int maxsize=0;
class queuenode
{
public:
queuenode()
{
next=NULL;
}
int data;
queuenode *next;
};
class queue
{
private:
int length;
queuenode *front,*rear,*node;
public:
queue();
int getlength();
void push(int x);
bool isempty();
void pop();
int gethead();
};
#include "queue.h"
#include <stdio.h>
#include <cstdio>
queue::queue()
{
front=NULL;
rear=NULL;
node=NULL;
length=0;
}
int queue::getlength()
{
return length;
}
void queue::push(int x)
{
node=new queuenode;
node->data=x;
if (rear==NULL)
{
front=node;
rear=node;
}
else
{
rear->next=node;
rear=node;
}
length++;
}
bool queue::isempty()
{
return rear==NULL;
}
void queue::pop()
{
if (rear==NULL)
return;
else
{
node=front;
front=front->next;
delete node;
length--;
}
}
int queue::gethead()
{
return front->data;
}
#endif // QUEUE_H_INCLUDED

浙公网安备 33010602011771号