Showing posts with label QUEUE. Show all posts
Showing posts with label QUEUE. Show all posts

Wednesday, 14 May 2014

Circular Queue Implementation Using Array

PROGRAM:

#include<stdio.h>
#include<conio.h>
#define SIZE 5
int count=0,front=-1,rear=-1,cq[SIZE];
void enqueue();
void dequeue();
void display();
void main()
{
 int ch;
 clrscr();
 printf("$$$ CIRCULAR QUEUE USING ARRAY $$$");
 while(1)
 {
  printf("\nCircular Queue operations are:");
  printf("\n1.enqueue\n2.dequeue\n3.display\n4.exit\n");
  printf("Enter your choice:");
  scanf("%d",&ch);
  switch(ch)
  {
   case 1:enqueue();break;
   case 2:dequeue();break;
   case 3:display();break;
   case 4:exit(0);
   default:printf("Enter correct option");
    break;
  }
 }
}
void enqueue()
{
 int item;
 if(count==SIZE)
 {
  printf("Circular Queue is full");
 }
 else
 {
  printf("Enter the element to be inserted:");
  scanf("%d",&item);
  if(front==-1)
  {
   front=0;
  }
  rear=(rear+1)%SIZE;
  cq[rear]=item;
  count=count+1;
 }
}
void dequeue()
{
 if(count==0)
 {
  printf("Circular Queue is empty");
 }
 else
 {
  printf("The deleted element is %d",cq[front]);
  front=(front+1)%SIZE;
  count=count-1;
 }
}
void display()
{
 int i,j;
 if(count==0)
 {
  printf("Circular Queue is empty");
 }
 else
 {
  printf("Elements of circular queue:\n");
  j=front;
  for(i=0;i<count;i++)
  {
   printf("%d\n",cq[j]);
   j=(j+1)%SIZE;
  }
 }
}


OUTPUT:

$$$ CIRCULAR QUEUE USING ARRAY $$$
Circular Queue operations are:
1.enqueue
2.dequeue
3.display
4.exit
Enter your choice:1
Enter the element to be inserted:11

Circular Queue operations are:
1.enqueue
2.dequeue
3.display
4.exit
Enter your choice:1
Enter the element to be inserted:22

Circular Queue operations are:
1.enqueue
2.dequeue
3.display
4.exit
Enter your choice:1
Enter the element to be inserted:33

Circular Queue operations are:
1.enqueue
2.dequeue
3.display
4.exit
Enter your choice:1
Enter the element to be inserted:44

Circular Queue operations are:
1.enqueue
2.dequeue
3.display
4.exit
Enter your choice:1
Enter the element to be inserted:55

Circular Queue operations are:
1.enqueue
2.dequeue
3.display
4.exit
Enter your choice:1
Circular Queue is full

Circular Queue operations are:
1.enqueue
2.dequeue
3.display
4.exit
Enter your choice:3
Elements of circular queue:11 22 33 44 55

Circular Queue operations are:
1.enqueue
2.dequeue
3.display
4.exit
Enter your choice:2
The deleted element is 11

Circular Queue operations are:
1.enqueue
2.dequeue
3.display
4.exit
Enter your choice:1
Enter the element to be inserted:66

Circular Queue operations are:
1.enqueue
2.dequeue
3.display
4.exit
Enter your choice:3
Elements of circular queue:22 33 44 55 66

Circular Queue operations are:
1.enqueue
2.dequeue
3.display
4.exit
Enter your choice:4

Sunday, 13 April 2014

Queue Implementation Using Linked List or Pointer

PROGRAM:


#include<stdio.h>
#include<conio.h>
typedef struct queue
{
                int data;
                struct queue *next;
}node;
node *newn,*front,*rear,*temp;
void enqueue();
void dequeue();
void display();
void main()
{
                int ch;
                clrscr();
                printf("\n\n\n$$$ QUEUE IMPLEMENTATION USING LINKED LIST OR POINTER $$$\n\n\n");
                while(1)
                {
                                printf("QUEUE OPERATIONS ARE\n");
                                printf("1.ENQUEUE\n2.DEQUEUE\n3.DISPLAY\n4.EXIT\n");
                                printf("Enter your choice:\t");
                                scanf("%d",&ch);
                                switch(ch)
                                {
                                                case 1:enqueue();break;
                                                case 2:dequeue();break;
                                                case 3:display();break;
                                                case 4:exit(0);
                                                default:printf("Enter the correct option\n");
                                                break;
                                }
                }
}
void enqueue()
{
                newn=(node *)malloc(sizeof(node));
                printf("Enter the element to be inserted:\t");
                scanf("%d",&newn->data);
                newn->next=NULL;
                if(front==NULL)
                {
                                front=newn;
                                rear=newn;
                }
                else
                {
                                rear->next=newn;
                                rear=newn;
                }
}
void dequeue()
{
                if(front==NULL)
                {
                                printf("Queue is empty or underflow\n");
                }
                else
                {
                                temp=front;
                                front=front->next;
                                printf("The deleted element is: %d",temp->data);
                }
                free(temp);
}
void display()
{
                if(front==NULL)
                {
                                printf("Queue is empty or underflow\n");
                }
                else
                {
                                temp=front;
                                printf("Content of the queue is:\n");
                                while(temp!=NULL)
                                {
                                                printf("%d\n",temp->data);
                                                temp=temp->next;
                                }
                }
}


 OUTPUT:

