SlideShare a Scribd company logo
1 of 35
DepartmentofComputerApplications
I BCA S-I
2023-2024 Even sem
23CAp01 – data structures and lab
UNIT II
• Stacks: Introduction- Stack operations- Stack implementations. Applications:
Recursive Programming-Evaluation of Expressions.
• Queue: Introduction- Queue operations- Queue implementations- Limitation of
Linear queue.
• Circular queues: Operations on a circular queue- implementation of insertion and
deletion in circular queue- Other types of queues- Applications.
Stacks
• A stack is an ordered list with the restriction that elements are added
or deleted from only one end of the list termed top of stack. The
other end of the list which lies ‘
inactive’is termed bottom of stack
• Thus if S is a stack with three elements a, b, c where c occupies the
top of stack position, and if d were to be added, the resultant stack
contents would be a, b, c, d. Note that d occupies the top of stack
position. Again, initiating a delete or remove operation would
automatically throw out the element occupying the top of stack, viz.,
d
• It needs to be observed that during insertion of elements into the
stack it is essential that their identities are specified, where as for
removal no identity need be specified since by virtue of its
functionality, the element which occupies the top of stack position is
automatically removed.
• The stack data structure therefore obeys the principle of Last In First
Out (LIFO). In other words, elements inserted or added into the stack
join last and those that joined last are the first to be removed.
Stack Operations
• The two operations which stack data structure supports are
• (i) Insertion or addition of elements known as Push
• (ii) Deletion or removal of elements known as Pop
Stack implementation
• A common and a basic method of implementing stacks is to make use
of another fundamental data structure viz., arrays. While arrays are
sequential data structures the other alternative of employing linked
data structures have been successfully attempted and applied.
• stack of four elements R, S, V, G represented by an array STACK[1:7].
In general, if a stack is represented as an array STACK[1 : n] then n
elements and not one more can be stored in the stack. It therefore
becomes essential to issue a signal or warning termed STACK_FULL
when elements whose number is over and above n are attempted to
be pushed into the stack.
• Again, during a pop operation, it is essential to ensure that one does
not delete an empty stack! Hence the necessity for a signal or a
warning termed STACK_EMPTY during the implementation of the pop
operation. While implementation of stacks using arrays necessitates
checking for STACK_FULL/STACK_EMPTY conditions during push/pop
operations respectively, the implementation of stacks with linked data
structures dispenses with these testing conditions.
Implementation of push and pop operations
• Let STACK [1:n] be an array implementation of a stack and top be a
variable recording the current top of stack position. top is initialized
to 0. item is the element to be pushed into the stack. n is the
maximum capacity of the stack.
In the case of pop operation, as said earlier, no element
identity need be specified since by default the element
occupying the top of stack position is deleted
Queues
• A Queue is a linear list in which all insertions are made at one end of
the list known as rear or tail of the queue and all deletions are made
at the other end known as front or head of the queue.
• An insertion operation is also referred to as enqueuing a queue and
• A deletion operation is referred to as dequeuing a queue
• Q is a queue of three elements a, b, c (Fig. 5.1(a)).
• When an element d is to join the queue, it is inserted at the rear end
of the queue (Fig. 5.1(b)) and when an element is to be deleted, the
one at the front end of the queue, viz, a, is deleted automatically (Fig.
5.1(c)).
• Thus a queue data structure obeys the principle of first in first out
(FIFO) or first come first served (FCFS).
Operations on Queues
• The queue data structure supports two operations, viz.,
• (i) Insertion or addition of elements to a queue
• (ii) Deletion or removal of elements from a queue
Queue Implementation
Implementation of insert and delete
operations on a queue
• Let Q[1 : n] be an array implementation of a queue.
• Let FRONT and REAR be variables recording the front and rear positions of
the queue.
• The FRONT variable points to a position which is physically one less than
the actual front of the queue.
• ITEM is the element to be inserted into the queue. n is the maximum
capacity of the queue.
• Both FRONT and REAR are initialized to 0.
Circular Queues
• As the name indicates a circular queue is not linear in structure but
instead it is circular. In other words, the FRONT and REAR variables
which displayed a linear (left to right) movement over a queue,
display a circular movement (clock wise) over the queue data
structure
Operations on a circular queue
• Let CIRC_Q be a circular queue with a capacity of three elements as
shown in Fig. 5.4(a). The queue is obviously full with FRONT pointing
to the element at the head of the queue and REAR pointing to the
element at the tail end of the queue. Let us now perform two
deletions and then attempt insertions of ‘d ’and ‘
e ’into the queue.
Implementation of insertion and deletion
operations in a circular queue
• The circular movement of FRONT and REAR variables is implemented using
the mod function which is cyclical in nature. Also the array data structure
CIRC_Q to implement the queue is declared to be CIRC_Q [0: n –1] to
facilitate the circular operation of FRONT and REAR variables. As in linear
queues, FRONT points to a position which is one less then the actual front
of the circular queue. Both FRONT and REAR are initialized to 0. Note that
(n –1) is the actual physical capacity of the queue in spite of the array
declaration as [0 : n –
1]
Other Types of Queues
• Priority queues
• Deques
Applications
• Application of a linear queue
• Application of priority queues
Data Structures - Lecture 2 - Unit 2.pptx

