SlideShare a Scribd company logo
Data Structures
Lecture 3
RUBYA AFRIN
NORTHERN UNIVERSITY OF BUSINESS AND TECHNOLOGY
Stack
 A stack is a collection of items into which items can be
inserted or deleted at one end.
 A stack implements the LIFO (Last In First Out)protocol
2
Stacks
 Stacks in real life: stack of books, stack of plates
 Add new items at the top
 Remove an item at the top
 Stack data structure similar to real life: collection of elements
arranged in a linear order.
 Can only access element at the top
3
Stack Operations
 Push(X) – insert X as the top element of the stack
 Pop() – remove the top element of the stack and return it.
 Top() – return the top element without removing it from the stack.
4
Stack Operations
push(2)
top 2
push(5)
top
2
5
push(7)
top
2
5
7
push(1)
top
2
5
7
1
1 pop()
top
2
5
7
push(21)
top
2
5
7
21
21 pop()
top
2
5
7
7 pop()
2
5top
5 pop()
2top
5
Stack Operation
 The last element to go into the stack is the first to come out: LIFO –
Last In First Out.
 What happens if we call pop() and there is no element?
 Have IsEmpty() boolean function that returns true if stack is empty,
false otherwise.
 Throw StackEmpty exception: advanced C++ concept.
6
Stack Operation
 Overflow: occurs if we try to insert an item into an array
which is full
 Underflow: occurs when we try to retrieve an item form
an empty list
7
Push Operation
 If the stack is full, print an error message (overflow)
 Make place for new item by incrementing top
 Put the item in place
8
Pop Operation
 If the stack is empty, print an error message an halt
execution (underflow)
 Remove the top element from the stack
 Return the element to the calling program
9
Stack using an Array
top
2
5
7
1
2 5 7 1
0 1 32 4
top = 3
10
Stack using an Array
 In case of an array, it is possible that the array may “fill-up” if we
push enough elements.
 Have a boolean function IsFull() which returns true is stack
(array) is full, false otherwise.
 We would call this function before calling push(x).
11
Stack Operations with Array
int pop()
{
return A[current--];
}
void push(int x)
{
A[++current] = x;
}
12
Stack Operations with Array
int top()
{
return A[current];
}
int IsEmpty()
{
return ( current == -1 );
}
int IsFull()
{
return ( current == size-1);
}
 A quick examination shows that all five operations take constant
time.
13
Stack Using Linked List
 We can avoid the size limitation of a stack implemented with an
array by using a linked list to hold the stack elements.
 As with array, however, we need to decide where to insert elements
in the list and where to delete them so that push and pop will run the
fastest.
14
Operations in dynamic stack 15
Use of Stack
 Example of use: prefix, infix, postfix expressions.
 Consider the expression A+B: we think of applying the operator “+”
to the operands A and B.
 “+” is termed a binary operator: it takes two operands.
 Writing the sum as A+B is called the infix form of the expression.
16
Prefix, Infix, Postfix
 Two other ways of writing the expression are
+ A B prefix
A B + postfix
 The prefixes “pre” and “post” refer to the position of the operator with
respect to the two operands.
17
Prefix, Infix, Postfix
 Consider the infix expression
A + B * C
 We “know” that multiplication is done before addition.
 The expression is interpreted as
A + ( B * C )
 Multiplication has precedence over addition.
18
Prefix, Infix, Postfix
 Conversion to postfix
(A + B ) * C infix form
19
Prefix, Infix, Postfix
 Conversion to postfix
(A + B ) * C infix form
( A B + ) * C convert addition
20
Prefix, Infix, Postfix
 Conversion to postfix
(A + B ) * C infix form
( A B + ) * C convert addition
( A B + ) C * convert multiplication
21
Prefix, Infix, Postfix
 Conversion to postfix
(A + B ) * C infix form
( A B + ) * C convert addition
( A B + ) C * convert multiplication
A B + C * postfix form
22
Precedence of Operators
 The five binary operators are: addition, subtraction, multiplication,
division and exponentiation.
 The order of precedence is (highest to lowest)
 Exponentiation 
 Multiplication/division*, /
 Addition/subtraction +, -
23
Infix to Postfix
Infix Postfix
A + B A B +
12 + 60 – 23 12 60 + 23 –
(A + B)*(C – D ) A B + C D – *
24
Converting Infix to Postfix
 Example: A + B * C
