You are on page 1of 54

STACK USING LINKED LIST

# include<iostream.h>
# include<conio.h>
# include<stdlib.h>
struct node
{
int data;
struct node *next;
};

class stack
{
struct node *top;
public:
stack()
{
top=NULL;
}
void push();
void pop();
void show();
};

void stack::push()
{
int value;
struct node *ptr;
cout<<"\nPUSH \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;
getch();
}

void stack::pop()
{
struct node *temp;
if(top==NULL)
{
cout<<"\nThe stack is empty!!!";
getch();
return;
}
temp=top;
top=top->next;
cout<<"\nPOP \nPoped value is "<<temp->data;
delete temp;
getch();
}

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();
}

int main()
{
clrscr();
stack s;
int choice;

cout<<"\n\t\tSTACK USING LINKED LIST\n\n";


cout<<"1:PUSH\n2:POP\n3:DISPLAY STACK\n4:EXIT";
while(1)
{
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;
}
QUEUE USING LINKED LIST

#include<conio.h>
#include<iostream.h>
#include<process.h>
#include<malloc.h>
struct node
{
int data;
struct node *next;
};
class queue
{
struct node *frnt,*rear;
public:
queue()
{
frnt=rear=NULL;
}
void insert();
void del();
void show();
};
void queue::insert()
{
int value;
struct node *ptr;
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;
getch();
}

void queue::del()
{
if(frnt==NULL)
{
cout<<"\nQueue is empty!!";
getch();
return;
}
struct node *temp;
temp=frnt;
frnt=frnt->next;
cout<<"\nDeletion \nDeleted value is "<<temp->data;
delete temp;
getch();
}

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;
}
getch();
}

int main()
{
clrscr();
queue q;
int choice;
cout<<"\n-----------------------------------------------";
cout<<"\n\t\tQUEUE USING LINKED LIST\n\n";
cout<<"1:INSERTION\n2:DELETION\n3:DISPLAY
QUEUE\n4:EXIT";
while(1)
{ cout<<"\nEnter ur choice:\n";
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;
}
APPLICATION OF STACK1- INFIX TO POSTFIX
CONVERSION

# include<iostream.h>
# include<conio.h>
# include<string.h>
# include<stdio.h>
# include<stdlib.h>
class convert
{
public:
char infix[50],postfix[50];
int stack[50];
int top;
void push(int);
int pop();
int evalpost();
void intopost();
char precchk(char);
};
void main()
{ int ch,p;
convert obj;
clrscr();
while(1)
{
printf("enter ur choice(1/0\n");
scanf("%d",&ch);
if(ch==1)
{
printf("enter infix expression\n");
scanf("%s",obj.infix);
obj.intopost();
printf("postfix expression is %s\n",obj.postfix);
}
else
exit(0);
}
}
void convert ::intopost()
{
int i,p=0,t,pr,l;
char next;
convert obj;
top=0;
stack[top]='#';
l=strlen(infix);
infix[l]='#';
for(i=0;infix[i]!='#';i++)
{
switch(infix[i])
{
case '(':push(infix[i]);
break;
case ')':while((next=pop())!='(')
postfix[p++]=next;
break;
case '+':
case '-':
case '*':
case '/':
case '%':
case '^':
pr=obj.precchk(infix[i]);
while(stack[top]!='#'&&pr<=precchk(stack[top]))
postfix[p++]=pop();
push(infix[i]);
break;
default:
postfix[p++]=infix[i];
}
}
while(stack[top]!='#')
postfix[p++]=pop();
postfix[p]='\0';
}
char convert::precchk(char symbol)
{
switch(symbol)
{
case '(': return 0;
case '+':
case '-': return 1;
case '*':
case '/':
case '%':
return 2;
case '^': return 3;
}
}

void convert::push(int symbol)


{
if(top>50)
{
printf("stack overflow");
exit(0);
}
else
{
top++;
stack[top]=symbol;
}
}
int convert::pop()
{
if(top==-1)
{
printf("stack underflow");
exit(0);
}
else
return (stack[top--]);
}

BINARY SEARCH TREE


#include<iostream.h>
#include<stdlib.h>
# include<malloc.h>
# include<conio.h>
struct node
{
int data;
struct node *left;
struct node *right;
};

struct node *tree=NULL;


struct node *insert(struct node *tree,int ele);

void preorder(struct node *tree);


void inorder(struct node *tree);
void postorder(struct node *tree);
void del(int);

void main()
{
int ch,ele,x;
clrscr();
cout<<" Menu\n";
cout<<"1.Insert\n";
cout<<"2.PreOrder Traversal\n";
cout<<"3.InOrder Traversal\n";
cout<<"4.PostOrder Traversal\n";
cout<<"5.Deletion\n";
do
{
cout<<"\n\tEnter Ur Choice::";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\n\tEnter the element::";
cin>>ele;
tree=insert(tree,ele);
break;

case 2:
cout<<"\n\t****PreOrder Traversal****\n";
preorder(tree);
break;

case 3:
cout<<"\n\t****InOrder Traversal****\n";
inorder(tree);
break;

case 4:
cout<<"\n\t\****PostOrder Traversal****\n";
postorder(tree);
break;
case 5:cout<<"Enter the element to be deleted\n";
cin>>x;
del(x);
break;
case 6:
exit(0);
}
}while(ch!=5);
}

