.

Posts Tagged ‘c and cpp’

C++ Program for Bubble sort

// December 28th, 2008 // 1 Comment » // C and C++

C++ Program for Bubble sort.

/**************************************************************	
	Author: Arun Vishnu M V
	Web: www.arunmvishnu.com
	Description: C++ Program for Bubble sort
***************************************************************/
#include<iostream.h>
#include<conio.h>
void main()
{
	int array[100],n,i,j,temp;
	clrscr();
	cout<<"How many numbers--> ";
	cin>>n;
	cout<<"Enter "<<n<<" numbers\n";
	for(i=0;i<n;i++)
		cin>>array[i];
	for(i=0;i<n;i++)
	{
		for(j=0;j<n-1;j++)
			if(array[j]>array[j+1])
			{
				temp=array[j];
				array[j]=array[j+1];
				array[j+1]=temp;
			}
	}
	cout<<"\nArray is sorted in ascending order.\n";
	for(i=0;i<n;i++)
		cout<<array[i]<<"   ";
	getch();
}
  • Share/Bookmark

C++ Program Program for Binary Search.

// December 28th, 2008 // 1 Comment » // C and C++

C++ Program Program for Binary Search.

/**************************************************************	
	Author: Arun Vishnu M V
	Web: www.arunmvishnu.com
	Description: C++ Program Program for Binary Search.
***************************************************************/
#include&lt;iostream.h&gt;
#include&lt;conio.h&gt;
#include&lt;process.h&gt;
void main()
{
	int ar[100],beg,mid,end,i,n,search;
	clrscr();
	cout<<"How many numbers in the array: ";
	cin>>n;
	cout<<"Enter "<<n<<" numbers in ascending order --> ";
	for(i=0;i<n;i++)
		cin>>ar[i];
	beg=0;
	end=n-1;
	cout<<"Enter a number to search: ";
	cin>>search;
	while(beg<=end)
	{
		mid=(beg+end)/2;
		if(ar[mid]==search)
		{
			cout<<"\nItem found at position "<<(mid+1);
			getch();
			exit(0);
		}
		if(search>ar[mid])
			beg=mid+1;
		else
			end=mid-1;
	}
	cout<<"\nSorry! "<<search<<" doesnot found.";
	getch();
}
  • Share/Bookmark

C++ Program to implement a stack using linked list

// November 28th, 2008 // 3 Comments » // C and C++

C++ Program to implement a stack using linked list.

/**************************************************************	
	Author: Arun Vishnu M V
	Web: www.arunmvishnu.com
	Description: C++ Program to implement a stack using linked list.
***************************************************************/
#include&lt;conio.h&gt;   	
#include&lt;iostream.h&gt; 
#include&lt;process.h&gt;  
#include&lt;malloc.h&gt;  
 
 
//   Creating a NODE Structure
struct node
{
   int data;
   struct node *next;
};
 
// Creating a class STACK
class stack
{
   struct node *top;
   public:
      stack() // constructure
      {
	 top=NULL;
      }
      void push(); // to insert an element
      void pop();  // to delete an element
      void show(); // to show the stack
};
// PUSH Operation
void stack::push()
{
   int value;
   struct node *ptr;
   cout<<"\nPUSH Operation\n";
   cout<<"Enter a number to insert: ";
   cin>>value;
   ptr=new node;
   ptr->data=value;
   ptr->next=NULL;
   if(top!=NULL)
      ptr->next=top;
   top=ptr;
   cout<<"\nNew item is inserted to the stack!!!";
   getch();
}
 
// POP Operation
void stack::pop()
{
   struct node *temp;
   if(top==NULL)
   {
      cout<<"\nThe stack is empty!!!";
      getch();
      return;
   }
   temp=top;
   top=top->next;
   cout<<"\nPOP Operation........\nPoped value is "<<temp->data;
   delete temp;
   getch();
}
 
// Show stack
void stack::show()
{
   struct node *ptr1=top;
   cout<<"\nThe stack is\n";
   while(ptr1!=NULL)
   {
      cout<<ptr1->data<<" ->";
      ptr1=ptr1->next;
   }
   cout<<"NULL\n";
   getch();
}
 
