Friday, September 30, 2011

Data Structure Programs


//PROGRAM FOR SINGLE LINKED LIST//


#include<stdio.h>
#include<conio.h>
struct node
{
 int data;
 struct node*next;
}*temp,*start,*prev,*ptr,*size;
void create();
void insert();
void finsert();
void minsert();
void einsert();
void delet();
void fdelet();
void mdelet();
void edelet();
void display();
void main()
{
 int choice;
 clrscr();
 printf("\n\t\t\tSINGLE LINKED LIST");
 printf("\n\t\t\t------------------------------");
 while(1)
 {
  printf("\n\nMAIN MENU");
  printf("\n---------\n");
  printf("1.Create\n");
  printf("2.Insert\n");
  printf("3.Delete\n");
  printf("4.Display\n");
  printf("5.Exit\n");
  printf("\nEnter your choice:");
  scanf("%d",&choice);
  switch(choice)
  {
   case 1:create();break;
   case 2:insert();break;
   case 3:delet();break;
   case 4:display();break;
   case 5:exit();break;
   default:printf("wrong choice");
               exit();
  }
 }
}
void create()
{
 int a;
 temp=(struct node*)malloc(sizeof(struct node));
 printf("\nEnter a new node:");
 scanf("%d",&a);
 temp->data=a;
 temp->next=NULL;
 if(start==NULL)
  start=temp;
 else
 {
  for(ptr=start;ptr->next!=NULL;ptr=ptr->next);
  ptr->next=temp;
 }
}
void insert()
{
 int choice1;
 while(1)
 {
  printf("\n\nINSERTION OPERTION");
  printf("\n------------------------------\n");
  printf("1.Front insertion\n");
  printf("2.MIDDLE insertion\n");
  printf("3.End insertion\n");
  printf("4.Back to main menu\n");
  printf("\nEnter your choice:");
  scanf("%d",&choice1);
  switch(choice1)
  {
   case 1:finsert();break;
   case 2:minsert();break;
   case 3:einsert();break;
   case 4:return;
  }
 }
}
void finsert()
{
 int b;
 printf("\nEnter the node to be inserted:");
 scanf("%d",&b);
 temp=(struct node*)malloc(sizeof(struct node));
 temp->data=b;
 temp->next=start;
 start=temp;
 printf("\nA NODE IS INSERTED IN FRONT\n");
}
void minsert()
{
 int b,c;
 printf("\nEnter the node to be inserted:");
 scanf("%d",&b);
 printf("\nEnter item after which it has to be inserted:");
 scanf("%d",&c);
 temp=(struct node*)malloc(sizeof(struct node));
 temp->data=b;
 temp->next=NULL;
 for(ptr=start;ptr->data!=c;ptr=ptr->next);
 temp->next=ptr->next;
 ptr->next=temp;
 printf("\nA NODE IS INSERTED IN MIDDLE\n");
}
void einsert()
{
 int d;
 printf("\nEnter the node to be inserted:");
 scanf("%d",&d);
 temp=(struct node*)malloc(sizeof(struct node));
 temp->data=d;
 temp->next=NULL;
 for(ptr=start;ptr->next!=NULL;ptr=ptr->next);
 ptr->next=temp;
 printf("\nA NODE IS INSERTED IN END\n");
}
void delet()
{
 int choice2;
 while(1)
 {
  printf("\n\nDELETION OPERTION");
  printf("\n------------------------------\n");
  printf("1.Front deletion\n");
  printf("2.Middle deletion\n");
  printf("3.End deletion\n");
  printf("4.Back to main menu\n");
  printf("\nEnter your choice:");
  scanf("%d",&choice2);
  switch(choice2)
  {
   case 1:fdelet();break;
   case 2:mdelet();break;
   case 3:edelet();break;
   case 4:return;
  }
 }
}
void fdelet()
{
 if(start==NULL)
  printf("\nTHE LIST IS EMPTY");
 else
 {
  ptr=start;
  start=ptr->next;
  printf("\nFRONT NODE IS DELETED\n");
 }
}
void mdelet()
{
 if(start==NULL)
  printf("\nTHE LIST IS EMPTY");
 else
 {
  int e;
  printf("\nEnter the node to be deleted:");
  scanf("%d",&e);
  for(ptr=start;ptr->data!=e;ptr=ptr->next);
  for(prev=start;prev->next!=ptr;prev=prev->next);
  prev->next=ptr->next;
  printf("\nMIDDLE NODE IS DELETED\n");
 }
}
void edelet()
{
 if(start==NULL)
  printf("\nTHE LIST IS EMPTY");
 else
 {
  for(ptr=start;ptr->next!=NULL;ptr=ptr->next);
  for(prev=start;prev->next!=ptr;prev=prev->next);
  prev->next=NULL;
  printf("\nEND NODE IS DELETED\n");
 }
}
void display()
{
 printf("\nThe element in the list:");
 ptr=start;
 while(ptr->next!=NULL)
 {
  printf("%d->",ptr->data);
  ptr=ptr->next;
 }
 printf("%d->",ptr->data);
 printf("NULL");
}

