Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 84

BASIC PROGRAMS FOR C++ CONCEPTS

EX NO: 1.1
// CLASSES AND OBJECTS
#include <iostream>
class CRectangle
{
int x, y;
public:
void set_values (int,int);
int area ()
{
return (x*y);
}
};
void CRectangle::set_values (int a, int b)
{
x = a;
y = b;
}
int main ()
{
CRectangle rect;
rect.set_values (3,4);
cout << "area: " << rect.area();
return 0;
}
OUTPUT:
Area: 12
EX NO: 1.2
// CONSTRUCTORS

#include <iostream>
class CRectangle
{
int width, height;
public:
CRectangle (int,int);
int area ()
{
return (width*height);
}
};
CRectangle::CRectangle (int a, int b)
{
width = a;
height = b;
}
int main ()
{
CRectangle rect (3,4);
CRectangle rectb (5,6);
cout << "rect area: " << rect.area() << endl;
cout << "rectb area: " << rectb.area() << endl;
return 0;
}

OUTPUT:
rect area: 12
rectb area: 30
EX NO: 1.3
// OPERATOR OVERLOADING AND PASSING OBJECTS AS PARAMETERS
#include <iostream>

class CVector
{
public:
int x,y;
CVector ()
{
};
CVector (int,int);
CVector operator + (CVector);
};
CVector::CVector (int a, int b)
{
x = a;
y = b;
}
CVector CVector::operator+ (CVector param)
{
CVector temp;
temp.x = x + param.x;
temp.y = y + param.y;
return (temp);
}
int main ()
{
CVector a (3,1);
CVector b (1,2);
CVector c;
c = a + b;
cout << c.x << "," << c.y;
return 0;
}

OUTPUT:
4,3

EX NO: 1.4
// INHERITANCE
#include <iostream>
class CPolygon

{
protected:
int width, height;
public:
void set_values (int a, int b)
{
width=a;
height=b;
}
};
class CRectangle: public CPolygon
{
public:
int area ()
{
return (width * height);
}
};
class CTriangle: public CPolygon
{
public:
int area ()
{
return (width * height / 2);
}
};
int main ()
{
CRectangle rect;
CTriangle trgl;
rect.set_values (4,5);

trgl.set_values (4,5);
cout << rect.area() << endl;
cout << trgl.area() << endl;
return 0;
}

OUTPUT:
20
10

ARRAY IMPLEMENTATION OF LIST ABSTRACT DATA TYPE (ADT)


EX. NO: 2
#include<stdio.h>
#include<conio.h>

#include<iostream.h>
#include<stdlib.h>
#define MAX 20 //maximum no of elements in the list
//user defined datatypes
struct
{
int list[MAX];
int element;
int pos;
int length;
//total no of elements
};
enum boolean
{ true, false };
class array_list
{
public:
int menu(void);
void create(void);

//function to create initial set of elements

void insert(int, int);

//function to insert the given element at specified position

void delet(int);

//function to delete the element at given position

void find(int);

//function to find the position of the given element, if exists

void display(void);

//function to display the elements in the list

boolean islistfull(void);//function to check whether the list is full or not


boolean islistempty(void); //function to check whether the list is empty or not
};
//function to display the list of elements
int array_list :: menu()
{
int ch;
clrscr();

cout<<"\n\t\t********************************************\n";
cout<<"\t\t******LIST Implementation Using Arrays******\n";
cout<<"\t\t********************************************\n\n";
cout<<"\t1. Create\n\t2. Insert\n\t3. Delete\n\t4. Count\n\t5. Find\n\t6.
Display\n\t7.Exit\n\n\tEnter your choice : ";
cin>>ch;
cout<<"\n\n";
return ch;
}
//function to create initial set of elements
void array_list :: create(void)
{
int element;
int flag=1;
while(flag==1)
{
cout<<"Enter an element : ";
cin>>element;
l.list[l.length] = element;
l.length++;
cout<<"To insert another element press '1' : ";
cin>>flag;
}}
//function to display the elements in the list
void array_list :: display(void)
{
int i;
for (i=0; i<l.length; i++)
cout<<"Element" << i+1<<":"<<l.list[i]<<endl;
cout<<"Press any key to continue...";
getch();

}
//function to insert the given element at specified position
void array_list :: insert(int element, int pos)
{
int i;
if (pos == 0)
{
cout<<"\n\nCannot insert at zeroth position";
getch();
return;
}
if (pos-1 > l.length)
{
cout<<"Only "<<l.length<<" elements exist. Cannot insert at "<<pos<<" postion";
cout<<"\nPress any key to continue...";
getch();
}
else
{
for (i=l.length; i>=pos-1; i--)
{
l.list[i+1] = l.list[i];
}
l.list[pos-1] = element;
l.length++;
}
}
//function to delete the element at given position
void array_list :: delet(int pos)
{

int i;
if(pos == 0)
{
cout<<"\n\nCannot delete at zeroth position";
getch();
return;
}
if (pos > l.length)
{
cout<<"\n\nOnly "<< l.length<<" elements exist. Cannot delete"<<pos;
cout<<"\nPress any key to continue...";
getch();
return;
}
for (i=pos-1; i<l.length; i++)
{
l.list[i] = l.list[i+1];
}
l.length--;
}
//function to find the position of the given element, if exists
void array_list :: find(int element)
{
int i;
int flag = 1;
for (i=0; i<l.length; i++)
{
if(l.list[i] == element)
{
cout<< element<<" exists at "<<i+1<<" position";
flag = 0;

cout<<("\nPress any key to continue...");


getch();
break;
}
}
if(flag == 1)
{
cout<<"Element not found.\nPress any key to continue...";
getch();
}
}
//function to check whether the list is full or not
boolean array_list :: islistfull(void)
{
if (l.length == MAX)
return true;
else
return false;
}
//function to check whether the list is empty or not
boolean array_list :: islistempty(void)
{
if (l.length == 0)
return true;
else
return false;
}
void main()
{
//array_list obj;
int ch;

int element;
int pos;
l.length = 0;
array_list obj;
while(1)
{
ch = obj.menu();
switch (ch)
{
case 1:
l.length = 0;
obj.create();
break;
case 2:
if (obj.islistfull() != true)
{
cout<<"\tEnter the New element : ";
cin>>element;
cout<<"\tEnter the Position : ";
cin>>pos;
obj.insert(element, pos);
}
else
{
cout<<"\tList if Full. Cannot insert";
cout<<"\nPress any key to continue...";
getch();
}
break;
case 3:
if (obj.islistempty() != true)

{
cout<<"Enter the position of element to be deleted : ";
cin>>pos;
obj.delet(pos);
}
else
{
cout<<"List is Empty.";
cout<<"\nPress any key to continue...";
getch();
}
break;
case 4:
cout<<"No of elements in the list is "<<l.length;
cout<<"\nPress any key to continue...";
getch();
break;
case 5:
cout<<"Enter the element to be searched : ";
cin>>element;
obj.find(element);
break;
case 6:
obj.display();
break;
case 7:
exit(0);
break;
default:
cout<<"Invalid Choice";
cout<<"\nPress any key to continue...";

getch();
}
}
}
OUTPUT:
1. Create
2. Insert
3. Delete
4. Count
5. Find
6. Display
7. Exit
Enter your Choice: 1
Enter an element: 25
To insert another element press 1: 1
Enter an element: 50
To insert another element press 1: 0
Enter your Choice: 6
Element in the List are
1. 25
2. 50
Enter your Choice: 2
Enter an element: 5
Enter the position: 1
Element in the List are
1. 25
2. 50

