· ec6312 oops and data structures laboratory objectives:

92
EC6312 OOPS AND DATA STRUCTURES LABORATORY OBJECTIVES: The student should be made to: Learn C++ programming language. Be exposed to the different data structures Be familiar with applications using different data structures LIST OF EXPERIMENTS: 1. Basic Programs for C++ Concepts. 2. Array implementation of List Abstract Data Type (ADT). 3. Linked list implementation of List ADT. 4. Cursor implementation of List ADT. 5. Stack ADT Array and linked list implementations. 6. The next two exercises are to be done by implementing the following source files i. Program source files for Stack Application 1 ii. Array implementation of Stack ADT iii. Linked list implementation of Stack ADT iv. Program source files for Stack Application 2 v. An appropriate header file for the Stack ADT should be included in (i) and (iv) 7. Implement any Stack Application using array implementation of Stack ADT (by implementing files (i) and (ii) given above) and then using linked list. 8. Implementation of Stack ADT (by using files (i) and implementing file (iii)). 9. Implement another Stack Application using array and linked list implementations of Stack ADT (by implementing files (iv) and using file (ii), and then by using files (iv) and (iii)). 10. Queue ADT – Array and linked list implementations. 11. Search Tree ADT Binary Search Tree. 12. Implement an interesting application as separate source files and using any of the searchable ADT files developed earlier. Replace the ADT file alone with other appropriate ADT files. Compare the performance. 13. Quick Sort. REFERENCE: spokentutorial.org. TOTAL: 45 PERIODS OUTCOMES: At the end of the course, the student should be able to: • Design and implement C++ programs for manipulating stacks, queues, linked lists, trees, and graphs. • Apply good programming design methods for program development. • Apply the different data structures for implementing solutions to practical problems. Ex. No. : 2 BASIC C++ PROGRAMS Date: www.studentsfocus.co

Upload: others

Post on 22-Mar-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1:  · ec6312 oops and data structures laboratory objectives:

EC6312 OOPS AND DATA STRUCTURES LABORATORY

OBJECTIVES:The student should be made to:• Learn C++ programming language.• Be exposed to the different data structures• Be familiar with applications using different data structures

LIST OF EXPERIMENTS:1. Basic Programs for C++ Concepts.2. Array implementation of List Abstract Data Type (ADT).3. Linked list implementation of List ADT.4. Cursor implementation of List ADT.5. Stack ADT ­ Array and linked list implementations.6. The next two exercises are to be done by implementing the following source files

i. Program source files for Stack Application 1ii. Array implementation of Stack ADTiii. Linked list implementation of Stack ADTiv. Program source files for Stack Application 2v. An appropriate header file for the Stack ADT should be included in (i) and (iv)

7. Implement any Stack Application using array implementation of Stack ADT (by implementing files (i) and (ii) given above) and then using linked list.

8. Implementation of Stack ADT (by using files (i) and implementing file (iii)).9. Implement another Stack Application using array and linked list implementations of

Stack ADT (by implementing files (iv) and using file (ii), and then by using files (iv) and (iii)).

10. Queue ADT – Array and linked list implementations.11. Search Tree ADT ­ Binary Search Tree.12. Implement an interesting application as separate source files and using any of

the searchable ADT files developed earlier. Replace the ADT file alone with other appropriate ADT files. Compare the performance.

13. Quick Sort. REFERENCE:spoken­tutorial.org. TOTAL: 45 PERIODS

OUTCOMES:At the end of the course, the student should be able to:

• Design and implement C++ programs for manipulating stacks, queues, linked lists, trees, and graphs.• Apply good programming design methods for program development.• Apply the different data structures for implementing solutions to practical problems.

Ex. No. : 2BASIC C++ PROGRAMS

Date:

www.studentsfocus.com

Page 2:  · ec6312 oops and data structures laboratory objectives:

AIM:

To write C++ programs for implementing basic concepts in C++ language.

PROGRAM:

i) DEFAULT ARGUMENTS

#include<iostream.h>

#include<conio.h>

int add(int a,int b=5); //Function Prototype with one argument as Default argument

void main()

int a,b,c;

cout<<"Enter a Number:";

cin>>a;

c=add(a);

cout<<"sum="<<c;

getch();

int add(int a,int b)

int c=a+b;

return c;

ii) INLINE FUNCTION

#include<iostream.h>

#include<conio.h>

inline int largest(int a,int b,int c) //Inline function

int large=0;

if((a>b)&&(a>c))

www.studentsfocus.com

Page 3:  · ec6312 oops and data structures laboratory objectives:

large=a;

else if(b>c)

large=b;

else

large=c;

void main()

int a,b,c;

clrscr();

cout<<"Enter Three Numbers To Find The Largest "<<"\n;

cout<<"a = "; cin>>a;

cout<<"\nb = "; cin>>b;

cout<<"\nc = "; cin>>c;

int large=largest(a,b,c);

cout<<"\n Largest of "<<a<<","<<b<<" and "<<c<<" is "<<large;

getch();

iii) CLASS AND OBJECT

#include<iostream.h>

#include<conio.h>

class record

public:

char name[20];

int regno,marks,m1,m2,m3;

www.studentsfocus.com

Page 4:  · ec6312 oops and data structures laboratory objectives:

float avg;

void getdata()

cout<<"\nenter the name: " ;

cin>>name;

cout<<"enter the regno: ";

cin>>regno;

cout<<"enter the m1,m2,m3: \n";

cin>>m1>>m2>>m3;

void calculate()

avg=(m1+m2+m3)/3;

void display()

cout<<"******************\n\nName: "<<name;

cout<<"\nRegno: "<<regno;

cout<<"\nMark1: "<<m1;

cout<<"\nMark2: "<<m2;

cout<<"\nMark3: "<<m3;

cout<<"\nAvg: "<<avg<<"******************\n";

;

void main()

record r;

clrscr();

r.getdata();

r.calculate();

r.display();

www.studentsfocus.com

Page 5:  · ec6312 oops and data structures laboratory objectives:

getch();

iv) STATIC DATA MEMBER AND MEMBER FUNCTION

#include<iostream.h>

#include<conio.h>

class test

int code;

static int count; //Declaration of Static data member

public:

void setcode()

code=++count;

void showcode()

cout<<"Object Number:"<<code<<"\n";

static void showcount() //Static member function

cout<<"Count:"<<count<<"\n";

;

int test::count;

void main()

clrscr();

test t1,t2;

t1.setcode();

t2.setcode();

test::showcount(); //Accessing static member function

www.studentsfocus.com

Page 6:  · ec6312 oops and data structures laboratory objectives:

test t3;

t3.setcode();

t1.showcode();

test::showcount(); //Accessing static member function

t2.showcode();

t3.showcode();

getch();

v) OPERATOR OVERLOADING

#include <iostream.h>

#include<conio.h>

class complex

float x;

float y;

public:

complex( )

complex(float real, float image)

x=real;

y=image;

complex operator + (complex);

void display(void);

;

complex complex::operator+(complex c)

complex temp;

temp.x=x+c.x;

temp.y=y+c.y;

return(temp);

www.studentsfocus.com

Page 7:  · ec6312 oops and data structures laboratory objectives:

void complex::display(void)

cout<<x<<"+j"<<y;

int main( )

clrscr( );

complex c1,c2,c3;

c1=complex(2.5,3.5);

c2=complex(1.6,2.7);

c3=c1+c2;

cout<<"c1 =";

c1.display( );

cout<<"c2 =";

c2.display( );

cout<<"c3 =";

c3.display( );

getch( );

return 0;

vi) VIRTUAL FUNCTIONS

#include<iostream.h>

class shape //Base Class

public:

int r,h,a;

float volume;

void virtual volume( )=0;

;

class sphere : public shape //Derived Class 1

www.studentsfocus.com

