Sunday 23 December 2012

Determine ASCII value for a inputted key/character

/* compiler- Turbo c++ author- Mangilal Sharma --------------------------------------------------------*/
/* A program to determine ASCII value for a input key. */

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

int getkey(void);

void main()
{
 int x;
 clrscr();
 printf("Enter any Key: ");
 x=getkey();
 printf("\nASCII VALUE  OF ENTERED KEY IS: %d",x);
 getch();
}

int getkey(void)
{
 int key, lo, hi;
 key = bioskey(0);
 lo  = key & 0X00FF;
 hi  = (key & 0XFF00) >> 8;
 return((lo == 0) ? hi + 256 : lo);
}

 /*=========================30Nov.,2011=============================*/

Count number of characters, blanks, tabs, lines, numerals of a file

/* compiler- Turbo c++ author- Mangilal Sharma --------------------------------------------------------*/
/* C++ program to determine number of characters, blanks, tabs, lines, numerals of a file. */

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

void main()
{
 FILE *fp;
 char ch;
 int nol=0,not=0,nos=0,noc=0,non=0;
 clrscr();
 fp=fopen("data.txt","r");
 while(1)
 {
  ch=fgetc(fp);
  if(ch==EOF)
  break;
  noc++;
  if(ch==' ')
  nos++;
  if(ch=='\n')
  nol++;
  if(ch=='\t')
  not++;
  if(ch>=48 && ch<=57)
  non++;
 }
 fclose(fp);
 printf("\n Number of characters = %d",noc);
 printf("\n Number of blanks = %d",nos);
 printf("\n Number of tabs = %d",not);
 printf("\n Number of lines = %d",nol);
 printf("\n Number of Numerals = %d",non);
 getch();
}

 /*=========================24Nov.,2011=============================*/

Text File: data.txt

This program taken from http://world-of-c-programming.blogspot.com/
Check regualrly for new programs.
For testing-
some symbols: *&%$$^@!
some numbers: 23445673
some tab spaces: a b c

Output:


Matrix creation by using only one dimensional array

/* compiler- Turbo c++ author- Mangilal Sharma --------------------------------------------------------*/
/* Do work of matrix by using only one dimensional array */
#include <conio.h>
#include <stdio.h>
#include <iostream.h>

void main()
{
 clrscr();
 int m,a[100],n;
 cout<<"Enter no. of rows for matrix:";
 cin>>n;
 cout<<"Enter no. of columns for matrix:";
 cin>>m;
 cout<<"Enter the elements for matrix:\n";
 int k=0;
 for(int i=0;i<n;i++)
 {
  for(int j=0;j<m;j++)
  {
   cout<<"Enter element for matrix location "<<i<<","<<j<<" :";
   cin>>a[k++];
  }
 }
 k=0;
 for(i=0;i<n;i++)
 {
  for(int j=0;j<m;j++)
  {
   cout<<"matrix location for"<<i<<","<<j<<" :";
   cout<<"is"<<&a[k++];cout<<endl;
  }
 }
 int row;
 printf("Which row we show u:");
 scanf("%d",&row);
 k=(row-1)*m;
 {
  for(int j=0;j<m;j++)
  {
   cout<<a[k++];cout<<"\t";
  }
  cout<<endl;
 }
 getch();
}
 /*=========================19Nov.,2011=============================*/

Thursday 29 November 2012

Ticket window - Program Using Queue


/*Ticket window - Program Using Queue
compiler- Turbo c++     author- Mangilal Sharma
--------------------------------------------------------*/
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>

class ticket
{
 private:
 int q[5];
 int front, rear, n;  //n..to store number of elements
 public:
 void enqueue(int);
 void ticketchecker();
 void print();
 int isempty();
 void queuebuilder();
 ticket();
};

void ticket::enqueue(int a)
{
 q[rear] = a;
 rear++;
 n++;
 if (rear == 5)
 rear = 0;
 cout<<""<<a<<" Is Enqeueued....."<<endl;
}

void ticket::ticketchecker()
{
 clrscr();
 int not,rt;
 if ( !isempty() )
 {
  not = q[front];
  if (not > 2)
  {
   rt = not - 2;
   if ( rt > 0 )
   {
    enqueue(rt);
    front++;
    cout<<""<<not<<"# Tickets were demanded. \n2 Tickets aregiven.\nEnqueued again for next go to get remaining #"<<rt<<"Tickets"<<endl;
   }
   else if ( rt <= 0 )
   front++;
   n--;
  }
  else if ( not <= 2 )
  {
   front++;
   n--;
   cout<<""<<not<<"# tickets are given...."<<endl;
  }
 }
 else
 cout<<"!!! NO REQUEST IN THE QUEUE " <<endl;
 getch();
 clrscr();
 return;
}

