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

Implementation of a Queue using dynamic memory allocation

/* Implementation of a Queue using dynamic memory allocation.*/

#include<conio.h>
#include<stdio.h>
#define MAX 10
typedef struct node
{
int data;
struct node *next;
}node;

typedef struct Q
{
node *R,*F;
}Q;

void initialise(Q *);
int empty(Q *);
int full(Q *);
void enqueue(Q *,int);
int dequeue(Q *);
void print(Q *);
void main()
{
Q q;
int x,i,op;
initialise(&q);
clrscr();
do{
printf("\n\n1)Insert\n2)Delete\n3)Print\n4)Quit");
printf("\nEnter Your Choice:");
scanf("%d",&op);
switch(op)
{ case 1: printf("\n Enter a value:");
scanf("%d",&x);
enqueue(&q,x);
break;
case 2:if(!empty(&q))
{x=dequeue(&q);
printf("\Deleted Data=%d",x);
}
else
printf("\nQueue is empty !!!!");
break;
case 3: print(&q);break;
}
}while(op!=4);
}

void initialise(Q *qP)
{
qP->R=NULL;
qP->F=NULL;
}
void enqueue(Q *qP,int x)
{
node *P;
P=(node*)malloc(sizeof(node));
P->data=x;
P->next=NULL;
if(empty(qP))
{
qP->R=P;
qP->F=P;
}
else
{
(qP->R)->next=P;
qP->R=P;
}
}
int dequeue(Q *qP)
{
int x;
node *P;
P=qP->F;
x=P->data;
if(qP->F==qP->R) //deleting the last element
initialise(qP);
else
qP->F=P->next;
free(P);
return(x);
}
void print(Q *qP)
{
int i;
node *P;
P=qP->F;
while(P!=NULL)
{
printf("\n%d",P->data);
P=P->next;
}
}
int empty(Q *qp)
{ if(qp->R==NULL)
return 1;
return 0;
}

OUTPUT:-

1)Insert
2)Delete
3)Print
4)Quit
Enter Your Choice:1

Enter a value:23


1)Insert
2)Delete
3)Print
4)Quit
Enter Your Choice:1

Enter a value:11


1)Insert
2)Delete
3)Print
4)Quit
Enter Your Choice:1

Enter a value:59


1)Insert
2)Delete
3)Print
4)Quit
Enter Your Choice:3

23
11
59

1)Insert
2)Delete
3)Print
4)Quit
Enter Your Choice:2
Deleted Data=23

1)Insert
2)Delete
3)Print
4)Quit
Enter Your Choice:2
Deleted Data=11

1)Insert
2)Delete
3)Print
4)Quit
Enter Your Choice:2
Deleted Data=59

1)Insert
2)Delete
3)Print
4)Quit
Enter Your Choice:2

Queue is empty !!!!

1)Insert
2)Delete
3)Print
4)Quit
Enter Your Choice:4

Implementation of stack using Dynamic memory allocation

Tuesday, 3 December 2013

/* Implementation of stack using Dynamic memory allocation */

#include<stdio.h>
#include<conio.h>
typedef struct stack
{
int data;
struct stack *next;
} stack;
void init(stack **);
int empty(stack *);
char pop(stack **);
void push(stack **,int);
void print(stack *);
void main()
{
stack *TOP;
int x,op;
init(&TOP);
clrscr();
do
{
printf("\n1)Push\n2)Pop\n3)Print\n4)Quit");
printf("\nEnter Your Choice : ");
scanf("%d",&op);
switch(op)
{
case 1: printf("\nEnter a data : ");
scanf("%d",&x);
push(&TOP,x);
break;
case 2: if(!empty(TOP))
{
x=pop(&TOP);
printf("\nPopped data= %d",x);
}
else
printf("\nStack is empty ....");
break;
case 3: print(TOP);
break;
}
} while(op!=4);
}

void init(stack **T)
{
*T=NULL;
}
int empty(stack *TOP)
{
if(TOP==NULL)
return(1);
return(0);
}
void push(stack **T,int x)
{
stack *P;
P=(stack *)malloc(sizeof(stack));
P->data=x;
P->next=*T;
*T=P;
}
char pop(stack **T)
{
int x;
stack * P;
P=*T;
*T=P->next;
x=P->data;
free(P);
return(x);
}
void print(stack *T)
{
printf("\n");
while (T!=NULL)
{
printf("%d ",T->data);
T=T->next;
}
}


OUTPUT:-