SAMPLE OUTPUT:

                                     SINGLE LINKED LIST

                       
MAIN MENU
1.Create
2.Insert
3.Delete
4.Display
5.Exit

Enter your choice:1

Enter a new node:5

MAIN MENU
1.Create
2.Insert
3.Delete
4.Display
5.Exit

Enter your choice:2

INSERTION OPERTION
1.Front insertion
2.MIDDLE insertion
3.End insertion
4.Back to main menu

Enter your choice:1

Enter the node to be inserted:3

A NODE IS INSERTED IN FRONT

INSERTION OPERTION
1.Front insertion
2.MIDDLE insertion
3.End insertion
4.Back to main menu

Enter your choice:2

Enter the node to be inserted:1

Enter item after which it has to be inserted:3
A NODE IS INSERTED IN MIDDLE

INSERTION OPERTION
1.Front insertion
2.MIDDLE insertion
3.End insertion
4.Back to main menu

Enter your choice:3

Enter the node to be inserted:4

A NODE IS INSERTED IN END

INSERTION OPERTION
1.Front insertion
2.MIDDLE insertion
3.End insertion
4.Back to main menu

Enter your choice:4

MAIN MENU
1.Create
2.Insert
3.Delete
4.Display
5.Exit

Enter your choice:4

The element in the list:3->1->5->4->NULL

MAIN MENU
1.Create
2.Insert
3.Delete
4.Display
5.Exit

Enter your choice:3

DELETION OPERTION
1.Front deletion
2.Middle deletion
3.End deletion
4.Back to main menu

Enter your choice:1

FRONT NODE IS DELETED

DELETION OPERTION
1.Front deletion       
2.Middle deletion
3.End deletion
4.Back to main menu

Enter your choice:2

Enter the node to be deleted:5

MIDDLE NODE IS DELETED

DELETION OPERTION
1.Front deletion
2.Middle deletion
3.End deletion
4.Back to main menu

Enter your choice:3

END NODE IS DELETED

DELETION OPERTION
1.Front deletion
2.Middle deletion
3.End deletion
4.Back to main menu

Enter your choice:4

MAIN MENU
1.Create
2.Insert
3.Delete
4.Display
5.Exit

Enter your choice:4

The element in the list:1->NULL
MAIN MENU
1.Create
2.Insert
3.Delete
4.Display
5.Exit

Enter your choice:5


//PROGRAM FOR CIRCULAR  LINKED LIST//


#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct node
{
 int data;
 struct node*next;
}*start,*ptr,*temp,*prev;
void insert();
void delet();
void display();
void main()
{
 int choice;
 clrscr();
 printf("\n\t\t\tCIRCULAR LINKED LIST");
 printf("\n\t\t\t------------------------------------");
 while(1)
 {
  printf("\n\nMENU");
  printf("\n----\n");
  printf("1.Insertion\n");
  printf("2.Deletion\n");
  printf("3.Display\n");
  printf("4.Exit\n");
  printf("\nEnter the choice:");
  scanf("%d",&choice);
  switch(choice)
  {
   case 1:insert();break;
   case 2:delet();break;
   case 3:display();break;
   case 4:exit();break;
   default:printf("wrong choice");
               exit();
  }
 }
}
void insert()
{
 int a;
 temp=(struct node*)malloc(sizeof(struct node));
 printf("\nEnter a node to be inserted:");
 scanf("%d",&a);
 temp->data=a;
 temp->next=NULL;
 if(start==NULL)
  start=temp;
 else
 {
  for(ptr=start;ptr->next!=NULL;ptr=ptr->next);
  ptr->next=temp;
 }
}
void delet()
{
 int e;
 if(start==NULL)
  printf("\nCIRCULAR LINKED LIST IS EMPTY");
 else
 {
  printf("\nEnter the node to be deleted:");
  scanf("%d",&e);
  if(start->data==e)
  {
   ptr=start;
   start=ptr->next;
  }
  else
  {
   for(ptr=start;ptr->data!=e;ptr=ptr->next);
   for(prev=start;prev->next!=ptr;prev=prev->next);
   prev->next=ptr->next;
  }
 }
 printf("\nThe node %d is deleted",e);
}
void display()
{
 if(start==NULL)
  printf("\nCIRCULAR LINKED LIST IS EMPTY");
 else
 {
  printf("\nItems in the circular linked list:");
  ptr=start;
  while(ptr->next!=NULL)
  {
   printf("%d->",ptr->data);
   ptr=ptr->next;
  }
  printf("%d->",ptr->data);
  printf("%d",start->data);
 }
}

SAMPLE OUTPUT:
                                    CIRCULAR LINKED LIST
                
MENU
1.Insertion
2.Deletion
3.Display
4.Exit