Enter your Choice: 3


Enter the position: 3
Enter your Choice: 6
Element in the List are
1. 5
2. 25
Enter your Choice: 4
Number of Elements: 2
Enter your Choice: 7

LINKED LIST IMPLEMENTATION OF LIST ADT


EX NO: 3
#include<stdio.h>

#include<stdlib.h>
#include<conio.h>
#include<iostream.h>
struct node;
typedef struct node *ptr;
typedef ptr list;
typedef ptr position;
typedef int data;
struct node
{
data element;
struct node *next;
};
//global variable declarations
position first;
position last;
position L;
int length;
class linkedlist
{
public:
//function prototypes
void makeempty(void);

//to make empty list

int isempty(void);

//to check list is empty or not

void create(void);

//to create initial set of elements

position findprevious(data);

//to find position of previous element

void delet(data);

//to delete given element

void display(void);

//to display all the elements

void insert(data, int);

//to insert a new element

position getprevposition(int);

//to find position of previous element

data getelement(int);

//to find the element at given position

int getposition(data);

//to find position of given element

int menu(void);
};
//to make empty list
void linkedlist :: makeempty(void)
{
position tmp;
tmp =(position)malloc(sizeof(list));
tmp->next = NULL;
L = tmp;
first = last = NULL;
}
//to check list is empty or not
int linkedlist :: isempty(void)
{
if (L->next == NULL)
return 1;
else
return 0;
}
//to create initial set of elements
void linkedlist :: create(void)
{
data e;
int n, i;
position tmp;
makeempty();
cout<<"Enter number of element : \
cin>>n;
for (i=0; i<n; i++)

";

{
cout<<"Enter an element : ";
cin>>e;
tmp =(position)malloc(sizeof(list));
tmp->element = e;
tmp->next = NULL;
if (L->next == NULL)
{
L->next = tmp;
first = last = tmp;
length++;
}
else
{
last->next = tmp;
last = tmp;
length++;
}
}
}
//to display all the elements
void linkedlist :: display()
{
position t;
for(t=first; t!=NULL; t=t->next)
cout<<t->element<<" --> ";
getch();
}
//to find position of previous element
position linkedlist :: getprevposition(int index)
{

position tmp;
int count = 1;
if (index>length)
{
cout<<"Invalid Position";
return NULL;
}
else
{
for (tmp=first; count<index-1; tmp=tmp->next)
count++;
return tmp;
}
}
//to insert a new element
void linkedlist :: insert(data x, int p)
{
position pos, tmp;
tmp =(position)malloc(sizeof(list));
tmp->element=x;
if (p==1)

//first position

{
tmp->next = first;
L->next = tmp;
first = tmp;
length++;
}
else if (p == length)
{
last->next = tmp;
last = tmp;

//last position

tmp->next = NULL;
length++;
}
else

//arbitrary position

{
pos = getprevposition(p);
if (pos == NULL)
{
cout<<"Invalid position";
getch();
}
else
{
tmp->next = pos->next;
pos->next = tmp;
length++;
}
}
}
//to find position of previous element
position linkedlist :: findprevious(data x)
{
position p;
p = L;
while (p->next->element!=x && p->next!=NULL)
p = p->next;
return p;
}
//to delete given element
void linkedlist :: delet(data x)
{

position p, tmp;
if (isempty())
{
cout<<"List is empty";
getch();
}
else
{
p = findprevious(x);
if (p->next == NULL)
{
cout<<"Element not found";
getch();
}
else
{
if (p->next == last)
{
free (p->next);
p->next = NULL;
last = p;
length--;
return;
}
if (p == L)
{
first = first->next;
}
tmp = p->next;
p->next = tmp->next;
free(tmp);

length--;
}
}
}
int linkedlist :: menu()
{
int choice;
cout<<"1. Create\n2. Display\n3.Insert\n4.Get Element\n5.Get Position\n6. Delete\n7. Exit\n\n
Enter your choice : ";
cin>>choice;
return choice;
}
//to find the element at given position
data linkedlist :: getelement(int pos)
{
position p;
int i;
p = L;
if (pos > length)
return NULL;
else
{
for(i=0; i<pos; i++)
p = p->next;
return p->element;
}
}
//to find position of given element
int linkedlist :: getposition(data e)
{
position p;

int i=0;
for (p=first; p!=NULL; p=p->next)
{
if (p->element == e)
return i+1;
else
i++;
}
return NULL;
}
void main()
{
int ch;
linkedlist obj;
data n, p;
while(1)
{
clrscr();
ch = obj.menu();
switch (ch)
{
case 1:
obj.create();
break;
case 2:
obj.display();
break;
case 3:
cout<<"Enter an element : ";
cin>>n;
cout<<"Enter Position : ";

cin>>p;
obj.insert (n, p);
break;
case 6:
cout<<"Enter an element : ";
cin>>n;
obj.delet(n);
cout<<"Element deleted successfully\n";
getch();
break;
case 5:
cout<<"Enter position : ";
cin>>p;
if (p<1 || p>length)
cout<<"Invalid position";
else
cout<<"Element at position "<<p<<" is "<<obj.getelement(p);
getch();
break;
case 4:
cout<<"Enter an element : ";
cin>>n;
if (obj.getposition(n) == NULL)
cout<<"element doesnot Exist";
else
cout<<n<<" exists at position "<<obj.getposition(n);
getch();
break;
case 7:
exit(0);
break;

default:
cout<<"Invalid Choice";
getch();
}
}
}