void ticket::print()
{
 clrscr();
 int i = front;
 if ( n > 0 )
 {
  cout<<"REQUESTS ENQUEUED ARE :" ;
  do
  {
   cout<<q[i]<<"  ";
   i++;
   if ( i == 5 )
   i = 0;
  }
  while ( i != rear );
 }
 else
 cout<<"NO REQUEST IN QUEUE...QUEUE IS EMPTY ";
 getch();
 clrscr();
 return;
}

int ticket :: isempty()
{
 if ( n == 0 )
 return 1;
 else
 return 0;
}

//This Function takes input from the user and shows Queue graphically
void ticket::queuebuilder()
{
 int tickets, z = 0, i = 0, x = 20, y = 10, h = 17;
 char c;
 cout<<"Enter number of tickets to purchase... "<<endl;
 while (!(c == 'E' || c == 'e'))
 {
  if ( z > 4)
  {
   gotoxy(5,15);
   cout<<" ERROR!!! Queue Size Violation...Exiting Now  "<<endl;
   getch();
   clrscr();
   return;
  }
  else
  {
   cout<<"Enter : ";
   cin>>tickets;
   enqueue(tickets);
   z++;
   gotoxy(17,10);
   cout<<"->";
   gotoxy(20,9);
   cout<<"----------------------------------";
   gotoxy(x,y);
   cout<<q[i];
   x+=4;
   gotoxy(20,11);
   cout<<"----------------------------------";
   gotoxy(5,15);
   cout<<"PRESS C/c to CONTINUE E/e to END: ";
   cin>>c;
   h += 6;
   i++;
  }
  gotoxy(1,2);
 }
 return;
}

ticket::ticket()
{
 n = rear = front = 0;
 int b;
 do
 {
  clrscr();
  gotoxy(15,5);
  cout<<"Press 1................ENTER NO OF TICKETS";
  gotoxy(15,8);
  cout<<"Press 2................TICKET CHECKER";
  gotoxy(15,11);
  cout<<"Press 3................ALL TICKET REQUESTS";
  gotoxy(15,14);
  cout<<"Press 4................MAIN PAGE";
  gotoxy(15,17);
  cout<<"Press 5................EXIT FROM PROGRAM";
  gotoxy(15,20);
  cout<<"NOW ENTER : ";
  cin>>b;
  switch(b)
  {
   case 1:
clrscr();queuebuilder();break;
   case 2:
ticketchecker();break;
   case 3:
print();break;
   case 4:
return;
   case 5:
exit(1);
   default:
cout<<"\n\t\tWrong Choice, Enter Again\n";
  }
 }
 while(1);
}
/*=========================19Nov.,2010=============================*/

Spars matrix operations


/*A program to empelement spars matrix operations.
compiler- Turbo c++     author- Mangilal Sharma
--------------------------------------------------------*/
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>

class spars
{
 private:
 int m[4][4],i,j,choice;
 public:
 void usm();
 void lsm();
 void tdsm();
 spars();
};

void spars::usm()
{
 cout<<"Enter the elements for upper sparce matrix:\n";
 for(i=0;i<3;i++)
 {
  for(j=0; j<3; j++)
  if(i<j) m[i][j]=0;
  else cin>>m[i][j];
 }
 cout<<"\n\nUpper sparce matrix is:\n";
 for(i=0;i<3;i++)
 {
  for(j=0; j<3; j++)
  cout<<m[i][j]<<"\t";cout<<"\n";
 }
}

void spars::lsm()
{
 cout<<"\n\nEnter the elements for lower sparce matrix:\n";
 for(i=0;i<3;i++)
 {
  for(j=0; j<3; j++)
  if(i>j) m[i][j]=0;
  else cin>>m[i][j];
 }
 cout<<"\n\nLower sparce matrix is:\n";
 for(i=0;i<3;i++)
 {
  for(j=0; j<3; j++)
  cout<<m[i][j]<<"\t";cout<<"\n";
 }
}

