数据结构与算法 --- 栈与队列

顺序栈 

#include  

using namespace std;

class SqStack
{
public:
    SqStack(int maxsize = 64);
    bool Push(int element);     // 入栈
    bool Pop(int & element);    // 出栈
    bool GetTop(int & element); // 获取栈顶元素
private:
    int * base; // 栈底指针
    int * top;  // 栈顶指针
    int stacksize;  // 栈的最大容量

};

SqStack::SqStack(int maxsize)
{
    base = new int [maxsize];
    stacksize = maxsize;
    top = base;
    stacksize = maxsize;
}

bool SqStack::Push(int element)
{
    if((top - base) == stacksize)       // 如果栈满
    {
        cout << "Stack is full" << endl;
        return false;
    }
    *top++ = element;
    return true;
}

bool SqStack::Pop(int & element)
{
    if(top == base)
    {
        cout << "Stack is empty" << endl;
        return false;
    }
    element = *(--top);
    return true;
}

bool SqStack::GetTop(int & element)
{
    if(top == base)
    {
        cout << "Stack is full" << endl;
        return false;
    }
    else
    {
        element =  *(top - 1);
        return true;
    }
}

int main()
{
    return 0;
}

 

链栈

#include 

using namespace std;

struct StackNode{
    int data;
    StackNode * next;
};

class LinkStack
{
public:
    LinkStack() { s = NULL; }
    bool Push(int element);     // 入栈
    bool Pop(int & element);    // 出栈
    bool GetTop(int & element); // 取栈顶元素
private:
    StackNode * s;
};

bool LinkStack::Push(int element)
{
    StackNode * p = new StackNode;
    if(p == NULL)
        return false;
    p->data = element;
    p->next = s;
    s = p;
    return true;
}

bool LinkStack::Pop(int & element)
{
    if(s == NULL)
        return false;
    element = s->data;
    StackNode * p = s;
    s = s->next;
    delete p;
    return true;
}

bool LinkStack::GetTop(int & element)
{
    if(s == NULL)
        return false;
    element = s->data;
    return true;
}

int main()
{
    return 0;
}

 

循环队列

#include 
using namespace std;

class SqQueue
{
public:
    SqQueue(int maxsize = 64);
    int QueueLength() { return (rear - front) % maxsize; } // 返回队列元素数量
    bool EnQueue(int element);           // 入列
    bool DeQueue(int & element);         // 出列
    bool GetHead(int & element);          // 获取队头元素
private:
    int * base;   // 基地址
    int front;   // 头指针
    int rear;   // 尾指针
    int maxsize;    // 队列长度
};

SqQueue::SqQueue(int maxsize)
{
    base = new int[maxsize];
    rear = front = 0;
    this->maxsize = maxsize;
}

bool SqQueue::EnQueue(int element)
{
    if((rear + 1)% maxsize == front)
    {
        cout << "Queue is full" << endl;
        return false;
    }
    *(base + rear) = element;
    rear = (rear + 1) % maxsize;
    return true;
}

bool SqQueue::DeQueue(int & element)
{
    if(rear == front)
    {
        cout << "Queue is empty" << endl;
        return false;
    }
    element = *(base + front);
    front = (front + 1) % maxsize;
    return true;
}

bool SqQueue::GetHead(int & element)
{
    if(front == rear)
    {
        cout << "Queue is empty" << endl;
        return false;
    }
    element = *(base + front);
    return true;
}

int main()
{
    SqQueue s;
    return 0;
}

 

队链

#include 

using namespace std;

struct QNode{
    int data;
    QNode * next;
};

class LinkQueue
{
public:
    LinkQueue();
    bool EnQueue(int element);
    bool DeQueue(int & element);
    bool GetHead(int & element);
private:
    QNode * front;
    QNode * rear;
    int listsize;
};

LinkQueue::LinkQueue()
{
    rear = front = new QNode;   // rear和front指向头节点
    rear->next = NULL;                      // 头节点指针域为空
}

bool LinkQueue::EnQueue(int element)
{

    QNode * p = new QNode;
    if(p == NULL)
        return false;
    p->data = element;
    p->next = NULL;
    rear->next = p;
    rear = p;
    return true;
}

bool LinkQueue::DeQueue(int & element)
{
    if(front == rear)
    {
        cout << "Queue is empty" << endl;
        return false;
    }

    QNode * p = front->next;
    element = p->data;
    front->next = p->next;
    if(rear == p)
        rear = front;
    delete p;
    return true;
}

bool LinkQueue::GetHead(int & element)
{
    if(front == rear)
    {
        cout << "Queue is empty" << endl;
        return false;
    }
    element = front->next->data;
    return element;
}

int main()
{
    return 0;
}

 

你可能感兴趣的:(数据结构与算法)