struct node *insert(struct node *tree,int ele)


{

if(tree==NULL)
{
tree=(struct node *)malloc(sizeof(struct node));
tree->left=tree->right=NULL;
tree->data=ele;

}
else
if(tree->data>=ele)
{
tree->left=insert(tree->left,ele);

}
else
{
tree->right=insert(tree->right,ele);
}
return(tree);
}

void preorder(struct node *tree)


{
if(tree!=NULL)
{
cout<<tree->data;
preorder(tree->left);
preorder(tree->right);
getch();
}
}

void inorder(struct node *tree)


{
if(tree!=NULL)
{
inorder(tree->left);
cout<<tree->data;
inorder(tree->right);
getch();
}
}

void postorder(struct node *tree)


{
if(tree!=NULL)
{
postorder(tree->left);
postorder(tree->right);
cout<<tree->data;
getch();
}
}
void del(int x)
{ struct node *ptr=tree,*parent=NULL,*t1,*t2,*temp;
while(ptr!=NULL &&ptr->data!=x)
{
parent=ptr;
if(x<ptr->data)
ptr=ptr->left;
else
ptr=ptr->right;
}
if(ptr==NULL)
{
cout<<"elt not found";
return;
}
if(ptr->left==NULL)
t1=ptr->right;
else
if(ptr->right==NULL)
t1=ptr->left;
else
{
t2=ptr;
t1=ptr->right;
temp=t1->left;
while(temp!=NULL)
{
t2=t1;
t1=temp;
temp=t1->left;
}
if(t2!=ptr)
{
t2->left=t1->right;
t1->right=ptr->right;
}
t1->left=ptr->left;
}
if(parent==NULL)
tree=t1;
else
{
if(parent->left==ptr)parent->left=t1;
else
parent->right=t1;
}
free(ptr);
preorder(tree);
}
SINGLY LINKED LIST