void spars::tdsm()
{
 cout<<"\n\nEnter the elements for tridiogonal sparce matrix:\n";
 for(i=0;i<4;i++)
 {
  for(j=0; j<4; j++)
  if(i<j-1||j<i-1) m[i][j]=0;
  else cin>>m[i][j];
 }
 cout<<"\n\nTridiogonal sparce matrix is:\n";
 for(i=0;i<4;i++)
 {
  for(j=0; j<4; j++)
  cout<<m[i][j]<<"\t";
  cout<<"\n";
 }
}

spars::spars()
{
 do
 {
  cout<<"\n\t\t\tSPARS MATRIX\n";
  cout<<"\t\t1. Upper sparce matrix\n";
  cout<<"\t\t2. Lower sparce matrix\n";
  cout<<"\t\t3. Tridiogonal sparce matrix\n";
  cout<<"\t\t4. To Main page\n";
  cout<<"\t\t5. To Exit from Program\n";
  cout<<"\n\t\t Enter Your Choice= ";
  cin>>choice;
  switch(choice)
  {
   case 1:
   usm();break;
   case 2:
   lsm();break;
   case 3:
   tdsm();break;
   case 4:
 return;
   case 5:
 exit(1);
   default:cout<<"\n\t\tWrong Choice, Enter Again\n";
  }
 }while(1);
}
/*=========================3Nov.,2010=============================*/

Singly Linked List operations


/*Program to impelement Singly Linked List operations
compiler- Turbo c++     author- Mangilal Sharma
--------------------------------------------------------*/
#include <process.h>
#include <conio.h>
#include <iostream.h>

class singly
{
 struct s_node
 {
  int data;
  s_node *link;
 }*p;
 public:
 void inslast(int);
 void insbeg(int);
 void insnext(int,int);
 void delelement(int);
 void delbeg();
 void dellast();
 void disp();
 int search(int);
 singly();
 ~singly();      //destructor that deallocate the memory adderess
};

void singly::inslast(int x)
{
 s_node *q,*t;
 if(p==NULL)
 {
  p=new s_node;
  p->data=x;
  p->link=NULL;
 }
 else
 {
  q=p;
  while(q->link!=NULL)
  q=q->link;
  t=new s_node;
  t->data=x;
  t->link=NULL;
  q->link=t;
 }
 cout<<" Inserted successfully at the end...\n";
 disp();
}

void singly:: insbeg(int x)
{
 s_node *q;
 q=p;
 p=new s_node;
 p->data=x;
 p->link=q;
 cout<<" Inserted successfully at the begining...\n";
 disp();
}

void singly::delelement(int x)
{
 s_node *q,*r;
 q=p;
 if(q->data==x)
 {
  p=q->link;
  delete q;
  return;
 }
 r=q;
 while(q!=NULL)
 {
  if(q->data==x)
  {
   r->link=q->link;
   delete q;
   return;
  }
  r=q;
  q=q->link;
 }
 cout<<" Element u entered   "<<x<<"  that  is not found..";
}

void singly:: delbeg()
{
 cout<<" The list before deletion:";
 disp();
 s_node *q;
 q=p;
 if(q==NULL)
 {
  cout<<" No data is present..";
  return;
 }
 p=q->link;
 delete q;
 return;
}

void singly:: dellast()
{
 cout<<" The list before deletion:";
 disp();
 s_node *q,*t;
 q=p;
 if(q==NULL)
 {
  cout<<" There is no data in the list...";
  return;
 }
 if(q->link==NULL)
 {
  p=q->link;
  delete q;
  return;
 }
 while(q->link->link!=NULL)
 q=q->link;
 q->link=NULL;
 return;
}

singly::~singly()
{
 s_node *q;
 if(p==NULL) return;
 while(p!=NULL)
 {
  q=p->link;
  delete p;
  p=q;
 }
}

void singly::disp()
{
 s_node *q;
 q=p;
 if(q==NULL)
 {
  cout<<" No data is in the list..";
  return;
 }
 cout<<" The items present in the list are :";
 while(q!=NULL)
 {
  cout<<" "<<q->data;
  q=q->link;
 }
}

void singly::insnext(int value,int position)
{
 s_node *temp,*temp1;
 temp=p;
 if(temp1==NULL)
 {
  temp1= new s_node;
  temp1->data=value;
  temp1->link=NULL;
  p=temp1;
  return;
 }
 for(int i=0;((i<position)&&(temp->link!=NULL)) ;i++)
 {
  if(i==(position-1))
  {
   temp1= new s_node;
   temp1->data= value;
   temp1->link=temp->link;
   temp->link=temp1;
  }
  temp=temp->link;
 }
 cout<<" Inserted successfully at the position...\n"<<position;
 disp();
}