symb postfix stack
A A
25
Converting Infix to Postfix
 Example: A + B * C
symb postfix stack
A A
+ A +
26
Converting Infix to Postfix
 Example: A + B * C
symb postfix stack
A A
+ A +
B AB +
27
Converting Infix to Postfix
 Example: A + B * C
symb postfix stack
A A
+ A +
B AB +
* AB + *
28
Converting Infix to Postfix
 Example: A + B * C
symb postfix stack
A A
+ A +
B AB +
* AB + *
C ABC + *
29
Converting Infix to Postfix
 Example: A + B * C
symb postfix stack
A A
+ A +
B AB +
* AB + *
C ABC + *
ABC * +
30
Converting Infix to Postfix
 Example: A + B * C
symb postfix stack
A A
+ A +
B AB +
* AB + *
C ABC + *
ABC * +
ABC * +
31
Prefix, Infix, Postfix
 Conversion to postfix
A + ( B * C ) infix form
32
Prefix, Infix, Postfix
 Conversion to postfix
A + ( B * C ) infix form
A + ( B C * ) convert multiplication
33
Prefix, Infix, Postfix
 Conversion to postfix
A + ( B * C ) infix form
A + ( B C * ) convert multiplication
A ( B C * ) + convert addition
34
Converting Infix to Postfix
 Handling parenthesis
 When an open parenthesis ‘(‘ is read, it must be pushed on the
stack.
 This can be done by setting prcd(op,‘(‘ ) to be FALSE.
 Also, prcd( ‘(‘,op ) == FALSE which ensures that an operator after ‘(‘
is pushed on the stack.
35
Converting Infix to Postfix
 When a ‘)’ is read, all operators up to the first ‘(‘ must be popped and
placed in the postfix string.
 To do this, prcd( op,’)’ ) == TRUE.
 Both the ‘(‘ and the ‘)’ must be discarded: prcd( ‘(‘,’)’ ) == FALSE.
 Need to change line 11 of the algorithm.
36
Prefix, Infix, Postfix
 Conversion to postfix
A + ( B * C ) infix form
A + ( B C * ) convert multiplication
A ( B C * ) + convert addition
A B C * + postfix form
Symbol
Scanned
Stack Postfix
Notation
A empty A
+ + A
( +,( A
B +,( AB
* +,(,* AB
C +,(,* ABC
) ABC*+
37

More Related Content

What's hot

Application of Stack - Yadraj Meena
Application of Stack - Yadraj MeenaApplication of Stack - Yadraj Meena
Application of Stack - Yadraj Meena
Dipayan Sarkar
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
A. S. M. Shafi
 
Stacks
StacksStacks
03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays03 stacks and_queues_using_arrays
03 stacks and_queues_using_arraystameemyousaf
 
Infix to Prefix (Conversion, Evaluation, Code)
Infix to Prefix (Conversion, Evaluation, Code)Infix to Prefix (Conversion, Evaluation, Code)
Infix to Prefix (Conversion, Evaluation, Code)Ahmed Khateeb
 
Stack data structure
Stack data structureStack data structure
Stack data structureTech_MX
 
Conversion of Infix To Postfix Expressions
Conversion of Infix To Postfix Expressions Conversion of Infix To Postfix Expressions
Conversion of Infix To Postfix Expressions
Kulachi Hansraj Model School Ashok Vihar
 
Prefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix NotationsPrefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix Notations
Afaq Mansoor Khan
 
2.2 stack applications Infix to Postfix & Evaluation of Post Fix
2.2 stack applications Infix to Postfix & Evaluation of Post Fix2.2 stack applications Infix to Postfix & Evaluation of Post Fix
2.2 stack applications Infix to Postfix & Evaluation of Post Fix
P. Subathra Kishore, KAMARAJ College of Engineering and Technology, Madurai
 
Stack Operation In Data Structure
Stack Operation In Data Structure Stack Operation In Data Structure
Stack Operation In Data Structure
DivyeshKumar Jagatiya
 
Stack_Application_Infix_Prefix.pptx
Stack_Application_Infix_Prefix.pptxStack_Application_Infix_Prefix.pptx
Stack_Application_Infix_Prefix.pptx
sandeep54552
 
Stack push pop
Stack push popStack push pop
Stack push pop
A. S. M. Shafi
 
Queue
QueueQueue
stack
stackstack
stack
Raj Sarode
 
Stack
StackStack
Stacks
StacksStacks
Project of data structure
Project of data structureProject of data structure
Project of data structure
Umme habiba
 
Operation on stack
Operation on stackOperation on stack
Operation on stack
chetan handa
 
MEngine Overview
MEngine OverviewMEngine Overview
MEngine Overview
euming
 
Lab 3 Multi-Function Gate
Lab 3   Multi-Function GateLab 3   Multi-Function Gate
Lab 3 Multi-Function Gate
Katrina Little
 

What's hot (20)

Application of Stack - Yadraj Meena
Application of Stack - Yadraj MeenaApplication of Stack - Yadraj Meena
Application of Stack - Yadraj Meena
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
 
Stacks
StacksStacks
Stacks
 
03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays
 
Infix to Prefix (Conversion, Evaluation, Code)
Infix to Prefix (Conversion, Evaluation, Code)Infix to Prefix (Conversion, Evaluation, Code)
Infix to Prefix (Conversion, Evaluation, Code)
 
Stack data structure
Stack data structureStack data structure
Stack data structure
 
Conversion of Infix To Postfix Expressions
Conversion of Infix To Postfix Expressions Conversion of Infix To Postfix Expressions
Conversion of Infix To Postfix Expressions
 
Prefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix NotationsPrefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix Notations
 
2.2 stack applications Infix to Postfix & Evaluation of Post Fix
2.2 stack applications Infix to Postfix & Evaluation of Post Fix2.2 stack applications Infix to Postfix & Evaluation of Post Fix
2.2 stack applications Infix to Postfix & Evaluation of Post Fix
 
Stack Operation In Data Structure
Stack Operation In Data Structure Stack Operation In Data Structure
Stack Operation In Data Structure
 
Stack_Application_Infix_Prefix.pptx
Stack_Application_Infix_Prefix.pptxStack_Application_Infix_Prefix.pptx
Stack_Application_Infix_Prefix.pptx
 
Stack push pop
Stack push popStack push pop
Stack push pop
 
Queue
QueueQueue
Queue
 
stack
stackstack
stack
 
Stack
StackStack
Stack
 
Stacks
StacksStacks
Stacks
 
Project of data structure
Project of data structureProject of data structure
Project of data structure
 
Operation on stack
Operation on stackOperation on stack
Operation on stack
 
MEngine Overview
MEngine OverviewMEngine Overview
MEngine Overview
 
Lab 3 Multi-Function Gate
Lab 3   Multi-Function GateLab 3   Multi-Function Gate
Lab 3 Multi-Function Gate
 

Similar to Data structures

CH4.pptx
CH4.pptxCH4.pptx
CH4.pptx
AliJama14
 
Stack
StackStack
stack-Intro.pptx
stack-Intro.pptxstack-Intro.pptx
stack-Intro.pptx
DEEPAK948083
 
stack-111104232459-phpapp02.pdf
stack-111104232459-phpapp02.pdfstack-111104232459-phpapp02.pdf
stack-111104232459-phpapp02.pdf
Kalpana Mohan
 
computer notes - Data Structures - 6
computer notes - Data Structures - 6computer notes - Data Structures - 6
computer notes - Data Structures - 6ecomputernotes
 
Please need help on C++ language.Infix to Postfix) Write a program.pdf
Please need help on C++ language.Infix to Postfix) Write a program.pdfPlease need help on C++ language.Infix to Postfix) Write a program.pdf
Please need help on C++ language.Infix to Postfix) Write a program.pdf
pristiegee
 
The Stack And Recursion
The Stack And RecursionThe Stack And Recursion
The Stack And Recursion
Ashim Lamichhane
 
Lec5-Stack-bukc-28022024-112316am (1) .pptx
Lec5-Stack-bukc-28022024-112316am (1) .pptxLec5-Stack-bukc-28022024-112316am (1) .pptx
Lec5-Stack-bukc-28022024-112316am (1) .pptx
haaamin01
 
Stack - Data Structure - Notes
Stack - Data Structure - NotesStack - Data Structure - Notes
Stack - Data Structure - Notes
Omprakash Chauhan
 
Stacks,queues,linked-list
Stacks,queues,linked-listStacks,queues,linked-list
Stacks,queues,linked-list
pinakspatel
 
Stacks.ppt
Stacks.pptStacks.ppt
Stacks.ppt
OliverKane3
 
Stacks.ppt
Stacks.pptStacks.ppt
Stacks.ppt
OliverKane3
 
Stacks
StacksStacks
Stacks
Sadaf Ismail
 
Unit 3 stack
Unit   3 stackUnit   3 stack
Unit 3 stack
Dabbal Singh Mahara
 
Stacks in DATA STRUCTURE
Stacks in DATA STRUCTUREStacks in DATA STRUCTURE
Stacks in DATA STRUCTURE
Mandeep Singh
 
Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manual
nikshaikh786
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
Vineeta Garg
 
Stack
StackStack
Stack and its operations, Queue and its operations
Stack and its operations, Queue and its operationsStack and its operations, Queue and its operations
Stack and its operations, Queue and its operations
poongothai11
 

Similar to Data structures (20)

CH4.pptx
CH4.pptxCH4.pptx
CH4.pptx
 
Stack
StackStack
Stack
 
stack-Intro.pptx
stack-Intro.pptxstack-Intro.pptx
stack-Intro.pptx
 
stack-111104232459-phpapp02.pdf
stack-111104232459-phpapp02.pdfstack-111104232459-phpapp02.pdf
stack-111104232459-phpapp02.pdf
 
computer notes - Data Structures - 6
computer notes - Data Structures - 6computer notes - Data Structures - 6
computer notes - Data Structures - 6
 
Please need help on C++ language.Infix to Postfix) Write a program.pdf
Please need help on C++ language.Infix to Postfix) Write a program.pdfPlease need help on C++ language.Infix to Postfix) Write a program.pdf
Please need help on C++ language.Infix to Postfix) Write a program.pdf
 