Page 8:  · ec6312 oops and data structures laboratory objectives:

public:

void input()

cout<<"\n Enter the radius";

cin>>r;

void vol()

V=(4/3)*3.14*r*r*r;

cout<<"\n The Volume of Sphere is "<<V;

;

class cylinder :public shape //Derived Class 2

public:

void input1()

cout<<"\n Enter the radius and height::";

cin>>r>>h;

void volume()

volume=3.14*r*h;

cout<<"\n The Volume of Cylinder is "<<V;

;

class square :public shape //Derived Class 3

public:

void input2()

www.studentsfocus.com

Page 9:  · ec6312 oops and data structures laboratory objectives:

cout<<"\n Enter the side";

cin>>a;

void area()

volume=a*a;

cout<<"\n The Area of Square is "<<V;

;

int main()

clrscr();

sphere S1;

S1.input();

S1.vol();

cylinder C1;

C1.input1();

C1.volume();

square S2;

S2.input2();

S2.area();

getch();

vii) FUNCTION OVERLOADING

www.studentsfocus.com

Page 10:  · ec6312 oops and data structures laboratory objectives:

#include<iostream.h>

#include<conio.h>

// Function overloading­ same function name with different type of arguments for area of circle,

// rectangle and square.

void area(float r);

void area(int l, int b);

void area(int a);

void area(float r)

float area1;

area1=3.14*r*r;

cout<<"\n Area of Circle is "<<area1;

void area(int l, int b)

int area1;

area1=l*b;

cout<<"\n Area of Rectangle is "<<area1;

void area(int a)

float area1;

area1=a*a;

cout<<"\n Area of Square is "<<area1;

void main()

clrscr();

float r1;

int l1,b1,a1;

www.studentsfocus.com

Page 11:  · ec6312 oops and data structures laboratory objectives:

cout<<"\n Enter the radius";

cin>>r1;

area(r1);

cout<<"\n Enter the length and breadth";

cin>>l1>>b1;

area(l1, b1);

cout<<"\n Enter the side";

cin>>a1;

area(a1);

getch();

viii) FRIEND FUNCTION

#include <iostream.h>

#include<conio.h>

class myclass

int a, b;

public:

friend int sum(myclass x);

void set_ab(int i, int j);

;

void myclass::set_ab(int i, int j)

a = i;

b = j;

// Note: sum() is not a member function of any class.

int sum(myclass x)

/* Because sum() is a friend of myclass, it can directly access a and b. */

return x.a + x.b;

www.studentsfocus.com

Page 12:  · ec6312 oops and data structures laboratory objectives:

int main()

myclass n;

n.set_ab(3, 4);

cout << sum(n);

return 0;

RESULT:

Thus the C++ programs for implementing various oops concept have been executed

successfully.Ex. No. : 2

ARRAY IMPLEMENTATION OF LIST ADT Date:

www.studentsfocus.com

Page 13:  · ec6312 oops and data structures laboratory objectives:

AIM:

To write a C++ program for array implementation of List ADT and perform the

following operations:

a) Create b) Insert c) Delete d) Display

ALGORITHM:1. Start the program.

2. Initialize the list with n items.

3. Get the choice of operation from user.

4. For insertion, get the item to be inserted and its position. Then position to last position

move the items in the list one step forward, then insert the new item in its

corresponding position.

5. For deletion get the item to be deleted and find its position then move the items in the

list one step backward from last position to position found.

6. If user choice is display then display the elements.

7. End the program.

PROGRAM:

#include<iostream.h>

#include<conio.h>

#define MAX 10

void create();

void insert();

void deletion();

void search();

void display();

int a,b[20], n, p, e, f, i, pos;

void main()

www.studentsfocus.com

Page 14:  · ec6312 oops and data structures laboratory objectives:

clrscr();

int ch;

char g='y';

do

cout<<"\n Main Menu";

cout<<"\t 1.Create \t 2.Delete \t 3.Search \t 4.Insert \t 5.Display\t 6.Exit \t";

cout<<"\n Enter your Choice";

cin>>ch;

switch(ch)

case 1:

create();

break;

case 2:

deletion();

break;

case 3:

search();

break;

case 4:

insert();

break;

case 5:

display();

break;

case 6:

exit(0);

break;

default:

www.studentsfocus.com

Page 15:  · ec6312 oops and data structures laboratory objectives:

cout<<"\n Enter the correct choice:";

cout<<"\n Do u want to continue:::";

cin>>g;

while(g=='y'||g=='Y');

getch();

void create()

cout<<"\n Enter the number of nodes";

cin>>n;

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

cout<<"\n Enter the Element:"<<i+1;

cin>>b[i];

void deletion()

cout<<"\n Enter the position u want to delete::";

cin>>pos;

if(pos>=n)

cout<<"\n Invalid Location::";

else

for(i=pos+1;i<n;i++)

b[i­1]=b[i];

www.studentsfocus.com

Page 16:  · ec6312 oops and data structures laboratory objectives:

n­­;

cout<<"\n The Elements after deletion";

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

cout<<"\t"<< b[i];

void search()

cout<<"\n Enter the Element to be searched:";

cin>>e;

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

if(b[i]==e)

cout<<"Value is in the Position"<<i;

void insert()

cout<<"\n Enter the position u need to insert::";

cin>>pos;

if(pos>=n)

cout<<"\n invalid Location::";

else

www.studentsfocus.com

Page 17:  · ec6312 oops and data structures laboratory objectives:

for(i=MAX­1;i>=pos­1;i­­)

b[i+1]=b[i];

cout<<"\n Enter the element to insert::\n";

cin>>p;

b[pos]=p;

n++;

cout<<"\n The list after insertion::\n";

display();

void display()

cout<<"\n The Elements of The list ADT are:";

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

cout<<"\n\n"<<b[i];

SAMPLE OUTPUT:

www.studentsfocus.com

Page 18:  · ec6312 oops and data structures laboratory objectives:

RESULT:

Thus the C++ program for array implementation of List ADT has been executed

successfully.Ex. No. : 3

LINKED LIST IMPLEMENTATION OF LIST ADT Date:

www.studentsfocus.com

Page 19:  · ec6312 oops and data structures laboratory objectives:

AIM:

To write a C++ program to create the linked list and perform the following operations:

a) Create b) Insert c) Delete d) Display.

DESCRIPTION:

A linked list is a data structure that consists of a sequence of data records such that in

each record there is a field that contains a reference (i.e., a link) to the next record in the

sequence. Each record of a linked list is often called an element or node.

The field of each node that contains address of the next node is usually called the next

link or next pointer. The remaining field may be called the data, information, value field. The

head of a list is its first node and the tail is the list minus that node.

Fig.1. Linked List whose nodes contain two fields: an integer value and a link to the next

node.

The principal benefit of a linked list over a conventional array is that the order of the

linked items may be the order that the data items are stored in memory or on disk. For that

reason, linked lists allow insertion and removal of nodes at any point in the list, with a constant

number of operations.

ALGORITHM:

Step 1: Start and include all the necessary header files.

Step 2: Create the structure 'node' having element (data) and pointer to next node, start node.

Step 3: Create a class as list and declare the functions create( ), insert( ), del( ), display( ).

Step 4: Defining the function create( ) to create a set of nodes as a new list through,

a) Declare a pointer variable *nxt_node,*pre_node for structure and assign NULL

value to it.

b) Declare the integer variables value, no and i.

c) Get the value for number of nodes (no) to be formed. Form a loop until 'i' is

less than or equal to 'no', get the elements (data) to be inserted.

d) Now allocate memory for new node and make the pointer of previous node to

point to the new element and make new element as last element.

34 56 99

www.studentsfocus.com

Page 20:  · ec6312 oops and data structures laboratory objectives:

Step 5: Create a object for class list and call the function where it is necessary.