Enter the choice:1

Enter a node to be insertd:3

MENU
1.Insertion
2.Deletion
3.Display
4.Exit

Enter the choice:1

Enter a node to be insertd:4

MENU
1.Insertion
2.Deletion
3.Display
4.Exit

Enter the choice:1

Enter a node to be insertd:5

MENU
1.Insertion
2.Deletion
3.Display
4.Exit

Enter the choice:3

Items in the circular linked list:3->4->5->3




MENU
1.Insertion
2.Deletion
3.Display
4.Exit

Enter the choice:2

Enter the node to be deleted:4

The node 4 is deleted

MENU
1.Insertion
2.Deletion
3.Display
4.Exit

Enter the choice:3

Items in the circular linked list:3->5->3

MENU
1.Insertion
2.Deletion
3.Display
4.Exit

Enter the choice:4

//PROGRAM FOR CIRCULAR QUEUE//


#include<stdio.h>
#include<conio.h>
void enqueue();
void dequeue();
void display();
int cqueue[100],front=-1,rear=0,size;
void main()
{
 int choice;
 clrscr();
 printf("\n\t\t\tCIRCULAR QUEUE");
 printf("\n\t\t\t----------------------------");
 printf("\n\nEnter the size of the circular queue:");
 scanf("%d",&size);
 while(1)
 {
  printf("\n\nMENU");
  printf("\n----\n");
  printf("1.Enqueue\n");
  printf("2.Dequeue\n");
  printf("3.Display\n");
  printf("4.Exit\n");
  printf("\nEnter the choice:");
  scanf("%d",&choice);
  switch(choice)
  {
   case 1:enqueue();break;
   case 2:dequeue();break;
   case 3:display();break;
   case 4:exit();break;
   default:printf("wrong choice");
               exit();
  }
 }
}
void enqueue()
{
 int x;
 if(front==(rear+1)%size)
  printf("\nCIRCULAR QUEUE IS FULL\n");
 else
 {
  printf("\nEnter the element to be inserted:");
  scanf ("%d",&x);
  if(front== -1)
   front=rear=0;
  else
   rear=(rear+1)%size;
  cqueue[rear]=x;
 }
}
void dequeue()
{
 if(front== -1)
  printf("\nCIRCULAR QUEUE IS EMPTY\n");
 else
 {
  printf("\nThe deleted number is %d",cqueue[front]);
  if(front==rear)
   front=rear= -1;
  else
   front=(front+1)%size;
 }
}
void display()
{
 int i=front;
 if(i== -1)
  printf("\nCIRCULAR QUEUE IS EMPTY");
 else
 {
  printf("\nItems in the circular queue:");
  while(i!=rear)
  {
   printf ("\t%d",cqueue[i]);
   i=(i+1)%size;
  }
  printf ("\t%d",cqueue[i]);
 }
}


SAMPLE OUTPUT:
                                      CIRCULAR QUEUE
                       
Enter the size of the circular queue:3

MENU
1.Enqueue
2.Dequeue
3.Display
4.Exit

Enter the choice:1

Enter the element to be inserted:3

MENU
1.Enqueue
2.Dequeue
3.Display
4.Exit

Enter the choice:1

Enter the element to be inserted:6

MENU
1.Enqueue
2.Dequeue
3.Display
4.Exit

Enter the choice:1

Enter the element to be inserted:9

MENU
1.Enqueue
2.Dequeue
3.Display
4.Exit

Enter the choice:1

CIRCULAR QUEUE IS FULL


MENU
1.Enqueue
2.Dequeue
3.Display
4.Exit

Enter the choice:3

Items in the circular queue:    3       6       9

MENU
1.Enqueue
2.Dequeue
3.Display
4.Exit

Enter the choice:2

The deleted number is 3

MENU
1.Enqueue
2.Dequeue
3.Display
4.Exit

Enter the choice:3

Items in the circular queue:    6       9

MENU
1.Enqueue
2.Dequeue
3.Display
4.Exit

Enter the choice:4

//PROGRAM FOR QUEUE USING ARRAYS//


#include<stdio.h>
#include<conio.h>
void enqueue();
void dequeue();
void display();
int queue[100],front=0,rear=0,size;
void main()
{
 int choice;
 clrscr();
 printf("\n\t\t\tQUEUE USING ARRAYS");
 printf("\n\t\t\t----------------------------------");
 printf("\n\nEnter the size of the queue:");
 scanf("%d",&size);
 while(1)
 {
  printf("\n\nMENU");
  printf("\n----\n");
  printf("1.Enqueue\n");
  printf("2.Dequeue\n");
  printf("3.Display\n");
  printf("4.Exit\n");
  printf("\nEnter the choice:");
  scanf("%d",&choice);
  switch(choice)
  {
   case 1:enqueue();break;
   case 2:dequeue();break;
   case 3:display();break;
   case 4:exit();break;
   default:printf("wrong choice");
               exit();
  }
 }
 getch();
}
void enqueue()
{
 int x;
 if(rear==size)
  printf("\nQUEUE IS FULL\n");
 else
 {
  printf("\nEnter the element to be inserted:");
  scanf ("%d",&x);
  rear++;
  queue[rear]=x;
 }
}
void dequeue()
{
 if(rear==front)
  printf("\nQUEUE IS EMPTY\n");
 else
 {
  front++;
  printf("\nThe deleted number is %d",queue[front]);
  queue[front]=0;
 }
}
void display()
{
 int i;
 if(rear==front)
  printf("\nQUEUE IS EMPTY");
 else
 {
  printf("\nItems in the queue:");
  for (i=front+1;i<=rear;i++)
   printf ("\t%d",queue[i]);
 }
}