OUTPUT:
1. Create
2. Display
3. Insert
4. Delete
5. Get element
6. Get position
7. Exit
Enter your Choice: 1
Enter number of element: 3
Enter an element: 10
Enter an element: 20
Enter an element: 30
Enter your Choice: 3
Enter element: 25
Enter Position: 3
Enter your Choice: 2
10 --> 20 --> 25 --> 30

Enter your Choice: 6


Enter an element:20
20 exists at position 2
Enter your Choice: 4
Enter an element 30
Enter your Choice: 2
10 --> 20 --> 25
Enter your Choice: 7

CURSOR IMPLEMENTATION OF LIST ADT


EX NO: 4

#include<stdio.h>
#include<conio.h>
#include<iostream.h>
struct cursor
{
int data;
int next;
}
c[10];
int fp=0;
class cursorlist
{
public:
void create();
void insert(int,int);
void display();
void del();
};
void cursorlist :: create()
{
int a;
int p,i;
for(i=0;i<10;i++)
{
c[i].data=0;
c[i].next=-1;
}
cout<<"Enter the first element to be inserted";
cin>>a;
cout<<"Enter the position";
cin>>p;

getch();
fp=p;
c[p].data=a;
c[p].next=0;
}
void cursorlist :: insert(int a,int pos)
{
int i;
if(c[pos].next==-1)
{
c[pos].data=a;
for(i=0;i<10;i++)
{
if(c[i].next==0)
{
c[i].next=pos;
}
}
c[pos].next=0;
}
else
cout<<"\n Already data is available";
}
void cursorlist :: display()
{
int i;
i=fp;
cout<<"Position";
cout<<"\tdata";
cout<<"\tnext element\n";
do

{
cout<<" "<<i;
cout<<"\t\t"<<c[i].data;
cout<<"\t\t"<<c[i].next<<"\n";
i=c[i].next;
}
while(i>0);
}
void cursorlist :: del()
{
int temp,i;
int d;
cout<<"\n Enter the character to be deleted";
cin>>d;
if(c[fp].data==d)
{
c[fp].data=0;
temp=c[fp].next;
c[fp].next=-1;
fp=temp;
}
else
{
i=fp;
do
{
if(c[c[i].next].data==d)
{
temp=c[i].next;
c[i].next=c[c[i].next].next;
c[temp].next=-1;

}
i=c[i].next;
}
while(i>0);
}
}
void main()
{
int opt,p;
int a;
cursorlist obj;
clrscr();
obj.create();
do
{
cout<<"\n 1.Insert";
cout<<"\n 2.Delete";
cout<<"\n 3.Display";
cout<<"\n 4.Exit";
cout<<"\n Enter your choice";
cin>>opt;
getch();
switch(opt)
{
case 1:
cout<<"\nEnter the element to be inserted";
cin>>a;
cout<<"\nEnter the position";
cin>>p;
getch();
obj.insert(a,p);

break;
case 2:
obj.del();
break;
case 3:
obj.display();
break;
}
}
while(opt<4);
}

OUTPUT:
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice:1
Enter the element to be inserted 20
Enter the position 1
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice:1
Enter the element to be inserted 40

Enter the position 2


