Queue codes in c - Programming Languages

Latest

Wednesday, 1 February 2017

Queue codes in c

                                  Queue codes

CONTENT:
1. Implement a Queue using an Array
2. Implement Queue Data Structure using Linked List
3. Implement priority Queue to Add and Delete Elements
4. Check if Expression is correctly Parenthesized
5. Implement various Queue Functions using Dynamic Memory Alloction
6. Implement Queue using Stacks
7. Implement Queue Function Using arrays and Macros

----------------------------------------------------------------------------------- 1.implement a queue using an array 
#include<stdio.h>
#include<conio.h>
#define MAX 50
int queue_array[MAX];
int rear=-1;
int font=-1;
void main()
{
int choice;

clrscr();
while(1)
{
printf("1. enter a queue element\n");
printf("2. delete element from\n");
printf("3. display entire queue\n");
printf("4. quit\n");
printf("enter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
insert();
break;
}
case 2:
{
delete();
break;
}
case 3:
{
display();
break;
}
case 4:
{
default;
printf("wrong choice you have entered\n");
}
}/*end of switch case*/
}/*end of while case*/
getch();
}
 void insert()
 {
int add_item;
if(rear==MAX-1)
{
printf("queue is overflow\n");
}
else
{
if(front==-1)
/* condition when queue is initially empty*/
{
front=0;
printf("insert the elemnt in queue\n");
scanf("%d",&add_item);
rear=rear-1;
queue_array[rear]=add_item;
}
}
}/*end of insert function*/

void delete()
{
 if(front==-1||front>rear)
{
printf("queue underflow\n");

return;
}
else
{
printf("element deleted from queue is=%d\n",queue_array[front]);
front=front+1;
}
}/*end of delete function*/

void display()
{
int i;
if(front==-1)
{
printf("queue empty\n");
}
else
{
printf("queue is:\n");
for(i=front;i<=rear;i++)
{
printf("%d",queue_array[i]);
}
}
}/*end of display function*/

-----------------------------------------------------------------------------------
2.implement queue data structure using linked list


#include<conio.h>
#include<stdio.h>
#include<stdlib.h>

struct node
{
int info;
struct node *ptr;
}*front,*rear,*temp,*front1;

int frontelement();
void enq(int data);
void deq();
void display();
void create();
void queuesize();
int count=0;

void main()
{
int no,ch,e;
clrscr();
printf("1-enque\n");
printf("2-deque\n");
printf("3-front element\n");
printf("4-empty\n");
printf("5-exit\n");
printf("6-display\n");
printf("7-queue size\n");
create();
while(1)
{
printf("please enter your choice:\n");
scanf("%d",&ch);
switch(ch)
{
case 1;
{
printf("enter data:\n");
scanf("%d",&no);
enq(no);
break;
}
case 2:
{
deq();
break;
}
case 3:
{
e=frontelement();
if(e!=0)
{
printf("front element is %d\n",e);
}
else
{
printf("there is no front element becouse queue is empty\n");

}
break;
}/*case 3 close*/
case 4:
{
empty();
break;
}
case 5:
{
exit (0);
break;
}
case 6:
{
display();
break;
}
case 7:
{
queuesize();
break;
}
default:
{
printf("you have input worng no.\n");
break;
}
}
}
}

void create()
{
front=rear=NULL;
}

void queuesize()
{
printf("queue size is %d\n",count);
}
void enq()
{
if(rear==NULL)
{
rear=(struct node*)malloc(sizeof(struct node));
rear->ptr=NULL;
rear->info=data;
front=rear;
}
else
{
temp=(struct node*)malloc(sizeof(struct node));
rear->ptr=temp;
temp->info=data;
temp->ptr=NULL;
rear=temp;
}
count++;
}

void display()
{
front1=front;
if((front==NULL)&&(rear==NULL)
{
printf("queue is empty\n");
return;
}
while(front1!=rear)
{
printf("%d",front->rear);
front1=front->ptr;
}
if(front==rear)
{
printf("%d",front1->info);
}
}

void deq()
{
front1=front;
if(front1==NULL)
{
printf("error:not possble to display a element from empty queue\n");
return;
}
else
{
if(front->ptr!=NULL)
{
front1=front1->ptr;
printf("deque value is %d",front->info);
free(front);
front=front1;
}
else
{
printf("dequed value is %d ",front->info);
free(front);
front=NULL;
rear=NULL;
}
count--;
}/*end of else*/
}

int frontelement()
{
if((front!=NULL)&&(rear!=NULL))
{
return (front->info);
}
else
{
return 0;
}
}

void empty()
{
if((front==NULL)&&(rear==NULL))
{
printf("queue is empty\n");
}
else
{
printf("queue is not empty\n");
}
}

----------------------------------------------------------------------------------
3. implement priority queue to add and delete elements

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 5

void insert_by_priority(int);
void delete_by_priority(int);
void create();
void check(int);
void display_pqueue();

int pri_que[MAX];
int front,rear;

void main()
{
int n,ch;
clrscr();
printf("1-for insert a element in queue\n");
printf("2-delete an element from queue\n");
printf("3-display queue elements \n");
printf("4-exit a queue\n");

create();
while()
{
printf("enter your choice\n");
scanf("%d",&ch);

switch(ch)
{
case 1:
{
printf("please enter value\n");
scanf("%d",&n);
insert_by_priority(n);
break;
}
case 2:
{
printf("please enter a value to delete\n");
scanf("%d",&n);
delete_by_priority(n);
break;
}
case 3:
{
display_pqueue();
break;
}
case 4:
{
exit(0);
}
default:
{
printf"choice is incorrect, enter a correct choice\n");
}
}
}
}
/*function to create an empty priority queue*/
void  create()
{
front=rear=-1;
}
/*function to insert value into queue*/
void insert_by_priority(int data)
{
if(rear>=MAX-1)
{
printf("sir queue is overflow no  more value can be insert\n");
return;
}
if(front==-1)&&(rear==-1))
{
front++;
rear++;
pri_que[rear]=data;
return;
}
else
{
check(data);
rear++;
}
}
 