Step 6: Construct the program using switch statement.

Step 7:.Stop the program.

PROGRAM:

#include<conio.h>

#include<iostream.h>

struct node // Creating a NODE Structure

int data; // element

struct node *next,*start; // link to next node and start node

;

class list // Creating a class LIST

struct node *start;

public:

void create(); // to create a list

void insert(); // insertion

void del(); // deletion

void display(); // display

;

void list::create() // Creating a new node

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 "<<i<<" ELEMENT: ";

cin>>value;

www.studentsfocus.com

Page 21:  · ec6312 oops and data structures laboratory objectives:

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 Linked List is created!";

display();

void list::display() // Displaying LIST

int data;

struct node *ptr=start;

cout<<"\n\nThe Linked List is \n";

while(ptr!=NULL)

data=ptr­>data;

cout<<data<<" ­> ";

ptr=ptr­>next;

cout<<"\b\b\b ";

getch();

void list::insert() // Insert node at any position

int position,dat;

struct node *ptr_b,*ptr_f,*ptr,*nxt_node,*pre_node;

cout<<"\nInsertion of a new node \n";

www.studentsfocus.com

Page 22:  · ec6312 oops and data structures laboratory objectives:

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

www.studentsfocus.com

Page 23:  · ec6312 oops and data structures laboratory objectives:

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

void list::del() //Delete node from any position

int position,dat;

struct node *ptr_b,*ptr_f,*ptr,*pntr;

cout<<"Enter the data to be deleted: ";

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

www.studentsfocus.com

Page 24:  · ec6312 oops and data structures laboratory objectives:

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

www.studentsfocus.com

Page 25:  · ec6312 oops and data structures laboratory objectives:

int main() // Main function

clrscr();

list list1;

list1.create(); // to create a new node

int choice;

while(1)

cout<<"\n­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­\n\t\t LINKED LIST\n\n";

cout<<"1:Insertion\n2:Deletion\n3:Display List\n4:Exit";

cout<<"\nEnter your choice between(1to4): ";

cin>>choice;

switch(choice)

case 1: // Insertion

list1.insert(); break;

case 2: // Deletion

list1.del(); break;

case 3: // Display

list1.display(); break;

case 4: // Exit

exit(0); break;

default:

cout<<"Please enter correct choice between (1to4)!!";

getch(); break;

return 0;

SAMPLE OUTPUT:

www.studentsfocus.com

Page 26:  · ec6312 oops and data structures laboratory objectives:

RESULT:

Thus the C++ program for Linked list implementation of List ADT has been executed

successfully.Ex. No. : 4

CURSOR IMPLEMENTATION OF LIST ADTDate:

www.studentsfocus.com

Page 27:  · ec6312 oops and data structures laboratory objectives:

AIM:

To write a C++ program for cursor implementation of list ADT.

ALGORITHM:

1. Start the program.

2. Create a node with two fields data and link field.

o Allocate space for the node dynamically.

o Create link between the created nodes and let the last node be with NULL

Link

o Insert the input data in the data field and press –1 to stop the same.

3. Get the choice of operations either insertion or deletion.

4. For insertion get the position in which insertion is to be done and the element to be

inserted. Check for the start, middle or end position of insertion. Insert the node and

change its link accordingly.

5. For deletion get the position in which deletion is to be done. Delete the node and then

link it to the next node. Before deletion check whether there is data in the list to be

deleted.

6. Using display option list the elements of the list.

7. Stop the program.

PROGRAM:

#include<iostream.h>

#include<conio.h>

#include<stdlib.h>

#define MAX 20

class LIST

private:

int List[MAX];

public:

int create();

void display(int);

int search(int);

www.studentsfocus.com

Page 28:  · ec6312 oops and data structures laboratory objectives:

void delet(int);

;

int LIST::create()

int n,i;

cout<<"\n how many elements you want in the list:";

cin>>n;

if(n>MAX)

cout<<"\n Error:Number of elements exceeds the limit";

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

cout<<"\nEnter the element number"<<i+1<<":";

cin>>List[i];

cout<<"\n The List is successfully created\n";

return(n);

void LIST::display(int n)

int i;

cout<<"\n the list is:\n";

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

cout<<"\n"<<List[i];

int LIST::search(int n)

int i,key;

cout<<"\n enter the number you want to search?";

cin>>key;

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

www.studentsfocus.com

Page 29:  · ec6312 oops and data structures laboratory objectives:

if(List[i]==key)

cout<<"\n the given number is at position:"<<i<<"\n";

return i;

cout<<"\n the given number is not in the list\n";

return ­1;

void LIST::delet(int n)

int i;

i=search(n);

if(i!=NULL)

List[i]=­1;

cout<<"Element deleted";

void main()

LIST obj;

int choice,len,position;

char ans;

clrscr();

cout<<"\n\t program to perform operations on ordered list";

cout<<"\n 1.Create\n 2.Display\n3.Search an Element\n4.Delete\n5.Exit";

do

cout<<"\n Enter your choice between(1­5)";

cin>>choice;

www.studentsfocus.com

Page 30:  · ec6312 oops and data structures laboratory objectives:

switch(choice)

case 1:

len=obj.create();

break;

case 2:

obj.display(len);

break;

case 3:

position=obj.search(len);

break;

case 4:

obj.delet(len);

break;

case 5:

exit(0);

break;

default:

cout<<"\n Invalid choice, try again";

break;

getch();

while(choice!=5);

SAMPLE OUTPUT:

www.studentsfocus.com

Page 31:  · ec6312 oops and data structures laboratory objectives:

RESULT:

Thus the C++ program for cursor implementation of List ADT was executed

successfully.Ex. No. : 5.1

IMPLEMENTATION OF STACK ADT USING ARRAYDate:

www.studentsfocus.com

Page 32:  · ec6312 oops and data structures laboratory objectives:

AIM:

To write a C++ program to implement Stack ADT using array and perform the following

operations:

a) Push b) Pop c) Display.

ALGORITHM:

Step 1: Start

Step 2: Declare the class for the stack pointers.

Step 3: Inside the class, define the push, pop and display function.

Step 4: Read the choice

Step 5: If choice = push

Create a cell for the TOP cell in the stack.

Place the date in the TOP cell

Place the TOP pointer to the new cell

Step 6: If choice=pop

Check if empty stack. If so, print stack is empty.

Otherwise, remove the TOP cell.

Step 7: If choice=display

Display all the elements in the Stack.

Step 8: Stop the program.

PROGRAM:

#include<iostream.h>

#include<conio.h>

#include<stdlib.h>

int const max=5;

class stack

int stk[max];

int top;

public:

www.studentsfocus.com

Page 33:  · ec6312 oops and data structures laboratory objectives:

stack()

top=­1;

void push(int x)

if(top= =max­1)

cout <<"Stack over flow";

return;

else

top=top+1;

stk[top]=x;

cout <<"Element pushed(inserted) onto the stack" <<"\t"<<x;

void pop()

if(top<0)

cout <<"Stack under flow";

return;

else

cout <<"Element popped(Deleted) out of stack" <<"\t"<<stk[top­­];

void display()

int i;

if(top<0)

www.studentsfocus.com

Page 34:  · ec6312 oops and data structures laboratory objectives:

cout <<" Stack empty";

return;

for(i=top;i>=0;i­­)

cout<<stk[i]<<" ";

;

void main()

int ch;

stack st;

clrscr();

cout<<"\t\tImplementation of Stack ADT­using ARRAY\n";

cout <<"\n1.Push\t\t2.Pop\t\t3.Display\t\t4.Exit";

while(1)

cout<<"\nEnter your choice";

cin >> ch;

switch(ch)

case 1:

cout <<"Enter the element";

cin >> ch;

st.push(ch);

break;

case 2:

st.pop();

break;

case 3:

st.display();

break;