More Related Content

Similar to Data Structures - Lecture 2 - Unit 2.pptx

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 - Lecture 2 - Unit 2.pptx (20)

Difference between stack and queue
Difference between stack and queueDifference between stack and queue
Difference between stack and queue
 
Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1
 
Stack in Sata Structure
Stack in Sata StructureStack in Sata Structure
Stack in Sata Structure
 
DS UNIT2QUEUES.pptx
DS UNIT2QUEUES.pptxDS UNIT2QUEUES.pptx
DS UNIT2QUEUES.pptx
 
Unit-ii-Queue ADT.pptx
Unit-ii-Queue ADT.pptxUnit-ii-Queue ADT.pptx
Unit-ii-Queue ADT.pptx
 
Data Structures - Lecture 6 [queues]
Data Structures - Lecture 6 [queues]Data Structures - Lecture 6 [queues]
Data Structures - Lecture 6 [queues]
 
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
 
Queue
QueueQueue
Queue
 
Array stack-queue1
Array stack-queue1Array stack-queue1
Array stack-queue1
 
Unit II - LINEAR DATA STRUCTURES
Unit II -  LINEAR DATA STRUCTURESUnit II -  LINEAR DATA STRUCTURES
Unit II - LINEAR DATA STRUCTURES
 
Queues
Queues Queues
Queues
 
STACK.pptx
STACK.pptxSTACK.pptx
STACK.pptx
 
Stack and Queue.pptx
Stack and Queue.pptxStack and Queue.pptx
Stack and Queue.pptx
 
Circular queue
Circular queueCircular queue
Circular queue
 
Circular queue
Circular queueCircular queue
Circular queue
 
Queue AS an ADT (Abstract Data Type)
Queue AS an ADT (Abstract Data Type)Queue AS an ADT (Abstract Data Type)
Queue AS an ADT (Abstract Data Type)
 
LEC4-DS ALGO.pdf
LEC4-DS  ALGO.pdfLEC4-DS  ALGO.pdf
LEC4-DS ALGO.pdf
 
Unit – iv queue
Unit – iv    queueUnit – iv    queue
Unit – iv queue
 
DSA Lab Manual C Scheme.pdf
DSA Lab Manual C Scheme.pdfDSA Lab Manual C Scheme.pdf
DSA Lab Manual C Scheme.pdf
 
stack.pptx
stack.pptxstack.pptx
stack.pptx
 

Recently uploaded

Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
fonyou31
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 

Recently uploaded (20)

IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 