1.Insert
2.Delete
3.Display
4.Exit
Enter your choice:1
Enter the element to be inserted 30
Enter the position 3
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice:3
1.20
2.40
3.30
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice:1
Enter the element to be inserted 60
Enter the position 2
1.Insert
2.Delete

3.Display
4.Exit
Enter your choice:3
1.20
2.60
3.40
4.30
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice:4

STACK ADT - ARRAY IMPLEMENTATIONS

EX NO: 5.1
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<iostream.h>
#define SIZE 5
class stack
{
public:
int top;
stack()
{
top=-1;
}
int stack[SIZE];
void push();
void pop();
void display();
int isempty();
int length();
};
void stack :: push()
{
int n;
if(top==SIZE-1)
cout<<"\n Stack is full";
else
{
cout<<"\nEnter the no.";
cin>>n;
top++;

stack[top]=n;
}
}
void stack :: pop()
{
int n;
if(isempty())
{
cout<<"\nStack is empty";
top=-1;
}
else
{
n=stack[top];
cout<<"\n"<<n<<" is popped from the stack \n";
--top;
}
}
void stack :: display()
{
int i,temp=top;
if(isempty())
{
cout<<"\n Stack Empty";
return;
}
cout<<"\n Elements in the stack:";
for(i=temp;i>=0;i--)
cout<<stack[i]<<"\n";
}
int stack :: isempty()

{
return (top==-1);
}
int stack :: length()
{
return (top+1);
}
void main()
{
int choice;
stack obj;
clrscr();
while(1)
{
cout<<"\n 1.Push";
cout<<"\n 2.POP";
cout<<"\n 3.Display";
cout<<"\n 4.Length ";
cout<<"\n 5.Quit";
cout<<"\n Enter the choice:\n";
cin>>choice;
switch(choice)
{
case 1:
obj.push();
break;
case 2:
obj.pop();
break;
case 3:
obj.display();

break;
case 4:
cout<<"\n No. of elements in the stack is "<<obj.length();
break;
case 5:
exit(0);
break;
default:
cout<<"\n Invalid choice";
}
}
}

OUTPUT:
1.Push
2. POP
3.Display
4. Length
5.Quit
Enter the choice: 1
Enter the no. 10
1.Push
2. POP
3.Display
4. Length
5.Quit
Enter the choice: 1
Enter the no. 20

1.Push
2. POP
3.Display
4. Length
5.Quit
Enter the choice: 1
Enter the no. 30
1.Push
2. POP
3.Display
4. Length
5.Quit
Enter the choice: 1
Enter the no. 40
1.Push
2. POP
3.Display
4. Length
5.Quit
Enter the choice: 3
Elements in the stack:
40
30
20
10
1.Push
2. POP

3.Display
4. Length
5.Quit
Enter the choice: 2
40 is popped from the stack
1.Push
2. POP
3.Display
4. Length
5.Quit
Enter the choice: 4
Number of elements in the stack is 3
1.Push
2. POP
3.Display
4. Length
5.Quit
Enter the choice: 5

STACK ADT - LINKED LIST IMPLEMENTATIONS

EX NO: 5.2
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<iostream.h>
struct node
{
int info;
struct node *link;
}
*top=NULL;
class stacklist
{
public:
void push();
void pop();
void display();
};
void stacklist :: push()
{
struct node *tmp;
int pushed_item;
tmp = new node;
cout<<"Input the new value to be pushed on the stack : ";
cin>>pushed_item;
tmp->info=pushed_item;
tmp->link=top;
top=tmp;
}/*End of push()*/
void stacklist :: pop()
{

struct node *tmp;


if(top == NULL)
cout<<"Stack is empty\n";
else
{
tmp=top;
cout<<"Popped item is "<<tmp->info<<endl;
top=top->link;
free(tmp);
}
}/*End of pop()*/
void stacklist :: display()
{
struct node *ptr;
ptr=top;
if(top==NULL)
cout<<"Stack is empty\n";
else
{
cout<<"Stack elements :\n";
while(ptr!= NULL)
{
cout<<ptr->info<<endl;
ptr = ptr->link;
}/*End of while */
}/*End of else*/
}/*End of display()*/
void main()
{
int choice;
stacklist obj;

while(1)
{
cout<<"1.Push\n";
cout<<"2.Pop\n";
cout<<"3.Display\n";
cout<<"4.Quit\n";
cout<<"Enter your choice : ";
cin>>choice;
switch(choice)
{
case 1:
obj.push();
break;
case 2:
obj.pop();
break;
case 3:
obj.display();
break;
case 4:
exit(1);
default :
cout<<"Wrong choice\n";
}/*End of switch */
}/*End of while */
}/*End of main() */

OUTPUT:

1.Push
2.Pop
3.Display
4.Quit
Enter your choice : 1
Input the new value to be pushed on the stack : 12
1.Push
2.Pop
3.Display
4.Quit
Enter your choice : 1
Input the new value to be pushed on the stack : 13
1.Push
2.Pop
3.Display
4.Quit
Enter your choice : 1
Input the new value to be pushed on the stack : 43
1.Push
2.Pop
3.Display
4.Quit
Enter your choice : 3

Stack elements :

43
13
12
1.Push
2.Pop
3.Display
4.Quit
Enter your choice : 2
Popped item is 43
1.Push
2.Pop
3.Display
4.Quit
Enter your choice : 4

PROGRAM SOURCE FILES FOR STACK APPLICATION1

