@bintou
2017-12-07T02:43:37.000000Z
字数 4246
阅读 2397
Source Code
修改了一下代码,分成三个文件:stack.h 、stack.cpp和调用stack.cpp的main.cpp文件.
练习:使用Linked list实现一种动态Stack,即,Stack的Size可以根据应用的需求增大,而无需预先定死。
/* C++ head file to define a class of stack.Push: Adds an item in the stack. If the stack is full, then it is said to be an Overflow condition.Pop: Removes an item from the stack. The items are popped in the reversed order in which they are pushed. If the stack is empty, then it is said to be an Underflow condition.Peek or Top: Returns top element of stack.isEmpty: Returns true if stack is empty, else fals.*/#define MAX 1000class Stack{int top;public:int a[MAX]; //Maximum size of StackStack() { top = -1; }bool push(int x);int pop();int peek();bool isEmpty();};
/* C++ program to implement a basic stack. */#include <stack_oo.h>bool Stack::push(int x){if (top >= MAX){//cout << "Stack Overflow";return false;}else{a[++top] = x;return true;}}int Stack::pop(){if (top < 0){//cout << "Stack Underflow";return 0;}else{int x = a[top--];return x;}}int Stack::peek(){if (top < 0){//cout << "Stack Underflow";return -1;}else{return a[top];}}bool Stack::isEmpty(){return (top < 0);}
/* C++ program to test basic stack operations */#include <stack_oo.h>#include <iostream>int main(){Stack s;s.push(10);std::cout << " Push a value to stack\n";s.push(20);std::cout << " Push a value to stack\n";s.push(30);std::cout << " Push a value to stack\n";std::cout << s.pop() << " Popped from stack\n";return 0;}
// C program for array implementation of stack/*Push: Adds an item in the stack. If the stack is full, then it is said to be an Overflow condition.Pop: Removes an item from the stack. The items are popped in the reversed order in which they are pushed. If the stack is empty, then it is said to be an Underflow condition.Peek or Top: Returns top element of stack.isEmpty: Returns true if stack is empty, else fals.*/#include <stdio.h>#include <stdlib.h>#include <limits.h>// A structure to represent a stackstruct Stack{int top;unsigned capacity;int* array;};// function to create a stack of given capacity. It initializes size of// stack as 0struct Stack* createStack(unsigned capacity){struct Stack* stack = (struct Stack*) malloc(sizeof(struct Stack));stack->capacity = capacity;stack->top = -1;stack->array = (int*) malloc(stack->capacity * sizeof(int));return stack;}// Stack is full when top is equal to the last indexint isFull(struct Stack* stack){ return stack->top == stack->capacity - 1; }// Stack is empty when top is equal to -1int isEmpty(struct Stack* stack){ return stack->top == -1; }// Function to add an item to stack. It increases top by 1void push(struct Stack* stack, int item){if (isFull(stack))return;stack->array[++stack->top] = item;printf("%d pushed to stack\n", item);}// Function to remove an item from stack. It decreases top by 1int pop(struct Stack* stack){if (isEmpty(stack))return INT_MIN;return stack->array[stack->top--];}// Driver program to test above functionsint main(){struct Stack* stack = createStack(100);push(stack, 10);push(stack, 20);push(stack, 30);printf("%d popped from stack\n", pop(stack));return 0;}
/* C++ program to implement basic stackoperations *//*Push: Adds an item in the stack. If the stack is full, then it is said to be an Overflow condition.Pop: Removes an item from the stack. The items are popped in the reversed order in which they are pushed. If the stack is empty, then it is said to be an Underflow condition.Peek or Top: Returns top element of stack.isEmpty: Returns true if stack is empty, else fals.*/#include<bits/stdc++.h>using namespace std;#define MAX 1000class Stack{int top;public:int a[MAX]; //Maximum size of StackStack() { top = -1; }bool push(int x);int pop();bool isEmpty();};bool Stack::push(int x){if (top >= MAX){cout << "Stack Overflow";return false;}else{a[++top] = x;return true;}}int Stack::pop(){if (top < 0){cout << "Stack Underflow";return 0;}else{int x = a[top--];return x;}}bool Stack::isEmpty(){return (top < 0);}// Driver program to test above functionsint main(){struct Stack s;s.push(10);s.push(20);s.push(30);cout << s.pop() << " Popped from stack\n";return 0;}
/*The functions associated with stack are:empty() – Returns whether the stack is emptysize() – Returns the size of the stacktop() – Returns a reference to the top most element of the stackpush(g) – Adds the element ‘g’ at the top of the stackpop() – Deletes the top most element of the stack*/#include <iostream>#include <stack>using namespace std;void showstack(stack <int> gq){stack <int> g = gq;while (!g.empty()){cout << '\t' << g.top();g.pop();}cout << '\n';}int main (){stack <int> gquiz;gquiz.push(10);gquiz.push(30);gquiz.push(20);gquiz.push(5);gquiz.push(1);cout << "The stack gquiz is : ";showstack(gquiz);cout << "\ngquiz.size() : " << gquiz.size();cout << "\ngquiz.top() : " << gquiz.top();cout << "\ngquiz.pop() : ";gquiz.pop();showstack(gquiz);return 0;}