# include<iostream.h>
# include<conio.h>
# include<malloc.h>
# include<stdlib.h>
void create(int d);
void addaf(int d,int pos);
void addbeg(int d);
void del(int d);
void dis();
struct node
{
int info;
struct node *link;
}*start;
void main()
{
int ch,n,m,pos,i;
clrscr();
start=NULL;

while(1)
{
cout<<"Enter ur choice";
cin>>ch;
switch(ch)
{
case 1:
cout<<"enter n\n";
cin>>n;
for(i=1;i<=n;i++)
{
cout<<"enter elts\n";
cin>>m;
create(m);
}
break;
case 2:
cout<<"enter elt\n";
cin>>m;
addbeg(m);
break;
case 3:
cout<<"enter the elt and pos\n";
cin>>m>>pos;
addaf(m,pos);
break;
case 4:
if(start==NULL)
{
cout<<"empty";
continue;
}
cout<<"enter elt for del\n";
cin>>m;
del(m);
break;
case 5: dis();
break;
case 6:
exit(1);
}
}
}
void create(int d)
{
struct node *q,*tmp;
tmp=new node;
tmp->info=d;
tmp->link=NULL;
if(start==NULL)
start=tmp;
else
{
q=start;
while(q->link!=NULL)
q=q->link;
q->link=tmp;;
}
}
void addbeg(int d)
{ struct node *tmp;
tmp->info=d;
tmp->link=start;
start=tmp;
}
void addaf(int d,int pos)
{
struct node *tmp,*q;
int i;
q=start;
for (i=0;i<pos-1;i++)
{
q=q->link;
if(q==NULL)
{
return;
}
}
tmp=new node;
tmp->info=d;
tmp->link=q->link;
q->link=tmp;
}
void del(int d)
{
struct node *tmp,*q;
if(start->info==d)
{
tmp=start;
start=start->link;
free(tmp);
return;
}
q=start;
while(q->link!=NULL)
{
if(q->link->info==d)
{
tmp=q->link;
q->link=tmp->link;
free(tmp);
return;
}
q=q->link;
}
if(q->link->info==d)
{
tmp=q->link;
free(tmp);
return;
}
cout<<"elt not found\n";
}
void dis()
{
struct node *q;
if(start==NULL)
{
cout<<"empty";
return;
}
q=start;
while(q!=NULL)
{
cout<<q->info;
q=q->link;
}
}

