生活随笔
收集整理的這篇文章主要介紹了
1. 栈和队列的数组实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
棧和隊列的比較
棧和隊列都屬于線性表,且在其上進行Insert和Delete操作所插入和移除的元素是預先設定的。在棧中,在一端插入,在同一端刪除,位于該端點的元素稱為棧頂元素;在隊列中,在一端插入,在另一端刪除,位于兩端點的元素分別稱為隊列尾和隊列頭。
所以在棧的實現中,需要記錄的變量是棧頂元素位置;而在隊列的實現中,需要記錄的變量有隊列頭和隊列尾的位置。
用數組實現一個棧
都需要記錄哪些信息?棧頂元素下標index,以及數組本身的指針p、總長度MAX_SIZE。
棧頂元素下標index與其對應的數組元素下標f(index)之間的映射關系是怎樣的?隨著棧的增長,棧頂元素下標依次為0,1,2...n-1,其中n表示棧的最大高度。我們需要制定一種映射規則f,方便我們通過index計算出f(index)。而最簡單的規則就是f(index) = index。
#include<iostream>
template<typename Object>
class Stack
{
public:Stack(){init();}Stack(const Stack& rhs){init();index = rhs.index;for(int i = 0; i != index + 1; ++i)p[i] = rhs.p[i];}Stack(Stack&& rhs):p(rhs.p),index(rhs.index){rhs.p = nullptr;rhs.index = -1;}Stack& operator =(const Stack& rhs){Stack copy(rhs);std::swap(p, copy.p);std::swap(index, copy.index);return *this;}Stack& operator =(Stack&& rhs){std::swap(p, rhs.p);std::swap(index, rhs.index);return *this;}~Stack(){if(p)delete[] p;}void push(const Object& object){if(index == MAX_SIZE-1)std::cerr << "overflow" << std::endl;elsep[++index] = object;}void push(Object&& object){if(index == MAX_SIZE-1)std::cerr << "overflow" << std::endl;elsep[++index] = std::move(object);}void pop(){if(empty())std::cerr << "underflow" << std::endl;else--index;}Object& top(){return p[index];}const Object& top() const{return p[index];}bool empty() const{return index == -1;}
private:static constexpr int MAX_SIZE = 4;Object* p;int index;void init(){p = new Object[MAX_SIZE];index = -1;}
};
#include <iostream>
template<typename Object>
class Queue
{
public:Queue(){init();}Queue(const Queue& rhs){init();head = rhs.head;size = rhs.size;for(int i = head, idx; i != head + size; ++i){idx = index(i);p[idx] = rhs.p[idx];}}Queue(Queue&& rhs):head(rhs.head), size(rhs.size),p(rhs.p){rhs.head = -1;rhs.size = 0;rhs.p = nullptr;}Queue& operator =(const Queue& rhs){Queue copy(rhs);std::swap(head, copy.head);std::swap(size, copy.size);std::swap(p, copy.p);return *this;}Queue& operator =(Queue&& rhs){std::swap(head, rhs.head);std::swap(size, rhs.size);std::swap(p, rhs.p);return *this;}~Queue(){if(p)delete[] p;}void enqueue(const Object& object){if(full()){std::cout << "overflow" << std::endl;return;}p[index(head + size++)] = object;}void enqueue(Object&& object){if(full()){std::cout << "overflow" << std::endl;return;}p[index(head + size++)] = std::move(object);}Object tail(){if(empty()){std::cout << "underflow" << std::endl;return Object();}return p[index(head + size - 1)];}Object dequeue(){if(empty()){std::cout << "underflow" << std::endl;return Object{};}Object& object = p[index(head)];head = index(head + 1);--size;return object;}bool empty() const{return size == 0;}bool full() const {return size == MAX_SIZE;}int getSize() const {return size;}
private:static constexpr int MAX_SIZE = 4;Object* p;int head;int size;void init(){p = new Object[MAX_SIZE];head = size = 0;}inline int index(int i){if(i >= MAX_SIZE)i -= MAX_SIZE;return i;}
};void testQueue()
{using namespace std;struct Student{char name[10];int age;};Queue<Student> q;q.enqueue(Student{"Tom", 12});q.enqueue(Student{"Micheal", 13});q.enqueue(Student{"Anna", 14});q.enqueue(Student{"Lily", 10});q.enqueue(Student{"James", 19});q.enqueue(q.dequeue());q.enqueue(q.dequeue());q.enqueue(q.dequeue());q.enqueue(q.dequeue());while(!q.empty()){Student stu = q.dequeue();cout << "name:" << stu.name << " age:" << stu.age << endl;}/*outputoverflowname:Tom age:12name:Micheal age:13name:Anna age:14name:Lily age:10*/
}
轉載于:https://www.cnblogs.com/meixiaogua/p/10167635.html
總結
以上是生活随笔為你收集整理的1. 栈和队列的数组实现的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。