int singly::search(int value)
{
 s_node *temp;
 temp=p;
 int position=0;
 while(temp!=NULL)
 {
  if(temp->data==value)
  return position+1;
  else
  {
   temp=temp->link;
   position=position+1;
  }
 }
 cout<<" Element "<<value<<" not found";
 return 0;
}

singly::singly()
{
 int v,p,ps,choice;
 p=NULL;
 while(1)
 {
  cout<<"\n\t\tOperations on  linked List..\n";
  cout<<"\t\t1. To Insert an Element in  linked list\n";
  cout<<"\t\t2. To Delete an Element from  linked list\n";
  cout<<"\t\t3. To Display the Elements of  linked list\n";
  cout<<"\t\t4. To Search an element\n";
  cout<<"\t\t5. To Main menu\n";
  cout<<"\t\t6. To Exit from Programme\n";
  cout<<"\n\t\t Enter Your Choice= ";
  cin>>choice;
  switch(choice)
  {
   case 1:
   cout<<"1.Insertion at begining\n2.Insertion at the end\n";
   cout<<"3.Insertion after the mentioned position\n";
   cout<<" Enter ur choice:";
   cin>>ps;
   cout<<" Enter the value to insert:";
   cin>>v;
   switch(ps)
   {
    case 1:insbeg(v);break;
    case 2:inslast(v);break;
    case 3:cout<<" Enter the position to insert the value:";
  cin>>p;
  insnext(v,p);break;
   default:cout<<" The choice is invalid";return;
   }break;

   case 2:
   cout<<"1.Delete the first element\n2.Delete the last element\n";
   cout<<"3.Enter the element to delete from the list\n";
   cout<<" Enter ur choice:";
   cin>>ps;
   switch(ps)
   {
    case 1:delbeg();
    cout<<" The list after deletion:";disp();break;
    case 2:dellast();
    cout<<" The list after deletion:";disp();break;
    case 3:disp();
    cout<<" Enter the element to delete:";
    cin>>v;
    delelement(v);
    cout<<" The list after deletion:";disp();break;
    default:
    cout<<" The option is invalid...";break;
   }break;
   case 3:
   disp();break;
   case 4:
   disp();
   cout<<" Enter the element to search:";
   cin>>v;
   cout<<" The position of the element "<< v<<"  is "<<search(v);
   getch();break;
   case 5:return;
   case 6:exit(1);
   default:cout<<" The option is invalid...";return;
  }
 }
}
/*=========================9Nov.,2010=============================*/

Binary Search tree (BST) operations


/*Binary Search tree with insertion, deletion.....processes
compiler- Turbo c++     author- Mangilal Sharma
--------------------------------------------------------*/
# include <iostream.h>
# include <stdlib.h>
# include <conio.h>

struct bs_node
{
 int element;
 bs_node *left;
 bs_node *right;
 int height;
};

typedef struct bs_node *bs_nodeptr;

class bst
{
 public:
  bst();
      void insert(int,bs_nodeptr &);
      void del(int,bs_nodeptr &);
       int deletemin(bs_nodeptr &);
      void find(int,bs_nodeptr &);
bs_nodeptr findmin(bs_nodeptr);
bs_nodeptr findmax(bs_nodeptr);
      void copy(bs_nodeptr &,bs_nodeptr &);
      void makeempty(bs_nodeptr &);
bs_nodeptr bs_nodecopy(bs_nodeptr &);
      void preorder(bs_nodeptr);
      void inorder(bs_nodeptr);
      void postorder(bs_nodeptr);
       int bsheight(bs_nodeptr);
bs_nodeptr srl(bs_nodeptr &);
bs_nodeptr drl(bs_nodeptr &);
bs_nodeptr srr(bs_nodeptr &);
bs_nodeptr drr(bs_nodeptr &);
       int nobs_nodes(bs_nodeptr);
       int maxx(int value1,int value2)
       {
return ((value1 > value2) ? value1 : value2);
       }
};