1)Push
2)Pop
3)Print
4)Quit
Enter Your Choice : 1

Enter a data : 45

1)Push
2)Pop
3)Print
4)Quit
Enter Your Choice : 1

Enter a data : 89

1)Push
2)Pop
3)Print
4)Quit
Enter Your Choice : 1

Enter a data : 65

1)Push
2)Pop
3)Print
4)Quit
Enter Your Choice : 3

65 89 45
1)Push
2)Pop
3)Print
4)Quit
Enter Your Choice :2
Popped data= 65
1)Push
2)Pop
3)Print
4)Quit
Enter Your Choice : 2

Popped data= 89
1)Push
2)Pop
3)Print
4)Quit
Enter Your Choice : 2

Popped data= 45
1)Push
2)Pop
3)Print
4)Quit
Enter Your Choice : 2

Stack is empty ....
1)Push
2)Pop
3)Print
4)Quit
Enter Your Choice :4

Array Implementation of list

Monday, 2 December 2013

/*Array Implementation of list */


#include <stdio.h>
#include <conio.h>
#include <string.h>
#define MAX 30
typedef struct list
 { int data[MAX];
   int  n;
 }list;
void insert( list *p,int position,int x);
void Delete(list *p,int position);
int  search(list *p,int x);
void print(list *p);
void read(list *p);
void init(list *l);
 void main()
  { list l;
    int x,op,position;
    clrscr();
    do{
       flushall();
       printf("\n1)create\n2)insert\n3)delete\n4)search\n5)print\n6)quit");
       printf("\nEnter Your Choice:");
       scanf("%d",&op);
       switch(op)
{ case 1: read(&l);
 break;
 case 2: printf("\n enter the position and the data to be inserted : ");
 scanf("%d%d",&position,&x);
 insert(&l,position,x);
 break;
 case 3:printf("\n enter the position : ");
scanf("%d",&position);
Delete(&l,position);
break;
 case 4: printf("\nenter the number to be searched:");
 scanf("%d",&x);
 position=search(&l,x);
 if(position==-1)
    printf("\nnot found");
 else
   printf("Found at the position : %d",position+1);

break;
 case 5: print(&l);
 break;
 default:break;
}
    }while(op!=6);
  }


void  insert( list *p,int position,int x)
{ int i;
 if(position<1 || position > p->n+1)
   { printf("\nIncorrect postion :");
     return;
   }
 for(i=p->n-1;i>=position-1;i--) /*index is 1 less than position*/
     p->data[i+1]=p->data[i];
 p->data[position-1]=x;
 p->n=p->n+1;
}

void  Delete(list *p,int position)
      { int i;
if(position<1 || position >p->n)
{ printf("\Incorrect Position ");
  return;
}
for(i=position;i<p->n;i++)
   p->data[i-1]=p->data[i];
p->n=p->n-1;
      }
int   search(list *p,int x)
       { int i;
for(i=0;i<p->n;i++)
  if(x==p->data[i])
    return(i);
return(-1);
       }
void print(list *p)
       { int i;
 printf("\n");
 for(i=0;i<p->n;i++)
    printf("%d   ",p->data[i]);
       }
void read(list  *p)
   { int i,n;
     printf("\nEnter No. of data : ");
     scanf("%d",&n);
     p->n=n;
     printf("\Enter data : ");
     for(i=0;i<n;i++)
       scanf("%d", &(p->data[i]));
    }

OUTPUT:-


1)create
2)insert
3)delete
4)search
5)print
6)quit
Enter Your Choice:1

Enter No. of data : 4
Enter data : 78 45 56 90

1)create
2)insert
3)delete
4)search
5)print
6)quit
Enter Your Choice:5

78   45   56   90
1)create
2)insert
3)delete
4)search
5)print
6)quit
Enter Your Choice:2

 enter the position and the data to be inserted : 2 5

1)create
2)insert
3)delete
4)search
5)print
6)quit
Enter Your Choice:5

78   5   45   56   90
1)create
2)insert
3)delete
4)search
5)print
6)quit
Enter Your Choice:3

 enter the position : 3
1)create
2)insert
3)delete
4)search
5)print
6)quit
Enter Your Choice:3

 enter the position : 6
Incorrect Position

1)create
2)insert
3)delete
4)search
5)print
6)quit
Enter Your Choice:5

78   5   56   90
1)create
2)insert
3)delete
4)search
5)print
6)quit
Enter Your Choice:4

enter the number to be searched:9

