The Stack Data Structure

1 / 7

# The Stack Data Structure - PowerPoint PPT Presentation

The Stack Data Structure. Mugurel Ionu ț Andreica Spring 2012. Operations. push(x) Adds the element x at the top of the stack pop() Removes the element from the top of the stack and returns it Returns an error if the stack is empty peek()

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

### The Stack Data Structure

Mugurel Ionuț Andreica

Spring 2012

Operations
• push(x)
• Adds the element x at the top of the stack
• pop()
• Removes the element from the top of the stack and returns it
• Returns an error if the stack is empty
• peek()
• Returns (but does not remove) the element at the top of the stack
• isEmpty()
• Returns 1 if the stack is empty and 0 otherwise
• The axioms are given implicitly – they determine the expected behavior of the stack for a given sequence of operations
• See the examples on the upcoming slides
Stack – Array-based Implementation

T peek() {

if (isEmpty()) {

fprintf(stderr, "Error 103 - The stack is empty!\n");

T x;

return x;

}

return stackArray[topLevel];}

int isEmpty() {

return (topLevel < 0);}

Stack() { // constructor

topLevel = -1; } // the stack is empty in the beginning

};

int main() {

Stack<int> s;

s.push(7);s.push(3);s.push(10);s.push(13);s.push(20);

s.push(9);s.push(1);printf("%d\n", s.pop());

printf("%d\n", s.peek());printf("%d\n", s.peek());

printf("%d\n", s.pop());printf("%d\n", s.peek());

s.push(100); printf("%d\n", s.pop());

return 0;

}

#include <stdio.h>

#define NMAX 100

template<typename T> class Stack {

private:

T stackArray[NMAX];

int topLevel;

public:

void push(T x) {

if (topLevel >= NMAX - 1) {

fprintf(stderr, "Error 101 - The stack is full!\n");

return;

}

topLevel++;

stackArray[topLevel] = x;

}

T pop() {

if (isEmpty()) {

fprintf(stderr, "Error 102 - The stack is empty!\n");

T x;

return x;

}

T x = stackArray[topLevel];

topLevel--;

return x;

}

Paranthesis Checking
• Given a string S composed of the characters ‘(‘, ‘)’, ‘[‘, ‘]’, ‘{‘, ‘}’ (round bracket, square bracket and curly bracket), determine if the parantheses are correctly balanced
• S=“([()[{([]()){}}[[]]]{}])[]({})” is correctly balanced
• S1=“([]({)})”, S2=“[{{}(())([]{“, S3=“[(){}]]” are not correctly balanced
• Algorithm
• We use a stack
• We traverse the string from left to right
• We push on the stack every open bracket
• For every closed bracket, the bracket at the top of the stack must be an open bracket of the same type (and the stack must not be empty) => then we pop the open bracket from the stack
• In the end, the stack must be empty
Paranthesis Checking - Implementation

#include <stdio.h>

#include <string.h>

// copy & paste the definition of the Stack class

char s[200]="([{()[]}(()[{}()])])(){}[{{()[]}()}]";

int error = 0;

int main(){

Stack<char> stk;

for (int i = 0; i < strlen(s); i++){

if (s[i] == \'(\' || s[i] == \'[\' || s[i] == \'{\')

stk.push(s[i]);

elseif (stk.isEmpty()){

fprintf(stderr, "Error 201 - Empty stack\n");

error = 1;

break;

}

else{

if ((s[i] == \')\' && stk.peek() != \'(\') ||

(s[i] == \']\' && stk.peek() != \'[\') ||

(s[i] == \'}\' && stk.peek() != \'{\')){

fprintf(stderr, "Error 202 -Wrongparanthesis\n");

error = 1;

break;

}

stk.pop();

}

}

if (!error && !stk.isEmpty())

fprintf(stderr, "Error 203 - The stack is not empty at the end\n");

elseif (!error)

printf("OK\n");

return 0;

}

Header with the Stack Class Definition
• Definition of the Stack class must be copy-pasted in every program in which it is used => annoying + prone to mistakes (maybe we do not copy-paste everything, or we copy-paste more than we need)
• Elegant solution: define the Stack class in a header file (e.g. mystack.h)
• Any program that uses the Stack class will include the header file
• #include “mystack.h” (or #include <mystack.h>)
• we will proceed similarly for the upcoming data structures