The Stack And Recursion
The Stack And RecursionThe Stack And Recursion
The Stack And Recursion
 
Lec5-Stack-bukc-28022024-112316am (1) .pptx
Lec5-Stack-bukc-28022024-112316am (1) .pptxLec5-Stack-bukc-28022024-112316am (1) .pptx
Lec5-Stack-bukc-28022024-112316am (1) .pptx
 
Stack - Data Structure - Notes
Stack - Data Structure - NotesStack - Data Structure - Notes
Stack - Data Structure - Notes
 
Stacks,queues,linked-list
Stacks,queues,linked-listStacks,queues,linked-list
Stacks,queues,linked-list
 
Stacks.ppt
Stacks.pptStacks.ppt
Stacks.ppt
 
Stacks.ppt
Stacks.pptStacks.ppt
Stacks.ppt
 
Stacks
StacksStacks
Stacks
 
Unit 3 stack
Unit   3 stackUnit   3 stack
Unit 3 stack
 
Stack
StackStack
Stack
 
Stacks in DATA STRUCTURE
Stacks in DATA STRUCTUREStacks in DATA STRUCTURE
Stacks in DATA STRUCTURE
 
Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manual
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
 
Stack
StackStack
Stack
 
Stack and its operations, Queue and its operations
Stack and its operations, Queue and its operationsStack and its operations, Queue and its operations
Stack and its operations, Queue and its operations
 