QUICK SORT

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class sort
{
private:
int a[100];
int n;
public:
void getdata()
{

cout<<"Enter the no of items";


cin>>n;

for(int i=0;i<n;i++)
{
cout<<"Enter the item\n";
cin>>a[i];
}
}
void dosort()
{
qsort(a,0,n-1);
}
int partition(int a[],int p,int r)
{
int i=p-1,j,t;
for(j=p;j<r;j++)
{
if(a[j]<a[r])
{
i++;
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
t=a[i+1];
a[i+1]=a[r];
a[r]=t;
return(i+1);
}
void qsort(int a[],int p,int r)
{
int q;
if(p<r)
{
q=partition(a,p,r);
qsort(a,p,q-1);
qsort(a,q+1,r);
}
}
void display()
{
cout<<"\nsorted items\n";
for(int i=0;i<n;i++)
{
cout<<a[i]<<endl;
}
}

};
void main()
{
clrscr();
sort s;
s.getdata();
s.dosort();
s.display();
getch();
}
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class sample
{
public:
virtual void display()
{
cout<<"Inside base class";
}
};
class der:public sample
{
public:
void display()
{
cout<<"Inside derived class";
}
};
void main()
{
clrscr();
sample *ptr;
sample ob1;
der ob2;
ptr=&ob1;
ptr->display();
ptr=&ob2;
ptr->display();
getch();
}
# include<iostream.h>
# include<conio.h>
# include<stdlib.h>
void area(float);
void area(float,float);
void area(int,int);
void main()
{
int ch;
int x,y;
float l,b;
clrscr();
while(1)
{
cout<<"Enter ur choice\n";
cin>>ch;
switch(ch)
{
case 1:cout<<"enter the a value";
cin>>l;
cout<<"Area of a square:"<<l*l;
break;
case 2:cout<<"enter the base and ht:";
cin>>l>>b;
cout<<"Area of a triangle:"<<0.5*l*b;
break;
case 3:cout<<"enter the len and b";
cin>>x>>y;
cout<<"Area of a rectangele:"<<x*y;
break;
case 4:exit(0);
break;
}
}
getch();
}
#include<iostream.h>
#include<conio.h>
#include"cursor.h"
void main()
{
int pl,x;
clrscr();
initialise();
display();
cout<<"enter the pos to insert";
cin>>pl;
cout<<"enter the element";
cin>>x;
insert(x,pl);
display();
getch();
}
//#include<iostream.h>
# include<stdio.h>
#include<conio.h>
struct node
{
int data;
int next;
};
int ptr,pos,list,x;
struct node c[10];
void initialise()
{
int i;
for(i=0;i<=9;i++)
{
c[i].next=i+1;
c[i].data=0;
}
c[9].next=-1;
}
void display()
{
int i;
for(i=0;i<=9;i++)
{
printf("%d %d",c[i].data,c[i].next);
printf("/n");
//cout<<(c[i].data<<c[i].next);
//cout<< endl;
}
}
void insert(int x,int p)
{
int temp=1;
c[temp].data=x;
c[temp].next=c[p].next;
c[p].next=temp;
}
# include<iostream.h>
# include<conio.h>
class consdes
{
int x,y;
public:
consdes()
{
cout<<"Default constructor is invoked\n";
x=0;
y=0;
}
consdes(int a,int b)
{
cout<<"parameterized constructor is invoked\n";
x=a;
y=b;
}
~consdes()
{
cout<<"destructor is invoked";
}
void show()
{
cout<<x<<y;
}
};
void main()
{
int a1,b1;
clrscr();
cout<<"Enter two values\n";
cin>>a1>>b1;
consdes ob1;
ob1.show();
consdes ob2(a1,b1);
ob2.show();
getch();
}
#include <iostream.h>
class frnd
{
private:
int a,b;
public:
frnd(int x,int y)
{
a=x;
b=y;
}
void show()
{
cout<<"The values are:\n";
cout<<a<<"\t"<<b;
}
friend void compute(frnd ob)
};

void compute(frnd ob)


{ int s;

s=ob.a+ob.b;
cout<<"The sum is:"<<s;

void main()
{
int x,y;
cout<<"Enter two values:";
cin>>x>>y;
frnd ob(x,y);
ob.show();
cout<<"The result is:";
compute(ob);
}

HEAP SORT
#include<iostream.h>
#include<conio.h>
#define swap(a,b,t)((t)=(a),(a)=(b)(b)=(t))
#define pivot_index x(begin+(end-begin)/2
class sort
{
private:
int a[100];
int n;
public:
void getdata()
{
cout<<"enter the number of items"<<endl;
cin>>n;
for(int i=0;i<n;i++)
{
cout<<"enter the item ["<<i<<"]";
cin>>a[i];
}
}
void dosort()
{
for(int i=n;i>1;i--)
{
heap sort(a,i-1);
}
}
void heapsort(int arr[],int arr_urbound)
{
int i,o,t;
int lchild,rchild,mchild,root,temp;
root=(arr-urbound-1)/2;
for(o=root;i>0;o--)
{
for(i=root;i>=0;i--)
{
lchild=(2*i)+1;
rchild=(2*i)+2;
if(lchild<=arr_urbound)&&((rchild<=arr_urbound))
{
if(arr[rchild]>=arr[lchild])
mchild=rchild;
else
mchild=lchild;
}
else
{
if(rchild>arr_urbound)
mchild=lchild;
else
mchild=rchild;
}
if(arr[i]<<arr[mchild])
{
swap(arr[i],arr[mchild],t);
}
}
}
swap(arr[0],arr[arr_urbound],t);
}
void display()
{
cout<<"sorted items"<<endl;
for(int i=0;i<n;i++)
{
cout<<a[i]<<endl;
}
}
};
void main()
{
clrscr();
sorts;
s.get data();
s.dosort();
s.display();
getch();
}
TOWER OF HANOI
#include<iostream.h>
#include<conio.h>
void main()
{
void hanoi(char,char,char,int);
char t1='A',t2='B',t3='c';
int n;
clrscr();
cout<<"Enter the no of disk on tower A:";
cin>>n;
if(n<1)
{
cout<<"\nNothing to move";
}
else
hanoi(t1,t2,t3,n);
getch();
}
void hanoi (char t1,char t2,char t3,int n)
{
static int step=0;
step++;
cout<<"t1,t2,t3,n\n";
if(n==1)
{
cout<<"\nMove top disk from tower";
return;
}
hanoi(t1,t2,t3,n-1);
cout<<"t1,t2,t3,n\n";
cout<<"\nMove top disk from tower,t1,t2";
cout<<"t1,t2,t3,n\n";
hanoi(t3,t2,t1,n-1);
cout<<"t1,t2,t3,n,step";
}
QUEUE USING ARRAY

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#define max 5
class queue
{
int q[max];
int front,rear;
public:
queue()
{
front=-1;
rear=-1;
}
void push();
void pop();
void show();
};
void queue::push()
{
int x;
if(rear==max-1)
cout<<"The queue is full";
else
{
cout<<"\nEnter the value to push";
cin>>x;
if(front==-1)
front=0;
q[++rear]=x;
}
}
void queue::pop()
{ if(front==rear)
cout<<"Queue is empty";
else
{
int y;
y=q[front];
cout<<"\nPopped out element is"<<y;

cout<<"\n";
front++;
}}
void queue::show()
{
int i;
for(i=front;i<=rear;i++)
cout<<q[i];
}
void main()
{
clrscr();
int ch;
queue obj;
cout<<"Menu"<<endl;
cout<<"1.Push"<<endl;
cout<<"2.Pop"<<endl;
cout<<"3.Display"<<endl;
cout<<"4.Quit"<<endl;
while(1)
{
cout<<"\nEnter your choice\n";
cin>>ch;
switch(ch)
{
case 1:obj.push();
break;
case 2:obj.pop();
break;
case 3:obj.show();
break;
case 4:exit(0);
}
}}
QUADRATIC EQUATION

#include<iostream.h>
#include<conio.h>
#include<math.h>
class quard
{
float a,b,c,r1,r2,d;
public:void eqn();
void call();
};
void quard::eqn()
{
cout<<"\n Quadratic Equation:";
cout<<"\n ax^2+bx+c=0";
cout<<"\nEnter the values of a,b,c";
cin>>a>>b>>c;
d=(b*b)-(4*a*c);
}
void quard::call()
{
if(d<0)
{
cout<<"\nRoots are imaginary";
r1=(-b/2*a);
r2=(sqrt(-d))/(2*a);
cout<<"\nroot1="<<r1<<"+i"<<r2;
cout<<"\nroot2="<<r1<<"-i"<<r2;
}
else if(d>0)
{
cout<<"\nRoots are equal";
r1=(-b+sqrt(d))/(2*a);
r2= (-b-sqrt(d))/(2*a);
cout<<"\nroot1="<<r1<<"root2"<<r2;
}
else
{
cout<<"\n\tRoots r equal";
r1=(-b)/(2*a);
cout<<"\nroots="<<r1;
}
}
void main()
{
clrscr();
quard q;
q.eqn();
q.call();
getch(); }

STACK USING ARRAY

#include<iostream.h>

#include<conio.h>

#include<stdlib.h>

#define max 5

class stack

int st[max],top;

clrscr();
public:

stack()

top=-1;

void push();

void pop();

void show();

};

void stack::push()

if(top<max-1)

int x;

cout<<"\n Enter any value";

cin>>x;

st[++top]=x;

else

cout<<"\n Stack overflow";


}

void stack::pop()

if(top<0)

cout<<"\n Stack is empty";

getch();

exit(1);

else

top--;

if(top>=0)

show();

void stack::show()

int c,i;

cout<<"\n Values of stack";


for(i=0;i<=top;i++)

cout<<st[i]<<" ";

void main()

int ch;stack ob;

clrscr();

cout<<"1.Push\n2.Pop\n3.Show\n4.Exit";

while(1)

cout<<"\nEnter Ur choice:";

cin>>ch;

switch(ch)

case 1:

ob.push();

break;

case 2:

ob.pop();
break;

case 3:

ob.show();

break;

case 4:

exit(1);

}
SINGLE INHERITANCE

