Open navigation menu
Close suggestions
Search
Search
en
Change Language
Upload
Sign in
Sign in
Download free for days
0 ratings
0% found this document useful (0 votes)
54 views
8 pages
Cos 232 Material
Uploaded by
Daniel Ihejirika
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as PDF or read online on Scribd
Download
Save
Save Cos 232 Material For Later
Share
0%
0% found this document useful, undefined
0%
, undefined
Print
Embed
Report
0 ratings
0% found this document useful (0 votes)
54 views
8 pages
Cos 232 Material
Uploaded by
Daniel Ihejirika
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as PDF or read online on Scribd
Carousel Previous
Carousel Next
Download
Save
Save Cos 232 Material For Later
Share
0%
0% found this document useful, undefined
0%
, undefined
Print
Embed
Report
Download
Save Cos 232 Material For Later
You are on page 1
/ 8
Search
Fullscreen
Chapter 4 Stack and Queue There are certain situations in computer science that one wants to jestrict Insertions and deletions so that they can take place only at the beginning or the end of the list, not in the middle. Two of such data structures that are useful are: Stack. : Queue. Linear lists and arrays allow one to insert and delete elements at any place in the list i.e., at the beginning, at the end or in the middle. © STACK: xd only at one nN delete stack is a list of elements in which an element may be inserted or 1 eey sad called the top of the stack. Stacks are sometimes known as LIFO (last in, lists. As the items can be added or removed only from the top i.e. the last item to be added to a stack is the first item to be removed. The two basic operations associated with stacks are: 1.5, Push: is the term used to insert an element into a stack. 1.6. Pop: is the term used to delete an element from a stack. “push” is the term used to insert an element into a stack. “Pop” is the term used to delete an element from the stack. All insertions and deletions take place at the same end, so the last element added to the stack will be the first element removed from the stack. When a stack is created, the stack base remains fixed while the stack top changes as elements are added and removed. The most accessible element is the top and the least accessible element is the bottom of the stack. . Representation of Stack: Let us consider a stack with 6 elements capacity. This is called as the size of the. The number of elements to be added should not exceed the maximum size of th If we attempt to add new element beyond the maximum size, we will en: ‘overflow condition. Similarly, you cannot remove elements beyond stack. If such is the case, we will reach a stack underflow condition, When an element is added to a th |shows the creation of a stack and additionFigure 4.1. Push operations on stack n is performed by pop() d shows the deletion of When an element is taken off from the stack, the operatior Figure 4.2 shows a stack initially with three elements an elements using pop(). Initial Stack Figure 4.2. Pop operations on stack ‘Source code for stack operations, using array: Include
Include
Include
define MAX 6 int stack{MAX];return; else printf("\n\nElements in ‘stack:"); for(i = 0; | < top; i++) ; printf("\t%ed", stack(iD); ‘ 1 > yold pond) top == 0) printf("\n\nStack Underfio return; < : en else } Printf("\n\npopped element is: %d *, stack{—top]); 7 void push() u int data; ion == max) printf("\n\nStack Overflow. return; printf("\n\nEnter data: "); scanf("%d", &data); stack({top] = data; top = top + 1; printf("\n\nData Pushed into the stack"); bie main() Int ch; do eyApplications of Queue! bi scene oi 1. is used to schedule the jobs to De P ang jo 8 Ket 5 ri 2. When multiple users send print Jobs to 6 printer, ee eording to first in "the printing queue. Then the printer prints thos first out (FIFO) basis. nt from @ 3. Breadth first search uses a queue data structure to find an eleme! graph. Circular Queue: jing the array QIMAX] as This implementation of @ ‘A more efficient queue representation is obtained by regardi if it were a circle circular. Any number of items could be placed on the queue. queue is called a circular queue because it uses its storage array as instead of a linear list. There are two problems associated with linear queue. They are: a Time consuming: linear time to be spent in shifting the elements to the beginning of the queue. s Signaling queue full: even if the queue is having vacant position. For example, let us consider a linear queue status as follows: frow = FRONT =2 eiRepresentation of Circular Queue! of six elements: yximum (MAX) Let us consider a circular queue, which 2” ee Initially the queue is empty. FR ke A : quevee ney Ke FRONT = REAR=0 couNT =0 2 Cire ular Que ue Now, insert 11 to the circular queue. Then circular queue status will be: = 8 a - FRONT =0 ‘ REAR = (REAR +1) %6=1 COUNT =1 2 Cire ular Que we sal aig eg ES Insert new elements 22, 33, 44 and 55 status is i: En aacfront of the circular element at the Now, delete an element, The element deleted Is the ; queue. So, 11 is deleted, The circular queue status is 2S follows: FRONT=(FRONT+1)%6=2 REAR = 5. Count =COUNT-1=4 Cire ular Que ue Again, delete an element. The element to b b .e deleted is always pointed to by the FRONT pointer. So, 22 is deleted. The circular queue status is as ee oe R, 0 es CT, Circ ular Que ue . FRONT =(FRONT + 1)%6= ferns oa COUNT = COUNT- faust arte one 10 — a iqueue. The circular queue status ‘ ; one OO” ; ire ular Que ue he Now, if we insert an element to the circular queue, as COUNT = MAX we cannot add tl element to circular queue. So, the circular queue Is full. Source code for Circular Queue operations, using array: # Include
# include
# define MAX 6 int CQIMAX]; int front = 0; int rear = 0; int count = 0; void insertCQQ) { int data; if(count == MAX) printf("\n Circular Queue is Full");vera displayCQ() inti, 3; if(count == 0) { printf("\n\n\t Circular Queue Is Empty else q "); printf(""\n Elements in Circular Queue are: i j= count; for(i = front; j != 0; j--) printf("%d\t", CQL): i= (i+ 1) % MAX; 2 > + int menu() int ch; clrscr(); brintf("\n \t Circular Queue Operations using ARRAY.."); 7 printf(*\n -~ Lancers --\n"); printf("\n 1. Insert"); printf("\n 2. Delete "); printf("\n 3. Display"); printf("\n 4. Quit "); printf("\n Enter Your Choice: "); scanf("%d", &ch);, return ch; } : 2 void main() <
You might also like
Dsu QB
PDF
No ratings yet
Dsu QB
23 pages
Stack and Queue
PDF
No ratings yet
Stack and Queue
42 pages
Ds Unit2
PDF
No ratings yet
Ds Unit2
28 pages
Module 2
PDF
No ratings yet
Module 2
50 pages
Linear Queue
PDF
No ratings yet
Linear Queue
45 pages
Queues
PDF
No ratings yet
Queues
19 pages
Module 2 - 2022 Scheme
PDF
No ratings yet
Module 2 - 2022 Scheme
95 pages
DSA Unit 3
PDF
No ratings yet
DSA Unit 3
22 pages
Chapter 4 Queue
PDF
No ratings yet
Chapter 4 Queue
27 pages
Module 2
PDF
No ratings yet
Module 2
29 pages
Queue
PDF
No ratings yet
Queue
23 pages
A Continuation of DSA RTU Part 2
PDF
No ratings yet
A Continuation of DSA RTU Part 2
139 pages
Dsa CH 4 Queues
PDF
No ratings yet
Dsa CH 4 Queues
47 pages
It 11200222017 Saideepmukhopadhyay DS Ca2
PDF
No ratings yet
It 11200222017 Saideepmukhopadhyay DS Ca2
25 pages
Chapter Three - DS Stack and Queue
PDF
No ratings yet
Chapter Three - DS Stack and Queue
36 pages
The Queues: Front
PDF
No ratings yet
The Queues: Front
22 pages
Queue
PDF
No ratings yet
Queue
27 pages
Queues
PDF
No ratings yet
Queues
23 pages
Queue Data Structure
PDF
No ratings yet
Queue Data Structure
20 pages
Queue
PDF
No ratings yet
Queue
8 pages
DS Unit2
PDF
No ratings yet
DS Unit2
34 pages
DS Lecture Week 8 QUEUE
PDF
No ratings yet
DS Lecture Week 8 QUEUE
29 pages
Chapter 4 Stack and Queues
PDF
No ratings yet
Chapter 4 Stack and Queues
44 pages
Dsmaterial 4
PDF
No ratings yet
Dsmaterial 4
5 pages
Queue:: Construct: Empty: Addq: Front: Removeq
PDF
No ratings yet
Queue:: Construct: Empty: Addq: Front: Removeq
23 pages
DS Module-2
PDF
No ratings yet
DS Module-2
32 pages
BCS304-DSA Notes M-2
PDF
100% (2)
BCS304-DSA Notes M-2
50 pages
Queues JSN
PDF
No ratings yet
Queues JSN
43 pages
FALLSEM2024-25 CSE2001 ETH AP2024252000441 Quiz-I-Question-Paper
PDF
No ratings yet
FALLSEM2024-25 CSE2001 ETH AP2024252000441 Quiz-I-Question-Paper
31 pages
Wa0000.
PDF
No ratings yet
Wa0000.
51 pages
Unit 4 - Queue
PDF
No ratings yet
Unit 4 - Queue
10 pages
DocScanner Apr 30, 2022 1.27 PM
PDF
No ratings yet
DocScanner Apr 30, 2022 1.27 PM
23 pages
Queue DataStructure
PDF
No ratings yet
Queue DataStructure
18 pages
Stack and Queue
PDF
No ratings yet
Stack and Queue
12 pages
08 DS Queue 2024
PDF
No ratings yet
08 DS Queue 2024
57 pages
Unit-5 OOP - DS
PDF
No ratings yet
Unit-5 OOP - DS
19 pages
Stack and Queue
PDF
No ratings yet
Stack and Queue
50 pages
3 Queue
PDF
No ratings yet
3 Queue
26 pages
DSA Chapter 03 Queue
PDF
No ratings yet
DSA Chapter 03 Queue
44 pages
2.2.2 Circular Queue
PDF
No ratings yet
2.2.2 Circular Queue
26 pages
8353 ARP Queue
PDF
No ratings yet
8353 ARP Queue
33 pages
Lecture 7 - Queues
PDF
No ratings yet
Lecture 7 - Queues
47 pages
Chapter 04 Queues
PDF
No ratings yet
Chapter 04 Queues
25 pages
Queue With Types
PDF
No ratings yet
Queue With Types
30 pages
CHP 2
PDF
No ratings yet
CHP 2
49 pages
05 Queues Orig
PDF
No ratings yet
05 Queues Orig
19 pages
DS Unit 4 Queue
PDF
No ratings yet
DS Unit 4 Queue
9 pages
Module 2 Data Structure Queue
PDF
No ratings yet
Module 2 Data Structure Queue
14 pages
6-Stack and Queue
PDF
No ratings yet
6-Stack and Queue
50 pages
DS Module2
PDF
No ratings yet
DS Module2
28 pages
Ds 02
PDF
No ratings yet
Ds 02
39 pages
Unit 2 Notes
PDF
No ratings yet
Unit 2 Notes
62 pages
Unit 2 Dsa
PDF
No ratings yet
Unit 2 Dsa
120 pages
CACS201 Unit 3 - Queue
PDF
No ratings yet
CACS201 Unit 3 - Queue
30 pages
DS-10 (1) CQDQ
PDF
No ratings yet
DS-10 (1) CQDQ
16 pages
Lecture 4 Data Structure Queue
PDF
No ratings yet
Lecture 4 Data Structure Queue
32 pages
Lecture 2.2.2 Circular Queue
PDF
No ratings yet
Lecture 2.2.2 Circular Queue
26 pages