SlideShare a Scribd company logo
1 of 19
1
Introduction to Data
Structures
Session I
Dr. V.Umadevi M.Sc(CS &IT). M.Tech (IT)., M.Phil., PhD., D.Litt.,
Director, Department of Computer Science, Jairams Arts and
Science College, Karur.
Data Structures & Algorithms
2
• Introduction to Data Structures
• Primitive Data Structures
• Types of Data structures
– Stacks
– Queues
– Lists
• Stack
– Basic Operation of Stack
– Operations on the Data Structures
– Applications of Infix, Postfix and Prefix
Data Structures & Algorithms
Introduction to Data Structures
3
Data Structures & Algorithms
• A Computer is a machine that manipulates information.
• Computer science studies includes:
– How information is organized in a computer
– How it can be manipulated
– How it can be utilized
Introduction to Data Structures
• Data is represented by the values held temporarily in the program’s data
area or recorded permanently on a file
• Different data values may be related to each other which need to be
organized to form a proper data structure.
• Program follow certain rules to access manipulate and process the
structured data.
Information – Processed data given as output
Data – Raw facts and figures given as input
4
Data Structures & Algorithms
• Organization of data in a computer's memory or in a file.
• Proper choice of a data structure can lead to more
efficient programs.
• Example: Array, Stack, Queue, Linked list, Tree and
Graph.
• Represented as follows:
Data structure=Data organization + allowed operations.
Data Structure
5
Primitive Data structure
• Data structures typically are directly operated upon by the machine
level instructions.
• Primitive data constitute the numbers and characters, which are
built into a programming language.
• Examples are: Integer, Boolean and Characters.
• Other data structures can be constructed from one or more
primitives.
• Simple data structures built from primitives are Strings, Arrays,
and Records
Data Structures & Algorithms
6
Types of Data Structures
– Linear data structure.
– Non-Linear data structure.
• Various names used for naming the elements of a data structure
( Commonly used names are data element, data item,
item aggregate, node list and data object)
• Various data structures are used depending upon the needs and convenience
Data Structures & Algorithms
7
Data Structures & Algorithms
•Elements or items form a sequence one after the other.
•Linear list can be realized in memory using either an
array or
a linked list structure.
•Data is also stored in memory locations by means of
pointers.
Examples are: Arrays, Stacks, Queues and Linked list.
Linear Data Structure
8
Linear Data Structures Types
• Arrays - elements stored in memory locations in a sequential
order
• Stack is an ordered collection of data items
– inserted or deleted from one end i.e., Last In First Out (LIFO)
principle.
• Queue is an ordered list
– inserted at one end and are retrieved from the other end i.e., First
in First out (FIFO) principle.
• Linked Lists -elements stored in memory locations by means of
pointers (chain of structures).
Data Structures & Algorithms
9
Non-Linear Data Structures
• Represents the hierarchical relationship between individual data
items.
• Examples are : Trees, Graphs
• Tree - hierarchical relation between its elements.
• Graph - collection of node and edges
– Node - data element
– Edge - path between two nodes.
Data Structures & Algorithms
10
Operations on the Data Structures
Data Structures & Algorithms
• Traversal-is a technique in which each element is processed individually
and separately.
• Search-is an activity in which a particular record or item is to be found.
• Insertion- is a process in which a new element is added into the structure.
• Deletion- is a process in which a given item is removed from the structure
• Sorting-is a process in which all elements are arranged in a specific order
so that each item can be retrieved easily.
• Merging-is a process in which two structures are combined into a single
structure.
11
Stack
• Ordered collection of items
• Item may be inserted or deleted only from the top of the stack.
• Works on Last In First Out (LIFO) concept.
• Helps backtracking or to return to the previous state.
Data Structures & Algorithms
12
Basic Stack Operations
Createstack Create and Initializes a stack to be empty before it is first used
Push adds an item to a stack
Pop extracts the most recently pushed item from the stack
( removing an item from the stack.)
Full returns non-zero if the stack is full
Empty returns non-zero if the stack is empty
Data Structures & Algorithms
13
Declaration of Stack
• For Contiguous implementation, set an array which hold the entries in the
stack and a counter.
• Create and Initializes a stack to be empty before it is first used
Data Structures & Algorithms
Push and Pop
• Stack is implemented using ‘Push’ and ‘Pop’ operations.
• Operations during implementation should be done with care;
For e.g.,
• popping an item from an empty stack
• pushing an item into a stack that is full should not be done.
[Create stack: create and initialize the stack to be empty]
void Createstack (stack sptr)
return [ sptr.top=0 ]
14
Algorithm for push
Procedure push [s,top ,x]
[this procedure inserts an element [x] to the top of the stack.]
[check for stack overflow, maxstack is constant for maximum size]
[allowed for stacks]
if top> maxstack then
write (“Stack overflow”)
exit
[increment top]
top=top+1
[insert element]
s[top]=x
[finished]
End push
Push Operation
Data Structures & Algorithms
• First step of the algorithm checks for an overflow condition.
• Such a condition exits then the insertion cannot be performed and an
appropriate error message result.
15
Pop Operation
Algorithm for pop
Procedure pop [s, top, x ]
[ this function removes the top element from a stack.]
[check for underflow on stack]
if top <=0 then
write (“Stack is Underflow”) exit
[Pop element from stack]
x=s [top]
[decrement pointer]
top = top-1
End pop
• First step itself checking underflow condition
• If exist an appropriate error message will display and the
procedure is terminated
Data Structures & Algorithms
16
Other operations
[Full: returns non-zero if the stack is full]
boolean_type full(stack_type sptr)
{return sptr .top>=MAXSTACK;}
[Empty: returns non-zero if the stack is empty]
boolean - type empty (stack_type sptr)
{return sptr .top<=0;}
Data Structures & Algorithms
17
Application of Stack
Stack based Language (Re_expressing the expression)
– Computers solve arithmetic expressions by restructuring them so that
the order of each calculation is embedded in the expression.
– Once converted, to the required notation (Either Prefix or Postfix), an
expression can be solved.
Types of Expression
• Any expression represent in 3 ways
• Infix, Prefix and Postfix.
• Expression 4 + 5 * 5 is represented as follows:
– Infix - Operators are embedded between operands- 4+5*5.
– Prefix - Operators precede the operands- + 4 * 5 5
– Postfix - Operators follow the operands- 4 5 5 * +
– Default representation of mathematical expression is Infix (or Polish)
notation.
Data Structures & Algorithms
18
Conversion of Expression-
• Infix to Postfix
– Applying the precedence rule first for operators
– Place the Operators follow the operands
– For e.g., an expression A+(B*C)
• A + ( B * C) -Parenthesis for emphasis
• A + ( BC *) -Convert the multiplication
• A (BC *) + -Convert the addition
• ABC * + -Postfix Form
• Infix to Prefix
– Applying the precedence rule first for operators
– Place operators precede the operands
– For e.g., an expression A+(B*C)
• A + ( B * C) -Parenthesis for emphasis
• A + (* BC) -Convert the multiplication
• + A ( * BC ) -Convert the addition
• * + ABC -Prefix Form
Data Structures & Algorithms
19
• Scratch pad
– Use to write down instructions that cannot be acted upon
immediately.
– Example of this is the rat-in-maze problem.
– Use to solve the problem of traversing a maze.
– Keep track of previously explored routes, or else an infinite loop
could occur.
• Text Editor
– Characters are pushed on a stack as the user enters text.
– Commands to delete one character or a command to delete a
series of characters would also push a character on a stack.
– Example, an identifier to delete one character would pop the
stack once.
– Example, an identifier to delete a sentence would pop all
characters until the stack is empty or a period is encountered.
Data Structures & Algorithms