EX NO: 6A
DATE:
AIM:

Application 1: checking well formedness of parenthesis.


A) STACK IMPLEMENTED AS ARRAYS(USING THE HEADER FILE OF STACK
OPERATIONS)
Step 1:
Crate a header file named stack.h . in this file we will declare the class and all the stack
Operations. The implementation of stack is using arrays.
stack.h
#define size 10
class stk_class
{
private:
struct stack
{
char s[size];
int top;
}st;
public:
stk_class();
void push(char item);
int stempty();
char pop();
};
stk_class::stk_class()
{
st.top=-1;

}
/* the push function
input:item which is to be pished
output:none simply pushes the item onto the stack
called by: main
calls: one
*/
void stk_class::push(char item)
{
st.top++;
st.s[st.top]=item;
}
/*
the stempty function
input:none
output:returns 1 or 0 for stack empty or not
called by:none
*/
int stk_class::stempty()
{
inr(st.top==-1)
return 1;
else
return 0;
}
/*
the stfull function
input:none
coutput:returns 1 or 0 for stack full or not
called by :main
calls:none

*/
int stk_class::stfull()
{
if(st.top==size)
return 1;
else
return 0;
}
/*
the pop function
input:none
output:returns the item which is poped from the stack
called by: main
calls:none
*/
char stk_class::pop()
{
char item;
item=st.s[st.top];
st.top--;
return(item);
}
Step 2:
Now we will create a stack application program. We have chosen an application as
checking well formedness of parenthesis for this application we will use stack operations.
These operations are used from the external file stack.h. Hence we will include this file in the
include file section. Here is an application program.
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include d:\stack1.h

#define size 10
/*the main function
input:none
ouput:none
called by:O.S
calls:push,pop,stempty
void main(void)
{
char item;
char ans,bracket[10];
stk_class obj;
int I;
clrscr();
cout<,\n\t\t program for stack application using separate header file;
cout<<\n enter the expression and put $at end ;
cin>>bracket;
i=0;
if(bracket[i]==))
cout<<\n the expressin is invalid;
else
{
do
{
while(brackt[i]==()
{
obj.push(bracket[i]);
i++;
}
while(bracket[i]==))
{
item=obj.pop();

i++;
}
}
while(brackt[i]!=$)
if(!obj.stempty())
cout<<\n the expression is invalid;
else
cout<<\n the expression has well formed parenthesis;
}
getch();
}
Step 3: Execute above program and following output can be obtained.
Output(run 1)
program for stack application using separate header file
enter the expression and put $ at the end (()(()))$
the expression has well formed parenthesis
Output(run 2)
program for stack application using separate header file
enter the expression and put $ at the end (()()$
the expression in invalid.

RESULT:

STACK IMPLEMENTED AS LINKED LIST(USING THE HEADER FILE OF STACK


OPERATION)
EX NO: 6B
STEP 1:
create a header file named stack. In this file we declare the class and all the stack
operations.
The implementation of stack using linked list.
class stk_class
{
private:
/*data structure for the linked stack*/
typedef struct stack
{
char data;
struct stack * next;
}node;
node *top;
public:
stk_class();
void push(char item);
int sempty();
void pop();
};
stk_class ::stk_class()
{
top=NULL;
}
void stk_class::push(char item)
{
node * New;
New=new node;

if(New==NULL)
cout<<\n memory cannot be allocated \n;
else
{
New->data=item;
new->next=top;
top=New;
}
}
int stk_class::sempty()
{
if(top==NULL)
return 1;
else
return 0;
}
void stk_class::pop()
{
node *temp;
temp=top;
top=top->next;
delete temp;}
Step 2:
Now we will create a stack application program. We have chosen an application as
checking well formedness of parenthesis for this application we will use stack operations.
These operations are used from the external file stack.h. Hence we will include this file in the
include file section. Here is an application program.
/*include file section*/
#include<iostream.h>
#include<conio.h>

