#include <string.h> #include <stdlib.h> #include <iostream> #include <exception> using namespace std; class MyException : public exception { public: MyException() :exception("the stack is empty...\n") { } MyException(int a) :exception("the queue is empty...\n") { } }; template <typename T> class Stack { int Size; int Room; T* data; public: Stack() { Size = 0; Room = 0; data = NULL; } Stack(int MAX) { Size=0; Room = MAX; data = (T*)malloc(sizeof(T)*MAX); } int size() { return Size; } int room() { return Room; } void push(T m) { if(Room>Size) { *(data+Size)=m; ++Size; } else { data = (T*)realloc(data,(Room+100)*sizeof(T)); *(data+Size)=m; ++Size; Room+=100; } } void pop() { try { if (Size) { --Size; } else throw MyException(); } catch (MyException &e) { cout << e.what(); } } T getTop() { try { if (Size) { return *(data + Size - 1); } else throw MyException(); } catch (MyException &e) { cout << e.what(); } } void clear() { Size=0; Room=0; free(data); data=NULL; } bool isEmpty() { if (!Size)return true; else return false; } }; template <typename T> class Queue { int Size; int Room; int Head; T* data; public: Queue() { Size = 0; Room = 0; Head = 0; data = NULL; } Queue(int MAX) { Size = 0; Room = MAX; Head = 0; data = (T*)malloc(sizeof(T)*MAX); } int size() { return Size; } int room() { return Room; } void enqueue(T m) { if (Room > Size) { *(data + Room - Size -1) = m; ++Size; } else { T* temp = (T*)malloc(sizeof(T)*(Room + 100)); memcpy(temp + 100, data, sizeof(T)*Room); data = temp; Room += 100; *(data + Room - Size -1) = m; ++Size; } } void dequeue() { try { if (Size) { --Size; ++Head; } else throw MyException(0); } catch (MyException &e) { cout << e.what(); } } T getHead() { try { if (Size) { return *(data + Room -Head -1); } else throw MyException(0); } catch (MyException &e) { cout << e.what(); } } void clear() { Size = 0; Room = 0; Head = 0; free(data); data = NULL; } bool isEmpty() { if (Size)return false; else return true; } }; int main(int argc, char* argv[]) { cout << "/*test of my stack*/\n\n"; cout << "***********************\n"; Stack<int> s; cout << "the stack has: " << s.size() << " elements...\n"; cout << "the stack can contain: " << s.room() << " elements...\n"; for (int i = 0; i < 100; ++i) s.push(i); cout << "the stack has: " << s.size() << " elements...\n"; cout << "the stack can contain: " << s.room() << " elements...\n"; s.push(30); cout << "the stack has: " << s.size() << " elements...\n"; cout << "the stack can contain: " << s.room() << " elements...\n"; cout << "the top element is " << s.getTop() <<"...\n"; s.pop(); cout << "the top element is " << s.getTop() << "...\n"; cout << "the stack is empty? " << s.isEmpty()<<"...\n"; s.clear(); cout << "the stack is empty? " << s.isEmpty() <<"...\n\n"; cout << "/*test of my queue*/\n\n"; cout << "***********************\n"; Queue<int> t; cout << "the queue has: " << t.size() << " elements...\n"; cout << "the queue can contain: " << t.room() << " elements...\n"; for (int j = 0; j < 100; ++j) t.enqueue(j); cout << "the queue has: " << t.size() << " elements...\n"; cout << "the queue can contain: " << t.room() << " elements...\n"; t.enqueue(-1); cout << "the queue has: " << t.size() << " elements...\n"; cout << "the queue can contain: " << t.room() << " elements...\n"; cout << "the top element is " << t.getHead() <<"...\n"; t.dequeue(); cout << "the queue has: " << t.size() << " elements...\n"; cout << "the queue can contain: " << t.room() << " elements...\n"; cout << "the top element is " << t.getHead() << "...\n"; cout << "the queue is empty? " << t.isEmpty()<<"...\n"; t.clear(); cout << "the queue is empty? " << t.isEmpty() <<"...\n"; return 0; } //栈的结构通过连续内存(数组)来模拟,数组的尾端相当于栈顶 //出栈操作并没有将栈顶元素删除,只是通过Size自减1,使得原栈顶元素无法访问 //队列的结构同样也是通过连续内存(数组)来模拟,数组的尾端相当于队首 //出列操作也同样没有将队首元素删除,只是通过Head自加1,使得原队首元素无法访问
时间: 2024-10-12 12:21:58