www.studentsfocus.com

Page 35:  · ec6312 oops and data structures laboratory objectives:

case 4:

exit(0);

getch();

SAMPLE OUTPUT:

RESULT:

Thus the C++ program for implementation of Stack ADT using array was executed

successfully.Ex. No. : 5.2

IMPLEMENTATION OF STACK ADT USING LINKED LISTDate:

www.studentsfocus.com

Page 36:  · ec6312 oops and data structures laboratory objectives:

AIM:

To write a C++ program to implement Stack ADT using linked list and perform the

following operations:

a) Push b) Pop c) Display.

ALGORITHM:

Step 1: Start the program.

Step 2: Create a structure as node with two fields, data and next pointer to point the next node.

Step 3: Declare "top" pointer to refer the top of the stack, which is assigned initially as NULL.

Step 4: Get the choice from the user and perform the stack operations.

Step 5: If choice==1, then perform PUSH operation as,

i) Get the element to be inserted on to the stack.

ii) If the stack is empty, make the element as data and next pointer as NULL and

assign it as top of the stack.

. iii) Repeat until top pointer reaches the maximum size.

Step 6: If choice==2, then perform POP operation as,

i) If top next is NULL then return as stack is empty.

ii) Otherwise, assign top as temp and top next as top and return (delete) the

temp as value popped from the stack.

Step 7: If choice==3, then perform DISPLAY function to list all elements from top of the stack

until stack is empty (i.e.,) until TOP==NULL.

Step 8: If choice==4, exit the program.

Step 9: Stop the program.

PROGRAM:

#include<iostream.h>

www.studentsfocus.com

Page 37:  · ec6312 oops and data structures laboratory objectives:

#include<malloc.h>

#include<conio.h>

#include<stdlib.h>

struct node

int info;

struct node *next;

;

class stack

struct node *top;

public:

stack();

void push();

void pop();

void display();

;

stack::stack()

top=NULL;

void stack::push()

int data;

struct node *p;

if((p=(node*)malloc(sizeof(node)))==NULL)

cout<<"Memory Exhausted";

exit(0);

cout<<"Enter a Number to insert:";

www.studentsfocus.com

Page 38:  · ec6312 oops and data structures laboratory objectives:

cin>>data;

p = new node;

p­>info = data;

p­>next = NULL;

if(top!=NULL)

p­>next = top;

top = p;

cout<<"\nNew item inserted"<<endl;

void stack::pop()

struct node *temp;

if(top==NULL)

cout<<"\nThe stack is Empty"<<endl;

else

temp = top;

top = top­>next;

cout<<"\nThe value popped is "<<temp­>info<<endl;

delete temp;

void stack::display()

www.studentsfocus.com

Page 39:  · ec6312 oops and data structures laboratory objectives:

struct node *p = top;

if(top==NULL)

cout<<"\nNothing to Display\n";

else

cout<<"\nThe contents of Stack\n";

while(p!=NULL)

cout<<p­>info<<endl;

p = p­>next;

int main()

clrscr();

stack s;

int choice;

cout<<"\nImplementation of Stack ADT­Linked List";

do

cout<<"\n1. PUSH\t\t2. POP\t\t3. DISPLAY\t\t4. EXIT\n";

cout<<"Enter your choice:";

cin>>choice;

switch(choice)

www.studentsfocus.com

Page 40:  · ec6312 oops and data structures laboratory objectives:

case 1:

s.push();

break;

case 2:

s.pop();

break;

case 3:

s.display();

break;

case 4:

exit(0);

break;

default:

cout<<"Invalid Choice";

break;

while(choice);

getch();

return 0;

SAMPLE OUTPUT:

www.studentsfocus.com

Page 41:  · ec6312 oops and data structures laboratory objectives:

RESULT:

Thus the C++ program for implementation of Stack ADT using linked list was executed

successfully.Ex. No. : 6

CREATION OF HEADER FILES FOR STACK ADTDate:

www.studentsfocus.com

Page 42:  · ec6312 oops and data structures laboratory objectives:

AIM:

To create program source files for Stack ADT using array and linked list implementation

for executing the next two exercises such as the stack applications.

PROGRAM:

a) Program source file for implementation of Stack ADT using array (to be included as

header file in another program).

//Save this program as "stack.h"

#include<iostream.h>

#include<conio.h>

#define size 10

class stk_class

private:

struct stack /*stack structure*/

char s[size];

int top;

st;

public:

stk_class( );

void push(char item);

int stempty( );

int stfull( );

char pop( );

;

stk_class::stk_class( )

st.top=­1;

www.studentsfocus.com

Page 43:  · ec6312 oops and data structures laboratory objectives:

void stk_class::push(char item)

st.top++;

st.s[st.top]=item;

int stk_class::stempty( )

if(st.top==­1)

return 1;

else

return 0;

int stk_class::stfull( )

if(st.top==size)

return 1;

else

return 0;

char stk_class::pop( )

char item;

item=st.s[st.top];

st.top­­;

return(item);

a) Program source file for implementation of Stack ADT using linked list (to be included

as header file in another program).

www.studentsfocus.com

Page 44:  · ec6312 oops and data structures laboratory objectives:

//Save this program as "stackll.h"

class stk_class

private:

typedef struct stack /*data structure for the linked stack*/

char data;

struct stack * next;

node;

node *top;

public:

stk_class();

void push(char item);

int sempty();

void pop();

;

void stk_class::push(char item)

node * element;

element=new node;

if(element==NULL)

cout<<”\n memory cannot be allocated \n”;

else

element­>data=item;

element­>next=top;

top=element;

stk_class ::stk_class()

www.studentsfocus.com

Page 45:  · ec6312 oops and data structures laboratory objectives:

top=NULL;

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;

RESULT:

Thus the C++ program to create program source files (header files) for Stack ADT

using array and linked list implementation was executed successfully.Ex. No. : 7 BALANCING PARENTHESIS

(IMPLEMENTED USING STACK ADT AS HEADER FILES)Date:

www.studentsfocus.com

Page 46:  · ec6312 oops and data structures laboratory objectives:

AIM:

To implement Stack application­Balancing Parenthesis using header files for Stack

ADT.

ALGORITHM:

Step 1: Create a header file named stack.h, in this file we will declare the class for stack and all

stack operations are implemented using arrays.

Step 2: Include the library files and stack header file as stack.h.

Step 3: Declare and define the object for the class in stack.h to call the push and pop functions.

Step 4: Read the input expression from the user (place $ at the end of the expression as end

marker).

Step 5: Scan each character in the expression

i) If "(" then push it on to the stack and continue the same until find the matching

right parenthesis.

ii) If the ")"is encountered ,then pop the corresponding "(" parenthesis.

iii) Repeat the above steps until you read "$".

Step 6: On reading "$", if the stack is empty then the symbols are balanced. Otherwise they not

balanced and its invalid expression.

Step 7: Stop the program.

PROGRAM:

A) APPLICATION OF STACK IMPLEMENTED USING ARRAYS (USING THE

HEADER FILE FOR STACK AND ITS OPERATIONS)

Step 1: Create a header file named stack.h, in this file we will declare the class for

stack and all the stack operations are implemented using arrays.

//Save this program as "stack.h"

#include<iostream.h>

#include<conio.h>

#define size 10

class stk_class

www.studentsfocus.com

Page 47:  · ec6312 oops and data structures laboratory objectives:

private:

struct stack /*stack structure*/

char s[size];

int top;

st;

public:

stk_class( );

void push(char item);

int stempty( );

int stfull( );

char pop( );

;

stk_class::stk_class( )

st.top=­1;

void stk_class::push(char item)

st.top++;

st.s[st.top]=item;

int stk_class::stempty( )

if(st.top==­1)

return 1;

else

return 0;

int stk_class::stfull( )

www.studentsfocus.com