SAMPLE  OUTPUT:
                                     QUEUE USING ARRAYS
                       
Enter the size of the queue:3

MENU
1.Enqueue
2.Dequeue
3.Display
4.Exit

Enter the choice:1

Enter the element to be inserted:2

MENU
1.Enqueue
2.Dequeue
3.Display
4.Exit

Enter the choice:1

Enter the element to be inserted:4

MENU
1.Enqueue
2.Dequeue
3.Display
4.Exit

Enter the choice:1

Enter the element to be inserted:5

MENU
1.Enqueue
2.Dequeue
3.Display
4.Exit

Enter the choice:1

QUEUE IS FULL


MENU
1.Enqueue
2.Dequeue
3.Display
4.Exit

Enter the choice:3

Items in the queue:     2       4       5

MENU
1.Enqueue
2.Dequeue
3.Display
4.Exit

Enter the choice:2

The deleted number is 2

MENU
1.Enqueue
2.Dequeue
3.Display
4.Exit

Enter the choice:3

Items in the queue:     4       5

MENU
1.Enqueue
2.Dequeue
3.Display
4.Exit

Enter the choice:4
                        

//PROGRAM FOR QUEUE USING LINKED LIST//


#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct queue
{
 int data;
 struct queue *next;
}*front=NULL,*rear=NULL;
void enqueue();
void dequeue();
void display();
void main()
{
 int choice;
 clrscr();
 printf("\n\t\t\tQUEUE USING LINKED LIST");
 printf("\n\t\t\t-----------------------------------------");
 while(1)
 {
  printf("\n\nMENU");
  printf("\n----\n");
  printf("1.Enqueue\n");
  printf("2.Dequeue\n");
  printf("3.Display\n");
  printf("4.Exit\n");
  printf("\nEnter the choice:");
  scanf("%d",&choice);
  switch(choice)
  {
   case 1:enqueue();break;
   case 2:dequeue();break;
   case 3:display();break;
   case 4:exit();break;
   default:printf("wrong choice");
               exit();
  }
 }
}
void enqueue()
{
 struct queue *t;
 int number;
 printf ("\nEnter the data:");
 scanf ("%d",&number);
 t=(struct queue *)malloc(sizeof(struct queue));
 t->data=number;
 t->next=NULL;
 if(rear==NULL)
 {
  rear=t;
  front=t;
 }
 else
 {
  rear->next=t;
  rear=t;
 }
}
void dequeue()
{
 int x;
 if(front==NULL)
  printf ("\nQUEUE UNDERFLOW");
 else
 {
  x=front->data;
  front=front->next;
  printf("\nThe deleted number is %d",x);
 }
}
void display()
{
 struct queue *q=front;
 if(front==NULL)
  printf ("\nQUEUE UNDERFLOW");
 else
 {
  printf("\nItems in the queue:");
  while(q!=NULL)
  {
   printf("%d->",q->data);
   q=q->next;
  }
  printf("NULL");
 }
}

SAMPLE  OUTPUT:
                                     QUEUE USING LINKED LIST

MENU
1.Enqueue
2.Dequeue
3.Display
4.Exit

Enter the choice:1

Enter the data:3

MENU
1.Enqueue
2.Dequeue
3.Display
4.Exit

Enter the choice:1

Enter the data:5

MENU
1.Enqueue
2.Dequeue
3.Display
4.Exit

Enter the choice:1

Enter the data:7

MENU
1.Enqueue
2.Dequeue
3.Display
4.Exit

Enter the choice:3

Items in the queue:3->5->7->NULL




MENU
1.Enqueue
2.Dequeue
3.Display
4.Exit

Enter the choice:2

The deleted number is 3

MENU
1.Enqueue
2.Dequeue
3.Display
4.Exit

Enter the choice:3

Items in the queue:5->7->NULL

MENU
1.Enqueue
2.Dequeue
3.Display
4.Exit

Enter the choice:4



//PROGRAM FOR DOUBLE ENDED QUEUE //




