Showing posts with label queue. Show all posts
Showing posts with label queue. Show all posts

Wednesday, August 13, 2014

write a program to perform circular queue using array

//circular queue using array
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<malloc.h>

#define S 5

int Q[S];
int front=-1,rear=-1;
void insert(int n){
if((front==0 && rear==S-1)||(front==rear+1)){
printf("\n overflow \n");
return ;
}
if(rear==-1)
rear=front=0;
else
if(rear==S-1)
rear=0;
else
rear++;
  Q[rear]=n;
}
int del(){
int val;
if(front==-1){
printf("\n underflow \n");
return -9999;
}
val=Q[front];
if(front==rear){
front=rear=-1;
}
else
if(front==S-1)
front=0;
else
front++;
    return val;
}
int main(){
int n,ch;
while(1){
printf("1.insert\n2.delete\n3.exit");
printf("\n enter y choice ");
scanf("%d",&ch);
switch(ch){
case 1:
printf("\n enter the data ");
scanf("%d",&n);
insert(n);
break;
case 2:
n=del();
if(n!=-9999){
printf("\n popped item is %d\n",n);
}
break;
case 3:
exit(0);
break;
default:
printf("\n check y option ");
}
}
getch();
return 0;
}

write a program to perform dqueue using link list

//write a program to perform insertion,deletion and display operation on dqueue using link list
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<malloc.h>
typedef struct dnode{
int data;
struct dnode *next;
}dnode;
dnode *front=NULL,*rear=NULL;
void insertf(int x){
dnode *temp;
temp=(dnode*)malloc(sizeof(dnode));
temp->data=x;
temp->next=NULL;
   if(front==NULL){
    front=rear=temp;
   }
   else{
      rear->next=temp;
     rear=temp;
 }
}
void insertr(int x){
dnode *temp,*temp1;
temp=(dnode*)malloc(sizeof(dnode));
temp->data=x;
temp->next=NULL;
if(rear==NULL){
front=rear=temp;
}
else{
temp1=front;
   while(temp1->next!=NULL)
temp1=temp1->next;
temp1->next=temp;
}
}
void delr(){
dnode *temp;
int i;
if(rear==NULL)
{
printf("\nunderflow");
return;
}
temp=front;
while(temp->next->next!=NULL)
temp=temp->next;
dnode *t=temp->next;
printf("\nDeleted node is %d\n",temp->next->data);
temp->next=NULL;
free(t);
rear=temp;
}
void delf(){
dnode *temp;
if(front==NULL)
{
printf("\nunderflow");
return;
}
temp=front;
front=front->next;
printf("\nDeleted node is %d\n",temp->data);
free(temp);
}
void display(){
dnode *temp;
    temp=front;
    printf("\n");
    while(temp!=NULL)
   {
    printf("\t%d",temp->data);
    temp=temp->next;
   
  }
  printf("\n");
}
int main(){
int n,ch;
while(1){
printf("1.insert at front \n2.insert at rear \n3.delete at rear \n4.delete at front \n5.display \n6.exit");
printf("\n enter y choice ");
scanf("%d",&ch);
switch(ch){
case 1:
printf("\n enter the data ");
scanf("%d",&n);
insertf(n);
break;
   case 2:
      printf("\n enter the data ");
scanf("%d",&n);
insertr(n);
break;
  case 3:
      delr();
  break;
  case 4:
     delf();
 break;
 case 5:
  display();
  break;
case 6:
exit(0);
break;
default:
printf("check y option ");
}
  }
  getch();
  return 0;
}

Tuesday, October 1, 2013

circular queue using linked list



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

#define SIZE 5

int Q[SIZE],front=-1,rear=-1;

void insert(int x)
{
if( (front==0 && rear==SIZE-1) || front==rear+1)
{
printf("\nOverflow");
return ;
}
if(rear==-1)
front=rear=0;
else
if(rear==SIZE-1)
rear=0;
else
rear++;
Q[rear]=x;
}

int del()
{
int item;
if(front==-1)
{
printf("\nUnderflow");
return -9999;
}
item=Q[front];
if(front==rear)
front=rear=-1;
else
if(front==SIZE-1)
front=0;
else
front++;
return item;
}

int main()
{
int n,ch;
while(1)
{
printf("\n\t\t\tMenu");
printf("\n\t\t\t1 : Insert");
printf("\n\t\t\t2 : Deletion");
printf("\n\t\t\t3 : Exit");
printf("\n\t\t\tEnter your choice :");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nEnter the item :");
scanf("%d",&n);
insert(n);
break;
case 2:
n=del();
if(n!=-9999)
printf("\nPopped item : %d",n);
break;
case 3:
exit(0);
default:
printf("\nInvalid Choice");
}
}
return 0;
}

priority queue using linked list

//priority queue using linked list

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

typedef struct node
{
int info,pr;
struct node *next;
}node;
node *start=NULL;

void insert_at_end(int x,int p);
void insert_at_beg(int x,int p);
void insert_at_specific(int ,int, int);
void del_at_beg();
void insert(int x,int p);

int main()
{
int n,po,ch,p;
while(1)
{
printf("\n\t\t\tMenu");
printf("\n\t\t\t1 : Insertion");
printf("\n\t\t\t2 : Deletion");
printf("\n\t\t\t3 : Exit");
printf("\n\t\t\tEnter ur choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nEnter the info :");
scanf("%d",&n);
printf("\nEnter the priority :");
scanf("%d",&p);
insert(n,p);
break;
case 2:
del_at_beg();
break;
case 3:
exit(0);
default:
printf("\nInvalid choice");
}
}
return 0;
}

void insert_at_end(int x, int p)
{
node *temp,*new_node;
new_node=(node*)malloc(sizeof(node));
new_node->info=x;
new_node->pr=p;
new_node->next=NULL;
if(start==NULL)
start=new_node;
else
{
temp=start;
while(temp->next!=NULL)
temp=temp->next;
temp->next=new_node;
}
}

void insert_at_beg(int x,int p)
{
node *new_node;
new_node=(node*)malloc(sizeof(node));
new_node->info=x;
new_node->pr=p;
new_node->next=start;
start=new_node;
}

void insert_at_specific(int x,int p,int po)
{
int i;
node *new_node,*temp;
if(po<1 || po>count()+1)
{
printf("\nInvalid position");
return;
}
if(po==1)
insert_at_beg(x,p);
else
{
if(po==count()+1)
insert_at_end(x,p);
else
{
new_node=(node*)malloc(sizeof(node));
new_node->info=x;
new_node->pr=p;
new_node->next=NULL;
temp=start;
for(i=1;i<po-1;i++)
temp=temp->next;
new_node->next=temp->next;
temp->next=new_node;
}
}
}


int count()
{
node *temp;
int c=0;
for(temp=start;temp!=NULL;temp=temp->next)
c++;
return c;
}

void del_at_beg()
{
node *temp;
if(start==NULL)
{
printf("\nList Empty");
return;
}
temp=start;
start=start->next;
printf("\nDeleted node : %d",temp->info);
printf("\nDeleted priority : %d",temp->pr);
free(temp);
}

void insert(int x,int p)
{
node *temp;
int po=0;
if(start==NULL)
{
insert_at_beg(x,p);
return;
}
temp=start;
while(temp!=NULL)
{
if(temp->pr >= p)
{
po++;
temp=temp->next;
}
else
break;
}
insert_at_specific(x,p,po+1);
}