Slip 12_2, Slip 15_2,Slip24_1 : Implement a circular queue library (cir_queue.h) of integers using a dynamic (circular linked list) implementation of the queue and implementing init(Q), AddQueue(Q) and DeleteQueue(Q) , peek(Q) operations. Write a menu driven program that includes queue library and calls different queue operations.

 Solution :

Header File : cir_queue.h

#include<stdio.h>

#include<stdlib.h>

struct node

{

 int data;

 struct node *next;

};

struct node *r;

void init()

{

 r=NULL;

}

int isempty()

{

 if(r==NULL)

 return 1;

 else

 return 0;

}

void Add(int n)

{

 struct node *nw;

 nw=(struct node *)malloc(sizeof(struct node));

 nw->data=n;

 if(r==NULL)

 {

 r=nw;

 r->next=r;

 }

 else

 {

 nw->next=r->next;

 r->next=nw;

 r=r->next;

 }

}

int Delete()

{

 int no;

 struct node *temp;

 temp=r->next;

 if(r==temp->next)

 { r=NULL;

 }

 else

{

 r->next=temp->next;

 }

 no=temp->data;

 free(temp);

 return (no);

}

int peek()

{

 return r->next->data;

}

Program File:

#include<stdio.h>

#include "cir_queue.h"

main()

{ int ch,no;

 init();

 do

 {

 printf("\n1.Add \n2.Delete \n0.Exit");

 printf("Enter choice");

 scanf("%d",&ch);

 switch(ch)

 {

 case 1:printf("\n Enter element");

 scanf("%d",&no);

 Add(no);

 break;

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

 printf("\n Queue is empty");

 else

 {

 printf("deleted

element is %d",Delete());

 break;

 }

 case 0:break;

 case 4:printf("Elemenent at peek %d ",peek());

 break;

 default:printf("Invalid 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”.