#include<stdio.h>
#include<conio.h>
struct node
{
 int data;
 struct node*next;
}*temp,*start,*prev,*ptr,*size;
void create();
void insert();
void finsert();
void einsert();
void delet();
void fdelet();
void edelet();
void display();
void main()
{
 int choice;
 clrscr();
 printf("\n\t\t\tDOUBLE ENDED QUEUE");
 printf("\n\t\t\t-----------------------------------");
 while(1)
 {
  printf("\n\nMAIN MENU");
  printf("\n---------------\n");
  printf("1.Create\n");
  printf("2.Insert\n");
  printf("3.Delete\n");
  printf("4.Display\n");
  printf("5.Exit\n");
  printf("\nEnter your choice:");
  scanf("%d",&choice);
  switch(choice)
  {
   case 1:create();break;
   case 2:insert();break;
   case 3:delet();break;
   case 4:display();break;
   case 5:exit();break;
   default:printf("wrong choice");
               exit();
  }
 }
}
void create()
{
 int item;
 temp=(struct node*)malloc(sizeof(struct node));
 printf("\nEnter a new node value:");
 scanf("%d",&item);
 temp->data=item;
 temp->next=NULL;
 if(start==NULL)
  start=temp;
 else
 {
  for(ptr=start;ptr->next!=NULL;ptr=ptr->next);
  ptr->next=temp;
 }
}
void insert()
{
 int choice1;
 while(1)
 {
  printf("\n\nINSERTION OPERTION");
  printf("\n------------------\n");
  printf("1.Front insertion\n");
  printf("2.End insertion\n");
  printf("3.Back to main menu\n");
  printf("\nEnter your choice:");
  scanf("%d",&choice1);
  switch(choice1)
  {
   case 1:finsert();break;
   case 2:einsert();break;
   case 3:return;
  }
 }
}
void finsert()
{
 int b;
 printf("\nEnter the node to be inserted:");
 scanf("%d",&b);
 temp=(struct node*)malloc(sizeof(struct node));
 temp->data=b;
 temp->next=start;
 start=temp;
 printf("\nA NODE IS INSERTED IN FRONT\n");
}
void einsert()
{
 int c;
 printf("\nEnter the node to be inserted:");
 scanf("%d",&c);
 temp=(struct node*)malloc(sizeof(struct node));
 temp->data=c;
 temp->next=NULL;
 for(ptr=start;ptr->next!=NULL;ptr=ptr->next);
 ptr->next=temp;
 printf("\nA NODE IS INSERTED IN END\n");
}
void delet()
{
 int choice2;
 while(1)
 {
  printf("\n\nDELETION OPERTION");
  printf("\n-----------------\n");
  printf("1.Front deletion\n");
  printf("2.End deletion\n");
  printf("3.Back to main menu\n");
  printf("\nEnter your choice:");
  scanf("%d",&choice2);
  switch(choice2)
  {
   case 1:fdelet();break;
   case 2:edelet();break;
   case 3:return;
  }
 }
}
void fdelet()
{
 ptr=start;
 start=ptr->next;
 printf("\nFRONT NODE IS DELETED\n");
}
void edelet()
{
 for(ptr=start;ptr->next!=NULL;ptr=ptr->next);
 for(prev=start;prev->next!=ptr;prev=prev->next);
 prev->next=NULL;
 printf("\nEND NODE IS DELETED\n");
}
void display()
{
 printf("\nThe element in the list:");
 ptr=start;
 while(ptr->next!=NULL)
 {
  printf("%d->",ptr->data);
  ptr=ptr->next;
 }
 printf("%d->",ptr->data);
 printf("NULL");
}




SAMPLE OUTPUT:
                                      DOUBLE ENDED QUEUE
                      
MAIN MENU
1.Create
2.Insert
3.Delete
4.Display
5.Exit

Enter your choice:1

Enter a new node value:5

MAIN MENU
1.Create
2.Insert
3.Delete
4.Display
5.Exit

Enter your choice:2

INSERTION OPERTION
1.Front insertion
2.End insertion
3.Back to main menu

Enter your choice:1

Enter the node to be inserted:6

A NODE IS INSERTED IN FRONT

INSERTION OPERTION
1.Front insertion
2.End insertion
3.Back to main menu

Enter your choice:2

Enter the node to be inserted:3

A NODE IS INSERTED IN END


INSERTION OPERTION
1.Front insertion
2.End insertion
3.Back to main menu

Enter your choice:3

MAIN MENU
1.Create
2.Insert
3.Delete
4.Display
5.Exit

Enter your choice:4

The element in the list:6->5->3->NULL

MAIN MENU
1.Create
2.Insert
3.Delete
4.Display
5.Exit

Enter your choice:3

DELETION OPERTION
1.Front deletion
2.End deletion
3.Back to main menu

Enter your choice:1

FRONT NODE IS DELETED

DELETION OPERTION
1.Front deletion
2.End deletion
3.Back to main menu

Enter your choice:3

MAIN MENU
1.Create
2.Insert
3.Delete
4.Display
5.Exit