More from Rokonuzzaman Rony

Course outline for c programming
Course outline for c  programming Course outline for c  programming
Course outline for c programming
Rokonuzzaman Rony
 
Operator Overloading & Type Conversions
Operator Overloading & Type ConversionsOperator Overloading & Type Conversions
Operator Overloading & Type Conversions
Rokonuzzaman Rony
 
Constructors & Destructors
Constructors  & DestructorsConstructors  & Destructors
Constructors & Destructors
Rokonuzzaman Rony
 
Classes and objects in c++
Classes and objects in c++Classes and objects in c++
Classes and objects in c++
Rokonuzzaman Rony
 
Object Oriented Programming with C++
Object Oriented Programming with C++Object Oriented Programming with C++
Object Oriented Programming with C++
Rokonuzzaman Rony
 
Humanitarian task and its importance
Humanitarian task and its importanceHumanitarian task and its importance
Humanitarian task and its importance
Rokonuzzaman Rony
 
Pointers
 Pointers Pointers
Loops
LoopsLoops
Introduction to C programming
Introduction to C programmingIntroduction to C programming
Introduction to C programming
Rokonuzzaman Rony
 
Array
ArrayArray
Constants, Variables, and Data Types
Constants, Variables, and Data TypesConstants, Variables, and Data Types
Constants, Variables, and Data Types
Rokonuzzaman Rony
 
C Programming language
C Programming languageC Programming language
C Programming language
Rokonuzzaman Rony
 