#include<process.h>
#include<stdlib.h>
#include<d:\stack.h
the main function
input:none
output:none
called by:O.S
calls:push ,pop,sempty
void main(void)
{
char ans,bracket[10];
Char data ,item;
stk_class obj;
int choice;
int I;
clrscr();
cout<<\n\t\t enter the expression and put $ at the end ;
cin>>bracket;
i=0;
if(bracket[i]==))
cout<<\n the expression is invalid;
else
{
do
{
if(bracket[i]==()
{
obj.push(bracket[i]);
}
else if(bracket[i]==))
{

if(obj.sempty())
{
cout<<\n the expression is invalid;
getch();
exit(0);
}
obj.pop();
}
i++;
}
while(bracket[i]!=$);
if(obj.sempty())
cout<<\n the expression is invalid;
else
cout<<\n the expression has well formed parenthesis;
}
getch();
}
Step 3:
The above program will be executed to get output as follows
output(run1)
enter the expression and put $ at the end ()()$
the expression has well formed parenthesis
output(run 2)
enter the expression and put $ at the end (()()$
the expression is invalid.
RESULT:

PROGRAM SOURCE FILES FOR STACK APPLICATION2


EX. NO:6C
Evaluation of postfix expression.
A.USING STACK (IMPLEMENTATION AS ARRAYS)
STEP 1:
#define Max 10
class stk_class
{
struct stack
{
double s[MAX];
int top;
}st;
public:
stk_class();
void push(double val);
double pop();
};
stk_class::stk_class()
{
st.top=0;
}
void stk_class::push(double val)
{
if(st.top+1>=MAX)
cout<<\n stack is full;
st.top++;
st.s[st.top]=val;
}
double stk_class::pop()

{
double val;
if(st.top==-1)
cout<<\n stack is empty\n;
st.top--;
return(val);l
}
STEP 2:
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
/*included the header file as stack using array*/
#define size 80
void main()
{
char exp[size];
int len;
double result;
double post(char exp[]);
clrscr();
cout<<enter the postfix expression\n;
cin>>exp;
len=strlen(exp);
exp[len]=$;/*append $ at the end a end marker*/
result=post(exp);
cout<<the value of the expression is<<result;
getch();
exit(0);

}
double post(char exp[])
{
stk_class obj;
char ch,*type;
double result ,val,op1,op2;
int i;
i=0;
ch=exp[i];
while(ch!=$)
{
if(ch>=0&&ch<=9)
type=operand:
else if(ch==+||ch==-||ch==^)
type=operator;
if(strcmp(type,operand)==0)/* if the character is operator*/
{
val=ch-48;
obj.push(val);
}
else
if(strcmp(type,operator)==0)/* if it is operator*/
{
op2=obj.pop();
op1=obj.pop();
switch(ch)
{
case +:result=op1+op2;
break;
case -:result=op1-op2;
break;

case *;:result=op1*op2;
break;
case /: result=op1/op2;
break;
case ^:result=pow(op1,op2)
break;
}
/*switch*/
obj.push(result);
}
i++;
ch=exp[i];
}/*while*/
result-obj.pop();/*pop the result*/
return(result);
}
OUTPUT:
Enter the postfix expression
12+3*
The value of the expression is 9

RESULT:

STACK IMPLANTED AS LINKED LIST(USE OF SEPARATE HEADER FILE FOR


STACK OPERATIONS)
EX NO:6D

STEP 1:
class stk_class
{
/* data structure for the linked stack*/
typedef struct stack
{
char data;
struct stack *next;
}
node;
public:
node *top;
stk_class();
void push(char item);
char pop();
};
stk_class::stk_class()
{
top=NULL:
}
/* functionality performed on linked linked stack*/
void stk_class::push(char item)
{
node *New;
New=new node;
New->data =Item;
New->next=top;
top=New;

}
char stk_class::pop()
{
char item;
node *temp;
item=top->data;
temp=top;
top=top->next;
delete temp;
return item;
}
STEP 2:
#include<iostream.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<conio.h>
/* included the stack.h file (given below) for linked stack*/
#included:\stack.h
#define size 80
void main()
{
char exp[size];
int len;
double result;
double post(char exp[])
clrscr();
cout<<enter the postfix expression\n;
cin>>exp;

len=strlen(exp);
cin>>exp;
len=strlen(exp);
exp[len]=$;/*append $at the end as a endmarker*/
result=post(exp);
cout<<the value of the expression is<<result;
getch();
exit(0);
}
double post(char exp[])
{
char ch,*type;
double result ,val,oip1,op2;
int I;
stk_class obj;
i=0;
ch=exp[i];
while(ch!=$)
{
if(ch>=0&&ch<=9)
type=operand;
else if(ch==+||ch==-||ch==*||ch==/||ch==^)
type=operator;
if(strcmp(type,operator)==0) /* if the character is operand*/
{
val=ch-48;
obj.push(val);
}
else
if(strcmp(type,operator)==0)/*if it is operator*/
{

op2=obj.pop();
op1=obj.pop();
switch(ch)
{
case +:result=op1+op2;
break;
case -:result=op1-op2;
break;
case *;:result=op1*op2;
break;
case /: result=op1/op2;
break;
case ^:result=pow(op1,op2)
break;
}
obj.push(result);
}
i++;
ch=exp[i];
}/*while*/
result-obj.pop();/*pop the result*/
return(result);
}
OUTPUT:
enter the expression
12+3*
the value of the expression is 9
RESULT:

QUEUE ADT ARRAY IMPLEMENTATIONS


EX NO: 7.1
#include<stdio.h>
#include<stdlib.h>
#include<iostream.h>
# define MAX 5
int queue_arr[MAX];
int rear = -1;
int front = -1;
class queue
{
public:
void insert();
void del();
void display();
};
void queue :: insert()
{
int added_item;
if (rear==MAX-1)
cout<<"Queue Overflow\n";
else
{
if (front==-1) /*If queue is initially empty */
front=0;
cout<<"Input the element for adding in queue : ";
cin>>added_item;
rear=rear+1;
queue_arr[rear] = added_item ;
}

}/*End of insert()*/
void queue :: del()
{
if (front == -1 || front > rear)
{
cout<<"Queue Underflow\n";
return ;
}
else
{
cout<<"Element deleted from queue is :"<<queue_arr[front]<<endl;
front=front+1;
}
}/*End of del() */
void queue :: display()
{
int i;
if (front == -1)
cout<<"Queue is empty\n";
else
{
cout<<"Queue is :\n";
for(i=front;i<= rear;i++)
cout<<queue_arr[i];
cout<<"\n";
}
}/*End of display() */
void main()
{
int choice;
queue obj;

while(1)
{
cout<<"1.Insert\n";
cout<<"2.Delete\n";
cout<<"3.Display\n";
cout<<"4.Quit\n";
cout<<"Enter your choice : ";
cin>>"%d",choice;
switch(choice)
{
case 1 :
obj.insert();
break;
case 2 :
obj.del();
break;
case 3:
obj.display();
break;
case 4:
exit(1);
default:
cout<<"Wrong choice\n";
}/*End of switch*/
}/*End of while*/
}/*End of main()*/

OUTPUT:
1.Insert
2.Delete
3.Display
4.Quit
Enter your choice:1
Input the element for adding in queue :10
1.Insert
2.Delete
3.Display
4.Quit
Enter your choice:1
Input the element for adding in queue :20
1.Insert
2.Delete
3.Display
4.Quit
Enter your choice:1
Input the element for adding in queue :30
1.Insert
2.Delete
3.Display
4.Quit
Enter your choice:1
Input the element for adding in queue :40
1.Insert
2.Delete

3.Display
4.Quit
Enter your choice:3
Queue is :
40
30
20
10
1.Insert
2.Delete
3.Display
4.Quit
Enter your choice:2
Element deleted from queue is :10
1.Insert
2.Delete
3.Display
4.Quit
Enter your choice:3
Queue is :
40
30
20
1.Insert
2.Delete
3.Display
4.Quit
Enter your choice:4

QUEUE ADT LINKED LIST IMPLEMENTATIONS


EX NO:7.2
#include<stdio.h>
#include<malloc.h>
#include<conio.h>
#include<iostream.h>
#define MAXSIZE 10
struct node
{
int info;
struct node *link;
}
*new1,*temp,*p,*front=NULL,*rear=NULL;
typedef struct node N;
class queuelist
{
public:
void insertion();
void deletion();
void display();
};
void queuelist :: insertion()
{
int item;
new1=(N*)malloc(sizeof(N));
cout<<"\nEnter the item : ";
cin>>item;
new1->info=item;
new1->link=NULL;
if(front==NULL)
front=new1;

else
rear->link=new1;
rear=new1;
}
void queuelist :: deletion()
{
if(front==NULL)
cout<<"\nQueue is empty";
else
{
p=front;
cout<<"\nDeleted element is : "<<p->info;
front=front->link;
free(p);
}
}
void queuelist :: display()
{
if(front==NULL)
cout<<"\nQueue is empty";
else
{
cout<<"\nThe elements are : \n";
temp=front;
while(temp!=NULL)
{
cout<<temp->info<<"->";
temp=temp->link;
}
}
}

void main()
{
int ch;
queuelist obj;
clrscr();
do
{
cout<<"\n\t\t\tLinked queue";
cout<<"\n 1.Insertion";
cout<<"\n 2.Deletion";
cout<<"\n 3.Display";
cout<<"\n 4.Exit";
cout<<"\n Enter your choice : ";
cin>>ch;
switch(ch)
{
case 1:
obj.insertion();
break;
case 2:
obj.deletion();
break;
case 3:
obj.display();
break;
default:
break;
}
}
while(ch<=3);
}

OUTPUT:
1.Insertion
2.Deletion
3.Display
4.Exit
Enter your choice:1
Enter the item :10
1.Insertion
2.Deletion
3.Display
4.Exit
Enter your choice:1
Enter the item :20
1.Insertion
2.Deletion
3.Display
4.Exit
Enter your choice:1
Enter the item :30
1.Insertion
2.Deletion
3.Display
4.Exit
Enter your choice:1
Enter the item :40
1.Insertion
2.Deletion
3.Display
4.Exit

Enter your choice:3


The elements are :
40
30
20
10
1.Insertion
2.Deletion
3.Display
4.Exit
Enter your choice:2
Deleted element is : 10
1.Insertion
2.Deletion
3.Display
4.Exit
Enter your choice:3
The elements are :
40
30
20
1.Insertion
2.Deletion
3.Display
4.Exit
Enter your choice:4

SEARCH TREE ADT - BINARY SEARCH TREE


EX NO: 8
#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<alloc.h>
struct tree
{
int data;
struct tree *lchild;
struct tree *rchild;
}
*t,*temp;
int element;
class bst
{
public:
//int element;
void inorder(struct tree *);
void preorder(struct tree *);
void postorder(struct tree *);
struct tree * create(struct tree *, int);
struct tree * find(struct tree *, int);
struct tree * insert(struct tree *, int);
struct tree * del(struct tree *, int);
struct tree * findmin(struct tree *);
struct tree * findmax(struct tree *);
};
struct tree * bst :: create(struct tree *t, int element)
{
t=(struct tree *)malloc(sizeof(struct tree));

t->data=element;
t->lchild=NULL;
t->rchild=NULL;
return t;
}
struct tree * bst :: find(struct tree *t, int element)
{
if(t==NULL)
return NULL;
if(element<t->data)
return(find(t->lchild,element));
else if(element>t->data)
return(find(t->rchild,element));
else
return t;
}
struct tree * bst :: findmin(struct tree *t)
{
if(t==NULL)
return NULL;
else if(t->lchild==NULL)
return t;
else
return(findmin(t->lchild));
}
struct tree* bst :: findmax(struct tree *t)
{
if(t!=NULL)
{
while(t->rchild!=NULL)
t=t->rchild;

}
return t;
}
struct tree* bst :: insert(struct tree *t,int element)
{
if(t==NULL)
{
t=(struct tree *)malloc(sizeof(struct tree));
t->data=element;
t->lchild=NULL;
t->rchild=NULL;
return t;
}
else
{
if(element<t->data)
{
t->lchild=insert(t->lchild,element);
}
else if(element>t->data)
{
t->rchild=insert(t->rchild,element);
}
else if(element==t->data)
{
printf("element already present\n");
}
return t;
}
}

struct tree * bst :: del(struct tree *t, int element)


{
if(t==NULL)
printf("element not found\n");
else if(element<t->data)
t->lchild=del(t->lchild,element);
else if(element>t->data)
t->rchild=del(t->rchild,element);
else if(t->lchild&&t->rchild)
{
temp=findmin(t->rchild);
t->data=temp->data;
t->rchild=del(t->rchild,t->data);
}
else
{
temp=t;
if(t->lchild==NULL)
t=t->rchild;
else if(t->rchild==NULL)
t=t->lchild;
free(temp);
}
return t;
}
void bst :: inorder(struct tree *t)
{
if(t==NULL)
return;
else
{

inorder(t->lchild);
printf("\t%d",t->data);
inorder(t->rchild);
}
}
void bst :: preorder(struct tree *t)
{
if(t==NULL)
return;
else
{
printf("\t%d",t->data);
preorder(t->lchild);
preorder(t->rchild);
}
}
void bst :: postorder(struct tree *t)
{
if(t==NULL)
return;
else
{
postorder(t->lchild);
postorder(t->rchild);
printf("\t%d",t->data);
}
}
void main()
{
int ch;
bst obj;

do
{
printf("\n\t\t\tBINARY SEARCH TREE");
printf("\n\t\t\t****** ****** ****");
printf("\nMain Menu\n");
printf("\n1.Create\n2.Insert\n3.Delete\n4.Find\n5.FindMin\n6.FindMax");
printf("\n7.Inorder\n8.Preorder\n9.Postorder\n10.Exit\n");
printf("\nEnter ur choice :");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nEnter the data:");
scanf("%d",&element);
t=obj.create(t,element);
obj.inorder(t);
break;
case 2:
printf("\nEnter the data:");
scanf("%d",&element);
t=obj.insert(t,element);
obj.inorder(t);
break;
case 3:
printf("\nEnter the data:");
scanf("%d",&element);
t=obj.del(t,element);
obj.inorder(t);
break;
case 4:
printf("\nEnter the data:");

scanf("%d",&element);
temp=obj.find(t,element);
if(temp->data==element)
printf("\nElement %d is at %d",element,temp);
else
printf("\nElement is not found");
break;
case 5:
temp=obj.findmin(t);
printf("\nMix element=%d",temp->data);
break;
case 6:
temp=obj.findmax(t);
printf("\nMax element=%d",temp->data);
break;
case 7:
obj.inorder(t);
break;
case 8:
obj.preorder(t);
break;
case 9:
obj.postorder(t);
break;
case 10:
exit(0);
}
}
while(ch<=10);
}

