0% found this document useful (0 votes)
70 views

Stack Data Structures

The document describes a stack data structure and its operations (push, pop, peek, isEmpty). It provides examples of using a stack and an array-based implementation of a stack in C++ with definitions of methods like push(), pop(), peek(), isEmpty(). It also shows how a stack can be used to check if parentheses in a string are balanced.

Uploaded by

claudiu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views

Stack Data Structures

The document describes a stack data structure and its operations (push, pop, peek, isEmpty). It provides examples of using a stack and an array-based implementation of a stack in C++ with definitions of methods like push(), pop(), peek(), isEmpty(). It also shows how a stack can be used to check if parentheses in a string are balanced.

Uploaded by

claudiu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 7

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 - Example

pop()
returns 13
peek() pop()
push(13) 13
returns 10 returns 10
pop() peek()
push(10) 10 10 10 10 returns 3 returns 20

push(3) 3 3 3 3 3 3 20
push(20)
push(7)
7 7 7 7 7 7 7 7 7
Stack – Array-based
Implementation
#include <stdio.h> T peek() {
if (isEmpty()) {
#define NMAX 100
fprintf(stderr, "Error 103 - The stack is empty!\n");
template<typename T> class Stack { T x;
private: return x;
T stackArray[NMAX]; }
int topLevel; return stackArray[topLevel]; }
public:
void push(T x) {
if (topLevel >= NMAX - 1) { int isEmpty() {
fprintf(stderr, "Error 101 - The stack is full!\n"); return (topLevel < 0); }
return;
} Stack() { // constructor
topLevel++;
stackArray[topLevel] = x; topLevel = -1; } // the stack is empty in the beginning
} };

T pop() { int main() {


if (isEmpty()) { Stack<int> s;
fprintf(stderr, "Error 102 - The stack is empty!\n");
T x; s.push(7); s.push(3); s.push(10); s.push(13); s.push(20);
return x; s.push(9); s.push(1); printf("%d\n", s.pop());
} printf("%d\n", s.peek()); printf("%d\n", s.peek());
T x = stackArray[topLevel]; printf("%d\n", s.pop()); printf("%d\n", s.peek());
topLevel--; s.push(100); printf("%d\n", s.pop());
return x;
} return 0;
}
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> fprintf(stderr, "Error 202 - Wrong paranthesis\n");
#include <string.h> error = 1;
// copy & paste the definition of the Stack class break;
}
char s[200]="([{()[]}(()[{}()])])(){}[{{()[]}()}]";
int error = 0; stk.pop();
}
int main() {
Stack<char> stk; }

for (int i = 0; i < strlen(s); i++) { if (!error && !stk.isEmpty())


if (s[i] == '(' || s[i] == '[' || s[i] == '{') fprintf(stderr, "Error 203 - The stack is not empty at the
stk.push(s[i]); end\n");
else if (stk.isEmpty()) { else if (!error)
fprintf(stderr, "Error 201 - Empty stack\n");
error = 1; printf("OK\n");
break;
} return 0;
else { }
if ((s[i] == ')' && stk.peek() != '(') ||
(s[i] == ']' && stk.peek() != '[') ||
(s[i] == '}' && stk.peek() != '{')) {
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

You might also like