Beruflich Dokumente
Kultur Dokumente
#include<iostream.h>
#include<conio.h>
#include<process.h>
#define max 6
class stack
{
private:
int elem[max];
int top;
public:
stack() {top=-1;}
void push(int);
void pop();
void display();
~stack() {cout<<"destroying stack";}
};
void stack::pop()
{
if(top==-1)
cout<<"Underflow :Stack empty";
else
{
cout<<"Element popped = "<<elem[top];
top--;
}
}
void stack::display()
{
cout<<"\n contents of stack= ";
for(int i=0;i<=top;i++)
cout<<elem[i]<<'\t';
}
void main()
{
clrscr();
stack s1;
int choice,val;
while(1)
{
cout<<"\n Choice\t 1.Push\t2.Pop\t 3.Display\t 4.Exit =";
cin>>choice;
switch(choice)
{
case 1:cout<<"\n Enter elemet to push =";
cin>>val;
s1.push(val);
break;
case 2:s1.pop();
break;
case 3:s1.display();
break;
case 4:exit(-1);
}
}
}
/*************************************************************/
OUTPUT
/*************************************************************/
int main()
{
clrscr();
queue q1;
int choice,val;
while(1)
{
cout<<"\n Choice\t 1.Insert\t2.Remove\t 3.Display\t 4.Exit =";
cin>>choice;
switch(choice)
{
case 1:cout<<"\n Enter elemet to push =";
cin>>val;
q1.insert(val);
break;
case 2:q1.remove();
break;
case 3:q1.display();
break;
case 4:exit(0);
}
}
}
/**************************************************************************/
OUTPUT
Choice 1) Insert 2) Remove 3) Display 4) Exit =2
Underflow :Queue is empty
Choice 1) Insert 2) Remove 3) Display 4) Exit =1
/**************************************************************************/
// (DS-3) PROGRAM TO CREATE AND DISPLAY A LINKED LIST
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
#include<process.h>
struct node
{
int info;
node* next;
};
class lnklst
{
private:
node* start;
public:
lnklst() {start=NULL;}
void create();
void remove();
void display();
~lnklst();
};
void lnklst::create()
{
node* ptr;
int x;
ptr=new node;
cout<<"Enter info. part of new node=";
cin>>x;
ptr->info=x;
ptr->next=start;
start=ptr;
cout.flush();
}
void lnklst::remove()
{
if(start==NULL)
cout<<"Underflow";
else
{
int y;
cout<<"\n Enter info.part of node to be deleted= ";
cin>>y;
if(start->info==y)
{
cout<<"Deleting first node";
node *save=start;
start=start->next;
delete save;
}
else
{
node *prev,*ptr;
prev=start;
ptr=start->next;
while(ptr!=NULL)
{
if(ptr->info==y)
{
prev->next=ptr->next;
delete ptr;
}
else
{
prev=ptr;
ptr=ptr->next;
}
}
}
}
}
void lnklst::display()
{
node *ptr=start;
while(ptr!=NULL)
{
cout<<ptr->info<<'\t';
ptr=ptr->next;
}
}
lnklst::~lnklst()
{
node *ptr=start;
cout<<"destroying........linked list\n";
while(ptr!=NULL)
{
ptr=ptr->next;
delete start;
start=ptr;
}
}
void main()
{
clrscr();
lnklst r1;
int choice;
while(1)
{
cout<<"\n Choice\t 1.create/add node\t2.Remove\t 3.Display\t 4.Exit =";
cin>>choice;
switch(choice)
{
case 1:r1.create();
break;
case 2:r1.remove();
break;
case 3:r1.display();
break;
case 4:exit(-1);
}
}
}
/**************************************************************************/
OUTPUT
/**************************************************************************/
// (DS-4) PROGRAM TO PERFORM HEAP SORT
#include<iostream.h>
#include<conio.h>
int m;
void heap_sort(int *);
void create_heap(int *);
void display(int *);
void create_heap(int a[])
{
int k,j,i,temp;
for(k=2;k<=m;k++)
{
i=k;
temp = a[k];
j = i/2 ;
while((i>1)&&(temp>a[j]))
{
a[i] = a[j];
i=j;
j = i/2;
if(j < 1)
j = 1;}
a[i]=temp;
}
}
void main()
{
int a[20];
int i;
clrscr();
cout<<"\n\n How many elements you want to insert: ";
cin>>m;
cout<<"\n\nEnter the elements:";
for (i=1; i<=m; i++)
{
cout<<"\n ",i;
cin>>a[i];
}
cout<<"\n\n\t ------------\n\t The list is:\n\t ------------";
display(a);
create_heap(a);
cout<<"\n\n\t ------------\n\t The heap is:\n\t ------------";
display(a);
heap_sort(a);
cout<<"\n\n\t -----------------------\n\t The list after sorting:\n\t -----------------------\n";
display(a);
getch();
}
/**********************************************************/
OUTPUT
----------------
The list is:
----------------
38
79
99
1024
12
-----------------
The heap is:
-----------------
1024
99
79
38
12
-----------------------------
The list after sorting:
-----------------------------
12
38
79
99
1024
/**********************************************************/
// (DS-5) PROGRAM TO PERFORM SELECTION SORT
#include<iostream.h>
#include<conio.h>
int m;
int minimum( int *, int);
void main()
{
int a[20];
int i;
clrscr();
cout<<"\n\n How many elements you want to insert: ";
cin>>m;
cout<<"\n\n Enter the elements:\n";
for (i=0;i<m;i++)
{
cin>>a[i];
}
cout<<"\n\n\n\t -------------------";
cout<<"\n\t List before sorting:";
cout<<"\n\t -------------------";
display(a);
select_sort(a);
cout<<"\n\n\n\t -------------------";
cout<<"\n\t List after sorting:";
cout<<"\n\t -------------------";
display(a);
getch();
}
/****************************************************************/
OUTPUT
-------------------
List after sorting:
-------------------
List 0 = 17
List 1 = 55
List 2 = 64
List 3 = 203
/****************************************************************/
// (DS-6) PROGRAM TO PERFORM INSERTION SORT
#include<iostream.h>
#include<conio.h>
int m;
void insertion_sort(int *);
void display(int *);
void insertion_sort(int a[])
{
int ele,i;
for(i=1;i<m;i++)
{
ele = a[i];
while(ele<a[i-1] && i>0)
{
a[i]=a[i-1];
i--;
}
a[i]=ele;
}
}
void main()
{
int a[20];
int i;
clrscr();
cout<<"\n\n How many elements you want to insert: ";
cin>>m;
cout<<"\n\nEnter the elements:\n";
for(i=0;i<m;i++)
{
cin>>a[i];
}
cout<<"\n\n\n\t -------------------";
cout<<"\n\t List before sorting:";
cout<<"\n\t -------------------";
display(a);
insertion_sort(a);
cout<<"\n\n\n\t -------------------";
cout<<"\n\t List after sorting:";
cout<<"\n\t -------------------";
display(a);
getch();
}
/*****************************************************/
OUTPUT
--------------------------
List before sorting:
--------------------------
List 0 = 61
List 1 = 34
List 2 = 11
List 3 = 27
------------------------
List after sorting:
------------------------
List 0 = 11
List 1 = 27
List 2 = 34
List 3 = 61
/*****************************************************/