More Related Content

What's hot

Data structure and its types
Data structure and its typesData structure and its types
Data structure and its types
Navtar Sidhu Brar
 
Stacks and queues
Stacks and queuesStacks and queues
Stacks and queues
Abbott
 
Data structure lecture 1
Data structure lecture 1Data structure lecture 1
Data structure lecture 1
Kumar
 
Data structures (introduction)
 Data structures (introduction) Data structures (introduction)
Data structures (introduction)
Arvind Devaraj
 

What's hot (20)

Data abstraction the walls
Data abstraction the wallsData abstraction the walls
Data abstraction the walls
 
Computer Science-Data Structures :Abstract DataType (ADT)
Computer Science-Data Structures :Abstract DataType (ADT)Computer Science-Data Structures :Abstract DataType (ADT)
Computer Science-Data Structures :Abstract DataType (ADT)
 
Sorting
SortingSorting
Sorting
 
02 Stack
02 Stack02 Stack
02 Stack
 
Abstract data types
Abstract data typesAbstract data types
Abstract data types
 
Data structures
Data structuresData structures
Data structures
 
Link List
Link ListLink List
Link List
 
Data structure and its types
Data structure and its typesData structure and its types
Data structure and its types
 
Stacks and queues
Stacks and queuesStacks and queues
Stacks and queues
 