#include<iostream.h>
#include<conio.h>
class personel
{
public:
int rno;
clrscr();
char name[50];
void getdata();
};
void personel::getdata()
{
cout<<"enter the roll no and name";
cin>>rno>>name;
}
class marks:public personel
{
int m;
public:
void getmarks();
void putmarks();
};
void marks::getmarks()
{ getdata();
cout<<"enter marks";
cin>>m;
}
void marks::putmarks()
{
cout<<”RollNo:”<<rno;
cout<<”Name:”<<name;
cout<<”Marks:”<<m;
}
void main()
{ clrscr();
marks ob;
ob.getmarks();
ob.putmarks();

}
Program for Cursor.h

#include<stdio.h>
#include<conio.h>
struct node
{
int data;
int next;
};
int ptr,pos,list,x;
struct node c[10];
void initialize()
{
int i;
for(i=0;i<=9;i++)
{
c[i].next=i+1;
c[i].data=1;
}
c[9].next=-1;
}
int calloc()
{
int p;
p=c[0].next;
c[0].next=c[p].next;
return p;
}
void display()
{
int i;
for(i=0;i<=9;i++)
{
printf("%d \t %d",c[i].data,c[i].next);
printf("\n");
}
}
void insert(int x,int p)
{
int temp;
if(temp==-1)
printf("\n out of space");
else
{
c[1].data=x;
c[1].next=c[p].next;
c[p].next=1;
}
}
Program for cursor.cpp