void bst::insert(int x,bs_nodeptr &p)
{
 if (p == NULL)
 {
  p = new bs_node;
  p->element = x;
  p->left=NULL;
  p->right = NULL;
  p->height=0;
  if (p==NULL)
  cout<<"Out of Space";
 }
 else
 {
  if (x<p->element)
  {
   insert(x,p->left);
   if ((bsheight(p->left) - bsheight(p->right))==2)
   {
    if (x < p->left->element)
    p=srl(p);
    else
    p = drl(p);
   }
  }
  else if (x>p->element)
  {
   insert(x,p->right);
   if ((bsheight(p->right) - bsheight(p->left))==2)
   {
    if (x > p->right->element)
    p=srr(p);
    else
    p = drr(p);
   }
  }
  else
  cout<<"Element Exists";
 }
 int m,n,d;
 m=bsheight(p->left);
 n=bsheight(p->right);
 d=maxx(m,n);
 p->height = d + 1;
}

bs_nodeptr bst::findmin(bs_nodeptr p)
{
 if (p==NULL)
 {
  cout<<"Empty Tree";
  return p;
 }
 else
 {
  while(p->left !=NULL)
  p=p->left;
  return p;
 }
}

bs_nodeptr bst::findmax(bs_nodeptr p)
{
 if (p==NULL)
 {
  cout<<"Empty Tree";
  return p;
 }
 else
 {
  while(p->right !=NULL)
  p=p->right;
  return p;
 }
}

void bst::find(int x,bs_nodeptr &p)
{
 if (p==NULL)
 cout<<"Element not found";
 else
 if (x < p->element)
 find(x,p->left);
 else
 if (x>p->element)
 find(x,p->right);
 else
 cout<<"Element found !";
}

void bst::copy(bs_nodeptr &p,bs_nodeptr &p1)
{
 makeempty(p1);
 p1 = bs_nodecopy(p);
}

void bst::makeempty(bs_nodeptr &p)
{
 bs_nodeptr d;
 if (p != NULL)
 {
  makeempty(p->left);
  makeempty(p->right);
  d=p;
  free(d);
  p=NULL;
 }
}

bs_nodeptr bst::bs_nodecopy(bs_nodeptr &p)
{
 bs_nodeptr temp;
 if (p==NULL) return p;
 else
 {
  temp = new bs_node;
  temp->element = p->element;
  temp->left = bs_nodecopy(p->left);
  temp->right = bs_nodecopy(p->right);
  return temp;
 }
}

void bst::del(int x,bs_nodeptr &p)
{
 bs_nodeptr d;
 if (p==NULL)
 cout<<"Element not found";
 else if ( x < p->element)
 del(x,p->left);
 else if (x > p->element)
 del(x,p->right);
 else if ((p->left == NULL) && (p->right == NULL))
 {
  d=p;
  free(d);
  p=NULL;
  cout<<" Element deleted !";
 }
 else if (p->left == NULL)
 {
  d=p;
  free(d);
  p=p->right;
  cout<<" Element deleted !";
 }
 else if (p->right == NULL)
 {
  d=p;
  p=p->left;
  free(d);
  cout<<" Element deleted !";
 }
 else
 p->element = deletemin(p->right);
}

int bst::deletemin(bs_nodeptr &p)
{
 int c;
 cout<<"inside deltemin";
 if (p->left == NULL)
 {
  c=p->element;
  p=p->right;
  return c;
 }
 else
 {
  c=deletemin(p->left);
  return c;
 }
}

void bst::preorder(bs_nodeptr p)
{
 if (p!=NULL)
 {
  cout<<p->element<<"-->";
  preorder(p->left);
  preorder(p->right);
 }
}

void bst::inorder(bs_nodeptr p)
{
 if (p!=NULL)
 {
  inorder(p->left);
  cout<<p->element<<"-->";
  inorder(p->right);
 }
}

void bst::postorder(bs_nodeptr p)
{
 if (p!=NULL)
 {
  postorder(p->left);
  postorder(p->right);
  cout<<p->element<<"-->";
 }
}

int bst::bsheight(bs_nodeptr p)
{
 int t;
 if (p == NULL) return -1;
 else
 {
  t = p->height;
  return t;
 }
}

bs_nodeptr bst:: srl(bs_nodeptr &p1)
{
 bs_nodeptr p2;
 p2 = p1->left;
 p1->left = p2->right;
 p2->right = p1;
 p1->height = maxx(bsheight(p1->left),bsheight(p1->right)) + 1;
 p2->height = maxx(bsheight(p2->left),p1->height) + 1;
 return p2;
}

bs_nodeptr bst:: srr(bs_nodeptr &p1)
{
 bs_nodeptr p2;
 p2 = p1->right;
 p1->right = p2->left;
 p2->left = p1;
 p1->height = maxx(bsheight(p1->left),bsheight(p1->right)) + 1;
 p2->height = maxx(p1->height,bsheight(p2->right)) + 1;
 return p2;
}

