0% found this document useful (0 votes)
66 views42 pages

Lecture 3.2.1 Stack New

The document outlines a course on Basic Data Structures using C++ at the University Institute of Engineering, detailing course objectives, outcomes, evaluation schemes, and specific content on stacks. It covers fundamental concepts such as stack operations (push and pop), implementations, and notations for arithmetic expressions. The assessment pattern is also described, highlighting the balance between theory and practical evaluations.

Uploaded by

Rahul
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)
66 views42 pages

Lecture 3.2.1 Stack New

The document outlines a course on Basic Data Structures using C++ at the University Institute of Engineering, detailing course objectives, outcomes, evaluation schemes, and specific content on stacks. It covers fundamental concepts such as stack operations (push and pop), implementations, and notations for arithmetic expressions. The assessment pattern is also described, highlighting the balance between theory and practical evaluations.

Uploaded by

Rahul
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/ 42

UNIVERSITY INSTITUTE OF ENGINEERING

DEPARTMENT- ACADEMIC UNITS


FIRST YEAR ENGINEERING PROGRAMMES
Subject Name : Basic Data Structures using C++
Subject Code: 24CSH-103

STACK DISCOVER . LEARN . EMPOWER


Basic 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

Direct Evaluation Weightage of actual Final Weightage in Mapping with SIs Remarks
Sl No. Frequency of Task BT Levels CO Mapping Mapping with PIs
Instruments conduct Internal Assessment (ABET) (Graded/Non-Graded)

SO1 1a,1b,1c
10 marks for each
1 Assignment One per unit 10 Hard CO4,CO5 Graded
assignment

SO6
2 Exam 20 marks for one MST 2 per semester 20 Medium CO1,CO2,CO3,CO4 6a,6b Graded

3 Case Study 8 marks 1 per unit 8 Easy CO2 SO1, SO6 1c,6b Graded
NA NA NA NA
One per lecture
4 Homework NA topic (of 2 NA Non-Graded
questions)

NA NA NA NA
5 Discussion Forum NA One per unit NA Non-Graded

NA NA NA NA
6 Presentation NA NA NA Non-Graded

7 Attendance NA NA 2 NA NA NA NA Graded

Remarks
Direct Evaluation Final Weightage in Mapping with SIs
S No. Weightage of actual conduct Frequency of Task BT Levels CO Mapping Mapping with PIs (Graded/Non-
Instruments Internal Assessment (ABET)
Graded)
Unit wise Practical 1a, 1b, 1c, 6a, 6b SO1, SO6
1 15 marks 3 45 Medium 1,2,3,4,5 Graded
Evaluation
1a, 1b, 1c, 6a, 6b
2 Exam 15 marks for one MST 1 per semester 15 Medium 1,2,3 SO1, SO6 Graded
3 Attendance NA NA 2 NA NA NA NA Graded

4
ASSESSMENT PATTERN
The performance of students is evaluated as follows:

Theory Practical
Continuous Internal Semester End Continuous Internal Semester End
Components Assessment (CAE) Examination (SEE) Assessment (CAE) Examination (SEE)

Marks 40 60 60 40
Total Marks 100 100

5
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).

6
Stack Representation

7
• 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 8
value of the stack without actually removing it.
Sequential/ Array representation of
Stack

• Stack may be represented in the computer in


various ways, usually by means of a one-way
list or a linear array.
• In case of linear array, a pointer variable TOP
contains the location of the top element of
the stack and a variable MAXSTK which gives
the maximum number of elements that can
be held by the stack.
• The condition TOP=0 or TOP=NULL will
indicate that the stack is empty.
Insertion in a Stack
• Stack is implemented here as a one dimensional array of size 7.
Insertion in a Stack(contd..)
Insertion in a Stack(contd..)
Deletion from stack
Deletion from stack(contd..)
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

15
Algorithm to insert in Stack
Algorithm: PUSH(STACK,TOP,MAXSTK,ITEM)
This procedure pushes an ITEM onto a stack.

1. [Stack already filled?]


If TOP=MAXSTK,
then: Print: OVERFLOW & Exit
2. Set TOP=TOP+1 [Increases TOP by 1]
3. Set STACK[TOP]=ITEM [Inserts ITEM in new
TOP position.]
4. Exit
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.

17
Algorithm to delete in Stack
Algorithm: POP(STACK,TOP,ITEM)
This procedure deletes the top element of STACK & assigns
it to the variable ITEM.

1. [Stack has an item to be removed?]