Page 48:  · ec6312 oops and data structures laboratory objectives:

if(st.top==size)

return 1;

else

return 0;

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

“Balancing Symbols or checking well formedness of parenthesis”, for this application we

will use header file created in STEP 1 to include stack and its operations. Finally compile

and run the .cpp file to get the results.

//Save this program as "balancesymbol.cpp"

#include<stdlib.h>

#include<iostream.h>

#include<conio.h>

#include “d:\stack.h” //Specify the path of your header file "stack.h"

#define size 10

void main(void)

char item;

char ans,bracket[10];

stk_class obj;

int i;

www.studentsfocus.com

Page 49:  · ec6312 oops and data structures laboratory objectives:

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]==’)’||bracket[i]==’]’||bracket[i]==’’)

cout<<”\n the expressin is invalid”;

else

do

while(bracket[i]==’(‘||bracket[i]==’[’||bracket[i]==’’)

obj.push(bracket[i]);

i++;

while(bracket[i]==’)’||bracket[i]==’]’||bracket[i]==’’)

item=obj.pop();

i++;

while(bracket[i]!=’$’);

if(!obj.stempty())

cout<<”\n the expression is invalid”;

else

cout<<”\n the expression has well formed parenthesis”;

getch();

B) APPLICATION OF STACK IMPLEMENTED USING LINKED LISTS (USING

www.studentsfocus.com

Page 50:  · ec6312 oops and data structures laboratory objectives:

THE HEADER FILE FOR STACK AND ITS OPERATIONS)

Step 1: Create a header file named stackll.h, in this file we will declare the class for

stack and all the stack operations are implemented using linked lists.

//Save this program as "stackll.h"

class stk_class

private:

typedef struct stack /*data structure for the linked 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 * element;

element=new node;

if(element==NULL)

cout<<”\n memory cannot be allocated \n”;

else

www.studentsfocus.com

Page 51:  · ec6312 oops and data structures laboratory objectives:

element­>data=item;

element­>next=top;

top=element;

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

“Balancing Symbols or checking well formedness of parenthesis”, for this application we

will use header file created in STEP 1 to include stack and its operations. Finally compile

and run the .cpp file to get the results.

//Save this program as "balancesymbol.cpp"

#include<stdlib.h>

#include<iostream.h>

#include<conio.h>

#include “d:\stackll.h” //Specify the path of your header file "stackll.h"

www.studentsfocus.com

Page 52:  · ec6312 oops and data structures laboratory objectives:

#define size 10

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]==’)’||bracket[i]==’]’||bracket[i]==’’)

cout<<”\n the expressin is invalid”;

else

do

while(brackt[i]==’(‘||bracket[i]==’[’||bracket[i]==’’)

obj.push(bracket[i]);

i++;

while(bracket[i]==’)’||bracket[i]==’]’||bracket[i]==’’)

item=obj.pop();

i++;

while(brackt[i]!=’$’);

if(!obj.stempty())

cout<<”\n the expression is invalid”;

www.studentsfocus.com

Page 53:  · ec6312 oops and data structures laboratory objectives:

else

cout<<”\n the expression has well formed parenthesis”;

getch();

SAMPLE OUTPUT:

RESULT:

Thus the C++ program for implementing stack application­balancing symbols using

header files was executed successfully.Ex. No. : 8 EVALUATION OF POSTFIX EXPRESSION

(IMPLEMENTED USING STACK ADT AS HEADER FILES)Date:

www.studentsfocus.com

Page 54:  · ec6312 oops and data structures laboratory objectives:

AIM:

To implement Stack application ­ Evaluation of Postfix (arithmetic) Expression using

header files for Stack ADT.

ALGORITHM:

Step 1: Start

Step 2: First initialize the stack to be empty

Step 3: For each character in the input string,

i) If input string is an operand or number, append to the output.

ii) If the input string is a left parenthesis, push it onto the stack.

Step 4: If input string is an operator, then

i) Remove top two elements from the stack such as ‘A’ and ‘B’, Evaluate “B

operator A”.

ii) Place the result back into the stack.

Step 5: Repeat the steps 3 & 4, until the end marker “#” is read.

Step 6: Return the value at the top of the stack as result.

Step 7: Stop the program.

PROGRAM:

A) APPLICATION OF STACK IMPLEMENTED USING ARRAYS (USING THE

HEADER FILE FOR STACK AND ITS OPERATIONS)

Step 1: Create a header file named stack.h, in this file we will declare the class for

stack and all the stack operations are implemented using arrays.

Step 2: Now we will create a stack application program. We have chosen an

application as “Evaluation of Postfix Expression”, for this application we will use header

file created in STEP 1 to include stack and its operations. Finally compile and run the .cpp

file to get the results.

// Save this file as "evalpostfix.cpp"

#include<iostream.h>

#include<conio.h>

#include<stdlib.h>

www.studentsfocus.com

Page 55:  · ec6312 oops and data structures laboratory objectives:

#include "d:\stack.h" //Specify the path of your header file "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);

exp[len]=’$’; /*append $ at the end of expression as 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=0;

ch=exp[i];

while(ch!=’$’)

if(ch>=’0’&&ch<=’9’) /* if the Number is operand*/

type=”operand”:

else if(ch==’+’||ch==’­‘||ch==’*’ ||ch==’/’||ch==’^’)

type=”operator”;

www.studentsfocus.com

Page 56:  · ec6312 oops and data structures laboratory objectives:

if(strcmp(type,”operand”)==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++;

www.studentsfocus.com

Page 57:  · ec6312 oops and data structures laboratory objectives:

ch=exp[i];

result=obj.pop(); /*pop the result*/

return(result);

B) APPLICATION OF STACK IMPLEMENTED USING LINKED LIST (USING THE

HEADER FILE FOR STACK AND ITS OPERATIONS)

Step 1: Create a header file named stackll.h, in this file we will declare the class for

stack and all the stack operations are implemented using linked list.

Step 2: Now we will create a stack application program. We have chosen an

application as “Evaluation of Postfix Expression”, for this application we will use header

file created in STEP 1 to include stack and its operations. (i.e) change this line of code in

the previous "evalpostfix.cpp" program as #include "d:\stackll.h" (Specify the path of

your header file "stackll.h" actually located).

Step 3: Finally compile and run the evalpostfix.cpp file to get the results using

linked stack.

SAMPLE OUTPUT:

RESULT:

Thus the C++ program for implementing stack application – evaluation of postfix

expression using header files has been executed successfully.Ex. No. : 9 CONVERSION OF INFIX TO POSTFIX EXPRESSION

(IMPLEMENTED USING STACK ADT AS HEADER FILES)Date:

AIM:

www.studentsfocus.com

Page 58:  · ec6312 oops and data structures laboratory objectives:

To implement Stack application – Conversion of Infix to Postfix Expression using

header files for Stack ADT.

ALGORITHM:

Step 1: Initialize an empty stack.

Step 2: Scan the Infix expression from left to right.

Step 3: If the scanned character is an operand, add it to the postfix expression i.e., output

array.

Step 4: If the scanned character is open parenthesis ‘(‘then push it into the stack.

Step 5: If the scanned character is an operator and if the stack is empty, then push it to the

stack.

Step 6: If the scanned character is operator and stack is not empty, then

i) Compare the precedence of scanned character with the operator on

the top of the stack.

ii) While operator at the top of stack has higher precedence over the scanned

character & stack is not empty,

a) Pop the stack.

b) Add the popped character to postfix string i.e., output array.

iii) Push the scanned character to top of the stack.

Step 7: If the scanned operator is less than or equal to the top of the stack then

i) Push the scanned operator into the stack

Step 8: If the scanned character is closing parentheses ’)’ then pop all the characters

