Sie sind auf Seite 1von 14

// (DS-1) PROGRAM TO IMPLEMENT BASIC OPERATIONS OF STACK

#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::push(int item)


{
if(top==(max-1))
cout<<"Overflow : stack is full";
else
{
top++;
elem[top]=item;
}
}

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

Choice 1) Push 2) Pop 3) Display 4) Exit =2


Underflow :Stack empty
Choice 1) Push 2) Pop 3) Display 4) Exit =1
Enter elemet to push =85
Choice 1) Push 2) Pop 3) Display 4) Exit =1
Enter elemet to push =64
Choice 1) Push 2) Pop 3) Display 4) Exit =1
Enter elemet to push =20
Choice 1) Push 2) Pop 3) Display 4) Exit =3
Contents of stack= 85 64 20
Choice 1) Push 2) Pop 3) Display 4) Exit =2
Element popped = 20
Choice 1) Push 2) Pop 3) Display 4) Exit =3
Contents of stack= 85 64
Choice 1) Push 2) Pop 3) Display 4) Exit =4

/*************************************************************/

// (DS-2) PROGRAM TO IMPLEMENT BASIC OPERATIONS OF SIMPLE QUEUES


#include<iostream.h>
#include<conio.h>
#include<process.h>
#define size 6
class queue
{
private:
int elem[size];
int front,rear;
public:
queue() {front=rear=-1;}
void insert(int);
void remove();
void display();
~queue() {cout<<"destroying simple queue";}
};
void queue::insert(int item)
{
if(rear==(size-1))
cout<<"Overflow : queue is full";
else if(rear==-1)
front=rear=0;
else if(rear<(size-1))
rear++;
elem[rear]=item;
}
void queue::remove()
{
if((front>=0)&&(front<size))
{
cout<<"Element removed = "<<elem[front];
front++;
}
else
cout<<"Underflow :Queue is empty";
}
void queue::display()
{
cout<<"\n contents of queue= ";
for(int i=front;i<=rear;i++)
cout<<"\n"<<elem[i]<<'\t';
}

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

Enter elemet to push =4


Choice 1) Insert 2) Remove 3) Display 4) Exit =1

Enter elemet to push =56


Choice 1) Insert 2) Remove 3) Display 4) Exit =1

Enter elemet to push =70


Choice 1) Insert 2) Remove 3) Display 4) Exit =3
Contents of queue=
4
56
70
Choice 1) Insert 2) Remove 3) Display 4) Exit =2
Element removed = 4
Choice 1) Insert 2) Remove 3) Display 4) Exit =3
Contents of queue=
56
70
Choice 1) Insert 2) Remove 3) Display 4) Exit =4

/**************************************************************************/
// (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

Choice 1) create/add node 2) Remove 3) Display 4) Exit =1


Enter info. part of new node=55

Choice 1) create/add node 2) Remove 3) Display 4) Exit =1


Enter info. part of new node=76

Choice 1) create/add node 2) Remove 3) Display 4) Exit =1


Enter info. part of new node=15

Choice 1) create/add node 2) Remove 3) Display 4) Exit =3


15 76 55
Choice 1) create/add node 2) Remove 3) Display 4) Exit =4

/**************************************************************************/
// (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 heap_sort(int a[])


{
int i,j,k,temp,val;
for(k=m;k>=2;k--)
{
temp = a[1];
a[1] = a[k];
a[k] = temp;
i = 1;
val = a[1];
j=2;
if ((j+1)<k)
{
if(a[j+1]>a[j])
j++;
}
while((j<=(k-1))&&(a[j]>val))
{
a[i] = a[j];
i = j;
j = 2*i;
if((j+1)<k)
{
if(a[j+1]>a[j])
{
j++;
}
else if(j>m)
j = m;
}
a[i] = val;
}
}
}

void display(int a[])


{
int i;
for(i=1;i<=m;i++)
cout<<"\n\n\t\t"<<a[i];
}

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

How many elements you want to insert: 5

Enter the elements:


38
79
99
1024
12

----------------
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 select_sort( int *);


int minimum(int a[], int i)
{
int mini, loc;
mini = a[i];
loc = i;
while ( i < m )
{
if (mini>a[i+1] )
{
mini = a[i+1];
loc = i+1;
}
i++;
}
return (loc);
}

void select_sort(int a[])


{
int temp,i=1 ,loc;
for (i=0;i<m;i++)
{
loc = minimum (a,i);
temp = a[i];
a[i] = a[loc];
a[loc] = temp;
}
}

void display(int a[])


{
int i;
for(i=0; i<m; i++)
cout<<"\n\n\t\tList "<<i<<" = "<<a[i];
}

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

How many elements you want to insert: 4

Enter the elements:


17
64
203
55
-------------------
List before sorting:
-------------------
List 0 = 17
List 1 = 64
List 2 = 203
List 3 = 55

-------------------
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 display(int a[])


{
int i;
for(i=0;i<m;i++)
cout<<"\n\n\t\tList "<<i<<" = "<<a[i];
}

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

How many elements you want to insert: 4

Enter the elements:


61
34
11
27

--------------------------
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

/*****************************************************/

Das könnte Ihnen auch gefallen