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


posted @ 2016-09-20 21:33  萌萌琪  阅读(419)  评论(0)    收藏  举报