above opening parenthesis ‘(‘from the stack.

Step 9: Repeat the steps 3­8 till all the characters are scanned.

Step 10: While stack not empty, pop the elements to the output array until stack is empty.

Step 11: Return the postfix string i.e., expression from output array.

Step 12: Stop the program.

PROGRAM:

Step 1: Create a header file named intopost.h, in this file we will declare the class for

stack and all the stack operations are implemented using arrays along with the steps for

www.studentsfocus.com

Page 59:  · ec6312 oops and data structures laboratory objectives:

converting infix to postfix expression.

//Save this program as intopost.h

#include <iostream.h>

#include <string.h>

#include <ctype.h>

#include<conio.h>

const int MAX = 50 ;

class infix

private :

char target[MAX], stack[MAX] ;

char *s, *t ;

int top ;

public :

infix( ) ;

void setexpr ( char *str ) ;

void push ( char c ) ;

char pop( ) ;

void convert( ) ;

int priority ( char c ) ;

void show( ) ;

;

infix :: infix( )

top = ­1 ;

strcpy ( target, "" ) ;

strcpy ( stack, "" ) ;

t = target ;

s = "" ;

www.studentsfocus.com

Page 60:  · ec6312 oops and data structures laboratory objectives:

void infix :: setexpr ( char *str )

s = str ;

void infix :: push ( char c )

if ( top == MAX )

cout << "\nStack is full\n" ;

else

top++ ;

stack[top] = c ;

char infix :: pop( )

if ( top == ­1 )

cout << "\nStack is empty\n" ;

return ­1 ;

else

char item = stack[top] ;

top­­ ;

return item ;

void infix :: convert( )

while ( *s )

www.studentsfocus.com

Page 61:  · ec6312 oops and data structures laboratory objectives:

if ( *s == ' ' || *s == '\t' )

s++ ;

continue ;

if ( isdigit ( *s ) || isalpha ( *s ) )

while ( isdigit ( *s ) || isalpha ( *s ) )

*t = *s ;

s++ ;

t++ ;

if ( *s == '(' )

push ( *s ) ;

s++ ;

char opr ;

if ( *s == '*' || *s == '+' || *s == '/' || *s == '%' || *s == '­' || *s == '^' )

if ( top != ­1 )

opr = pop( ) ;

while ( priority ( opr ) >= priority ( *s ) )

*t = opr ;

t++ ;

opr = pop( ) ;

www.studentsfocus.com

Page 62:  · ec6312 oops and data structures laboratory objectives:

push ( opr ) ;

push ( *s ) ;

else

push ( *s ) ;

s++ ;

if ( *s == ')' )

opr = pop( ) ;

while ( ( opr ) != '(' )

*t = opr ;

t++ ;

opr = pop( ) ;

s++ ;

while ( top != ­1 )

char opr = pop( ) ;

*t = opr ;

t++ ;

*t = '\0' ;

int infix :: priority ( char c )

if ( c == '^' )

www.studentsfocus.com

Page 63:  · ec6312 oops and data structures laboratory objectives:

return 3 ;

if ( c == '*' || c == '/' || c == '%' )

return 2 ;

else

if ( c == '+' || c == '­' )

return 1 ;

else

return 0 ;

void infix :: show( )

cout << target ;

Step 2: We will create a stack application program and chosen the application as

“Conversion of Infix to Postfix Expression”, for this application we will use header file

created in STEP 1 to include stack and its operations. (i.e) include this line of code

#include "d:\stackll.h" in the "infix.cpp" program (Specify the path of your header file

actually located).

//Save this program as infix.cpp//

#include<iostream.h>

#include"d:\intopost.h"

void main( )

char expr[MAX] ;

infix q ;

clrscr();

cout << "\nEnter an expression in infix form: " ;

www.studentsfocus.com

Page 64:  · ec6312 oops and data structures laboratory objectives:

cin.getline ( expr, MAX ) ;

q.setexpr ( expr ) ;

q.convert( ) ;

cout << "\nThe postfix expression is: " ;

q.show( ) ;

getch();

Step 3: Now run the program infix.cpp to get the appropriate results of your input

infix expression.

SAMPLE OUTPUT:

RESULT:

Thus the C++ program for implementing stack application – conversion of infix to

postfix expression using header files has been executed successfully.Ex. No. : 10.1

IMPLEMENTATION OF QUEUE ADT USING ARRAYDate:

AIM:

www.studentsfocus.com

Page 65:  · ec6312 oops and data structures laboratory objectives:

To write a C++ program to implement Queue ADT using array and perform the

following operations:

a) Enqueue b) Dequeue c) Display.

ALGORITHM:

i) Enqueue( ):

Description: Here QUEUE is an array with N locations. FRONT and REAR points to the front

and rear of the QUEUE. ITEM is the value to be inserted.

1. If (REAR == N) Then [Check for overflow]

2. Print: Overflow

3. Else

4. If (FRONT and REAR == 0) Then [Check if QUEUE is empty]

(a) Set FRONT = 1

(b) Set REAR = 1

5. Else

6. Set REAR = REAR + 1 [Increment REAR by 1]

[End of Step 4 If]

7. QUEUE[REAR] = ITEM

8. Print: ITEM inserted

[End of Step 1 If]

9. Exit

ii)Dequeue ( ):

Description: Here QUEUE is an array with N locations. FRONT and REAR points to the front

and rear of the QUEUE.

1. If (FRONT == 0) Then [Check for underflow]

2. Print: Underflow

3. Else

4. ITEM = QUEUE[FRONT]

5. If (FRONT == REAR) Then [Check if only one element is left]

(a) Set FRONT = 0

(b) Set REAR = 0

www.studentsfocus.com

Page 66:  · ec6312 oops and data structures laboratory objectives:

6. Else

7. Set FRONT = FRONT + 1 [Increment FRONT by 1]

[End of Step 5 If]

8. Print: ITEM deleted

[End of Step 1 If]

9. Exit

PROGRAM:

#include<iostream.h>

#include<conio.h>

#include<stdlib.h>

int const max=5;

class queue

int queue1[max];

int rear,front;

public:

queue()

rear=­1;

front=­1;

void insert(int);

void delet();

void display();

;

void queue::insert(int x)

if(rear==max­1)

www.studentsfocus.com

Page 67:  · ec6312 oops and data structures laboratory objectives:

cout <<"queue over flow";

front=rear=­1;

return;

rear=rear+1;

queue1[rear]=x;

cout <<"inserted: " <<x;

void queue::delet()

if(front==max­1)

cout <<"queue under flow";

return;

cout <<"deleted: "<<queue1[++front];

void queue::display()

if(rear==front)

cout <<"queue empty";

return;

for(int i=front+1;i<=rear;i++)

cout <<queue1[i]<<" ";

void main()

int ch;

www.studentsfocus.com

Page 68:  · ec6312 oops and data structures laboratory objectives:

clrscr();

queue qu;

while(1)

cout <<"\n1.Enqueue 2.dequeue 3.display 4.exit\nEnter your choice: ";

cin >> ch;

switch(ch)

case 1:

cout <<"Enter an element: ";

cin >> ch;

qu.insert(ch);

break;

case 2:

qu.delet();

break;

case 3:

qu.display();

break;

case 4:

exit(0);

SAMPLE OUTPUT:

www.studentsfocus.com

Page 69:  · ec6312 oops and data structures laboratory objectives:

RESULT:

Thus the C++ program for implementation of Queue ADT using array was executed

successfully.Ex. No. : 10.2

IMPLEMENTATION OF QUEUE ADT LINKED LISTDate:

www.studentsfocus.com

Page 70:  · ec6312 oops and data structures laboratory objectives:

AIM:

To write a C++ program to implement Queue ADT using linked list and perform the

following operations:

a) Enqueue b) Dequeue c) Display.

ALGORITHM:

Step 1: Start

Step 2: Define structure for queue