Stack and Queue by M.Gomathi Lecturer
Stack and Queue by M.Gomathi LecturerStack and Queue by M.Gomathi Lecturer
Stack and Queue by M.Gomathi Lecturer
 
Introduction to data structure and algorithms
Introduction to data structure and algorithmsIntroduction to data structure and algorithms
Introduction to data structure and algorithms
 
Data structure lecture 1
Data structure lecture 1Data structure lecture 1
Data structure lecture 1
 
Introduction to data structure
Introduction to data structureIntroduction to data structure
Introduction to data structure
 
Stack Data Structure
Stack Data StructureStack Data Structure
Stack Data Structure
 
Abstract data types (adt) intro to data structure part 2
Abstract data types (adt)   intro to data structure part 2Abstract data types (adt)   intro to data structure part 2
Abstract data types (adt) intro to data structure part 2
 
Introduction to data structure
Introduction to data structureIntroduction to data structure
Introduction to data structure
 
Introduction to data structure by anil dutt
Introduction to data structure by anil duttIntroduction to data structure by anil dutt
Introduction to data structure by anil dutt
 
Data structures and algorithm analysis in java
Data structures and algorithm analysis in javaData structures and algorithm analysis in java
Data structures and algorithm analysis in java
 
Data structures project
Data structures projectData structures project
Data structures project
 
Data structures (introduction)
 Data structures (introduction) Data structures (introduction)
Data structures (introduction)
 

Similar to Data Structures

Queue ADT for data structure for computer
Queue ADT for data structure for computerQueue ADT for data structure for computer
Queue ADT for data structure for computer
abinathsabi
 

Similar to Data Structures (20)

stack.pptx
stack.pptxstack.pptx
stack.pptx
 
2.1 STACK & QUEUE ADTS
2.1 STACK & QUEUE ADTS2.1 STACK & QUEUE ADTS
2.1 STACK & QUEUE ADTS
 
STACK.pptx
STACK.pptxSTACK.pptx
STACK.pptx
 
Data structure , stack , queue
Data structure , stack , queueData structure , stack , queue
Data structure , stack , queue
 
Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1
 
TSAT Presentation1.pptx
TSAT Presentation1.pptxTSAT Presentation1.pptx
TSAT Presentation1.pptx
 
Data structures
Data structuresData structures
Data structures
 
stacks and queues for public
stacks and queues for publicstacks and queues for public
stacks and queues for public
 
Stack in Sata Structure
Stack in Sata StructureStack in Sata Structure
Stack in Sata Structure
 
Data Structures 2
Data Structures 2Data Structures 2
Data Structures 2
 
Stack and Queue.pptx
Stack and Queue.pptxStack and Queue.pptx
Stack and Queue.pptx
 
Data strucer
Data strucerData strucer
Data strucer
 
Queue ADT for data structure for computer
Queue ADT for data structure for computerQueue ADT for data structure for computer
Queue ADT for data structure for computer
 
Stacks
StacksStacks
Stacks
 
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority QueueWhat is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
 
01-Introduction of DSA-1.pptx
01-Introduction of DSA-1.pptx01-Introduction of DSA-1.pptx
01-Introduction of DSA-1.pptx
 
Fallsem2015 16 cp1699-20-jul-2015_rm01_stacks_and_queues
Fallsem2015 16 cp1699-20-jul-2015_rm01_stacks_and_queuesFallsem2015 16 cp1699-20-jul-2015_rm01_stacks_and_queues
Fallsem2015 16 cp1699-20-jul-2015_rm01_stacks_and_queues
 
Unit 1.ppt
Unit 1.pptUnit 1.ppt
Unit 1.ppt
 
Unit i(dsc++)
Unit i(dsc++)Unit i(dsc++)
Unit i(dsc++)
 
linked list in c++
linked list in c++linked list in c++
linked list in c++
 

