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;
}