bs_nodeptr bst:: drl(bs_nodeptr &p1)
{
 p1->left=srr(p1->left);
 return srl(p1);
}

bs_nodeptr bst::drr(bs_nodeptr &p1)
{
 p1->right = srl(p1->right);
 return srr(p1);
}

int bst::nobs_nodes(bs_nodeptr p)
{
 int count=0;
 if (p!=NULL)
 {
  nobs_nodes(p->left);
  nobs_nodes(p->right);
  count++;
 }
 return count;
}

bst::bst()
{
 clrscr();
 bs_nodeptr root,root1,minn,maxx;
 int a,choice,findele,delele,leftele,rightele,flag;
 root =NULL;
 root1=NULL;
 while(1)
 {
  cout<<"\n\tOperations on Binary Search tree..\n";
  cout<<"\t\t1.  Insertion\n";
  cout<<"\t\t2.  FindMin\n";
  cout<<"\t\t3.  FindMax\n";
  cout<<"\t\t4.  Find\n";
  cout<<"\t\t5.  Copy\n";
  cout<<"\t\t6.  Delete\n";
  cout<<"\t\t7.  Preorder\n";
  cout<<"\t\t8.  Inorder\n";
  cout<<"\t\t9.  Postorder\n";
  cout<<"\t\t10. Height\n";
  cout<<"\t\t11. To main page\n";
  cout<<"\t\t12. Exit\n";
  cout<<"\n\t\t      Enter Your Choice= ";
  cin>>choice;
  switch(choice)
  {
   case 1:
 cout<<"Enter new bs_nodes value:";
 cin>>a;
 insert(a,root);
 break;
   case 2:
 if (root !=NULL)
 {
  minn=findmin(root);
  cout<<"Min element : "<<minn->element;
 }
 break;
   case 3:
 if (root !=NULL)
 {
  maxx=findmax(root);
  cout<<"Max element : "<<maxx->element;
 }
 break;
   case 4:
 cout<<"Enter Search bs_node :";
 cin>>findele;
 if (root != NULL)
 find(findele,root);
 break;
   case 5:
 copy(root,root1);
 inorder(root1);
 break;
   case 6:
 cout<<"Enter bs_node that u want to delete:";
 cin>>delele;
 del(delele,root);
 inorder(root);
 break;
   case 7:
 cout<<" Preorder travers... :";
 preorder(root);
 break;
   case 8:
 cout<<" Inorder travers.... :";
 inorder(root);
 break;
   case 9:
 cout<<" Postorder travers... :";
 postorder(root);
 break;
   case 10:
 cout<<" Height and Depth is ";
 cout<<bsheight(root);
 cout<<"\nNo. of bs_nodes is "<<nobs_nodes(root);
 break;
   case 11:
  return;
   case 12:
  exit(1);
   default:
  cout<<"\n\t\tWrong Choice, Enter Again\n";
  }
 }
}
/*=========================14Nov.,2010=============================*/

Priority Queue operations


/*Program to empelement Priority Queue operations
compiler- Turbo c++     author- Mangilal Sharma
--------------------------------------------------------*/
#include<iostream.h>
#include<conio.h>
#include<process.h>

class pqueue
{
 int front,rear,choice;
 public:
 struct data
 {
  int val,p,o;
 }d[MAX];

 void insert();
 void deletion();
 void display();
 pqueue();
};

void pqueue::insert()
{
 data d1;
 if(rear==MAX-1)
 cout<<"Priority Queue is Full";
 else
 {
  cout<<"Enter Value ";
  cin>>d1.val;
  cout<<"Enter Priority ";
  cin>>d1.p;
  cout<<"Enter Order ";
  cin>>d1.o;
  rear++;
  d[rear]=d1;
  if(front==-1)
  front=0;
  data temp;
  for(int i=front;i<=rear;i++)
  for(int j=i+1;j<=rear;j++)
  {
   if(d[i].p > d[j].p)
   {
    temp=d[i];
    d[i]=d[j];
    d[j]=temp;
   }
   else
   {
    if(d[i].p==d[j].p)
     if(d[i].o > d[j].o)
     {
      temp=d[i];
      d[i]=d[j];
      d[j]=temp;
     }
   }
  }
 }
}