Enter your choice:4

The element in the list:5->3->NULL

MAIN MENU
1.Create
2.Insert
3.Delete
4.Display
5.Exit

Enter your choice:3

DELETION OPERTION
1.Front deletion
2.End deletion
3.Back to main menu

Enter your choice:2

END NODE IS DELETED

DELETION OPERTION
1.Front deletion
2.End deletion
3.Back to main menu

Enter your choice:3

MAIN MENU
1.Create
2.Insert
3.Delete
4.Display
5.Exit

Enter your choice:4

The element in the list:5->NULL



MAIN MENU
1.Create
2.Insert
3.Delete
4.Display
5.Exit

Enter your choice:5

//PROGRAM FOR PRIORITY QUEUE//


#include<stdio.h>
#include<conio.h>
void enqueue();
void dequeue();
void display();
void priority();
int queue[100],front=0,rear=0,size;
void main()
{
 int choice;
 clrscr();
 printf("\n\t\t\tPRIORITY QUEUE");
 printf("\n\t\t\t--------------------------");
 printf("\n\nEnter the size of the queue:");
 scanf("%d",&size);
 while(1)
 {
  printf("\n\nMENU");
  printf("\n----\n");
  printf("1.Enqueue\n");
  printf("2.Dequeue\n");
  printf("3.Display\n");
  printf("4.Exit\n");
  printf("\nEnter the choice:");
  scanf("%d",&choice);
  switch(choice)
  {
   case 1:enqueue();break;
   case 2:dequeue();break;
   case 3:display();break;
   case 4:exit();break;
   default:printf("wrong choice");
               exit();
  }
 }
 getch();
}
void enqueue()
{
 int x;
 if(rear==size)
  printf("\nQUEUE IS FULL\n");
 else
 {
  printf("\nEnter the element to be inserted:");
  scanf ("%d",&x);
  rear++;
  queue[rear]=x;
  priority();
 }
}
void dequeue()
{
 if(rear==front)
  printf("\nQUEUE IS EMPTY\n");
 else
 {
  front++;
  printf("\nThe deleted number is %d",queue[front]);
  queue[front]=0;
 }
}
void display()
{
 int i;
 if(rear==front)
  printf("\nQUEUE IS EMPTY");
 else
 {
  printf("\nThe priority queue:");
  for (i=front+1;i<=rear;i++)
   printf ("\t%d",queue[i]);
 }
}
void priority()
{
 int i,j,temp;
 for(i=front;i<=rear-1;i++)
 {
  for(j=front;j<rear;j++)
  {
   if(queue[j]>queue[j+1])
   {
    temp=queue[j];
    queue[j]=queue[j+1];
    queue[j+1]=temp;
   }
  }
 }
}

SAMPLE OUTPUT:
                                      PRIORITY QUEUE

Enter the size of the queue:3

MENU
1.Enqueue
2.Dequeue
3.Display
4.Exit

Enter the choice:1

Enter the element to be inserted:4

MENU
1.Enqueue
2.Dequeue
3.Display
4.Exit

Enter the choice:1

Enter the element to be inserted:6


MENU
1.Enqueue
2.Dequeue
3.Display
4.Exit

Enter the choice:1

Enter the element to be inserted:2

MENU
1.Enqueue
2.Dequeue
3.Display
4.Exit

Enter the choice:1

QUEUE IS FULL


MENU
1.Enqueue
2.Dequeue
3.Display
4.Exit

Enter the choice:3

The priority queue:     2       4       6

MENU
1.Enqueue
2.Dequeue
3.Display
4.Exit

Enter the choice:2

The deleted number is 2

MENU
1.Enqueue
2.Dequeue
3.Display
4.Exit

Enter the choice: 3

The priority queue:     4       6

MENU
1.Enqueue
2.Dequeue
3.Display
4.Exit

Enter the choice:4


//PROGRAM FOR DOUBLY LINKED LIST//


