Stack Data Structure: Overview, Implementation, Applications, and Operations, Essays (high school) of Computer science

An overview of the Stack data structure, its implementation using arrays and linked lists, fundamental operations such as push, pop, top, size, and capacity, and various applications including symbol balancing, infix to postfix conversion, and redo-undo features. It also covers the time complexity and efficiency of stacks.

Typology: Essays (high school)

2017/2018

Uploaded on 03/04/2022

joshua13
joshua13 🇬🇧

11 documents

1 / 10

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
24/10/2018
1
Data
Structure:
Stack
LECTURE 2
Stack Overview
Stack ADT
Basic operations of stack
Pushing, popping etc.
Implementations of stacks using
array
linked list
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Stack Data Structure: Overview, Implementation, Applications, and Operations and more Essays (high school) Computer science in PDF only on Docsity!

Data

Structure:

Stack

LECTURE 2

Stack Overview

 Stack ADT  Basic operations of stack  Pushing, popping etc.  Implementations of stacks using  array  linked list

The Stack ADT

 Stack is a linear data structure which follows a

particular order in which the operations are

performed. The order may be LIFO(Last In First

Out) or FILO(First In Last Out).

 Fundamental operations:

 Push: Equivalent to an insert  Pop: Deletes the most recently inserted element  Top: Examines the most recently inserted element  Size: Number of elements a stack contains at present  Capacity: Number of elements it is capable of holding

Stack ADT

 Stacks are less flexible  but are more efficient and easy to implement  Stacks are known as LIFO (Last In, First Out) lists or or FILO(First In Last Out)The last element inserted will be the first to be retrieved.

Implementation:

There are two ways to implement

a stack:

  • Using array
  • Using linked list.

Array Implementation

 Need to declare an array size ahead of time  Associated with each stack is TopOfStack  for an empty stack, set TopOfStack to -  Push  (1) Increment TopOfStack by 1.  (2) Set Stack[TopOfStack] = X  Pop  (1) Set return value to Stack[TopOfStack]  (2) Decrement TopOfStack by 1  These operations are performed in very fast constant time

Stack class

class Stack

{

private int[] ele; // name of the array for stack

private int top; // storage index

private int max; // total capacity

public Stack(int size)

{

ele = new int[size];//Maximum size of Stack

top = -1;

max = size;

}

Stack class

 Attributes of Stack

 maxTop: the max size of stack  top: the index of the top element of stack  values: point to an array which stores elements of stack

 Operations of Stack

 IsEmpty: return true if stack is empty, return false otherwise  IsFull: return true if stack is full, return false otherwise  Top: return the element at the top of stack  Push: add an element to the top of stack  Pop: delete the element at the top of stack  DisplayStack: print all the data in the stack

Push Stack

 void Push(const double x);  Push an element onto the stack  If the stack is full, print the error information.  Note top always represents the index of the top element. After pushing an element, increment top.

public void push(int item){ if (top == max - 1){ Console.WriteLine("Stack Overflow");return; }else{ ele[++top] = item; }}

Printing all the elements

 void printstack()

 Print all the elements

public void printStack(){if (top == -1){ Console.WriteLine("Stack is Empty");return; }else{for (int i = 0; i <= top; i++){ Console.WriteLine("{0} pushed into stack", ele[i]); }}}

Using Stack

static main(void) {  Stack p = new Stack(5);p.push(5);p.push(6);p.push(-3);p.push(-8);p.printStack();p.size();p.pop(); }

result

Implementation based on Linked List

Now, we will create our stack class. We will define a pointer, top, and initialize it to null. So, our LinkedListStack class will be:

Push an Element Onto a Stack

Pop an Element From the Stack

Peek the Element From Stack

Link for further studies

Prefix to Infix Conversion

 https://www.geeksforgeeks.org/prefix-infix-

conversion/

Sorting array using stacks

 https://www.geeksforgeeks.org/sorting-array-using-stacks/

Iterative tower of Hanoi

 https://www.geeksforgeeks.org/iterative-tower-of-hanoi/

How to create mergable stack?

 https://www.geeksforgeeks.org/create-mergable-stack/

Expression Evaluation

 https://www.geeksforgeeks.org/expression-evaluation/

The Celebrity Problem

 https://www.geeksforgeeks.org/the-celebrity-problem/