$$$ QUEUE IMPLEMENTATION USING LINKED LIST OR POINTER $$$

 QUEUE OPERATIONS ARE
1.ENQUEUE
2.DEQUEUE
3.DISPLAY
4.EXIT
Enter your choice:      1
Enter the element to be inserted:       11

QUEUE OPERATIONS ARE
1.ENQUEUE
2.DEQUEUE
3.DISPLAY
4.EXIT
Enter your choice:      1
Enter the element to be inserted:       22

QUEUE OPERATIONS ARE
1.ENQUEUE
2.DEQUEUE
3.DISPLAY
4.EXIT
Enter your choice:      1
Enter the element to be inserted:       33

QUEUE OPERATIONS ARE
1.ENQUEUE
2.DEQUEUE
3.DISPLAY
4.EXIT
Enter your choice:      3
Content of the queue is:
11
22
33

QUEUE OPERATIONS ARE
1.ENQUEUE
2.DEQUEUE
3.DISPLAY
4.EXIT
Enter your choice:      2
The deleted element is: 11

QUEUE OPERATIONS ARE
1.ENQUEUE
2.DEQUEUE
3.DISPLAY
4.EXIT
Enter your choice:           3
Content of the queue is:
22
33

QUEUE OPERATIONS ARE
1.ENQUEUE
2.DEQUEUE
3.DISPLAY
4.EXIT

Enter your choice:      4

Queue Implementation Using Array

PROGRAM:


#include<stdio.h>
#include<conio.h>
#define SIZE 5
void enqueue();
void dequeue();
void display();
int q[SIZE],i,item,front=-1,rear=-1;
void main()
{
                int ch;
                clrscr();
                printf("\n\n\n$$$ QUEUE IMPLEMENTATION USING ARRAY $$$\n\n\n");
                while(1)
                {
                                printf("QUEUE OPERATIONS ARE\n");
                                printf("1.ENQUEUE\n2.DEQUEUE\n3.DISPLAY\n4.EXIT\n");
                                printf("Enter your choice:\t");
                                scanf("%d",&ch);
                                switch(ch)
                                {
                                                case 1:enqueue();break;
                                                case 2:dequeue();break;
                                                case 3:display();break;
                                                case 4:exit(0);
                                                default:printf("Enter the correct option\n");
                                                break;
                                }
                }
}
void enqueue()
{
                if(rear==SIZE-1)
                {
                                printf("Queue is full or overflow\n");
                }
                else
                {
                               if(front==-1)
                               {
                                           front=0;
                                }
                                printf("Enter the element to be enqueued:\t");
                                scanf("%d",&item);
                                rear=rear+1;
                                q[rear]=item;
                }
}
void dequeue()
{
                if(front==-1 || front>rear)
                {
                                printf("Queue is empty or underflow\n");
                }
                else
                {
                                printf("The deleted element is: %d",q[front]);
                                front=front+1;
                }
}
void display()
{
                if(front==-1 || front>rear)
                {
                                printf("Queue is empty or underflow\n");
                }
                else
                {
                                printf("Content of the queue is:\n");
                                for(i=front;i<=rear;i++)
                                {
                                                printf("%d\n",q[i]);
                                }
                }
}



 OUTPUT:

$$$ QUEUE IMPLEMENTATION USING ARRAY $$$

 QUEUE OPERATIONS ARE
1.ENQUEUE
2.DEQUEUE
3.DISPLAY
4.EXIT
Enter your choice:      1
Enter the element to be enqueued:       11

QUEUE OPERATIONS ARE
1.ENQUEUE
2.DEQUEUE
3.DISPLAY
4.EXIT
Enter your choice:      1
Enter the element to be enqueued:       22

QUEUE OPERATIONS ARE
1.ENQUEUE
2.DEQUEUE
3.DISPLAY
4.EXIT
Enter your choice:      1
Enter the element to be enqueued:       33

QUEUE OPERATIONS ARE
1.ENQUEUE
2.DEQUEUE
3.DISPLAY
4.EXIT
Enter your choice:      3
Content of the queue is:
11
22
33

QUEUE OPERATIONS ARE
1.ENQUEUE
2.DEQUEUE
3.DISPLAY
4.EXIT
Enter your choice:      2
The deleted element is: 11

QUEUE OPERATIONS ARE
1.ENQUEUE
2.DEQUEUE
3.DISPLAY
4.EXIT
Enter your choice:3
Content of the queue is:
22
33

QUEUE OPERATIONS ARE
1.ENQUEUE
2.DEQUEUE
3.DISPLAY
4.EXIT

Enter your choice:      4