Implementation of a circular queue represented using dynamic data structure

Wednesday, 4 December 2013

/* Implementation of a circular queue represented
using dynamic data structure. */

#include<stdio.h>
#include<conio.h>
typedef struct node
{
int data;
struct node *next;
}node;
void init(node **R);
void enqueue(node **R,int x);
int dequeue(node **R);
int empty(node *rear);
void print(node *rear);
int front_element(node *rear);
int rear_element(node *rear);
void main()
{
int x,option;
int n = 0,i;
node *rear;
init(&rear);
clrscr();
do
{
printf("\n1. Insert\n2. Delete\n3. Print");
printf("\n4. Front element\n5. Rear Element\n6. Quit");
printf("\n your option: ");
scanf("%d",&option);
switch(option)
{
case 1: printf("\nEnter queue data : ");
scanf("\n %d",&x);
enqueue(&rear,x);
break;
case 2 : if(! empty(rear))
{
x=dequeue(&rear);
printf("\n Element deleted = %d",x);
}
else
printf("\n Uderflow..... Cannot delete");
break;
case 3 : print(rear);
break;
case 4 :if(!empty(rear))
printf("\nFront Element = %d",front_element(rear));
else
printf("\nQueue is empty");
break;
case 5 :if(!empty(rear))
printf("\nRear Element = %d",rear_element(rear));
else
printf("\nQueue is empty");
break;
}
}while(option != 6);

}

void init(node **R)
{
*R = NULL;
}

void enqueue(node **R,int x)
{
node *p;
p = (node *)malloc(sizeof(node));
p->data = x;
if(empty(*R))
{
p->next = p;
*R = p;
}
else
{
p->next = (*R)->next;
(*R)->next = p;
(*R) = p;
}
}

int dequeue(node **R)
{
int x;
node *p;
p=(*R)->next;
x=p->data;
if(p->next == p)
{
*R = NULL;
free(p);
return(x);
}
(*R)->next = p->next;
free(p);
return(x);
}

void print(node *rear)
{
node *p;
if(!empty(rear))
{
p = rear->next;
}
else
{
printf("\nQueue is empty");
return;
}

printf("\n");
do
{
printf("%d ",p->data);
p = p->next;
}while(p != rear->next);
}

int empty(node *P)
{
if(P->next== NULL)
return(1);
return(0);
}

int front_element(node *rear)
{
return(rear->next->data);
}

int rear_element(node *rear)
{
return(rear->data);
}


OUTPUT:-



1. Insert
2. Delete
3. Print
4. Front element
5. Rear Element
6. Quit
your option: 1

Enter queue data : 34

1. Insert
2. Delete
3. Print
4. Front element
5. Rear Element
6. Quit
your option: 4

Front Element = 34
1. Insert
2. Delete
3. Print
4. Front element
5. Rear Element
6. Quit
your option: 5

Rear Element = 34
1. Insert
2. Delete
3. Print
4. Front element
5. Rear Element
6. Quit
your option: 1

Enter queue data : 56

1. Insert
2. Delete
3. Print
4. Front element
5. Rear Element
6. Quit
your option: 1

Enter queue data : 78

1. Insert
2. Delete
3. Print
4. Front element
5. Rear Element
6. Quit
your option: 3

34 56 78
1. Insert
2. Delete
3. Print
4. Front element
5. Rear Element
6. Quit
your option: 4

Front Element = 34
1. Insert
2. Delete
3. Print
4. Front element
5. Rear Element
6. Quit
your option: 5

Rear Element = 78
1. Insert
2. Delete
3. Print
4. Front element
5. Rear Element
6. Quit
your option: 2

Element deleted = 34
1. Insert
2. Delete
3. Print
4. Front element
5. Rear Element
6. Quit
your option: 2

Element deleted = 56
1. Insert
2. Delete
3. Print
4. Front element
5. Rear Element
6. Quit
your option: 2

Element deleted = 78
1. Insert
2. Delete
3. Print
4. Front element
5. Rear Element
6. Quit
your option: 2

Uderflow..... Cannot delete
1. Insert
2. Delete
3. Print
4. Front element
5. Rear Element
6. Quit
your option:6

1 comment

  1. If you really desire to get such type of information, visit this blog quickly.
    tutorial for c++

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...
Related Posts Plugin for WordPress, Blogger...
 

Most Reading

Labels