More from Dr.Umadevi V (11)

Data Structures 8
Data Structures 8Data Structures 8
Data Structures 8
 
Data Structures 7
Data Structures 7Data Structures 7
Data Structures 7
 
Data Structures 6
Data Structures 6Data Structures 6
Data Structures 6
 
Data Structures 5
Data Structures 5Data Structures 5
Data Structures 5
 
Data Structures 4
Data Structures 4Data Structures 4
Data Structures 4
 
Data Structures 3
Data Structures 3Data Structures 3
Data Structures 3
 
computer architecture 4
computer architecture 4 computer architecture 4
computer architecture 4
 
Computer architecture 3
Computer architecture 3Computer architecture 3
Computer architecture 3
 
computer architecture
computer architecture computer architecture
computer architecture
 
computer architecture
computer architecture computer architecture
computer architecture
 
Multiple access techniques for wireless communication
Multiple access techniques for wireless communicationMultiple access techniques for wireless communication
Multiple access techniques for wireless communication
 

Recently uploaded

The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 

Recently uploaded (20)

Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 

Data Structures

  • 1. 1 Introduction to Data Structures Session I Dr. V.Umadevi M.Sc(CS &IT). M.Tech (IT)., M.Phil., PhD., D.Litt., Director, Department of Computer Science, Jairams Arts and Science College, Karur. Data Structures & Algorithms
  • 2. 2 • Introduction to Data Structures • Primitive Data Structures • Types of Data structures – Stacks – Queues – Lists • Stack – Basic Operation of Stack – Operations on the Data Structures – Applications of Infix, Postfix and Prefix Data Structures & Algorithms Introduction to Data Structures
  • 3. 3 Data Structures & Algorithms • A Computer is a machine that manipulates information. • Computer science studies includes: – How information is organized in a computer – How it can be manipulated – How it can be utilized Introduction to Data Structures • Data is represented by the values held temporarily in the program’s data area or recorded permanently on a file • Different data values may be related to each other which need to be organized to form a proper data structure. • Program follow certain rules to access manipulate and process the structured data. Information – Processed data given as output Data – Raw facts and figures given as input
  • 4. 4 Data Structures & Algorithms • Organization of data in a computer's memory or in a file. • Proper choice of a data structure can lead to more efficient programs. • Example: Array, Stack, Queue, Linked list, Tree and Graph. • Represented as follows: Data structure=Data organization + allowed operations. Data Structure
  • 5. 5 Primitive Data structure • Data structures typically are directly operated upon by the machine level instructions. • Primitive data constitute the numbers and characters, which are built into a programming language. • Examples are: Integer, Boolean and Characters. • Other data structures can be constructed from one or more primitives. • Simple data structures built from primitives are Strings, Arrays, and Records Data Structures & Algorithms
  • 6. 6 Types of Data Structures – Linear data structure. – Non-Linear data structure. • Various names used for naming the elements of a data structure ( Commonly used names are data element, data item, item aggregate, node list and data object) • Various data structures are used depending upon the needs and convenience Data Structures & Algorithms
  • 7. 7 Data Structures & Algorithms •Elements or items form a sequence one after the other. •Linear list can be realized in memory using either an array or a linked list structure. •Data is also stored in memory locations by means of pointers. Examples are: Arrays, Stacks, Queues and Linked list. Linear Data Structure
  • 8. 8 Linear Data Structures Types • Arrays - elements stored in memory locations in a sequential order • Stack is an ordered collection of data items – inserted or deleted from one end i.e., Last In First Out (LIFO) principle. • Queue is an ordered list – inserted at one end and are retrieved from the other end i.e., First in First out (FIFO) principle. • Linked Lists -elements stored in memory locations by means of pointers (chain of structures). Data Structures & Algorithms
  • 9. 9 Non-Linear Data Structures • Represents the hierarchical relationship between individual data items. • Examples are : Trees, Graphs • Tree - hierarchical relation between its elements. • Graph - collection of node and edges – Node - data element – Edge - path between two nodes. Data Structures & Algorithms
  • 10. 10 Operations on the Data Structures Data Structures & Algorithms • Traversal-is a technique in which each element is processed individually and separately. • Search-is an activity in which a particular record or item is to be found. • Insertion- is a process in which a new element is added into the structure. • Deletion- is a process in which a given item is removed from the structure • Sorting-is a process in which all elements are arranged in a specific order so that each item can be retrieved easily. • Merging-is a process in which two structures are combined into a single structure.
  • 11. 11 Stack • Ordered collection of items • Item may be inserted or deleted only from the top of the stack. • Works on Last In First Out (LIFO) concept. • Helps backtracking or to return to the previous state. Data Structures & Algorithms
  • 12. 12 Basic Stack Operations Createstack Create and Initializes a stack to be empty before it is first used Push adds an item to a stack Pop extracts the most recently pushed item from the stack ( removing an item from the stack.) Full returns non-zero if the stack is full Empty returns non-zero if the stack is empty Data Structures & Algorithms
  • 13. 13 Declaration of Stack • For Contiguous implementation, set an array which hold the entries in the stack and a counter. • Create and Initializes a stack to be empty before it is first used Data Structures & Algorithms Push and Pop • Stack is implemented using ‘Push’ and ‘Pop’ operations. • Operations during implementation should be done with care; For e.g., • popping an item from an empty stack • pushing an item into a stack that is full should not be done. [Create stack: create and initialize the stack to be empty] void Createstack (stack sptr) return [ sptr.top=0 ]
  • 14. 14 Algorithm for push Procedure push [s,top ,x] [this procedure inserts an element [x] to the top of the stack.] [check for stack overflow, maxstack is constant for maximum size] [allowed for stacks] if top> maxstack then write (“Stack overflow”) exit [increment top] top=top+1 [insert element] s[top]=x [finished] End push Push Operation Data Structures & Algorithms • First step of the algorithm checks for an overflow condition. • Such a condition exits then the insertion cannot be performed and an appropriate error message result.
  • 15. 15 Pop Operation Algorithm for pop Procedure pop [s, top, x ] [ this function removes the top element from a stack.] [check for underflow on stack] if top <=0 then write (“Stack is Underflow”) exit [Pop element from stack] x=s [top] [decrement pointer] top = top-1 End pop • First step itself checking underflow condition • If exist an appropriate error message will display and the procedure is terminated Data Structures & Algorithms
  • 16. 16 Other operations [Full: returns non-zero if the stack is full] boolean_type full(stack_type sptr) {return sptr .top>=MAXSTACK;} [Empty: returns non-zero if the stack is empty] boolean - type empty (stack_type sptr) {return sptr .top<=0;} Data Structures & Algorithms
  • 17. 17 Application of Stack Stack based Language (Re_expressing the expression) – Computers solve arithmetic expressions by restructuring them so that the order of each calculation is embedded in the expression. – Once converted, to the required notation (Either Prefix or Postfix), an expression can be solved. Types of Expression • Any expression represent in 3 ways • Infix, Prefix and Postfix. • Expression 4 + 5 * 5 is represented as follows: – Infix - Operators are embedded between operands- 4+5*5. – Prefix - Operators precede the operands- + 4 * 5 5 – Postfix - Operators follow the operands- 4 5 5 * + – Default representation of mathematical expression is Infix (or Polish) notation. Data Structures & Algorithms
  • 18. 18 Conversion of Expression- • Infix to Postfix – Applying the precedence rule first for operators – Place the Operators follow the operands – For e.g., an expression A+(B*C) • A + ( B * C) -Parenthesis for emphasis • A + ( BC *) -Convert the multiplication • A (BC *) + -Convert the addition • ABC * + -Postfix Form • Infix to Prefix – Applying the precedence rule first for operators – Place operators precede the operands – For e.g., an expression A+(B*C) • A + ( B * C) -Parenthesis for emphasis • A + (* BC) -Convert the multiplication • + A ( * BC ) -Convert the addition • * + ABC -Prefix Form Data Structures & Algorithms
  • 19. 19 • Scratch pad – Use to write down instructions that cannot be acted upon immediately. – Example of this is the rat-in-maze problem. – Use to solve the problem of traversing a maze. – Keep track of previously explored routes, or else an infinite loop could occur. • Text Editor – Characters are pushed on a stack as the user enters text. – Commands to delete one character or a command to delete a series of characters would also push a character on a stack. – Example, an identifier to delete one character would pop the stack once. – Example, an identifier to delete a sentence would pop all characters until the stack is empty or a period is encountered. Data Structures & Algorithms