User defined functions
User defined functionsUser defined functions
User defined functions
Rokonuzzaman Rony
 

More from Rokonuzzaman Rony (20)

Course outline for c programming
Course outline for c  programming Course outline for c  programming
Course outline for c programming
 
Pointer
PointerPointer
Pointer
 
Operator Overloading & Type Conversions
Operator Overloading & Type ConversionsOperator Overloading & Type Conversions
Operator Overloading & Type Conversions
 
Constructors & Destructors
Constructors  & DestructorsConstructors  & Destructors
Constructors & Destructors
 
Classes and objects in c++
Classes and objects in c++Classes and objects in c++
Classes and objects in c++
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
Object Oriented Programming with C++
Object Oriented Programming with C++Object Oriented Programming with C++
Object Oriented Programming with C++
 
Humanitarian task and its importance
Humanitarian task and its importanceHumanitarian task and its importance
Humanitarian task and its importance
 
Structure
StructureStructure
Structure
 
Pointers
 Pointers Pointers
Pointers
 
Loops
LoopsLoops
Loops
 
Introduction to C programming
Introduction to C programmingIntroduction to C programming
Introduction to C programming
 
Array
ArrayArray
Array
 
Constants, Variables, and Data Types
Constants, Variables, and Data TypesConstants, Variables, and Data Types
Constants, Variables, and Data Types
 
C Programming language
C Programming languageC Programming language
C Programming language
 
User defined functions
User defined functionsUser defined functions
User defined functions
 
Numerical Method 2
Numerical Method 2Numerical Method 2
Numerical Method 2
 
Numerical Method
Numerical Method Numerical Method
Numerical Method
 
Data structures
Data structuresData structures
Data structures
 
Data structures
Data structures Data structures
Data structures
 

Recently uploaded

A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
thanhdowork
 
Digital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion DesignsDigital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion Designs
chanes7
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
vaibhavrinwa19
 
Chapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdfChapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdf
Kartik Tiwari
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
DhatriParmar
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
Scholarhat
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
tarandeep35
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 

Recently uploaded (20)

A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
 
Digital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion DesignsDigital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion Designs
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
 
Chapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdfChapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdf
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 