Step 3: Read choice

Step 4: If choice = Enqueue,

i) Read the element

ii) Create a data structure

iii) If empty queue then front of queue pointer points to newly created node,

Otherwise end of the queue points to newly created node.

Step 5: If choice = Dequeue,

i) Check if queue is empty. If so, print queue is empty.

ii) Otherwise read the element pointed by front of the queue, temp pointer points

to front of queue.

iii) Front of queue points to next element, free the element pointed by temp

pointer.

iv) Return and Print the element.

Step 6: If choice = display

i) Check of empty queue if so, print queue is empty.

ii) Otherwise print the elements from front of the queue until the end of the queue.

Step 7: If choice = exit, stop.

PROGRAM:

www.studentsfocus.com

Page 71:  · ec6312 oops and data structures laboratory objectives:

#include<iostream.h>

#include<conio.h>

#include<process.h>

struct node

int info;

struct node *next;

*front,*rear;

void enqueue(int n);

void dequeue();

void display();

int main()

int ch,n;

rear=NULL;

front=NULL;

while(1)

cout<<"\n\n\tMENU";

cout<<"\n#############################";

cout<<"\n1. Insert\n2. Delete\n3. Display\n4. Exit\n";

cout<<"\nEnter Your Choice: ";

cin>>ch;

switch(ch)

case 1:

cout<<"\nEnter The Element: \n";

cin>>n;

enqueue(n);

display();

break;

www.studentsfocus.com

Page 72:  · ec6312 oops and data structures laboratory objectives:

case 2:

dequeue();

break;

case 3:

display();

break;

case 4:

exit(0);

break;

default:

cout<<"\nWrong Choice!!! Try Again.";

getch();

return 0;

void enqueue(int n)

node *p;

p=new node[sizeof(node)];

p­>info=n;

p­>next=NULL;

if(rear==NULL||front==NULL)

front=p;

else

rear­>next=p;

rear=p;

void dequeue()

www.studentsfocus.com

Page 73:  · ec6312 oops and data structures laboratory objectives:

node *p;

int n;

if(front==NULL||rear==NULL)

cout<<"\nQueue Empty!!!";

else

p=front;

n=p­>info;

front=front­>next;

delete(p);

cout<<"The Deleted Element = "<<n<<"\n";

display();

void display()

node *t;

t=front;

if(front==NULL||rear==NULL)

cout<<"\nQueue Empty!!!";

else

cout<<"\nQueue Elements are:\n";

while(t!=NULL)

cout<<t­>info<<"\n";

www.studentsfocus.com

Page 74:  · ec6312 oops and data structures laboratory objectives:

t=t­>next;

SAMPLE OUTPUT:

www.studentsfocus.com

Page 75:  · ec6312 oops and data structures laboratory objectives:

RESULT:

Thus the C++ program for implementation of Queue ADT using linked list was executed

successfully.Ex. No. : 11IMPLEMENTATION OF BINARY SEARCH TREE

Date:

www.studentsfocus.com

Page 76:  · ec6312 oops and data structures laboratory objectives:

AIM:

To write a C++ program for implementation of Binary Search tree and perform the

following operations:

a) Create b) Search c) Delete d) Display

ALGORITHM:

Step 1: Start the Program.

Step 2: Declare structure for binary search tree to hold the root (element) and the pointer field to

link the left and right subtree or node.

Step 3: Get the elements one by one and insert it using insert( ) function as follows:

(i) Place the first element as root node.

(ii) Repeat the below steps for next subsequent insertions,

a) If the element is less than the root, then place it as the left child.

b) If the element is greater than the root, then place it as the right child.

c) If the left or right node is already present, then repeat the above two steps for

the input so that it inserts as left or right child of that subtree.

Step 4: For search any element in the tree, compare the search key with the root element and then

(i) If the search key is less than the root, then search recursively in left subtree.

(ii) If the search key is greater than the root, then search recursively in right subtree.

(iii) Repeat the above two steps until the element is found, otherwise return as element

not found.

Step 5: For delete, get the element to be deleted from the user and compare the element with the

root element then,

(i) If the element is leaf node then delete it.

(ii) If the element is the root node with one child then delete it and link the child to the

grand parent.

(iii) If the element is the root node with two children then delete it and make the smallest

value among two children as root.

Step 6: Finally display function is used the list the elements of the tree.

Step 7: Stop the program.

PROGRAM:

www.studentsfocus.com

Page 77:  · ec6312 oops and data structures laboratory objectives:

#include<iostream.h>

#include<conio.h>

class bintree

typedef struct bst

int data;

struct bst *left,*right;

node;

node *root,*element,*temp,*parent;

public:

bintree()

root=NULL;

void create();

void display();

void delet();

void find();

void insert(node *,node*);

void inorder(node *);

void search(node**,int,node **);

void del(node *,int);

;

void bintree::create()

element=new node;

element­>left=NULL;

element­>right=NULL;

cout<<"\n enter the element";

cin>>element­>data;

www.studentsfocus.com

Page 78:  · ec6312 oops and data structures laboratory objectives:

if(root==NULL)

root=element;

else

insert(root,element);

void bintree::insert(node *root,node *element)

if(element­>data<root­>data)

if(root­>left==NULL)

root­>left=element;

else

insert(root­>left,element);

if(element­>data>root­>data)

if(root­>right==NULL)

root­>right=element;

else

insert(root­>right,element);

void bintree::display()

if(root==NULL)

cout<<"tree is not created";

else

cout<<"\n the tree is: ";

inorder(root);

www.studentsfocus.com

Page 79:  · ec6312 oops and data structures laboratory objectives:

void bintree::inorder(node *temp)

if(temp!=NULL)

inorder(temp­>left);

cout<<" "<<temp­>data;

inorder(temp­>right);

void bintree::find()

int key;

cout <<"\n enter the element which you want to search";

cin>>key;

temp=root;

search(&temp,key,&parent);

if(temp==NULL)

cout<<"\n element is not present";

else

cout<<"\n parent of node "<<temp­>data<<" is"<<parent­>data;

void bintree::search(node **temp,int key,node ** parent)

if(*temp==NULL)

cout<<endl<<" tree is not created"<<endl;

else

while(*temp!=NULL)

if((*temp)­>data==key)

www.studentsfocus.com

Page 80:  · ec6312 oops and data structures laboratory objectives:

cout<<"\n the "<<(*temp)­>data<<" element is present";

break;

*parent=*temp; //stores the parent value

if((*temp)­>data>key)

*temp=(*temp)­>left;

else

*temp=(*temp)­>right;

return;

void bintree::delet()

nt key;

cout<<"\n enter the element you want to delete";

cin>>key;

if(key==root­>data)

bintree(); // assigning a value NULL to root

else

del(root,key);

void bintree::del(node *root,int key)

node *temp_succ;

if(root==NULL)

cout<<" tree is not created";

else

www.studentsfocus.com

Page 81:  · ec6312 oops and data structures laboratory objectives:

temp=root;

search(&temp,key,&parent);

if(temp­>left!=NULL&&temp­>right!=NULL)

parent=temp;

temp_succ=temp_succ­>left;

while(temp_succ­>left!=NULL)

parent=temp_succ;

temp_succ=temp_succ­>left;

temp­>data=temp_succ­>data;

temp­>right=NULL;

cout<<" now deleted it!";

return;

if(temp­>left!=NULL&&temp­>right==NULL)

if(parent­>left==temp)

parent­>left=temp­>left;

else

parent­>right=temp­>left;

temp=NULL;

delete temp;

cout<<" now deleted it!";

return;

if(temp­>left==NULL&&temp­>right!=NULL)

www.studentsfocus.com

Page 82:  · ec6312 oops and data structures laboratory objectives:

if(parent­>left==temp)

parent­>left=temp­>right;

else

parent­>right=temp­>right;

