0% found this document useful (0 votes)
25 views20 pages

Stack

The document outlines a course on Elementary Data Structures using C++ for Bachelor of Engineering students, focusing on understanding C++ constructs, data structures, and algorithm efficiency. It details the objectives, outcomes, and evaluation scheme, while specifically discussing the stack data structure, its operations (push and pop), and various notations for arithmetic expressions. Additionally, it includes applications of stacks and references for further reading.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views20 pages

Stack

The document outlines a course on Elementary Data Structures using C++ for Bachelor of Engineering students, focusing on understanding C++ constructs, data structures, and algorithm efficiency. It details the objectives, outcomes, and evaluation scheme, while specifically discussing the stack data structure, its operations (push and pop), and various notations for arithmetic expressions. Additionally, it includes applications of stacks and references for further reading.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

UNIVERSITY INSTITUTE OF ENGINEERING

DEPARTMENT- ACADEMIC UNITS


Bachelor of Engineering (Computer Science &
Engineering)
Subject Name : Elementary Data Structures using C++
Subject Code: 23CSH-103

STACK DISCOVER . LEARN . EMPOWER


Elementary Data
Structure Using C++

Course Objectives

• To enable the students to understand


various stages and constructs of C++
programming language.
• To improve their ability to analyze and
address variety of problems in C++.
• To understand the concept of data
structures and various operations on
them.
• To understand the properties of
various data structures and able to
identify the strengths and weaknesses
of different data structures.
• To analyze and compare the efficiency
of algorithms and learn to design
2
efficient algorithms for solving
Course Outcomes
CO Course Outcome
Number

CO1 Understand the concepts of object-oriented programming including


programming process and compilation process.
CO2 Apply different techniques to decompose a problem and
programmed a solution with various concepts of object-oriented
programming language.
CO3 Analyse and explain the behaviour of linear data structure
operations using the programming addressed in the course.
CO4 Implement and evaluate the programs using the syntax and
semantics of object-oriented programming.
CO5 Design the solution of real-world problems in order to determine
that the program performs as expected.

3
Scheme of Evaluation

4
DEFINITION • The last element to be added is the first to
be removed (LIFO: Last In, First Out).
A stack is an Abstract Data Type (ADT),
commonly used in most programming
languages. It is named stack as it behaves
like a real-world stack, for example – a deck
of cards or stack of trays etc.
It is an ordered group of homogeneous items
of elements.
Elements are added to and removed from
the top of the stack (the most recently
added items are at the top of the stack).

5
Stack Representation

6
• A stack can be implemented by means of Array, Structure, Pointer, and Linked List. Stack
can either be a fixed size one or it may have a sense of dynamic resizing. Here, we are
going to implement stack using arrays, which makes it a fixed size stack
implementation.
Basic Operations
Stack operations may involve initializing the stack, using it and then de-initializing it.
Apart from these basic stuffs, a stack is used for the following two primary operations −
• push() − Pushing (storing) an element on the stack.
• pop() − Removing (accessing) an element from the stack.

• To use a stack efficiently, we need to check the status of stack as well. For the same
purpose, the following functionality is added to stacks −
• peek() − get the top data element of the stack, without removing it.
• isFull() − check if stack is full.
• isEmpty() − check if stack is empty.
• At all times, we maintain a pointer to the last PUSHed data on the stack. As this pointer
always represents the top of the stack, hence named top. The top pointer provides top 7
value of the stack without actually removing it.
8
9
Push Operation
• The process of putting a new data element onto stack is known as a Push Operation. Push
operation involves a series of steps −
• Step 1 − Checks if the stack is full.
• Step 2 − If the stack is full, produces an error and exit.
• Step 3 − If the stack is not full, increments top to point next empty space.
• Step 4 − Adds data element to the stack location, where top is pointing.
• Step 5 − Returns success

If the linked list


is used to
implement the
stack, then in
step 3, we
need to
allocate space
dynamically
10
11
Pop Operation

• Accessing the content while removing it from the stack, is known as a Pop Operation. In an
array implementation of pop() operation, the data element is not actually removed,
instead top is decremented to a lower position in the stack to point to the next value. But in
linked-list implementation, pop() actually removes data element and deallocates memory
space.
• A Pop operation may involve the following steps −
• Step 1 − Checks if the stack is empty.
• Step 2 − If the stack is empty, produces an error and exit.
• Step 3 − If the stack is not empty, accesses the data element at which top is pointing.
• Step 4 − Decreases the value of top by 1.
• Step 5 − Returns success.