Data structures

  • 1. Data Structures Lecture 3 RUBYA AFRIN NORTHERN UNIVERSITY OF BUSINESS AND TECHNOLOGY
  • 2. Stack  A stack is a collection of items into which items can be inserted or deleted at one end.  A stack implements the LIFO (Last In First Out)protocol 2
  • 3. Stacks  Stacks in real life: stack of books, stack of plates  Add new items at the top  Remove an item at the top  Stack data structure similar to real life: collection of elements arranged in a linear order.  Can only access element at the top 3
  • 4. Stack Operations  Push(X) – insert X as the top element of the stack  Pop() – remove the top element of the stack and return it.  Top() – return the top element without removing it from the stack. 4
  • 5. Stack Operations push(2) top 2 push(5) top 2 5 push(7) top 2 5 7 push(1) top 2 5 7 1 1 pop() top 2 5 7 push(21) top 2 5 7 21 21 pop() top 2 5 7 7 pop() 2 5top 5 pop() 2top 5
  • 6. Stack Operation  The last element to go into the stack is the first to come out: LIFO – Last In First Out.  What happens if we call pop() and there is no element?  Have IsEmpty() boolean function that returns true if stack is empty, false otherwise.  Throw StackEmpty exception: advanced C++ concept. 6
  • 7. Stack Operation  Overflow: occurs if we try to insert an item into an array which is full  Underflow: occurs when we try to retrieve an item form an empty list 7
  • 8. Push Operation  If the stack is full, print an error message (overflow)  Make place for new item by incrementing top  Put the item in place 8
  • 9. Pop Operation  If the stack is empty, print an error message an halt execution (underflow)  Remove the top element from the stack  Return the element to the calling program 9
  • 10. Stack using an Array top 2 5 7 1 2 5 7 1 0 1 32 4 top = 3 10
  • 11. Stack using an Array  In case of an array, it is possible that the array may “fill-up” if we push enough elements.  Have a boolean function IsFull() which returns true is stack (array) is full, false otherwise.  We would call this function before calling push(x). 11
  • 12. Stack Operations with Array int pop() { return A[current--]; } void push(int x) { A[++current] = x; } 12
  • 13. Stack Operations with Array int top() { return A[current]; } int IsEmpty() { return ( current == -1 ); } int IsFull() { return ( current == size-1); }  A quick examination shows that all five operations take constant time. 13
  • 14. Stack Using Linked List  We can avoid the size limitation of a stack implemented with an array by using a linked list to hold the stack elements.  As with array, however, we need to decide where to insert elements in the list and where to delete them so that push and pop will run the fastest. 14
  • 16. Use of Stack  Example of use: prefix, infix, postfix expressions.  Consider the expression A+B: we think of applying the operator “+” to the operands A and B.  “+” is termed a binary operator: it takes two operands.  Writing the sum as A+B is called the infix form of the expression. 16
  • 17. Prefix, Infix, Postfix  Two other ways of writing the expression are + A B prefix A B + postfix  The prefixes “pre” and “post” refer to the position of the operator with respect to the two operands. 17
  • 18. Prefix, Infix, Postfix  Consider the infix expression A + B * C  We “know” that multiplication is done before addition.  The expression is interpreted as A + ( B * C )  Multiplication has precedence over addition. 18
  • 19. Prefix, Infix, Postfix  Conversion to postfix (A + B ) * C infix form 19
  • 20. Prefix, Infix, Postfix  Conversion to postfix (A + B ) * C infix form ( A B + ) * C convert addition 20
  • 21. Prefix, Infix, Postfix  Conversion to postfix (A + B ) * C infix form ( A B + ) * C convert addition ( A B + ) C * convert multiplication 21
  • 22. Prefix, Infix, Postfix  Conversion to postfix (A + B ) * C infix form ( A B + ) * C convert addition ( A B + ) C * convert multiplication A B + C * postfix form 22
  • 23. Precedence of Operators  The five binary operators are: addition, subtraction, multiplication, division and exponentiation.  The order of precedence is (highest to lowest)  Exponentiation   Multiplication/division*, /  Addition/subtraction +, - 23
  • 24. Infix to Postfix Infix Postfix A + B A B + 12 + 60 – 23 12 60 + 23 – (A + B)*(C – D ) A B + C D – * 24
  • 25. Converting Infix to Postfix  Example: A + B * C symb postfix stack A A 25
  • 26. Converting Infix to Postfix  Example: A + B * C symb postfix stack A A + A + 26
  • 27. Converting Infix to Postfix  Example: A + B * C symb postfix stack A A + A + B AB + 27
  • 28. Converting Infix to Postfix  Example: A + B * C symb postfix stack A A + A + B AB + * AB + * 28
  • 29. Converting Infix to Postfix  Example: A + B * C symb postfix stack A A + A + B AB + * AB + * C ABC + * 29
  • 30. Converting Infix to Postfix  Example: A + B * C symb postfix stack A A + A + B AB + * AB + * C ABC + * ABC * + 30
  • 31. Converting Infix to Postfix  Example: A + B * C symb postfix stack A A + A + B AB + * AB + * C ABC + * ABC * + ABC * + 31
  • 32. Prefix, Infix, Postfix  Conversion to postfix A + ( B * C ) infix form 32
  • 33. Prefix, Infix, Postfix  Conversion to postfix A + ( B * C ) infix form A + ( B C * ) convert multiplication 33
  • 34. Prefix, Infix, Postfix  Conversion to postfix A + ( B * C ) infix form A + ( B C * ) convert multiplication A ( B C * ) + convert addition 34
  • 35. Converting Infix to Postfix  Handling parenthesis  When an open parenthesis ‘(‘ is read, it must be pushed on the stack.  This can be done by setting prcd(op,‘(‘ ) to be FALSE.  Also, prcd( ‘(‘,op ) == FALSE which ensures that an operator after ‘(‘ is pushed on the stack. 35
  • 36. Converting Infix to Postfix  When a ‘)’ is read, all operators up to the first ‘(‘ must be popped and placed in the postfix string.  To do this, prcd( op,’)’ ) == TRUE.  Both the ‘(‘ and the ‘)’ must be discarded: prcd( ‘(‘,’)’ ) == FALSE.  Need to change line 11 of the algorithm. 36
  • 37. Prefix, Infix, Postfix  Conversion to postfix A + ( B * C ) infix form A + ( B C * ) convert multiplication A ( B C * ) + convert addition A B C * + postfix form Symbol Scanned Stack Postfix Notation A empty A + + A ( +,( A B +,( AB * +,(,* AB C +,(,* ABC ) ABC*+ 37