temp=NULL;

delete temp;

cout<<" now deleted it!";

return;

/*deleting a node which is having no child*/

if(temp­>left==NULL&&temp­>right==NULL)

if(parent­>left==temp)

parent­>left=NULL;

else

parent­>right=NULL;

cout<<" now deleted it!";

return;

void main()

int choice;

char ans='N';

bintree tr;

clrscr();

cout<<"\n\t program for binary search tree";

cout<<"\n1.create\n2.search\n3.delete\n4.display";

do

www.studentsfocus.com

Page 83:  · ec6312 oops and data structures laboratory objectives:

cout<<"\n\n enter your choice:";

cin>>choice;

switch(choice)

case 1:

do

tr.create();

cout<<"do you want to enter more elements: (y/n)"<<endl;

ans=getche();

while(ans=='y');

break;

case 2:

tr.find();

break;

case 3:

tr.delet();

break;

case 4:

tr.display();

break;

while(choice!=5);

SAMPLE OUTPUT:

www.studentsfocus.com

Page 84:  · ec6312 oops and data structures laboratory objectives:

RESULT:

Thus the C++ program for implementation of Binary Search tree ADT has been executed

successfully.Ex. No. : 12 IMPLEMENTATION OF SEARCHABLE ADT

(LINEAR SEARCH & BINARY SEARCH)Date:

www.studentsfocus.com

Page 85:  · ec6312 oops and data structures laboratory objectives:

AIM:

To write a C++ program for implementation of Linear search and Binary search as

searchable ADT.

ALGORITHM:

a) Linear Search ( ):

Description: Here A is an array having N elements. ITEM is the value to be searched.

1. Repeat For J = 1 to N

2. If (ITEM == A[J]) Then

3. Print: ITEM found at location J

4. Return

[End of If]

[End of For Loop]

5. If (J > N) Then

6. Print: ITEM doesn’t exist

[End of If]

7. Exit

b) Binary Search ( ):

Description: Here A is a sorted array having N elements. ITEM is the value to be searched. BEG

denotes first element and END denotes last element in the array. MID denotes the middle value.

1. Set BEG = 1 and END = N

2. Set MID = (BEG + END) / 2

3. Repeat While (BEG <= END) and (A[MID] ≠ ITEM)

4. If (ITEM < A[MID]) Then

5. Set END = MID – 1

6. Else

7. Set BEG = MID + 1

[End of If]

8. Set MID = (BEG + END) / 2

[End of While Loop]

www.studentsfocus.com

Page 86:  · ec6312 oops and data structures laboratory objectives:

9. If (A[MID] == ITEM) Then

10. Print: ITEM exists at location MID

11. Else

12. Print: ITEM doesn’t exist

[End of If]

13. Exit

PROGRAM:

a) Code for Linear search ADT using class

#include<iostream.h>

#include<conio.h>

class lsearch

public:

int data[10],n,key;

void getdata();

void display();

;

void lsearch :: getdata()

cout<<"\nEnter the length of the array:";

cin>>n;

for(int i=0;i<n;i++)

cout<<"\nEnter the element in the "<< (i+1)<<" position of the array:";

cin>>data[i];

cout<<"\nEnter the key to find the element in the array:";

cin>>key;

void lsearch :: display()

www.studentsfocus.com

Page 87:  · ec6312 oops and data structures laboratory objectives:

int flag=0;

for(int i=0;i<n;i++)

if(key==data[i])

cout<<”\n\nThe element”<<key<<”is present in the position

“<<(i+1)<<”of the array”;

flag++;

if (flag==0)

cout<<”Given key “<<key<<”is not present in the array”;

void main()

clrscr();

lsearch ob;

ob.getdata();

ob.display();

getch();

b) Code for Binary search ADT using class by replacing the block of code in the previous

linear search program.

#include<iostream.h>

#include<conio.h>

class bsearch

public:

int data[10],n,key,first,last,middle;

void getdata();

www.studentsfocus.com

Page 88:  · ec6312 oops and data structures laboratory objectives:

void display();

;

void bsearch :: getdata()

cout<<"\nEnter the length of the array:";

cin>>n;

for(int i=0;i<n;i++)

cout<<"\nEnter the element at position"<< (i+1)<<" of the array:";

cin>>data[i];

cout<<"\nEnter the key to find the element in the array:";

cin>>key;

void bsearch :: display()

first=0;

last=n­1;

middle=(first+last)/2;

while(last>=first)

middle=(first+last)/2;

if(key>data[middle])

first=middle+1;

else if(key<data[middle])

last=middle­1;

else

cout<<"\nKey "<<key<<" found in the given array";

break;

www.studentsfocus.com

Page 89:  · ec6312 oops and data structures laboratory objectives:

void main()

clrscr();

bsearch ob;

ob.getdata();

ob.display();

getch();

SAMPLE OUTPUT:

RESULT:

Thus the C++ program for implementation of searchable ADT through linear search and

binary search was executed successfullyEx. No. : 13

IMPLEMENTATION OF QUICK SORTDate:

www.studentsfocus.com

Page 90:  · ec6312 oops and data structures laboratory objectives:

AIM:

To write a C++ program to sort the given numbers using quick sorting algorithm.

ALGORITHM:

Quick Sort (A, BEG, END ):

Desciption: Here A is an unsorted array having N elements. BEG is the lower bound and END

is the upper bound.

1. If ( BEG < END ) then

2. Find the element that divides the array into two parts using subfunction Partition( ).

3. Quick Sort ( Left Half )

4. Quick Sort ( Right Half )

[ End of If ]

5. Exit

Partition ( ):

1. Set LEFT = BEG, RIGHT = END and LOC = BEG

2. Beginning with the element pointed by RIGHT, scan the array from right to left, comparing

each element with the element pointed by LOC until:

(a) Element smaller than the element pointed by LOC is found.

(b) Interchange elements pointed by LOC and RIGHT.

(c) If RIGHT becomes equal to LOC, terminate the subfunction Partition ( ).

3. Beginning with the element pointed by LEFT, scan the array from left to right, comparing

each element with the element pointed by LOC until:

(a) Element greater than the element pointed by LOC is found.

(b) Interchange elements pointed by LOC and LEFT.

(c) If LEFT becomes equal to LOC, terminate the subfunction Partition ( ).

4. Exit

PROGRAM:

www.studentsfocus.com

Page 91:  · ec6312 oops and data structures laboratory objectives:

#include<iostream.h>

#include<conio.h>

int Partition(int a[], int beg, int end) //Function to Find Pivot Point

int p=beg, pivot=a[beg], loc;

for(loc=beg+1;loc<=end;loc++)

if(pivot>a[loc])

a[p]=a[loc];

a[loc]=a[p+1];

a[p+1] =pivot;

p=p+1;

return p;

void QuickSort(int a[], int beg, int end)

if(beg<end)

int p=Partition(a,beg,end); //Calling Procedure to Find Pivot

QuickSort(a,beg,p­1); //Calls Itself (Recursion)

QuickSort(a,p+1,end); //Calls Itself (Recursion)

void main()

clrscr();

int a[100],i,n,beg,end;

cout<<"\n­­­­­­­ QUICK SORT ­­­­­­­\n\n";

www.studentsfocus.com

Page 92:  · ec6312 oops and data structures laboratory objectives:

cout<<"Enter the No. of Elements : ";

cin>>n;

for(i=1;i<=n;i++)

cin>>a[i];

beg=1;

end=n;

QuickSort(a,beg,end); //Calling of QuickSort Function

cout<<"\nAfter Sorting : \n";

for(i=1;i<=n;i++)

cout<<a[i]<<endl;

getch();

SAMPLE OUTPUT:

RESULT:

Thus the C++ program for sorting the given numbers using quick sorting algorithm was

executed successfully.

www.studentsfocus.com