Data Structures - Lecture 2 - Unit 2.pptx

  • 1. DepartmentofComputerApplications I BCA S-I 2023-2024 Even sem 23CAp01 – data structures and lab
  • 2. UNIT II • Stacks: Introduction- Stack operations- Stack implementations. Applications: Recursive Programming-Evaluation of Expressions. • Queue: Introduction- Queue operations- Queue implementations- Limitation of Linear queue. • Circular queues: Operations on a circular queue- implementation of insertion and deletion in circular queue- Other types of queues- Applications.
  • 3. Stacks • A stack is an ordered list with the restriction that elements are added or deleted from only one end of the list termed top of stack. The other end of the list which lies ‘ inactive’is termed bottom of stack
  • 4. • Thus if S is a stack with three elements a, b, c where c occupies the top of stack position, and if d were to be added, the resultant stack contents would be a, b, c, d. Note that d occupies the top of stack position. Again, initiating a delete or remove operation would automatically throw out the element occupying the top of stack, viz., d
  • 5.
  • 6. • It needs to be observed that during insertion of elements into the stack it is essential that their identities are specified, where as for removal no identity need be specified since by virtue of its functionality, the element which occupies the top of stack position is automatically removed.
  • 7. • The stack data structure therefore obeys the principle of Last In First Out (LIFO). In other words, elements inserted or added into the stack join last and those that joined last are the first to be removed.
  • 8. Stack Operations • The two operations which stack data structure supports are • (i) Insertion or addition of elements known as Push • (ii) Deletion or removal of elements known as Pop
  • 9. Stack implementation • A common and a basic method of implementing stacks is to make use of another fundamental data structure viz., arrays. While arrays are sequential data structures the other alternative of employing linked data structures have been successfully attempted and applied.
  • 10.
  • 11.
  • 12. • stack of four elements R, S, V, G represented by an array STACK[1:7]. In general, if a stack is represented as an array STACK[1 : n] then n elements and not one more can be stored in the stack. It therefore becomes essential to issue a signal or warning termed STACK_FULL when elements whose number is over and above n are attempted to be pushed into the stack.
  • 13. • Again, during a pop operation, it is essential to ensure that one does not delete an empty stack! Hence the necessity for a signal or a warning termed STACK_EMPTY during the implementation of the pop operation. While implementation of stacks using arrays necessitates checking for STACK_FULL/STACK_EMPTY conditions during push/pop operations respectively, the implementation of stacks with linked data structures dispenses with these testing conditions.
  • 14. Implementation of push and pop operations • Let STACK [1:n] be an array implementation of a stack and top be a variable recording the current top of stack position. top is initialized to 0. item is the element to be pushed into the stack. n is the maximum capacity of the stack.
  • 15.
  • 16. In the case of pop operation, as said earlier, no element identity need be specified since by default the element occupying the top of stack position is deleted
  • 17. Queues • A Queue is a linear list in which all insertions are made at one end of the list known as rear or tail of the queue and all deletions are made at the other end known as front or head of the queue. • An insertion operation is also referred to as enqueuing a queue and • A deletion operation is referred to as dequeuing a queue
  • 18. • Q is a queue of three elements a, b, c (Fig. 5.1(a)). • When an element d is to join the queue, it is inserted at the rear end of the queue (Fig. 5.1(b)) and when an element is to be deleted, the one at the front end of the queue, viz, a, is deleted automatically (Fig. 5.1(c)). • Thus a queue data structure obeys the principle of first in first out (FIFO) or first come first served (FCFS).
  • 19.
  • 20. Operations on Queues • The queue data structure supports two operations, viz., • (i) Insertion or addition of elements to a queue • (ii) Deletion or removal of elements from a queue
  • 22. Implementation of insert and delete operations on a queue • Let Q[1 : n] be an array implementation of a queue. • Let FRONT and REAR be variables recording the front and rear positions of the queue. • The FRONT variable points to a position which is physically one less than the actual front of the queue. • ITEM is the element to be inserted into the queue. n is the maximum capacity of the queue. • Both FRONT and REAR are initialized to 0.
  • 23.
  • 24.
  • 25. Circular Queues • As the name indicates a circular queue is not linear in structure but instead it is circular. In other words, the FRONT and REAR variables which displayed a linear (left to right) movement over a queue, display a circular movement (clock wise) over the queue data structure
  • 26. Operations on a circular queue • Let CIRC_Q be a circular queue with a capacity of three elements as shown in Fig. 5.4(a). The queue is obviously full with FRONT pointing to the element at the head of the queue and REAR pointing to the element at the tail end of the queue. Let us now perform two deletions and then attempt insertions of ‘d ’and ‘ e ’into the queue.
  • 27.
  • 28. Implementation of insertion and deletion operations in a circular queue • The circular movement of FRONT and REAR variables is implemented using the mod function which is cyclical in nature. Also the array data structure CIRC_Q to implement the queue is declared to be CIRC_Q [0: n –1] to facilitate the circular operation of FRONT and REAR variables. As in linear queues, FRONT points to a position which is one less then the actual front of the circular queue. Both FRONT and REAR are initialized to 0. Note that (n –1) is the actual physical capacity of the queue in spite of the array declaration as [0 : n – 1]
  • 29.
  • 30.
  • 31.
  • 32.
  • 33. Other Types of Queues • Priority queues • Deques
  • 34. Applications • Application of a linear queue • Application of priority queues