// Main function
int main()
{
   clrscr();
   stack s;
   int choice;
   while(1)
   {
      cout<<"\n-----------------------------------------------------------";
      cout<<"\n\t\tSTACK USING LINKED LIST\n\n";
      cout<<"1:PUSH\n2:POP\n3:DISPLAY STACK\n4:EXIT";
      cout<<"\nEnter your choice(1-4): ";
      cin>>choice;
      switch(choice)
      {
       case 1:
	  s.push();
	  break;
       case 2:
	  s.pop();
	  break;
       case 3:
	  s.show();
	  break;
       case 4:
	  exit(0);
	  break;
       default:
	  cout<<"Please enter correct choice(1-4)!!";
	  getch();
	  break;
       }
   }
   return 0;
}
 
//---------------------- END--------------------
  • Share/Bookmark

C++ Program implement a stack using an array

// November 28th, 2008 // 2 Comments » // C and C++

C++ Program implement a stack using an array.

/**************************************************************
	Author: Arun Vishnu M V
	Web: www.arunmvishnu.com
	Description: C++ Program implement a stack using an array
***************************************************************/
#include<conio.h>
#include<iostream.h>
#include<process.h>  

void main()
{
	int stack[150],top,n,i,max_stack,choice,element,n_pop;
	max_stack=150;
	cout<<"How many elements are in the stack: ";
	cin>>n;
	cout<<"Enter "<>stack[top];
	cout<<"\nStack is implemented.\nThe stack is\n";
	for(i=top-1;i>=0;i--)
		cout<>choice;
		switch(choice)
		{
			case 1:     //  PUSH
				cout<<"\nPUSH OPERATION\n";
				if(top>=max_stack)
            {
					cout<<"Stack is full!";
					getch();
					break;
				}
				cout<<"Enter an element: ";
				cin>>element;
				stack[top]=element;
            top++;
				cout<<"Item Inserted!";
				getch();
				break;
			case 2:             // POP
				cout<<"POP OPERATION\n";
				if(top<0)
				{
					cout<<"Stack is empty";
					getch();
					break;
				}
				cout<<"How many elements you want to pop: ";
            cin>>n_pop;
				if(n_pop>top)
				{
					cout<<"\nError!\nStack is small.";
					getch();
               break;
				}
				top=top-n_pop;
				cout<<"Items POPed";
				getch();
				break;
			case 3:       //  Display
				cout<<"\The stack is\n";
				for(i=top-1;i>=0;i--)
					cout<
  • Share/Bookmark

C++ Program to add and subtract 2 sparse matrices

// November 28th, 2008 // No Comments » // C and C++

C++ Program to add and subtract 2 sparse matrices.

/**************************************************************	
	Author: Arun Vishnu M V
	Web: www.arunmvishnu.com
	Description: C++ Program to add and subtract 2 sparse matrices
***************************************************************/
#include&lt;conio.h&gt;   	
#include&lt;iostream.h&gt; 
#include&lt;process.h&gt;  
 
int main()
{
   clrscr();
   int sparse1[10][3],sparse2[10][3],sum[10][3],diff[10][3];
   int m,n,p,q,t1,t2,s,d,element;
   int i,j;
   cout<<"Enter the number of rows and columns : ";
   cin>>m>>n;
   t1=t2=0;
 
   cout<<"\nEnter the first matrix("<<m<<"*"<<n<<"):\n";
   for(i=1;i<=m;i++)
   {
      for(j=1;j<=n;j++)
      {
	 cin>>element;
	 if(element!=0)
	 {
	    t1=t1+1;
	    sparse1[t1][1]=i;
	    sparse1[t1][2]=j;
	    sparse1[t1][3]=element;
	 }
      }
   }
   sparse1[0][1]=m;
   sparse1[0][2]=n;
   sparse1[0][3]=t1;
   cout<<"\nEnter the second matrix("<<m<<"*"<<n<<"):\n";
   for(i=1;i<=m;i++)
   {
      for(j=1;j<=n;j++)
      {
	 cin>>element;
	 if(element!=0)
	 {
	    t2=t2+1;
	    sparse2[t2][1]=i;
	    sparse2[t2][2]=j;
	    sparse2[t2][3]=element;
	 }
      }
   }
   sparse2[0][1]=m;
   sparse2[0][2]=n;
   sparse2[0][3]=t2;
 
   // displaying the first sparse matrix
   cout<<"\n\nThe first sparse matrix is :\n\nRow\tColumn\tElement";
   cout<<"\n-----------------------\n";
   for(i=0;i<=t1;i++)
   {
      cout<<sparse1[i][1]<<"\t"<<sparse1[i][2]<<"\t"<<sparse1[i][3]<<"\n";
   }
    // displaying the second sparse matrix
   cout<<"\n\nThe second sparse matrix is :\n\nRow\tColumn\tElement";
   cout<<"\n-----------------------\n";
   for(i=0;i<=t2;i++)
   {
      cout<<sparse2[i][1]<<"\t"<<sparse2[i][2]<<"\t"<<sparse2[i][3]<<"\n";
   }
 
   // Addition and subtraction
   i=j=s=d=1;
   while((i<=t1)&&(j<=t2))
   {
      if(sparse1[i][1]==sparse2[j][1])    // if rows are equal
      {
	 if(sparse1[i][2]==sparse2[j][2]) // if columns are equal
	 {
	    sum[s][1]=diff[d][1]=sparse1[i][1];
	    sum[s][2]=diff[d][2]=sparse1[i][2];
	    sum[s][3]=sparse1[i][3]+sparse2[j][3];
	    diff[d][3]=sparse1[i][3]-sparse2[j][3];
	    i++;
	    j++;
	    if(sum[s][3]!=0)
	       s++;
	    if(diff[d][3]!=0)
	       d++;
	 }
	 else   // if columns are not equal
	 {
	    if(sparse1[i][2]<sparse2[j][2])
	    {
	       sum[s][1]=diff[d][1]=sparse1[i][1];
	       sum[s][2]=diff[d][2]=sparse1[i][2];
	       sum[s][3]=diff[d][3]=sparse1[i][3];
	       i++;
	       s++;
	       d++;
	    }
	    else
	    {
	       sum[s][1]=diff[d][1]=sparse2[j][1];
	       sum[s][2]=diff[d][2]=sparse2[j][2];
	       sum[s][3]=sparse2[j][3];
	       diff[d][3]=0-sparse2[j][3];
	       j++;
	       d++;
	       s++;
	    }
	 }
      }
      else   //   if rows are not equal
      {
	 if(sparse1[i][1]<sparse2[j][1])
	 {
	    sum[s][1]=diff[d][1]=sparse1[i][1];
	    sum[s][2]=diff[d][2]=sparse1[i][2];
	    sum[s][3]=diff[d][3]=sparse1[i][3];
	    i++;
	    d++;
	    s++;
	 }
	 else
	 {
	    sum[s][1]=diff[d][1]=sparse2[j][1];
	    sum[s][2]=diff[d][2]=sparse2[j][2];
	    sum[s][3]=sparse2[j][3];
	    diff[d][3]=0-sparse2[j][3];
	    j++;
	    s++;
	    d++;
	 }
      }
   }  // end of while
   if(i<=t1)
   {
      for(p=i;p<=t1;p++)
      {
	 sum[s][1]=diff[d][1]=sparse1[p][1];
	 sum[s][2]=diff[d][2]=sparse1[p][2];
	 sum[s][3]=diff[d][3]=sparse1[p][3];
	 s++;
	 d++;
      }
   }
   else if(j<=t2)
   {
      for(p=j;p<=t2;p++)
      {
	 sum[s][1]=diff[d][1]=sparse2[p][1];
	 sum[s][2]=diff[d][2]=sparse2[p][2];
	 sum[s][3]=sparse2[p][3];
	 diff[d][3]=0-sparse2[j][3];
	 s++;
	 d++;
      }
   }
  // end of addition and subtraction
  sum[0][1]=diff[0][1]=m;
  sum[0][2]=diff[0][2]=n;
  sum[0][3]=s-1;
  diff[0][3]=d-1;
 
  // displaying the sum matrix
   cout<<"\n\nThe sum is :\n\nRow\tColumn\tElement";
   cout<<"\n-----------------------\n";
   for(i=0;i<s;i++)
   {
      cout<<sum[i][1]<<"\t"<<sum[i][2]<<"\t"<<sum[i][3]<<"\n";
   }
    // displaying the difference matrix
   cout<<"\n\nThe difference is :\n\nRow\tColumn\tElement";
   cout<<"\n-----------------------\n";
   for(i=0;i<d;i++)
   {
      cout<<diff[i][1]<<"\t"<<diff[i][2]<<"\t"<<diff[i][3]<<"\n";
   }
   getch();
   return 0;
}
//---------------------- END--------------------
  • Share/Bookmark

C++ Program to find the transpose of a Sparse matrix

// November 28th, 2008 // No Comments » // C and C++

C++ Program to find the transpose of a Sparse matrix.

/**************************************************************	
	Author: Arun Vishnu M V
	Web: www.arunmvishnu.com
	Description: C++ Program to find the transpose of a Sparse matrix 
***************************************************************/
#include<conio.h?  	
#include<iostream.h? 
#include<process.h?  
 
void main()
{
   clrscr();
   int sparse[10][10],transpose[10][10];
   int m,n,p,q,t,col,element;
   int i,j;
   cout<<"Enter the number of rows and columns : ";
   cin>>m>>n;
   t=0;
	// assigning the value of matrix
 
   cout<<"\nEnter the matrix:\n";
   for(i=1;i<=m;i++)
   {
      for(j=1;j<=n;j++)
      {
	 cin>>element;
	 if(element!=0)
	 {
	    t=t+1;
	    sparse[t][1]=i;
	    sparse[t][2]=j;
	    sparse[t][3]=element;
	 }
      }
   }
   cout<<"\n\nThe sparse matrix is :\n\nRow\tColumn\tElement";
 
// displaying the matrix of non-zero value
   cout<<"\n\n"<<m<<"\t"<<n<<"\t"<<t<<"\n\n";
   for(i=1;i<=t;i++)
   {
      cout<<sparse[i][1]<<"\t"<<sparse[i][2]<<"\t"<<sparse[i][3]<<"\n";
   }
   sparse[0][1]=n; sparse[0][2]=m; sparse[0][3]=t;
   q=1;
 
// transpose of the matrix
   if(t>0)
   {
      for(i=1;i<=n;i++)
      {
	 for(j=1;j<=t;j++)
	 {
	    if(sparse[j][2]==i)
	    {
		transpose[q][1]=sparse[j][2];
		transpose[q][2]=sparse[j][1];
		transpose[q][3]=sparse[j][3];
		q=q+1;
	    }
	 }
      }
   }
 
   cout<<"\n\nThe transpose of the sparse matrix :\n ";
   cout<<"\nRow\tColumn\tElement\n\n";
   cout<<sparse[0][1]<<"\t"<<sparse[0][2]<<"\t"<<sparse[0][3]<<"\n\n";
   for(i=1;i<=t;i++)
   {
      cout<<transpose[i][1]<<"\t"<<transpose[i][2]<<"\t"<<transpose[i][3]<<"\n";
   }
   getch();
}
//---------------------- END--------------------
  • Share/Bookmark

C++ Program to implement a single linked list

// November 28th, 2008 // 1 Comment » // C and C++

C++ Program to implement a single linked list and Perform the following operations.

/**************************************************************	
	Author: Arun Vishnu M V
	Web: www.arunmvishnu.com
	Description: C++ Program to implement a single linked list and Perform the following operations
		 1: Insertioin
		 2: Deletion
         3: Display
***************************************************************/
#include<conio.h>   	
#include<iostream.h> 
#include<process.h>  
 
//   Creating a NODE Structure
struct node
{
	int data;  // data
	struct node *next,*start; //  link to next node and previous node
};
 
// Creating a class LIST
class list
{
	struct node *start;
	public:
		void create(); // to create a list
		void insert();  // insertion
		void del();    // deletion
		void show();   // show
};
// Creating a new node
void list::create()
{
	struct node *nxt_node,*pre_node;
	int value,no,i;
	start=nxt_node=pre_node=NULL;
	cout<<"\nCREATING A NEW LIST.........\n\nHow many nodes : ";
	cin>>no;
	for(i=1;i<=no;i++)
	{
		cout<<"Enter "<<no<<" DATAs: ";
		cin>>value;
		nxt_node=new node;
		if(pre_node!=NULL)
			pre_node->next=nxt_node;
		if(start==NULL)
		  start=nxt_node;
		nxt_node->data=value;
		nxt_node->next=NULL;
		pre_node=nxt_node;
	}
	cout<<"\nThe list is created!";
	show();
}
 
// Displaying LIST
void list::show()
{
	int data;
	struct node *ptr=start;
	cout<<"\n\nThe List is \n";
	while(ptr!=NULL)
	{
		data=ptr->data;
		cout<<data<<" -> ";
		ptr=ptr->next;
	}
	cout<<"\b\b\b   ";
	getch();
}
 
// Insert node at any position
void list::insert()
{
	int position,dat;
	struct node *ptr_b,*ptr_f,*ptr,*nxt_node,*pre_node;
	cout<<"\nInsertion of a new node \n";
	cout<<"Enter the DATA after which the new node is to be inserted.\n";
	cout<<"[ If the data is not found the new node will be";
	cout<<" created at first ]\n\t->:  ";
	cin>>position;
	cout<<"Enter the data to insert: ";
	cin>>dat;
	ptr_b=start;
	ptr=new node;
	ptr->data=dat;
	if(start!=NULL)
	{
		while(ptr_b->next!=NULL && ptr_b->data!=position)
		{
			ptr_b=ptr_b->next;
		}
	}
	else
	{
		clrscr();
		start=nxt_node=pre_node=NULL;
		nxt_node=new node;
		if(pre_node!=NULL)
			pre_node->next=nxt_node;
		if(start==NULL)
		  start=nxt_node;
		nxt_node->data=dat;
		nxt_node->next=NULL;
		pre_node=nxt_node;
		return;
	}
	if(ptr_b->next==NULL && ptr_b->data!=position)
	//Insertion at first
	{
		ptr->next=start;
		start=ptr;
	}
	else if(ptr_b->next==NULL && ptr_b->data==position)
	//insertion at the end of list
	{
		ptr_b->next=ptr;
		ptr->next=NULL;
 	}
	else
	//Insertion between two nodes
	{
		ptr_f=ptr_b->next;
		ptr->next=ptr_b->next;
		ptr_b->next=ptr;
	}
	cout<<"\nNew node is inserted!!";
	getch();
}  //End of insertion
 
//Delete node from any position
void list::del()
{
	int position,dat;
	struct node *ptr_b,*ptr_f,*ptr,*pntr;
	cout<<"Enter the data to be beleted: ";
	cin>>position;
	ptr=start;
	while(ptr->next!=NULL && ptr->data!=position)
	{
		pntr=ptr;
		ptr=ptr->next;
	}
	if(ptr->next==NULL && ptr->data!=position)
	//Data not found
	{
		cout<<"\nData not found!!";
		getch();
		return;
	}
	else if(ptr->next==NULL && ptr->data==position)
	//Deletion from the end of list
	{
		ptr_b=pntr;
		if(pntr->next)//  Only one node
			start=NULL;
		dat=ptr->data;
		ptr_b->next=NULL;
	}
	else
	//Deletion between two nodes or first node
	{
		dat=ptr->data;
		if(start==ptr)  // delete first node
		{
			start=ptr->next;
			ptr_f=start;
		}
		else // Deletion between two nodes
		{
			dat=ptr->data;
			ptr_b=pntr;
			ptr_b->next=ptr->next;
			//ptr_f=ptr_b->next;
		}
	}
	delete ptr;
	cout<<"\nThe node is deleted!!\nData= "<<dat;
	getch();
}  //End of deletion
 
 
// Main function
int main()
{
	clrscr();
	list l;
	l.create(); // to create a new node
	int choice;
	while(1)
	{
		cout<<"\n-------------------------------------------";
		cout<<"\n\t\t LINKED LIST\n\n";
		cout<<"1:Insertion\n2:Deletion\n3:Display List\n4:Exit";
		cout<<"\nEnter your choice(1-4): ";
		cin>>choice;
		switch(choice)
		{
		 case 1:  // Insertion
			l.insert();
			 break;
		 case 2:   // Deletion
			l.del();
			break;   
		 case 3:  // Display
			l.show();
			break;
		 case 4:  // Exit
			exit(0);
			break;
		 default:
			cout<<"Please enter correct choice(1-4)!!";
		 getch();
		 break;
		}
	}
	return 0;
}
//---------------------- END--------------------
  • Share/Bookmark

C++ Program for Selection sort

// November 28th, 2008 // No Comments » // C and C++

C++ Program for Selection sort .

/**************************************************************	
	Author: Arun Vishnu M V
	Web: www.arunmvishnu.com
	Description: C++ Program for Selection sort .
***************************************************************/
#include<conio.h>   	
#include<iostream.h> 
#include<process.h>  
 
void main()
{
	int array[100],n,i,j,temp;
	clrscr();
	cout<<"How many numbers--> ";
	cin>>n;
	cout<<"Enter "<<n<<" numbers\n";
	for(i=0;i<n;i++)
		cin>>array[i];
	for(i=0;i<n;i++)
	{
		for(j=i+1;j<n;j++)
			if(array[i]>array[j])
			{
				temp=array[i];
				array[i]=array[j];
				array[j]=temp;
			}
	}
	cout<<"\nArray is sorted in ascending order.\n";
	for(i=0;i<n;i++)
		cout<<array[i]<<"   ";
	getch();
}
//---------------------- END--------------------
  • Share/Bookmark

C++ Program for QUICK SORT

// November 28th, 2008 // 2 Comments » // C and C++

C++ Program for QUICK SORT.

/**************************************************************	
	Author: Arun Vishnu M V
	Web: www.arunmvishnu.com
	Description: C++ Program for QUICK SORT.
***************************************************************/
#include<conio.h>  	
#include<iostream.h> 
#include<process.h>  
 
void quickSort(int numbers[], int array_size);
void q_sort(int numbers[], int left, int right);
 
int numbers[150];
 
int main()
{
   clrscr();
   int i,n;
   cout<<"How many numbers you want to sort: ";
   cin>>n;
   cout<<"Enter "<<n<<" numbers.\n";
   for (i = 0; i<n; i++)
      cin>>numbers[i];
//perform quick sort on array
   q_sort(numbers,0,n-1);
 
   cout<<"Numbers are sorted\n";
   for (i = 0; i<n; i++)
      cout<<numbers[i]<<"   ";
   getch();
   return(0);
}
 
// Function to sort
void q_sort(int numbers[], int left, int right)
{
   int pivot, l_hold, r_hold;
   l_hold = left;
   r_hold = right;
   pivot = numbers[left];
   while (left < right)
   {
      while ((numbers[right] >= pivot) && (left < right))
      right--;
      if (left != right)
      {
	 numbers[left] = numbers[right];
	 left++;
      }
      while ((numbers[left] <= pivot) && (left < right))
	 left++;
      if (left != right)
      {
	  numbers[right] = numbers[left];
	  right--;
      }
   }
   numbers[left] = pivot;
   pivot = left;
   left = l_hold;
   right = r_hold;
   if (left < pivot)
      q_sort(numbers, left, pivot-1);
   if (right > pivot)
      q_sort(numbers, pivot+1, right);
}
 
 
//---------------------- END--------------------
  • Share/Bookmark

C++ Program to implement a QUEUE using linked list

// November 28th, 2008 // 1 Comment » // C and C++

C++ Program to implement a QUEUE using linked list.

/**************************************************************	
	Author: Arun Vishnu M V
	Web: www.arunmvishnu.com
	Description: C++ Program to implement a QUEUE using linked list.
***************************************************************/
#include<conio.h>   	
#include<iostream.h> 
#include<process.h>  
#include<malloc.h>  
 
//   Creating a NODE Structure
struct node
{
   int data;
   struct node *next;
};
 
// Creating a class QUEUE
class queue
{
   struct node *frnt,*rear;
   public:
      queue() // constructure
      {
	 frnt=rear=NULL;
      }
      void insert(); // to insert an element
      void del();  // to delete an element
      void show(); // to show the stack
};
// Insertion
void queue::insert()
{
   int value;
   struct node *ptr;
   cout<<"\nInsertion\n";
   cout<<"Enter a number to insert: ";
   cin>>value;
   ptr=new node;
   ptr->data=value;
   ptr->next=NULL;
   if(frnt==NULL)
      frnt=ptr;
   else
      rear->next=ptr;
   rear=ptr;
   cout<<"\nNew item is inserted to the Queue!!!";
   getch();
}
 
// Deletion
void queue::del()
{
   if(frnt==NULL)
   {
      cout<<"\nQueue is empty!!";
      getch();
      return;
   }
   struct node *temp;
   temp=frnt;
   frnt=frnt->next;
   cout<<"\nDeletion Operation........\nDeleted value is "<<temp->data;
   delete temp;
   getch();
}
 
// Show Queue
void queue::show()
{
   struct node *ptr1=frnt;
   if(frnt==NULL)
   {
      cout<<"The Queue is empty!!";
      getch();
      return;
   }
   cout<<"\nThe Queue is\n";
   while(ptr1!=NULL)
   {
      cout<<ptr1->data<<" ->";
      ptr1=ptr1->next;
   }
   cout<<"END";
   getch();
}
 
// Main function
int main()
{
   clrscr();
   queue q;
   int choice;
   while(1)
   {
      cout<<"\n-----------------------------------------------------------";
      cout<<"\n\t\tQUEUE USING LINKED LIST\n\n";
      cout<<"1:INSERTION\n2:DELETION\n3:DISPLAY QUEUE\n4:EXIT";
      cout<<"\nEnter your choice(1-4): ";
      cin>>choice;
      switch(choice)
      {
       case 1:
	  q.insert();
	  break;
       case 2:
	  q.del();
	  break;
       case 3:
	  q.show();
	  break;
       case 4:
	  exit(0);
	  break;
       default:
	  cout<<"Please enter correct choice(1-4)!!";
	  getch();
	  break;
       }
   }
   return 0;
}
 
//---------------------- END--------------------
  • Share/Bookmark