0% found this document useful (0 votes)
54 views8 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
0% found this document useful (0 votes)
54 views8 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
You are on page 1/ 8
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 addition Figure 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 ey Applications 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 ei Representation 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 aac front 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 i queue. 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