OUTPUT:
BINARY SEARCH TREE
****** ****** ****
Main Menu
1.Create
2.Insert
3.Delete
4.Find
5.FindMin
6.FindMax
7.Inorder
8.Preorder
9.Postorder
10.Exit
Enter ur choice : 1
Enter the data: 12
12
BINARY SEARCH TREE
****** ****** ****
Main Menu
1.Create
2.Insert
3.Delete
4.Find
5.FindMin
6.FindMax
7.Inorder
8.Preorder
9.Postorder
10.Exit

Enter ur choice :2
Enter the data:43
12

43
BINARY SEARCH TREE
****** ****** ****

Main Menu
1.Create
2.Insert
3.Delete
4.Find
5.FindMin
6.FindMax
7.Inorder
8.Preorder
9.Postorder
10.Exit
Enter ur choice :8
12

23

45

32

65

56

BINARY SEARCH TREE


****** ****** ****
Main Menu
1.Create
2.Insert
3.Delete
4.Find
5.FindMin
6.FindMax
7.Inorder

8.Preorder
9.Postorder
10.Exit
Enter ur choice :9
32

56

65

45

23

12

BINARY SEARCH TREE


****** ****** ****
Main Menu
1.Create
2.Insert
3.Delete
4.Find
5.FindMin
6.FindMax
7.Inorder
8.Preorder
9.Postorder
10.Exit
Enter ur choice :7
12