If TOP=0,
then: Print: UNDERFLOW & Exit
2. Set ITEM=STACK[TOP] [Assigns TOP element to ITEM]
3. Set TOP=TOP-1 [Decreases TOP by 1]
4. Exit
Linked representation of Stack
• The INFO fields of the nodes hold the elements of the stack and the LINK fields hold pointers to the
neighboring element in the stack.
• The START pointer of the linked list behaves as the TOP pointer variable of the stack and the null pointer of
the last node in the list signals the bottom of stack.
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.
20
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-


21
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
(−)
22
INFIX, POSTFIX & PREFIX
• Conversion from INFIX to POSTFIX- The only rules to remember during
the conversion process are that operators with highest precedence are
converted first and that after a portion of the expression has been
converted to postfix it is to be treated as a single operand.
• When un-parenthesized operators of the same precedence are scanned, the
order is assumed to be left to right except in the case of exponentiation, where
the order is assumed to be from right to left.
• Thus, A+B+C means (A+B)+C whereas, A^B^C means A^(B^C)

PRECEDENCE – HIGHEST to LOWEST


BRACKETS
EXPONENTIATION (↑^$)
MULTIPLICATION/DIVISION
ADDITION/SUBTRACTION
INFIX to POSTFIX Examples
INFIX POSTFIX
A+B AB+
A+B-C AB+C-
(A+B) * (C-D) AB+CD-*
A$B*C-D+E/F/(G+H) AB$C*D-EF/GH+/+
((A+B)*C-(D-E))$(F+G) AB+C*DE- -FG+$
A-B/(C*D$E) ABCDE$*/-
A+((B+C)*(D+E))+F/G ABC+DE+*+FG/+
A+(B+C*D+E)+F/G ABCD*+E++FG/+
A-B/(C^D)+(E*F) ABCD^/-EF*+
A*(B+C)+(B/D)*A+Z*U ABC+*BD/A*+ZU*+
A+(B*C-(D/E^F)*G)*H ABC*DEF^/G*-H*+
Algorithm for Infix to Postfix
Suppose Q is an arithmetic expression written in infix notation. This algorithm also finds the
equivalent postfix expression P.
1) Push “(“ onto stack and add “)” to the end of Q.
2) Scan Q from left to right and repeat steps 3 to 6 for each element of Q until the stack is empty:
3) If an operand is encountered, add it to P.
4) If a left parenthesis is encountered, push it onto stack.
5) If an operator ⊗ is encountered, then:
(a) Repeatedly pop from stack and add to P each operator(on top of stack)
which has the same precedence as or higher precedence than ⊗.
(b) Add ⊗ to stack.
[End of if structure]
6) If a right parenthesis is encountered, then:
(a) Repeatedly pop from stack and add to P each operator until a left
parenthesis is encountered.
(b) Remove a left parenthesis.
[end of if structure]
Application of stacks :conversion from infix to postfix representation
Symbol scanned Stack Expression P
2 ( 2
* (* 2
3 (* 23
Suppose we want to convert 2*3/(2-1)+5*3
into Postfix form,
/ (/ 23*
Push “(“ onto stack and add “)” to the end ( (/( 23*
of expression.
2 (/( 23*2
- (/(- 23*2
1 (/(- 23*21
) (/ 23*21-
+ (+ 23*21-/
5 (+ 23*21-/5
* (+* 23*21-/53
3 (+* 23*21-/53
) Empty 23*21-/53*+
Application of stacks :conversion from infix to postfix representation

Symbol scanned Stack Expression P


( ((
A (( A

Suppose we want to convert (A+B)*(C-D)


+ ((+ A
into Postfix form, B ((+ AB
Push “(“ onto stack and add “)” to the end
of expression. ) ( AB+
* (* AB+
( (*( AB+
C (*( AB+C
- (*(- AB+C
D (*(- AB+CD
) (* AB+CD-
) AB+CD-*
Application of stacks :conversion from infix to postfix representation
Symbol scanned Stack Expression P
( ((
A (( A
+ ((+ A
B ((+ AB
Suppose we want to convert (A+B)*C – (D-
E)^F into Postfix form, ) ( AB+
Push “(“ onto stack and add “)” to the end * (* AB+
of expression.
C (* AB+C
- (- AB+C*
( (-( AB+C*
D (-( AB+C*D
- (-(- AB+C*D
E (-(- AB+C*DE
) (- AB+C*DE-
^ (-^ AB+C*DE-
F (-^ AB+C*DE-F
) AB+C*DE-F^-
Symbol scanned Stack Expression P
A ( A
+ (+ A
( (+( A
B (+( AB
Application of stacks :conversion * (+(* AB
from infix to postfix representation
C (+(* ABC
- (+(- ABC*
( (+(- ABC*
D (+(- ABC*D
Suppose we want to convert / (+(-(/ ABC*D
A+(B*C-(D/E^F)*G)*H into Postfix form,
E (+(-(/ ABC*DE
^ (+(-(/^ ABC*DE
F (+(-(/^ ABC*DEF
) (+(- ABC*DEF^/
* (+(-* ABC*DEF^/
G (+(-* ABC*DEF^/G
) (+ ABC*DEF^/G*-
* (+* ABC*DEF^/G*-
H (+* ABC*DEF^/G*-H
) ABC*DEF^/G*-H*+
Symbol scanned Stack Expression P
A ( A
^ (^ A
B (^ AB
* (* AB^
Application of stacks :conversion C (* AB^C
from infix to postfix representation
- (- AB^C*
D (- AB^C*D
+ (+ AB^C*D-
E (+ AB^C*D-E
Suppose we want to convert / (+/ AB^C*D-E
A^B*C-D+E/F/(G+H)
F (+/ AB^C*D-EF
into Postfix form,
/ (+/ AB^C*D-EF/
( (+/( AB^C*D-EF/
G (+/( AB^C*D-EF/G
+ (+/(+ AB^C*D-EF/G
H (+/(+ AB^C*D-EF/GH
) (+/ AB^C*D-EF/GH+
) AB^C*D-EF/GH+/+
Symbol scanned Stack Expression P
A ( A
* (* A
B (* AB
- (- AB*
Application of stacks :conversion C (- AB*C
from infix to postfix representation
- (- AB*C-
D (- AB*C-D
+ (+ AB*C-D-
E (+ AB*C-D-E
Suppose we want to convert A*B-C- * (+* AB*C-D-E
D+E*F-G/H*T
F (+* AB*C-D-EF
into Postfix form,
- (- AB*C-D-EF*+
G (- AB*C-D-EF*+G
/ (-/ AB*C-D-EF*+G
H (-/ AB*C-D-EF*+GH
* (-* AB*C-D-EF*+GH/
T (-* AB*C-D-EF*+GH/T
) AB*C-D-EF*+GH/T*-
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

32
Application of stacks: Evaluating a postfix expression
• This algorithm finds the value of an arithmetic expression P written in postfix
notation.
1) Add a right parenthesis “)” at the end of P(expression).
2) Scan P from left to right and repeat steps 3 & 4 for each element of P until the
sentinel “)” is encountered.
3) If an operand is encountered, put it on STACK.
4) If an operator ⊗ is encountered , then:
a) Remove the two top elements of stack, where A is the top element and
B is the next-to-top element.
b) Evaluate B ⊗ A.
c) Place the result of (b) back on stack.
[End of if structure]
[End of step2 loop]
5) Set value equal to the top element on stack.
6) Exit
Evaluating a postfix
expression(contd..)

Final result is: 45


Evaluate these postfix expressions
• 623+-382/+*2^3+
• 5 6 2 + * 12 4 / -
• 231*+9–
• 2 10 + 9 6 - /
• 123*+4-

Ans 1: 52
Ans 2: 37
Ans 3: -4
Ans 4: 4
Ans 5: 3
INFIX to PREFIX Examples
INFIX POSTFIX
A+B +AB
A+B-C -+ABC
(A+B) * (C-D) *+AB-CD
A$B*C-D+E/F/(G+H) +-*$ABCD//EF+GH
((A+B)*C-(D-E))$(F+G) $-*+ABC-DE+FG
A-B/(C*D$E) -A/B*C$DE
A+((B+C)*(D+E))+F/G ++A*+BC+DE/FG
A+(B+C*D+E)+F/G ++A++B*CDE/FG
A-B/(C^D)+(E*F) +-A/B^CD*EF
A*(B+C)+(B/D)*A+Z*U ++*A+BC*/BDA*ZU
A+(B*C-(D/E^F)*G)*H +A*-*BC*/D^EFGH
Prefix to Infix

operator Left operand Right operand

PREFIX : +A-BC
+A-BC
+A(B-C)
INFIX: (A+(B-C))
Postfix to Infix - So pop

POSTFIX: ABCDE-+/*EF*-
E E
D D D (D-E)
C C C C C
B B B B B B
A A A A A A A

+ So pop / So pop * So pop

(D-E)
C (C+(D-E)) (C+(D-E))
B B B (B/(C+(D-E)) (B/(C+(D-E))
A A A A A
Postfix to Infix(contd..)
* So pop

F F
E E E
(A*(B/(C+(D-E)))) (A*(B/(C+(D-E)))) (A*(B/(C+(D-E)))) (A*(B/(C+(D-E))))

- So pop

(E*F)-(A*(B/(C+(D-E))))
(E*F)
(E*F)
(A*(B/(C+(D-E))))
(A*(B/(C+(D-E))))
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

40
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
41
THANK YOU

You might also like