Slip 10_1 ,22_1: Implement a linear queue library (st_queue.h) of integers using a static implementation of the queue and implementing the init(Q), add(Q) and peek(Q) operations. Write a program that includes queue library and calls different queue operations

 Solution :

Header File :st_queue.h

#include<stdio.h>

int Q[20];

int f,R;

void init()

{ f=R=-1;

}

int isempty()

{

 if(f==R)

 return 1;

 else

 return 0;

}

int isfull()

{

 if(R==19)

 return 1;

 else return 0;

}

void Add(int no)

{

 if(isfull()==1)

 printf("Queue is Full ");

 else

 {

 R++;

 Q[R]=no;

 }

}

int Delete()

{ int no;

 if(isempty()==1)

 printf("Queue is empty ");

 else

 {

 f++;

 no=Q[f];

 }

 return no;

}

oid display()

{

 int i;

 for(i=f+1;i<=R;i++)

 { printf("%d ",Q[i]);

 }

}

Program File :

#include<stdio.h>

#include "st_queue.h"

main()

{ int n,ch;

 init();

 do

 {

 printf("\n\n1.Add \n2.Delete \n3.Display \n0.EXit");

 printf("\nEnter choice ");

 scanf("%d",&ch);

 switch(ch)

 { case 1:printf("\nEnter element ");

 scanf("%d",&n);

 Add(n);

 break;

 case 2:if(isempty()==1)

 printf("\nQueue is empty ");

 else

 printf("deleted elemet =%d

",Delete());

 break;

 case 3:display();

 break;

 case 0:break;

 default:printf("\nInvalid choice ");

 }

 }while(ch!=0);

}


Comments

Popular posts from this blog

Slip 22_2: Read the data from file 'cities.txt' containing names of cities and their STD codes. Accept a name of the city from user and use sentinel linear search algorithm to check whether the name is present in the file and output the STD code, otherwise output “city not in the list”. Solution :

Slip10_2, 30_1 : Read the data from the file “employee.txt” and sort on names in alphabetical order (use strcmp) using bubble sort or selection sort

Slip 23_2: Read the data from file ‘sortedcities.txt’ containing sorted names of cities and their STD codes. Accept a name of the city from user and use binary search algorithm to check whether the name is present in the file and output the STD code, otherwise output “city not in the list”.