void check(int data)
{
int i,j;
for(i=0;i<=rear;i++)
{
if(data>=pri_que[i])
{
for(j=rear+1;j>i;j--)
{
pri_que[j]=pri_que[j-1];
}
pri_que[i]=data;
return;
}
}
pri_que[i]=data;
}
/*function to delete an element from queue*/
void delete_by_priority(int data)
{
int i;
if((front==-1)&&(rear==-1))
{
printf("queue is empty\n");
return;
}
for(i=0;i<=rear;i++)
{
if(data==pri_que[i])
{
for(;i<rear;i++)
{
pri_que[i]=pri_que[i+1])
}
pri_que[i]=-99;
rear--;
if(rear==-1)
fornt=-1;
return;
}}
printf("%d not found in queue to delete\n",data);
}
/*function to display queue elements*/

void display_pqueue()
{
if((front==-1)&&(rear==-1))
{
printf("queue is empty\n");
return;
}
for(;front<=rear;front++)
{
printf("%d",pri_que[front]);
}
front=0;
}

-----------------------------------------------------------------------------------
4.check if expression is correctly parenthesized


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>

int top=-1;
char stack[100];
//function prototypes
void push(char);
void pop();
void find_top();
void main()
{
int i;
char a[100];
clrscr();
printf("enter expression\n");
scanf("%s",&a);
for(i=0;a[i]!=";i++)
{
if(a[i]=='(')
{
push(a[i]);
}
else if(a[i]==')')
{
pop();
}
}
find_top();
getch();
}
//to push elements in stack

void push(char a)
{
stack[top]=a;
top++;
}

//to pop elements from stack
void pop()
{
if(top==-1)
{
printf("expression is invalid\n");
exit(0);
}
else
{
top--;
}
}


//to find top element of stack

void find_top()
{
if(top==-1)
{
printf("expression is vaild\n");
}
else
{
printf("expression is invalid\n");
}
}

----------------------------------------------------------------------------------
5. implement various queue functions using dynamic memory allocation

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 10
struct node
{
int data;
struct node *link;
}*front,*rear;
//function protypes
void insert();
void delete();
void queue_size();
void check();
void first_element();

void main()
{
int choice,value;
clrscr();
while(1)
{
printf("please enter choice\n");
printf("1-create an empty queue\n");
printf("2-insert element\n");
printf("3-dequeue an element\n");
printf("4-check if empty\n");
printf("5-get the first element of queue\n");
printf("6- to get entire in the queue\n");
printf("7- exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
printf("create queue with capacity of %d\n",MAX);
break;
}
case 2:
{
insert();
break;
}
case 3:
{
delete();
break;
}
case 4:
{
check();
break;
}
case 5:
{
first_element();
break;
}
case 6:
{
queue_size();
break;
}
case 7:
{
exit(0);
break;
}
default:
{
printf("wrong choice\n");
break;
}
}/*end of switch case*/
getch();
}
//to insert element in queue
void insert()
{
struct node *temp;
temp=(struct node*)malloc(sizeof(struct node));
printf("enter value to be inserted\n");
scanf("%d",&temp->data);
temp->link=NULL;
if(rear==NULL)
{
front rear=temp;
}
else
{
rear->link=temp;
rear=temp;
}
}
//delete elements from queue
void delete()
{
struct node *temp;
temp=front;
if(front==NULL)
{
printf("queue is empty\n");
front=rear=NULL;
}
else
{
printf("deleted element is %d\n",front->data);
front=front->link;
free(temp);
}
}
//check if queue is empty or not
void check()
{
if(front==NULL)
{
printf("queue is empty\n")
}
else
{
printf("***************************elements in queue************\n");
}
}
//returns first element of queue
void first_element()
{
if(front==NULL)
{
printf("********************************the queue is empty********************\n");
}
else
{
printf("*************the front element is %d************\n",front->data);
}/*end of function*/
//return number of entires and display aswell*/

void queue_size()
{
struct node *temp;
temp=front;
int cnt=0;
if(front==NULL)
{
printf("queue empty\n");
}
while(temp)
{
printf("%d\n",temp->data);
temp=temp->link;
cnt++;
}
printf("*************************size of queue is %d***********\n",cnt);
}


6.implement queues using stacks

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

void push1(int);
void push2(int);
int pop1();
int pop2();
void enqueue();
void dequeue();
void display();
void create();

int st1[100],st2[100];
int top1=-1,top2=-1;
int count=0;
void main()
{
int ch;
clrscr();
printf("1-enqueue element into queue\n");
printf("2-dequeue element from queue\n");
printf("3-display from queue\n");
printf("4-exit\n");
create();
while(1)
{
printf("enter your choice\n");
sacnf("%d",&ch);
switch(ch)
{
case 1:
{
enqueue();
break;
}
case 2:
{
dequeue();
break;
}
case 3:
{
display();
break;
}
case 4:
{
exit(0);
break;
}
default:
{
printf("wrong input\n");
break;
}
}/*end of switch case*/
}/*end of function*/

/*function to create a queue*/
void create()
{
top1=top2=-1;
}
/*function to push element on the stack*/
void push1(int data)
{
sr1[++top]=data;
}
/*function to pop the element from the stack*/
int pop1()
{
return(st1[top1--]);
}
/*function to push element on to stack*/
void push2(int data)
{
st2[++top2]=data;
}
/*function to pop an element from the stack*/
int pop2()
{
return(st2[top2--]);
}
/*function to add an element into the queue using stack*/
void enqueue()
{
int data,i;
printf("enter data into queue\n");
scanf("%d",&data);
push1(data);
count++;
}
/*function to delete an element from the queue using stack*/

void dequeue()
{
int i;
for(i=0;i<=count;i++)
{
push2(pop1());
}
pop2();
count--;
for(i=0;i<=count;i++)
{
push1(pop2();
}
}
/*function of display stack element*/
void display()
{
int i;
for(i=0;i<=top1;i++)
{
printf("%d\n",st1[i]);
}
}


7.implement queue functions using arrays and macros
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

/*macro definition*/
#define MAX 10
#define EMPTY "QUEUE EMPTY"
#define ISFULL rear>=MAX-1
#define FULL "QUEUE FULL"
#define ISEMPTY rear==-1

/*global variable declaration*/
int queue[MAX],front=0,rear=-1;
/*function prototypes*/
void insert_rear();
void delete_front();
void display_queue();
void display_queue();
void empty_queue();
void front_ele();
int queue_size();
void destroy();

void main()
{
int choice,n,flag=0;
char ch;
clrscr();
do
{
printf("menu\n");
printf("enter 1 to 'insert' an element \n");
printf("enter 2 to 'delete' an element\n");
printf("enter 3 to 'display' the elements of queue\n");
printf("enter 4 to 'check ' if the queue is empty\n");
printf("enter 5 to 'know the first elements of queue'\n");
printf("enter 6 to 'know the queueu size'\n");
printf("enter 7 to 'destroy the queue'\n");
printf("enter 8 to 'exit the program'\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert_rear();
break;
case 2:
delete_front();
break;
case 3:
display_queue();
break;
case 4:
empty_queue();
break;
case 5:
front_ele();
break;
case 6:
n=queue_size();
printf("size of queue is %d\n",n);
break;
case 7:
destroy();
flag=1;
break;
case 8:
exit(0);
break;
default:
printf("wrong choice u have entered\n");
}
printf(" do you wish to continue\n");
scanf(" %c",&ch);
}while(ch=='y');
if(flag==0)
{

destroy();
}
}
/*code to insert a element in queue*/
void insert_rear()
{
int val;

if(ISFULL)
{
printf("FULL");
}
else
{
printf("enter the value to be insert  as a element\n");
scanf("%d",&val);
rear++;
queue[rear]=val;
printf("value inserted successfully\n");
}
}
/*codes to delete  element in queue*/

void delete_front()
[
if(ISEMPTY)
{
printf(EMPTY);
}
else
{
printf("the deleted element in queue is %d\n",queue[front]);
front++;
}}
/*code to display elements of queue*/

void display_queue()
{
int i;
if(ISEMPTY)
{
printf(EMPTY)
}
else
{
for(i=front;i<=rear;i++)
{
printf("%d\n",queue[i]);
}
}
}

/*code to check the queue is empty or not*/
void empty_queue()
{
if(ISEMPTY)
{
printf(EMPTY)
}
else
{
printf("the queue has elements\n");
}
}

/*code to check the first element oof queue*/
void front_ele()
{
if(ISEMPTY)
{
printf(EMPTY);
}
else
{
printf("the first element of the queue is : %d\n",queue[front]);

}}
/*code tp check the size of queue*/

int queue_size()
{
int i=0,count=0;
if(ISEMPTY)
{
printf(EMPTY);
}
else
{
for(i=front;i<rear;i++)
{count++;
}
}
return count;
}
/*code to destroy the queue*/
void destroy()
{
int size,i;
if(ISEMPTY)
{
printf("queue could not b destroyed [empty]\n");
}
else
{
size=queue_size();
for(i=0;i<size;i++)
{
front++;
}
front=0;
rear=-1;
printf("queue is destroyed\n');
}
}

No comments:

Post a Comment