void pqueue::deletion()
{
 if(front==-1)
 cout<<"Priority Queue is Empty";
 else
 {
  cout<<"deleted followings...\n";
  cout<<"Value = "<<d[front].val<<endl;
  cout<<"Priority = "<<d[front].p<<endl;
  cout<<"Order = "<<d[front].o<<endl;
  if(front==rear)
  front=rear=-1;
  else
  front++;
 }
}

void pqueue::display()
{
 if(front==-1)
 cout<<"Priority Queue is Empty";
 else
 {
  for(int i=front;i<=rear;i++)
  {
   cout<<"Object  :"<<i+1<<endl;
   cout<<"Value ="<<d[i].val<<endl;
   cout<<"Priority="<<d[i].p<<endl;
   cout<<"Order =  "<<d[i].o<<endl<<endl;
  }
 }
}

pqueue::pqueue()
{
 front=rear=-1;
 while(1)
 {
  int choice;
  cout<<"\n\t\t\t    PRIORITY QUEUE\n";
  cout<<"\t\t1. To Insert an Element in priority Queue\n";
  cout<<"\t\t2. To Delete an Element from priority Queue\n";
  cout<<"\t\t3. To Display the Elements of priority Queue\n";
  cout<<"\t\t4. To Main menu\n";
  cout<<"\t\t5. To Exit from Programme\n";
  cout<<"\n\t\t Enter Your Choice= ";
  cin>>choice;
  switch(choice)
  {
   case 1:
   insert();break;
   case 2:
   deletion();break;
   case 3:
   display();break;
   case 4:
 return;
   case 5:
 exit(1);
   default:
  cout<<"\n\t\tWrong Choice, Enter Again\n";
  }
 }
}
/*=========================8Nov.,2010=============================*/

Doubly Linked List operations


/*Program to impelement Doubly Linked List operations
compiler- Turbo c++     author- Mangilal Sharma
--------------------------------------------------------*/
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>

class doubly
{
 struct d_node
 {
  int data;
  d_node *rnext;
  d_node *lnext;
 }*new1,*head,*tail,*ptr,*temp;
 public:
 void creation();
 void insertion();
 void deletion();
 void display();
 doubly();
};

void doubly :: creation()
{
 if(head==NULL)
 {
  new1=new d_node[sizeof(d_node)];
  new1->rnext=NULL;
  new1->lnext=NULL;
  cout<<"enter No. of nodes:";
  cin>>new1->data;
  head=new1;
  tail=new1;
  head->rnext=tail;
  head->lnext=tail;
  tail->rnext=head;
  tail->lnext=head;
 }
 else
 cout<<"Creation done only once  !";
}

void doubly :: insertion()
{
 int i,pos;
 new1=new d_node[sizeof(d_node)];
 new1->rnext=NULL;
 new1->lnext=NULL;
 cout<<"enter number:";
 cin>>new1->data;
 cout<<"enter position you want to insert:";
 cin>>pos;
 if(pos==1)
 {
  new1->rnext=head;
  head=new1;
  tail->lnext=head;
  tail->rnext=head;
  head->lnext=tail;
 }
 else
 {
  i=1;
  temp=head;
  while(i < pos-1 && temp->rnext!=tail)
  {
   i++;
   temp=temp->rnext;
  }
  if(temp->rnext==tail)
  {
   new1->rnext=tail->rnext;
   tail->rnext=new1;
   new1->lnext=tail;
   tail=new1;
   head->lnext=tail;
  }
  else
  {
   new1->rnext=temp->rnext;
   new1->lnext=temp;
   temp->rnext=new1;
   new1->rnext->lnext=new1;
  }
 }
}

void  doubly::deletion()
{
 int pos,i;
 cout<<"Enter Position you want to Delete ?";
 cin>>pos;
 if(pos==1 && head!=tail)
 {
  ptr=head;
  head=head->rnext;
  head->lnext=tail;
  tail->rnext=head;
  delete ptr;
 }
 else
 {
  i=1;
  temp=head;
  while(i < pos-1 && temp->rnext!=tail)
  {
   i++;
   temp=temp->rnext;
  }
  if(temp->rnext!=tail)
  {
   ptr=temp->rnext;
   temp->rnext=ptr->rnext;
   ptr->rnext->lnext=ptr->lnext;
   delete ptr;
  }
  else
  {
   if(temp->rnext==tail && head!=tail)
   {
    ptr=tail;
    tail=temp;
    tail->rnext=head;
    head->lnext=tail;
    delete ptr;
   }
   else
   {
    head=NULL;
    tail=NULL;
    delete head;
    delete tail;
   }
  }
 }
}