#include<iostream.h>
#include<conio.h>
#include"cursor.h"
#include<stdlib.h>
void main()
{
int pl,x,ch;
clrscr();
while(1)
{
cout<<"enter your choice";
cin>>ch;
switch(ch)
{
case 1:initialize();
break;
case 2:display();
break;
case 3:cout<<"where you want to insert";
cin>>pl;
cout<<"enter the element";
cin>>x;
insert(x,pl);
break;
case 4:exit(0);
}
}
}
ARRAY IMPLEMENTATION OF LIST ADT

# include<iostream.h>
# include<conio.h>
# include<stdlib.h>
class arradt
{
int a[100],n,i,j;
int count;
public:
arradt()
{
count=0;
}
void insert();
void get();
void display();
void del();
};
void arradt:: get()
{
cout<<"enter n value";
cin>>n;
for(i=1;i<=n;i++)
{
cin>>a[i];
count++;
}
}
void arradt::insert()
{
int x,pos;
int t[100];
int j=0;
count++;
cout<<"enter the pos and elt to insert"<<endl;
cin>>pos>>x;
if(pos>n)
a[pos]=x;
else
{
for(i=pos;i<=n;i++)
{
t[j]=a[i];
j++;
}
a[pos]=x;
j=0;
for(i=pos+1;i<=count;i++)
{
a[i]=t[j];
j++;
}
}
}
void arradt::del()
{
int i,j,x,ind;
cout<<"enter the element to delete";
cin>>x;
for(i=1;i<=count;i++)
{
if(a[i]==x)
{ count--;
ind=i;
break;
}
}
for(j=ind;j<=n;j++)
{
a[j]=a[j+1];
}
}
void arradt::display()
{
for(i=1;i<=count;i++)
{
cout<<a[i];
}
}

void main()
{
int ch;
arradt obj;
clrscr();
obj.get();
cout<<"1.Insert"<<endl;
cout<<"2.Delete"<<endl;
cout<<"3.Display"<<endl;
cout<<"4.Quit"<<endl;
while(1)
{
cout<<"Enter ur choice"<<endl;
cin>>ch;
switch(ch)
{
case 1:obj.insert();
break;
case 2:obj.del();
break;
case 3:obj.display();
break;
case 4:
exit(0);
}
}
}

You might also like