23

32

45

56

65

QUICK SORT
EX NO: 9
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
int i,j,n,pi,a[20];
class quicksort
{
public:
void qsort(int a[],int ft,int lt);
void swap(int a[],int i,int j);
};
void quicksort :: qsort(int a[],int lb,int ub)
{
if(lb<ub)
{
pi=a[lb];
i=lb;
j=ub;
while(i<j)
{
while(a[i]<=pi && i<ub)
i++;
while(a[i]>=pi && j>lb)
j--;
if(i<j)
swap(a,i,j);
}
swap(a,lb,j);
qsort(a,lb,j-1);

qsort(a,j+1,ub);
}
}
void quicksort :: swap(int a[],int i,int j)
{
int t;
t=a[i];
a[i]=a[j];
a[j]=t;
}
void main()
{
int n,i,a[20];
quicksort obj;
clrscr();
cout<<"\nEnter the size:";
cin>>n;
cout<<"Enter the elements:";
for(i=0;i<n;i++)
cin>>a[i];
obj.qsort(a,0,n-1);
cout<<"\nSorted List:";
for(i=0;i<n;i++)
cout<<"\n"<<a[i];
getch();
}

OUTPUT:
Enter the size: 5
Enter the elements:
85
32
46
04
96
Sorted list:
04
32
46
85
96

You might also like