void doubly::display()
{
 int ch;
 cout<<"1.forward traverse\n2.backward traverse";
 cout<<"Enter your choice<1/2>?  :";
 cin>>ch;
 switch(ch)
 {
  case 1:
  if(head!=NULL)
  {
   temp=head;
   while(temp!=tail)
   {
    cout<<temp->data<<"    ";
    temp=temp->rnext;
   }
   if(temp==tail)
   cout<<temp->data;
  }
  break;

  case 2 :
  if(tail!=NULL)
  {
   temp=tail;
   while(temp!=head)
   {
    cout<<temp->data<<"    ";
    temp=temp->lnext;
   }
   if(temp==head)
   cout<<temp->data;
  }
  break;
 }
}

doubly::doubly()
{
 head=tail=NULL;
 int choice;
 while(1)
 {
  cout<<"\n\t\tOperations on Doubly linked List..\n";
  cout<<"\t\t1. To Create a linked list\n";
  cout<<"\t\t2. To Insert an Element from  linked list\n";
  cout<<"\t\t3. To Delete the Elements of  linked list\n";
  cout<<"\t\t4. To Display the elements\n";
  cout<<"\t\t5. To Main menu\n";
  cout<<"\t\t6. To Exit from Programme\n";
  cout<<"\n\t\t Enter Your Choice= ";
  cin>>choice;
  switch(choice)
  {
   case 1 : creation();break;
   case 2 : insertion();break;
   case 3 : deletion();break;
   case 4 : display();break;
   case 5 : return;
   case 6 : exit(1);
   }
 }
}
/*=========================10Nov.,2010=============================*/

Deapth First Search (DFS) graph traversal


/*Program to create a graph and use Deapth First Search(DFS)
compiler- Turbo c++     author- Mangilal Sharma
--------------------------------------------------------*/
#include<conio.h>
#include<iostream.h>
#include<stdlib.h>

/*struct node  // Structure for elements in the graph
{
 int data,status;
 struct node *next;
 struct link *adj;
};
struct link  // Structure for adjacency list
{
 struct node *next;
 struct link *adj;
};*/

class dfs
{
 public:
 void create();  // For creating a graph
 dfs();
 struct node *start,*p,*q;
 struct link *l,*k;
};

void dfs::create()    // Creating a graph
{
 int dat,flag=0;
 start=NULL;
 cout<<"Enter the nodes in the graph(0 to end): ";
 while(1)
 {
  cin>>dat;
  if(dat==0) break;
  p=new node;
  p->data=dat;
  p->status=0;
  p->next=NULL;
  p->adj=NULL;
  if(flag==0)
  {
   start=p;
   q=p;
   flag++;
  }
  else
  {
   q->next=p;
   q=p;
  }
 }
 p=start;
 while(p!=NULL)
 {
  cout<<"Enter the links to "<<p->data<<" (0 to end) : ";
  flag=0;
  while(1)
  {
   cin>>dat;
   if(dat==0) break;
   k=new link;
   k->adj=NULL;
   if(flag==0)
   {
    p->adj=k;
    l=k;
    flag++;
   }
   else
   {
    l->adj=k;
    l=k;
   }
   q=start;
   while(q!=NULL)
   {
    if(q->data==dat)
    k->next=q;
    q=q->next;
   }
  }
  p=p->next;
 }
 cout<<"-------------------------";
 return;
}

// Deapth First Search(DFS) Traversal.
void dfs::dfs()
{
 char ch='y';
 while(ch=='y'||ch=='Y')
 {
  clrscr();
  create();
  int stack[25],top=1;
  cout<<"Deapth First Search Results";
  cout<<"---------------------------";
  p=start;
  while(p!=NULL)
  {
   p->status=0;
   p=p->next;
  }
  p=start;
  stack[0]=0;
  stack[top]=p->data;
  p->status=1;
  while(1)
  {
   if(stack[top]==0) break;
   p=start;
   while(p!=NULL)
   {
    if(p->data==stack[top]) break;
    p=p->next;
   }
   cout<<stack[top]<<"  ";
   top--;
   k=p->adj;
   while(k!=NULL)
   {
    q=k->next;
    if(q->status==0)
    {
     top++;
     stack[top]=q->data;
     q->status=1;
    }
    k=k->adj;
   }
  }
  cout<<"\n\nEnter Y/y For again enter:";
  cin>>ch;
 }return;
}
/*=========================17Nov.,2010=============================*/