#include<stdio.h>
#include<conio.h>
struct node
{
 int data;
 struct node *next;
 struct node *prev;
}*start,*ptr,*temp;
void create();
void insert();
void finsert();
void minsert();
void einsert();
void delet();
void fdelet();
void mdelet();
void edelet();
void fdisplay();
void rdisplay();
void main()
{
 int choice;
 clrscr();
 printf("\n\t\t\tDOUBLY LINKED LIST");
 printf("\n\t\t\t---------------------------------");
 while(1)
 {
  printf("\n\nMAIN MENU");
  printf("\n----------\n");
  printf("1.Create\n");
  printf("2.Insert\n");
  printf("3.Delete\n");
  printf("4.Forward display\n");
  printf("5.Reverse display\n");
  printf("6.Exit\n");
  printf("\nEnter your choice:");
  scanf("%d",&choice);
  switch(choice)
  {
   case 1:create();break;
   case 2:insert();break;
   case 3:delet();break;
   case 4:fdisplay();break;
   case 5:rdisplay();break;
   case 6:exit();break;
   default:printf("wrong choice");
               exit();
  }
 }
}
void create()
{
 int a;
 temp=(struct node*)malloc(sizeof(struct node));
 printf("\nEnter a new node:");
 scanf("%d",&a);
 temp->data=a;
 temp->next=NULL;
 temp->prev=NULL;
 if(start==NULL)
  start=temp;
 else
 {
  for(ptr=start;ptr->next!=NULL;ptr=ptr->next);
  ptr->next=temp;
  temp->prev=ptr;
 }
}
void insert()
{
 int choice1;
 while(1)
 {
  printf("\n\nINSERTION OPERTION");
  printf("\n---------------------------------\n");
  printf("1.Front insertion\n");
  printf("2.MIDDLE insertion\n");
  printf("3.End insertion\n");
  printf("4.Back to main menu\n");
  printf("\nEnter your choice:");
  scanf("%d",&choice1);
  switch(choice1)
  {
   case 1:finsert();break;
   case 2:minsert();break;
   case 3:einsert();break;
   case 4:return;
  }
 }
}
void finsert()
{
 int b;
 printf("\nEnter the node to be inserted:");
 scanf("%d",&b);
 temp=(struct node*)malloc(sizeof(struct node));
 temp->data=b;
 temp->next=start;
 ptr->prev=temp;
 start=temp;
 printf("\nA NODE IS INSERTED IN FRONT\n");
}
void minsert()
{
 int b,c;
 printf("\nEnter the node to be inserted:");
 scanf("%d",&b);
 printf("\nEnter item after which it has to be inserted:");
 scanf("%d",&c);
 temp=(struct node*)malloc(sizeof(struct node));
 temp->data=b;
 temp->next=NULL;
 for(ptr=start;ptr->data!=c;ptr=ptr->next);
 temp->prev=ptr;
 temp->next=ptr->next;
 ptr->next->prev=temp;
 ptr->next=temp;
 printf("\nA NODE IS INSERTED IN MIDDLE\n");
}
void einsert()
{
 int d;
 printf("\nEnter the node to be inserted:");
 scanf("%d",&d);
 temp=(struct node*)malloc(sizeof(struct node));
 for(ptr=start;ptr->next!=NULL;ptr=ptr->next);
 temp->data=d;
 temp->next=NULL;
 temp->prev=ptr;
 ptr->next=temp;
 printf("\nA NODE IS INSERTED IN END\n");
}
void delet()
{
 int choice2;
 while(1)
 {
  printf("\n\nDELETION OPERTION");
  printf("\n--------------------------------\n");
  printf("1.Front deletion\n");
  printf("2.Middle deletion\n");
  printf("3.End deletion\n");
  printf("4.Back to main menu\n");
  printf("\nEnter your choice:");
  scanf("%d",&choice2);
  switch(choice2)
  {
   case 1:fdelet();break;
   case 2:mdelet();break;
   case 3:edelet();break;
   case 4:return;
  }
 }
}
void fdelet()
{
 if(start==NULL)
  printf("\nTHE LIST IS EMPTY");
 else
 {
  ptr=start;
  start=ptr->next;
  printf("\nFRONT NODE IS DELETED\n");
 }
}
void mdelet()
{
 if(start==NULL)
  printf("\nTHE LIST IS EMPTY");
 else
 {
  int e;
  printf("\nEnter the node to be deleted:");
  scanf("%d",&e);
  for(ptr=start;ptr->data!=e;ptr=ptr->next);
  ptr->next->prev=ptr->prev;
  ptr->prev->next=ptr->next;
  printf("\nMIDDLE NODE IS DELETED\n");
 }
}
void edelet()
{
 if(start==NULL)
  printf("\nTHE LIST IS EMPTY");
 else
 {
  for(ptr=start;ptr->next!=NULL;ptr=ptr->next);
  ptr->prev->next=NULL;
  printf("\nEND NODE IS DELETED\n");
 }
}
void fdisplay()
{
 if(start==NULL)
  printf("\nTHE LIST IS EMPTY\n");
 else
 {
  printf("\nDISPLAY IN FORWARD ORDER:");
  printf("NULL");
  for(ptr=start;ptr->next!=NULL;ptr=ptr->next)
  {
   printf("<=>%d",ptr->data);
  }
  printf("<=>%d<=>",ptr->data);
  printf("NULL");
 }
}
void rdisplay()
{
 if(start==NULL)
  printf("\nTHE LIST IS EMPTY");
 else
 {
  printf("\nDISPLAY IN REVERSE ORDER:");
  printf("NULL");
  for(ptr=start;ptr->next!=NULL;ptr=ptr->next);
  while(ptr!=start)
  {
   printf("<=>%d",ptr->data);
   ptr=ptr->prev;
  }
  printf("<=>%d<=>",ptr->data);
  printf("NULL");
 }
}
SAMPLE OUTPUT:
                                    DOUBLY LINKED LIST