not found
1)create
2)insert
3)delete
4)search
5)print
6)quit
Enter Your Choice:4

enter the number to be searched:5
Found at the position : 2
1)create
2)insert
3)delete
4)search
5)print
6)quit
Enter Your Choice:6

A program for Circular Queue using an array

/* A program for Circular Queue usimg an array */


#include<conio.h>
#include<stdio.h>
#define MAX 10

typedef struct Q
{
int R,F;
int data[MAX];
}Q;

void initialise(Q *P);
int empty(Q *P);
int full(Q *P);
void enqueue(Q *P,int x);
int dequeue(Q *P);
void print(Q *P);
void main()
{ Q q;
       int op,x;
     initialise(&q);
     clrscr();
     do{
printf("\n\n1)Insert\n2)Delete\n3)Print\n4)Quit");
printf("\nEnter Your Choice:");
scanf("%d",&op);
switch(op)
{ case 1: printf("\n Enter a value:");
  scanf("%d",&x);
  if(!full(&q))
   enqueue(&q,x);
  else
   printf("\nQueue is full !!!!");
  break;
  case 2:if(!empty(&q))
   {x=dequeue(&q);
    printf("\Deleted Data=%d",x);
   }
  else
   printf("\nQueue is empty !!!!");
  break;
  case 3: print(&q);break;
}
      }while(op!=4);
}

void initialise(Q *P)
{
P->R=-1;
P->F=-1;
}

int empty(Q *P)
{
if(P->R==-1)
return(1);
return(0);
}

int full(Q *P)
{
if((P->R+1)%MAX==P->F)
return(1);
return(0);
}

void enqueue(Q *P,int x)
{
if(P->R==-1)
{
P->R=P->F=0;
P->data[P->R]=x;
}
else
{
P->R=(P->R+1)%MAX;
P->data[P->R]=x;
}
}

int dequeue(Q *P)
{
int x;
x=P->data[P->F];
if(P->R==P->F)
{
P->R=-1;
P->F=-1;
}
else
P->F=(P->F+1)%MAX;
return(x);
}

void print(Q *P)
{
int i;

if(empty(P))
  return;
printf("\n");
for(i=P->F;i!=P->R;i=(i+1)%MAX)
   printf("%d\t",P->data[i]);
printf("%d\t",P->data[i]);
}


OUTPUT:-



1)Insert
2)Delete
3)Print
4)Quit
Enter Your Choice:1

 Enter a value:34


1)Insert
2)Delete
3)Print
4)Quit
Enter Your Choice:1

 Enter a value:67


1)Insert
2)Delete
3)Print
4)Quit
Enter Your Choice:1

 Enter a value:90


1)Insert
2)Delete
3)Print
4)Quit
Enter Your Choice:3

34      67      90

1)Insert
2)Delete
3)Print
4)Quit
Enter Your Choice:2
Deleted Data=34

1)Insert
2)Delete
3)Print
4)Quit
Enter Your Choice:1

 Enter a value:56


1)Insert
2)Delete
3)Print
4)Quit
Enter Your Choice:2
Deleted Data=67

1)Insert
2)Delete
3)Print
4)Quit
Enter Your Choice:2
Deleted Data=90

1)Insert
2)Delete
3)Print
4)Quit
Enter Your Choice:2
Deleted Data=56

1)Insert
2)Delete
3)Print
4)Quit
Enter Your Choice:2

Queue is empty !!!!

1)Insert
2)Delete
3)Print
4)Quit
Enter Your Choice:4

A program for queue usimg an array

/* A program for queue usimg an array */

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

typedef struct Q
{
int R,F;
int data[MAX];
}Q;

void initialise(Q *P);
int empty(Q *P);
int full(Q *P);
void enqueue(Q *P,int x);
int dequeue(Q *P);
void print(Q *P);
void main()
{ Q q;
       int op,x;
     initialise(&q);
     clrscr();
     do{
printf("\n\n1)Insert\n2)Delete\n3)Print\n4)Quit");
printf("\nEnter Your Choice:");
scanf("%d",&op);
switch(op)
{ case 1: printf("\n Enter a value:");
  scanf("%d",&x);
  if(!full(&q))
   enqueue(&q,x);
  else
   printf("\nQueue is full !!!!");
  break;
  case 2:if(!empty(&q))
   {x=dequeue(&q);
    printf("\Deleted Data=%d",x);
   }
  else
   printf("\nQueue is empty !!!!");
  break;
  case 3: print(&q);break;
}
      }while(op!=4);
}

