- #include <stdlib.h>
- #include <stdio.h>
- class Stack
- {
- private:
- int ele[100];
- int top;
- public:
- Stack() {
- top = 0;
- }
- void push(int v) {
- ele[top++] = v;
- }
- int pop() {
- if(isEmpty()) {
- printf("The stack is Empty.\n");
- return -1;
- }
- return ele[--top];
- }
- bool isEmpty() {
- return top == 0;
- }
- ~Stack() {
- }
- };
- #define SIZE 100
- class Queue
- {
- private:
- int ele[SIZE];
- int head;
- int tail;
- public:
- Queue() {
- head = 0;
- tail = 0;
- }
- bool isFull() {
- return (tail+1) % SIZE == head;
- }
- bool isEmpty() {
- return head == tail;
- }
- void enqueue(int val) {
- if(isFull()) {
- printf("The queue is full, can not enqueue for: %d\n", val);
- return;
- }
- ele[tail] = val;
- tail = (tail+1) %SIZE;
- }
- int dequeue() {
- if(isEmpty()) {
- printf("The queue is empty, can not dequeue.\n");
- return -1;
- }
- int value = ele[head];
- head = (head+1)%SIZE;
- return value;
- }
- };
- int main()
- {
- Stack s;
- s.push(123);
- s.push(12);
- while(!s.isEmpty()) {
- printf("%d ", s.pop());
- }
- printf("\n");
- Queue q;
- q.enqueue(88);
- q.enqueue(13);
- q.enqueue(123);
- while(!q.isEmpty()) printf("%d ", q.dequeue());
- printf("\n");
- system("pause");
- }
[root@localhost C++]# g++ push.cpp -o push
[root@localhost C++]# ./push
12 123
88 13 123
sh: pause: command not found
阅读(3978) | 评论(0) | 转发(3) |