MAIN MENU
1.Create
2.Insert
3.Delete
4.Forward display
5.Reverse display
6.Exit

Enter your choice:1

Enter a new node:3

MAIN MENU
1.Create
2.Insert
3.Delete
4.Forward display
5.Reverse display
6.Exit

Enter your choice:2

INSERTION OPERTION
1.Front insertion
2.MIDDLE insertion
3.End insertion
4.Back to main menu

Enter your choice:1

Enter the node to be inserted:4

A NODE IS INSERTED IN FRONT

INSERTION OPERTION
1.Front insertion
2.MIDDLE insertion
3.End insertion
4.Back to main menu

Enter your choice:2

Enter the node to be inserted:5
Enter item after which it has to be inserted:4

A NODE IS INSERTED IN MIDDLE

INSERTION OPERTION
1.Front insertion
2.MIDDLE insertion
3.End insertion
4.Back to main menu

Enter your choice:3

Enter the node to be inserted:7

A NODE IS INSERTED IN END

INSERTION OPERTION
1.Front insertion
2.MIDDLE insertion
3.End insertion
4.Back to main menu

Enter your choice:4

MAIN MENU
1.Create
2.Insert
3.Delete
4.Forward display
5.Reverse display
6.Exit

Enter your choice:4

DISPLAY IN FORWARD ORDER:NULL<=>4<=>5<=>3<=>7<=>NULL

MAIN MENU
1.Create
2.Insert
3.Delete
4.Forward display
5.Reverse display
6.Exit

Enter your choice:5

DISPLAY IN REVERSE ORDER:NULL<=>7<=>3<=>5<=>4<=>NULL

MAIN MENU
1.Create
2.Insert
3.Delete
4.Forward display
5.Reverse display
6.Exit

Enter your choice:3

DELETION OPERTION
1.Front deletion
2.Middle deletion
3.End deletion
4.Back to main menu

Enter your choice:1

FRONT NODE IS DELETED

DELETION OPERTION
1.Front deletion
2.Middle deletion
3.End deletion
4.Back to main menu

Enter your choice:3

END NODE IS DELETED

DELETION OPERTION
1.Front deletion
2.Middle deletion
3.End deletion
4.Back to main menu

Enter your choice:4

MAIN MENU
1.Create
2.Insert
3.Delete
4.Forward display
5.Reverse display
6.Exit

Enter your choice: 4

DISPLAY IN FORWARD ORDER:NULL<=>1<=>3<=>NULL

MAIN MENU
1.Create
2.Insert
3.Delete
4.Forward display
5.Reverse display
6.Exit

Enter your choice:5

DISPLAY IS REVERSE ORDER:NULL<=>3<=>1<=>NULL

MAIN MENU
1.Create
2.Insert
3.Delete
4.Forward display
5.Reverse display
6.Exit

Enter your choice:6
//PROGRAM FOR INFIX TO POSTFIX//


#include<stdio.h>
#include<conio.h>
#include<alloc.h>
char inf[40],post[40];
int top=0,st[20];
void postfix();
void push(int);
char pop();
void main()
{
 clrscr();
 printf ("\n\t\tINFIX TO POSTFIX");
 printf ("\n\t\t---------------------------");
 printf("\n\nEnter the infix expression:");
 scanf("%s",inf);
 postfix();
 getch();
}
void postfix()
{
 int i,j=0;
 for(i=0;inf[i]!='\0';i++)
 {
  switch(inf[i])
  {
   case '+': while(st[top]>=1)
                  post[j++]=pop();
                 push(1);
                 break;
   case '-': while(st[top]>=1)
                  post[j++]=pop();
                 push(2);
                 break;
   case '*':while(st[top]>=3)
                  post[j++]=pop();
                 push(3);
                 break;
   case '/': while(st[top]>=3)
                  post[j++]=pop();
                 push(4);
                 break;
   case '^': while(st[top]>=4)
                  post[j++]=pop();
                 push(5);
                 break;
   case '(': push(0);
                 break;
   case ')': while(st[top]!=0)
                  post[j++]=pop();
                 top--;
                 break;
   default:post[j++]=inf[i];
  }
 }
 while(top>0)
 post[j++]=pop();
 printf("\nThe postfix expression is %s",post);
}
void push(int ele)
{
 top++;
 st[top]=ele;
}
char pop()
{
 int e1;
 char e;
 e1=st[top];
 top--;
 switch(e1)
 {
  case 1:e='+';
             break;
  case 2:e='-';
             break;
  case 3:e='*';
             break;
  case 4:e='/';
             break;
  case 5:e='^';
             break;
 }
 return (e);
}



SAMPLE OUTPUT:
                                     INFIX TO POSTFIX

Enter the infix expression: a+b*c/d-e

The postfix expression is abc*d/+e-

No comments:

Post a Comment