void initialise(Q *P)
{
P->R=-1;
P->F=-1;
}

int empty(Q *P)
{
if(P->R==-1)
return(1);
return(0);
}

int full(Q *P)
{
if(P->R==MAX-1)
return(1);
return(0);
}

void enqueue(Q *P,int x)
{
if(P->R==-1)
{
P->R=P->F=0;
P->data[P->R]=x;
}
else
{
P->R=P->R+1;
P->data[P->R]=x;
}
}

int dequeue(Q *P)
{
int x;
x=P->data[P->F];
if(P->R==P->F)
{
P->R=-1;
P->F=-1;
}
else
P->F=P->F+1;
return(x);
}

void print(Q *P)
{
int i;
if(!empty(P))
{
printf("\n");
for(i=P->F; i <= P->R; i=i+1)
printf("%d\t",P->data[i]);
}
}


OUTPUT:-


1)Insert
2)Delete
3)Print
4)Quit
Enter Your Choice:1

 Enter a value:45


1)Insert
2)Delete
3)Print
4)Quit
Enter Your Choice:1

 Enter a value:67


1)Insert
2)Delete
3)Print
4)Quit
Enter Your Choice:1

 Enter a value:78


1)Insert
2)Delete
3)Print
4)Quit
Enter Your Choice:3

45      67      78

1)Insert
2)Delete
3)Print
4)Quit
Enter Your Choice:2
Deleted Data=45

1)Insert
2)Delete
3)Print
4)Quit
Enter Your Choice:2
Deleted Data=67

1)Insert
2)Delete
3)Print
4)Quit
Enter Your Choice:2
Deleted Data=78

1)Insert
2)Delete
3)Print
4)Quit
Enter Your Choice:2

Queue is empty !!!!

1)Insert
2)Delete
3)Print
4)Quit
Enter Your Choice:4

Program for Array Implementation of Stack

/* program for Array Implementation of Stack*/

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

typedef struct stack
{
int data[MAX];
int top;
}stack;

void init(stack *);
int  empty(stack *);
int  full(stack *);
int  pop(stack *);
void push(stack *,int );
void print(stack *);
void main()
 { stack s;
   int x,op;
   init(&s);
   clrscr();
   do
     {
printf("\n1)Push\n2)Pop\n3)Print\n4)Quit");
printf("\nEnter Your Choice : ");
scanf("%d",&op);
switch(op)
{
     case 1: printf("\nEnter a Number : ");
     scanf("%d",&x);
     if(!full(&s))
 push(&s,x);
     else
 printf("\nStack is full");
     break;
     case 2: if(!empty(&s))
{
 x=pop(&s);
 printf("\nPopped Data is :%d",x);
}
     else
 printf("\nStack is empty");
     break;
     case 3: print(&s);break;
}
    }while(op!=4);
 }
void init(stack *s)
{
s->top=-1;
}

int empty(stack *s)
{
if(s->top==-1) return(1);
return(0);
}

int full(stack *s)
{
if(s->top==MAX-1)    return(1);
return(0);
}

void push(stack *s,int x)
{
s->top=s->top+1;
s->data[s->top]=x;
}

int pop(stack *s)
{
int x;
x=s->data[s->top];
s->top=s->top-1;
return(x);
}
void print(stack *s)
 { int i;
   printf("\n");
   for(i=s->top;i>=0 ;i--)
      printf("%d   ",s->data[i]);
 }



OUTPUT:-


1)Push
2)Pop
3)Print
4)Quit
Enter Your Choice : 1

Enter a Number : 12

1)Push
2)Pop
3)Print
4)Quit
Enter Your Choice : 1

Enter a Number : 45

1)Push
2)Pop
3)Print
4)Quit
Enter Your Choice : 1

Enter a Number : 56

1)Push
2)Pop
3)Print
4)Quit
Enter Your Choice : 3

56   45   12
1)Push
2)Pop
3)Print
4)Quit
Enter Your Choice :2
Popped Data is :56
1)Push
2)Pop
3)Print
4)Quit
Enter Your Choice : 2

Popped Data is :45
1)Push
2)Pop
3)Print
4)Quit
Enter Your Choice : 2

Popped Data is :12
1)Push
2)Pop
3)Print
4)Quit
Enter Your Choice : 2

Stack is empty
1)Push
2)Pop
3)Print
4)Quit
Enter Your Choice :4
Related Posts Plugin for WordPress, Blogger...
Related Posts Plugin for WordPress, Blogger...
 

Most Reading

Labels