12
13
Notations
An arithmetic expression can be written in three different but equivalent notations, i.e., without
changing the essence or output of an expression. These notations are −
• Infix Notation
• Prefix (Polish) Notation
• Postfix (Reverse-Polish) Notation
These notations are named as how they use operator in expression. We shall learn the same here
in this chapter.
Infix Notation
We write expression in infix notation, e.g. a - b + c, where operators are used in-between
operands. It is easy for us humans to read, write, and speak in infix notation but the same does
not go well with computing devices. An algorithm to process infix notation could be difficult and
costly in terms of time and space consumption.
Prefix Notation
In this notation, operator is prefixed to operands, i.e. operator is written ahead of operands. For
example, +ab. This is equivalent to its infix notation a + b. Prefix notation is also known as Polish
Notation.
14
Postfix Notation
This notation style is known as Reversed Polish Notation. In this notation style, the operator is postfixed
to the operands i.e., the operator is written after the operands. For example, ab+. This is equivalent to its
infix notation a + b.
The following table briefly tries to show the difference in all three notations −

Sr.No. Infix Notation Prefix Notation Postfix Notation

1 a+b +ab ab+

2 (a + b) ∗ c ∗+abc ab+c∗

3 a ∗ (b + c) ∗a+bc abc+∗

4 a/b+c/d +/ab/cd ab/cd/+

5 (a + b) ∗ (c + d) ∗+ab+cd ab+cd+∗

6 ((a + b) ∗ c) - d -∗+abcd ab+c∗d-


15
Precedence
When an operand is in between two different operators, which operator will take the operand
first, is decided by the precedence of an operator over others.
Associativity
Associativity describes the rule where operators with the same precedence appear in an
expression. For example, in expression a + b − c, both + and – have the same precedence,
then which part of the expression will be evaluated first, is determined by associativity of those
operators. Here, both + and − are left associative, so the expression will be evaluated as (a + b)
− c.
Precedence and associativity determines the order of evaluation of an expression. Following is
an operator precedence and associativity table (highest to lowest) −
In a + b*c, the expression part b*c will be evaluated first, with multiplication as precedence over
addition. We here use parenthesis for a + b to be evaluated first, like (a + b)*c.
Sr.No. Operator Precedence Associativity
1 Exponentiation ^ Highest Right Associative
2 Multiplication ( ∗ ) & Second Highest Left Associative
Division ( / )
3 Addition ( + ) & Subtraction Lowest Left Associative
(−)
16
Postfix Evaluation Algorithm

• Step 1 − scan the expression from left to right


• Step 2 − if it is an operand push it to stack
• Step 3 − if it is an operator pull operand from stack and perform
operation
• Step 4 − store the output of step 3, back to stack
• Step 5 − scan the expression until all operands are consumed
• Step 6 − pop the stack and perform operation

17
Applications of Stack
• Express Evolution
• Express interchange
• Infix changes to Postfix
• Infix changes to Prefix
• Postfix changes to Infix
• Prefix changes to Infix
• Parsing easily done
• Simulation of recursion
• Function call

18
REFERENCES

• https://www.cs.cmu.edu/~adamchik/15-121/lectures/Stacks%20and%20Queues/Stacks%20and%2
0Queues.html
• Data Structures with C/ schaum outline series/ volume 2
• https://www.geeksforgeeks.org/stack-data-structure-introduction-program/
• https://www.geeksforgeeks.org/queue-set-1introduction-and-array-implementation/
• http://www.yashcode.com/2017/11/prefix-to-postfix-conversion-using-stack.html
• Lipschutz, Seymour, “Data Structures”, Schaum's Outline Series, Tata McGraw Hill.
• Gilberg/Forouzan,” Data Structure with C ,Cengage Learning.
• Augenstein,Moshe J , Tanenbaum, Aaron M, “Data Structures using C and C++”, Prentice Hall of
India.
• Goodrich, Michael T., Tamassia, Roberto, and Mount, David M., “Data Structures and Algorithms in
C++”, Wiley Student Edition.
• Aho, Alfred V., Ullman, Jeffrey D., Hopcroft ,John E. “Data Structures and Algorithms”, Addison
Wesley
19
THANK YOU

You might also like