85
EC6312 – OOPS and Data Structures Lab 1 Ms.M.Abinaya, Lect/IT DEFAULT ARGUMENTS Aim: To implement function with default arguments. Algorithm: 1. Declare the Default function in the outside of the main function. 2. Get the values. 3. Define the default function 4. Print the values Program: #include<iostream.h> void printLine(char =’_’,int =70); void main() { printLine(); printLine(‘/’); printLine(‘*’,40); printLine(‘R’,55); } void printLine(char ch, int Repeatcount) { int i; cout<<endl; for(i=0;i<Repeatcount;i++) cout<<ch; } Output: -------- --------------------------- / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / * * * * * * * * * * * * * * * * * * * * * * * * * * * * R R R R R R R R R R R R R R R R R R R R R R R Result: Thus the program for default arguments has been executed and verified successfully.

EC6312 - lab manual

Embed Size (px)

DESCRIPTION

EC6312 - OOPS and Data Structures lab manual

Citation preview

  • EC6312 OOPS and Data Structures Lab 1Ms.M.Abinaya, Lect/IT

    DEFAULT ARGUMENTS

    Aim:

    To implement function with default arguments.

    Algorithm:

    1. Declare the Default function in the outside of the main function.2. Get the values.3. Define the default function4. Print the values

    Program:

    #includevoid printLine(char =_,int =70);void main(){printLine();printLine(/);printLine(*,40);printLine(R,55);}void printLine(char ch, int Repeatcount){int i;cout

  • EC6312 OOPS and Data Structures Lab 2Ms.M.Abinaya, Lect/IT

    CLASS WITH STATIC DATA MEMBER

    Aim:

    To implement static data member in class.Algorithm:

    1. Create class ITEM with static data member as count.2. Create a member function to increment the count.3. Declare the static datamember using scope resolution operator.4. Display the count value.

    Program:#includeclass item{static int count;int num;public:void getdata(int a){num=a;count++;cout

  • EC6312 OOPS and Data Structures Lab 3Ms.M.Abinaya, Lect/IT

    c.getdata(40);a.showcount();b.showcount();c.showcount();}

    Output:count 0count 0count 0Number 20Number 30Number 40count 3count 3count 3

    Result:

    Thus the program for static data member has been executed and verifiedsuccessfully

    CLASS WITH STATIC MEMBER FUNCTIONAim:

    To implement static member function in class.

    Algorithm:

    1. Create class ITEM with static data member as count.2. Create a member function to increment the count.3. Declare the static data member using scope resolution operator.4. Display the count value.

    Program:

    #includeclass test{int code;static int count;public:

  • EC6312 OOPS and Data Structures Lab 4Ms.M.Abinaya, Lect/IT

    void setcode(void){code= ++count;

    }void showcode(void){

    cout

  • EC6312 OOPS and Data Structures Lab 5Ms.M.Abinaya, Lect/IT

    ADDITION OF TWO COMPLEX NUMBERS

    Aim:To write a program for object as argument and returning an object

    using complex number addition.

    Algorithm:

    1. The class complex contains two member variables real and imaginary.2. Assign the value for real and imaginary part.3. Declare a temporary variable temp.4. Add real part with real of other object and store it in temps real.5. Add imaginary part with imaginary of other object and store it in temps

    imaginary.6. Display temp.

    Program:

    #includetemplateclass complex{private:

    T real;T imag;Public:complex(){real=imag=0;}void getdata(){coutreal;coutimag;}complex operator +(complex c2);void outdata(char *msg){cout

  • EC6312 OOPS and Data Structures Lab 6Ms.M.Abinaya, Lect/IT

    cout

  • EC6312 OOPS and Data Structures Lab 7Ms.M.Abinaya, Lect/IT

    C3=c1+c2:(4,6)

    Addition of float complexobjectsEnter complex number c4..Real part?1.5Imag part?2.5Enter complex number c5..Real part?2.4Imag part?3.7C6=c4+c5:(3.9,6.2)

    Result:Thus the program for addition of two complex numbers has been

    executed and verified successfully.

    FRIEND FUNCTIONAim:

    To write a c++ program for friend function.

    Algorithm:

    1. Create the class and declare the data member as private.2. Declare the friend function using the keyword friend.3. Perform the operation of adding two private variables in the friend

    function.4. Display the result.

    Program:

    #include using namespace std;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;

  • EC6312 OOPS and Data Structures Lab 8Ms.M.Abinaya, Lect/IT

    b = j;}// Note: sum() is not a member function of any class.int sum(myclass x){/* Because sum() is a friend of myclass, it candirectly access a and b. */return x.a + x.b;}int main(){myclass n;n.set_ab(3, 4);cout

  • EC6312 OOPS and Data Structures Lab 9Ms.M.Abinaya, Lect/IT

    #include class employee{char name[80]; // private by defaultpublic:void putname(char *n); // these are publicvoid getname(char *n);private:double wage; // now, private againpublic:void putwage(double w); // back to publicdouble getwage();};void employee::putname(char *n){strcpy(name, n);}void employee::getname(char *n){strcpy(n, name);}void employee::putwage(double w){wage = w;}double employee::getwage(){return wage;}int main(){employee ted;char name[80];ted.putname("Ted Jones");ted.putwage(75000);ted.getname(name);cout

  • EC6312 OOPS and Data Structures Lab 10Ms.M.Abinaya, Lect/IT

    Output:

    Ted Jones makes $75000 per year.

    Result:

    Thus the program for classes and objects has been executed and verifiedsuccessfully.

    INHERITANCEAim:

    To Write a C++ program for implementing the inheritance.

    Algorithm:

    1. Define the base class with variables and functions.2. Define the derived class with variables and functions.3. Define main function4. Declare the variables.5. Inherits base class.6. Access member of derived class.

    Program:

    #includeclass base{public:int x;void set_x(int n){x=n;}void show_x(){cout

  • EC6312 OOPS and Data Structures Lab 11Ms.M.Abinaya, Lect/IT

    public:void set_y(int n){y=n;}void show_xy(){cout

  • EC6312 OOPS and Data Structures Lab 12Ms.M.Abinaya, Lect/IT

    FUNCTION OVERLOADING

    Aim:To Write a C++ program for implementing the function overloading.

    Algorithm:

    1. Define class.2. Define the functions with different arguments.3. Define main function4. Declare the variables.5. Call the Different task of function.6. Print the output.

    Program:

    #includeclass test{public:int sum(int,int);float sum(float,float);double sum(double,double);};int test::sum(int a.int b){return(a+b);}float test::sum(float a ,float b){return(a+b);}double test::sum(double a,double b){return(a+b);}void main(){test obj;int choice;int a,b;

  • EC6312 OOPS and Data Structures Lab 13Ms.M.Abinaya, Lect/IT

    float x,y;double m,n;double result=0;cout

  • EC6312 OOPS and Data Structures Lab 14Ms.M.Abinaya, Lect/IT

    enter 2 number 1.13 5.6result 6.73

    Result:Thus the program for function overloading has been executed and

    verified successfully.

    CALL BY RREFERENCEAim:

    To write a C++ program for Call by Reference

    Algorithm:

    1. Declare the swap function.2. The function has the values with reference3. Call the swap function in function main4. Define the swap function with the reference value.5. Display the values.

    Program:

    #include void swap(int &x, int &y);int main (){

    int a = 100;int b = 200;cout

  • EC6312 OOPS and Data Structures Lab 15Ms.M.Abinaya, Lect/IT

    return;}

    Output:Before swap, value of a:100Before swap, value of b:200

    After swap, value of a:200After swap, value of b:100

    Result:Thus the program for Call by Reference has been executed and verified

    successfully.

    CALL BY VALUEAim:

    To write a C++ program for call by value.

    Algorithm:

    1. Declare the swap function.2. The function has the values with value3. Call the swap function in function main4. Define the swap function with the value.5. Display the values.

    Program:

    #include void swap(int x, int y);int main (){

    int a = 100;int b = 200;cout

  • EC6312 OOPS and Data Structures Lab 16Ms.M.Abinaya, Lect/IT

    {int temp;

    temp = x;x = y;y = temp;return;

    }

    Output:Before swap, value of a:100Before swap, value of b:200

    After swap, value of a:200After swap, value of b:100

    Result:Thus the program for Call by value has been executed and verified

    successfully.

    MATRIX MULTIPLICATION USING STATIC, FRIEND ANDDEFAULT FUNCTIONS

    Aim:To write a C++ program to perform matrix manipulation using static

    variable, default argument and friend function.

    Algorithm:

    1. Declare the class as Matrix.2. Declare the data member as r, c and **x.3. Declare the member function as Member function with default argument

    is used to initialize the value of the matrix.4. get() function is used to get the values of two matrices.5. add() and mul() function are used to perform addition and multiplication

    of thematrices.

    6. In the main, create objects A and B for the Matrix class.7. Call the get() method to get the value of matrix A and B.8. Call the add() and mul() method to perform the particular operation and

    finallydisplay the result.

  • EC6312 OOPS and Data Structures Lab 17Ms.M.Abinaya, Lect/IT

    Program:

    #include#include#include

    class matrix{static int r,c;int**x;public:matrix(int r1=2,int c1=2);void get();void put();friend matrix add(matrix,matrix);friend matrix mul(matrix,matrix);};

    matrix::matrix(int r1,int c1){r=r1;c=c1;x=new int*[r];for(int i=0;i

  • EC6312 OOPS and Data Structures Lab 18Ms.M.Abinaya, Lect/IT

    for(int i=0;i

  • EC6312 OOPS and Data Structures Lab 19Ms.M.Abinaya, Lect/IT

    cout

  • EC6312 OOPS and Data Structures Lab 20Ms.M.Abinaya, Lect/IT

    IMPLEMENTATION OF VARIOUS LIST OPERATIONS USINGARRAYS

    Aim:To Implement a program for various List operations using arrays

    Algorithm:

    1. Create a structure called Node2. Create an array of size 10.3. Create a constructor for initializing the variables4. Create a variables head.5. Create functions for list & create , insert & delete.6. In create function get the values and keep it as a head, and increment the

    index value and add the values after head.7. In display function display all the values as a list format.8. In delete function delete any node and add next node to the previous

    node link.9. In insert function, insert the new node, break the link and add the new

    inserted node.10.In search function, search the new node whether it is present or not.11.Write the function definition12.Run the program.

    Program:

    #include#include#include#includeclass arr_list{private:struct node{int data;int next;}a[10];public:int head;

  • EC6312 OOPS and Data Structures Lab 21Ms.M.Abinaya, Lect/IT

    arr_list();int create();void display(int);void insert();void del();void search();};arr_list::arr_list(){for(int i=0;i

  • EC6312 OOPS and Data Structures Lab 22Ms.M.Abinaya, Lect/IT

    }cout

  • EC6312 OOPS and Data Structures Lab 23Ms.M.Abinaya, Lect/IT

    {if(a[i].next==current){a[i].next=new_next;a[current].data=-1;}}}void arr_list::search(){int i,temp,flag=0;couttemp;for(i=0;i

  • EC6312 OOPS and Data Structures Lab 24Ms.M.Abinaya, Lect/IT

    {coutchoice;switch(choice){case 1:obj.head=obj.create();break;case 2:obj.display(obj.head);break;case 3:obj.insert();obj.display(obj.head);break;case 4:obj.del();obj.display(obj.head);break;case 5:obj.search();break;case 6:exit(0);}cout

  • EC6312 OOPS and Data Structures Lab 25Ms.M.Abinaya, Lect/IT

    Output:

    program for implementing list using arraymain menu1.creation2.display3.insertion of element in the list4.deletion of element form the list5.searching of element from the list6.exitenter your choice1

    enter the index for first node3

    enter the data and index of the first element2 5

    enter the data and index of the first element5 4

    enter the data and index of the first element4 -1

    do you wish to go to main menu?enter your choice22->5->4->NULLdo you wish to go to main menu?enter your choice3

    enter the new data which is to be inserted6

    enter the data after whice you want to insert52->5->6->4->NULLdo you wish to go to main menu?enter your choice4

    enter the node to be delete52->6->4->NULLdo you wish to go to main menu?enter your choice5

    enter the node to be searched6

    the6node is present is the list

  • EC6312 OOPS and Data Structures Lab 26Ms.M.Abinaya, Lect/IT

    do you wish to go to main menu?enter your choice5

    enter the node to be searched1

    the node is not presentdo you wish to go to main menu?enter your choice6

    Result:

    Thus the Array implantation of List ADT program was executedsuccessfully.

    LINKED LIST IMPLEMENTATION USING LISTADTAim:

    To implement a linked list and do all operations on it.

    Algorithm:

    1. Start the process.2. Initialize and declare variables.3. Enter the choice.4. If choice is INSERT then

    a. Enter the element to be inserted.b. Get a new node and set DATA[NEWNODE] = ITEM.c. Find the node after which the new node is to be inserted.d. Adjust the link fields.e. Print the linked list after insertion.

    5. If choice is DELETE thena. Enter the element to be deleted.b. Find the node containing the element (LOC) and its preceding node

    (PAR).c. Set ITEM = DATA[LOC] and delete the node LOC.d. Adjust the link fields so that PAR points to the next element. Ie

    LINK[PAR] = LINK [ LOC].e. Print the linked list after deletion.

  • EC6312 OOPS and Data Structures Lab 27Ms.M.Abinaya, Lect/IT

    6. Stop the process.

    Program:

    #include#include#include#define TRUE 1#define FALSE 0class sll{private:struct node{int data;struct node*next;}*head;public:sll();void create();void display();void search(int key);void insert_head();void insert_after();void insert_last();void dele();~sll();};sll::sll(){head=NULL;}sll::~sll(){node*temp,*temp1;temp=head->next;delete head;while(temp!=NULL){

  • EC6312 OOPS and Data Structures Lab 28Ms.M.Abinaya, Lect/IT

    temp1=temp->next;delete temp;temp=temp1;}}void sll::create(){node*temp,*New;int val,flag;char ans;flag=TRUE;do{coutval;New=new node;if(New==NULL)coutnext=NULL;if(flag==TRUE){head=New;temp=head;flag=FALSE;}else{temp->next=New;temp=New;}cout

  • EC6312 OOPS and Data Structures Lab 29Ms.M.Abinaya, Lect/IT

    if(temp==NULL){cout

  • EC6312 OOPS and Data Structures Lab 30Ms.M.Abinaya, Lect/IT

    node*temp,*prev;int key;temp=head;coutkey;while(temp!=NULL){if(temp->data==key)break;prev=temp;temp=temp->next;}if(temp==NULL)coutnext=temp->next;delete temp;coutnext!=NULL)temp=temp->next;temp->next=New;New->next=NULL;}}

  • EC6312 OOPS and Data Structures Lab 31Ms.M.Abinaya, Lect/IT

    void sll::insert_after(){int key;node*temp,*New;New=new node;coutNew->data;if(head==NULL){head=New;}else{coutkey;temp=head;do{if(temp->data==key){New->next=temp->next;temp->next=New;break;}elsetemp=temp->next;}while(temp!=NULL);}}void sll::insert_head(){node*New,*temp;New=new node;coutNew->data;if(head==NULL)head=New;else{temp=head;

  • EC6312 OOPS and Data Structures Lab 32Ms.M.Abinaya, Lect/IT

    New->next=temp;head=New;}}void main(){sll s;int choice,val,ch1;clrscr();cout

  • EC6312 OOPS and Data Structures Lab 33Ms.M.Abinaya, Lect/IT

    switch(ch1){case 1:s.insert_head();break;case 2:s.insert_after();break;case 3:s.insert_last();break;default:cout

  • EC6312 OOPS and Data Structures Lab 34Ms.M.Abinaya, Lect/IT

    enter the data2

    do you want to enter more elements?(Y,n)enter the data3

    do you want to enter more elements?(Y,n)enter the data4

    do you want to enter more elements?(Y,n)the singly linked list is created

    enter your choice(1-6)22 3 4enter your choice(1-6)3

    enter the element you want to search2

    the element is present in the list

    enter your choice(1-6)3

    enter the element you want to search1

    enter your choice(1-6)4

    the list is:2 3 4menu1.insert at beginning2.insert after3.insert at endenter your choice1

    enter the element which you want to insert11 2 3 4enter your choice(1-6)4

    the list is:1 2 3 4menu

  • EC6312 OOPS and Data Structures Lab 35Ms.M.Abinaya, Lect/IT

    1.insert at beginning2.insert after3.insert at endenter your choice2

    enter the element whice you want to insert5

    enter the element after whice you want to insert the node31 2 3 5 4enter your choice(1-6)4

    the list is:1 2 3 5 4menu1.insert at beginning2.insert after3.insert at endenter your choice3

    enter the element which you want to insert61 2 3 5 4 6enter your choice(1-6)5

    enter the data of the node you want to delete:2

    the element is deleted1 3 5 4 6enter your choice(1-6)6

    Result:

    Thus the given program Linked List implementation of the list ADT wasexecuted successfully.

  • EC6312 OOPS and Data Structures Lab 36Ms.M.Abinaya, Lect/IT

    CURSOR IMPLEMENTATION LIST ADT

    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 Linko 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 ofinsertion. Insert the node and change its link accordingly.

    5. For deletion get the position in which deletion is to be done. Delete thenode and then link it to the next node. Before deletion check whetherthere is data in the list to be deleted.

    6. Using display option list the elements of the list.7. Stop the program.

    Program:

    #include#include#include#define MAX 20class LIST{private:int list[MAX];public:int create();void display(int);void reverse(int);int search(int);void delet(int);};

  • EC6312 OOPS and Data Structures Lab 37Ms.M.Abinaya, Lect/IT

    int LIST::create(){int n,i;clrscr();coutn;if(n>MAX)cout

  • EC6312 OOPS and Data Structures Lab 38Ms.M.Abinaya, Lect/IT

    coutkey;for(i=0;i

  • EC6312 OOPS and Data Structures Lab 39Ms.M.Abinaya, Lect/IT

    switch(choice){case 1:len=obj.create();break;case 2:obj.display(len);break;case 3:position=obj.search(len);break;case 4:obj.reverse(len);break;case 5:obj.delet(len);break;case 6:cout

  • EC6312 OOPS and Data Structures Lab 40Ms.M.Abinaya, Lect/IT

    6.quitenter your choice(1-6)1

    how many elements you want in the list:4

    enter the element number1:1

    enter the element number2:2

    enter the element number3:3

    enter the element number4:4

    the List is successfullyprogram to perform operations on ordered list

    1.create2.display3.search for a number4.reverse5.delete6.quitenter your choice(1-6)2

    the List is...

    1234press any key to continue...

    program to perform operations on ordered list1.create2.display3.search for a number4.reverse5.delete6.quitenter your choice(1-6)3

    enter the number you want to search?2

  • EC6312 OOPS and Data Structures Lab 41Ms.M.Abinaya, Lect/IT

    the given number is at position:1program to perform operations on ordered list

    1.create2.display3.search for a number4.reverse5.delete6.quitenter your choice(1-6)4

    the reversed list is...

    4321press any key to continue..

    program to perform operations on ordered list1.create2.display3.search for a number4.reverse5.delete6.quitenter your choice(1-6)5

    enter thenumber you want to search?3

    the given numbre is at position:2

    the elements is now deletedwe put -1 to indicate empty location

    the List is...

    12-14press any key to continue...

  • EC6312 OOPS and Data Structures Lab 42Ms.M.Abinaya, Lect/IT

    Result:Thus the Given Program Cursor implementation of list was executed

    successfully.

    STACK ADT USING ARRAY IMPLEMENTATION

    Aim:To write a program for stack using array implementation.

    Algorithm:1. Define a array which stores stack elements..2. The operations on the stack are

    a. PUSH data into the stackb. POP data out of stack

    3. PUSH DATA INTO STACKa. Enter the data to be inserted into stack.b. If TOP is NULL

    a. the input data is the first node in stack.b. the link of the node is NULL.c. TOP points to that node.

    c. If TOP is NOT NULLa. the link of TOP points to the new node.b. TOP points to that node.

    4. POP DATA FROM STACKa. If TOP is NULL the stack is emptyb. If TOP is NOT NULL

    i. the link of TOP is the current TOP.ii. the pervious TOP is popped from stack.

    5. The stack represented by linked list is traversed to display its content.

    Program:

    #include#include#includeclass stack{int stk[5];int top;public:

  • EC6312 OOPS and Data Structures Lab 43Ms.M.Abinaya, Lect/IT

    stack(){top=-1;}void push(int x){if(top>4){cout

  • EC6312 OOPS and Data Structures Lab 44Ms.M.Abinaya, Lect/IT

    {coutch;switch(ch){case 1:coutch;st.push(ch);break;case 2:st.pop();break;case 3:st.display();break;case 4:exit(0);}}return 0;}

    Output:

    1.push2.pop3.display4.exitenter a choice1entre the element3inserted3enter a choice1entre the element2inserted2enter a choice1entre the element4inserted4enter a choice1

  • EC6312 OOPS and Data Structures Lab 45Ms.M.Abinaya, Lect/IT

    entre the element5inserted5enter a choice35 4 2 3enter a choice2deleted5enter a choice34 2 3enter a choice 4

    Result:Thus the program for implementation of stack ADT using array has been

    executed and verified successfully.

    STACK ADT USING LINKED LIST IMPLEMENTATION

    Aim:To write a program for stack ADT using linked list implementation.

    Algorithm:

    1. Define a struct for each node in the stack.a. Each node in the stack contains data and link to the next node.

    TOP pointer points to last node inserted in the stack.2. The operations on the stack are

    a. PUSH data into the stackb. POP data out of stack

    3. PUSH DATA INTO STACKa. Enter the data to be inserted into stack.b. If TOP is NULL

    i. the input data is the first node in stack.ii. the link of the node is NULL.

    iii. TOP points to that node.c. If TOP is NOT NULL

    i. the link of TOP points to the new node.ii. TOP points to that node.

    4. POP DATA FROM STACK

  • EC6312 OOPS and Data Structures Lab 46Ms.M.Abinaya, Lect/IT

    a. If TOP is NULL the stack is emptyb. If TOP is NOT NULL

    i. the link of TOP is the current TOP.ii. the pervious TOP is popped from stack.

    5. The stack represented by linked list is traversed to display its content.

    Program:

    #include#include#includeclass Linked_list_Stack{private:struct node{int data;node *next;};node *top;node *entry;node *print;node *bottom;node *last_entry;node *second_last_entry;public:Linked_list_Stack( );void pop( );void push( );void print_list( );void show_working( );};Linked_list_Stack::Linked_list_Stack ( ){top=NULL;bottom=NULL;}void Linked_list_Stack::push( ){int num;

  • EC6312 OOPS and Data Structures Lab 47Ms.M.Abinaya, Lect/IT

    coutnum;entry=new node;if(bottom==NULL){entry->data=num;entry->next=NULL;bottom=entry;top=entry;}else{entry->data=num;entry->next=NULL;top->next=entry;top=entry;}cout

  • EC6312 OOPS and Data Structures Lab 48Ms.M.Abinaya, Lect/IT

    cout

  • EC6312 OOPS and Data Structures Lab 49Ms.M.Abinaya, Lect/IT

    obj.show_working( );return 0;}

    Output:

    ********** Implementation of Linked List as a Stack**********

    1.Push elements to stack2.Pop elements to stack3.Print the elements of stack4.ExitEnter your Choice : 1

    Enter value to push onto Stack : 2

    *** 2 is pushed onto the Stack.Enter your Choice : 1

    Enter value to push onto Stack : 3

    *** 3 is pushed onto the Stack.Enter your Choice : 1

    Enter value to push onto Stack : 4

    *** 4 is pushed onto the Stack.Enter your Choice : 3

    Values pushed onto Stack are :234

    Enter your Choice : 2*** 4 is poped from the Stack.

    Enter your Choice : 3Values pushed onto Stack are :23

    Enter your Choice : 4

  • EC6312 OOPS and Data Structures Lab 50Ms.M.Abinaya, Lect/IT

    Result:

    Thus the Given Program for stack ADT using linked list implementationwas executed successfully.

    SOURCE FILES FOR STACK APPLICATION1 ARRAYIMPLEMENTATIN

    Aim:

    To write a C++ program for the application of stack for checking wellformed parenthesis of the expression using array implementation.

    Algorithm:

    1. Crate a header file named stack.h .2. In this file declare the class and all the stack operations.3. In push operation increment the top value and get the value4. In pop operation delete the value and decrement the top.5. In full and empty operation check the stack whether it have value or not.6. Now create a stack application program.7. Chosen an application as checking well formed of parenthesis for this

    application use stack operations.8. These operations are used from the external file stack.h.9. Hence will include this file in the include file section.

    Program:stack.h

    #define size 10class stk_class{private:struct stack{char s[size];int top;}st;public:stk_class();void push(char item);

  • EC6312 OOPS and Data Structures Lab 51Ms.M.Abinaya, Lect/IT

    int stempty();char pop();int stfull();};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;elsereturn 0;}int stk_class::stfull(){if(st.top==size)return 1;elsereturn 0;}char stk_class::pop(){char item;item=st.s[st.top];st.top--;return(item);}

    program.cpp

    #include#include#include#include "d:\stack.h"

  • EC6312 OOPS and Data Structures Lab 52Ms.M.Abinaya, Lect/IT

    #define size 10void main(void){char item;char ans,bracket[10];stk_class obj;int i;clrscr();cout

  • EC6312 OOPS and Data Structures Lab 53Ms.M.Abinaya, Lect/IT

    the expression has well formed parenthesisprogram for stack application using separate header file

    enter the expression and put $at end ((())$the expression is invalidResult:

    Thus the program for application of stack checking well formed ofparenthesis implemented using arrays was executed successfully.

    SOURCE FILES FOR STACK APPLICATION1 LINKED LISTIMPLEMENTATIN

    Aim:

    To write a C++ program for the application of stack for checking wellformed parenthesis of the expression using linked list implementation.

    Algorithm:

    1. Crate a header file named stack.h .2. In this file declare the class and all the stack operations.3. In push operation increment the top value and get the value4. In pop operation delete the value and decrement the top.5. In full and empty operation check the stack whether it have value or not.6. Now create a stack application program.7. Chosen an application as checking well formed of parenthesis for this

    application use stack operations.8. These operations are used from the external file stack.h.9. Hence will include this file in the include file section.

    Program:stack.h

    class stk_class{private:typedef struct stack{char data;struct stack * next;}node;

  • EC6312 OOPS and Data Structures Lab 54Ms.M.Abinaya, Lect/IT

    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)coutnext=top;top=New;}}int stk_class::sempty(){if(top==NULL)return 1;elsereturn 0;}void stk_class::pop(){node *temp;temp=top;top=top->next;delete temp;}

  • EC6312 OOPS and Data Structures Lab 55Ms.M.Abinaya, Lect/IT

    program.cpp

    #include#include#include#include#include"d:\stack1.h"void main(void){char ans,bracket[10];char data,item;stk_class obj;int choice;int i;clrscr();coutbracket;i=0;if(bracket[i]==')')cout

  • EC6312 OOPS and Data Structures Lab 56Ms.M.Abinaya, Lect/IT

    if(obj.sempty())cout

  • EC6312 OOPS and Data Structures Lab 57Ms.M.Abinaya, Lect/IT

    11.Hence will include this file in the include file section.

    ProgramStack2.h

    #define MAX 10class 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

  • EC6312 OOPS and Data Structures Lab 58Ms.M.Abinaya, Lect/IT

    #include#include#include#include#include "d:\stack2.h"#define size 80void main(){char exp[size];int len;double result;double post(char exp[]);clrscr();coutexp;len=strlen(exp);exp[len]='$';result=post(exp);cout

  • EC6312 OOPS and Data Structures Lab 59Ms.M.Abinaya, Lect/IT

    elseif(strcmp(type,"operator")==0){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];}result=obj.pop();return(result);}

    Output:

    enter the postfix expression19*3+5/the value of the expression is 2

    Result:

    Thus the program for application of stack evaluating postfix expressionusing array implementation has been executed and verified successfully.

  • EC6312 OOPS and Data Structures Lab 60Ms.M.Abinaya, Lect/IT

    SOURCE FILES FOR STACK APPLICATION2 LINKEDLIST IMPLEMENTATIN

    Aim:

    To write a C++ program for the application of stack for evaluation ofpostfix expression using array implementation.

    Algorithm:

    1. Crate a header file named stack.h .2. In this file declare the class and all the stack operations.3. In push operation increment the top value and get the value4. In pop operation delete the value and decrement the top.5. In full and empty operation check the stack whether it have value or not.6. Now create a stack application program.7. Chosen an application as evaluating the postfix expression. for this

    application use stack operations.8. Check the expression. If it is operand then push the value or the operator

    pop the value.9. Then do the corresponding operation like +, -, *, /.10. These operations are used from the external file stack.h.11.Hence will include this file in the include file section.

    Program

    stack3.hclass stk_class{typedef struct stack{char data;struct stack *next;}node;public:node *top;stk_class();void push(char item);char pop();};

  • EC6312 OOPS and Data Structures Lab 61Ms.M.Abinaya, Lect/IT

    stk_class::stk_class(){top=NULL;}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;}

    program.cpp#include#include#include#include#include#include#include"d:\stack3.h"#define size 80void main(){char exp[size];int len;double result;double post(char exp[]);clrscr();cout

  • EC6312 OOPS and Data Structures Lab 62Ms.M.Abinaya, Lect/IT

    cin>>exp;len=strlen(exp);exp[len]='$';result=post(exp);cout

  • EC6312 OOPS and Data Structures Lab 63Ms.M.Abinaya, Lect/IT

    break;case '^':result=pow(op1,op2);break;}obj.push(result);}i++;ch=exp[i];}result=obj.pop();/*pop the result*/return(result);}

    Output:enter the postfix expression19*3+5/the value of the expression is 2

    Result:Thus the program for application of stack evaluating postfix expression

    using linked list implementation has been executed and verified successfully.

    QUEUE ADT USING ARRAY IMPLEMENTATION

    Aim:To write a program for Queue using array implementation.

    Algorithm:

    1. Define a array which stores queue elements.2. The operations on the queue are

    a. INSERT data into the queueb. DELETE data out of queue

    3. INSERT DATA INTO queuea. Enter the data to be inserted into queue.b. If TOP is NULL

    i. the input data is the first node in queue.ii. the link of the node is NULL.

    iii. OP points to that node.c. If TOP is NOT NULL

    i. the link of TOP points to the new node.ii. TOP points to that node.

  • EC6312 OOPS and Data Structures Lab 64Ms.M.Abinaya, Lect/IT

    4. DELETE DATA FROM queuea. If TOP is NULL, the queue is emptyb. If TOP is NOT NULL

    i. the link of TOP is the current TOP.ii. the pervious TOP is popped from queue.

    5. The queue represented by linked list is traversed to display its content.

    Program:

    #include#include#includeclass node{public:class node *next;int data;};class queue:public node{node *head;int front,rear;public:queue(){front=-1;rear=-1;}void enqueue(int x){if(rearnext=NULL;head->data=x;rear++;}else{node *temp,*temp1;temp=head;

  • EC6312 OOPS and Data Structures Lab 65Ms.M.Abinaya, Lect/IT

    if(rear>=4){coutnext;temp1=new node;temp->next=temp1;temp1->next=NULL;temp1->data=x;}}void display(){node *temp;temp=head;if(rear

  • EC6312 OOPS and Data Structures Lab 66Ms.M.Abinaya, Lect/IT

    head=NULL;return;}front++;head=head->next;}};main(){queue s1;int ch;clrscr();cout

  • EC6312 OOPS and Data Structures Lab 67Ms.M.Abinaya, Lect/IT

    Output:

    QUEUE USING LINKED LIST1.enqueue2.dequeue3.DISPLAY4.EXITenter your choice:1enter a element2enter your choice:1enter a element3enter your choice:1enter a element4enter your choice:32 3 4enter your choice:2enter your choice:33 4enter your choice:4

    Result:Thus the Program for Queue using array implementation has been

    executed and verified successfully.

    QUEUE ADT OPERATIONS USING LINKED LISTAim:

    To write a program for Queue using Linked implementation.

    Algorithm:1. Define a struct for each node in the queue.2. Each node in the queue contains data and link to the next node.3. Front and rear pointer points to first and last node inserted in the queue.4. The operations on the queue are

    a. INSERT data into the queueb. DELETE data out of queue

    5. INSERT DATA INTO queuea. Enter the data to be inserted into queue.

  • EC6312 OOPS and Data Structures Lab 68Ms.M.Abinaya, Lect/IT

    b. If TOP is NULLi. the input data is the first node in queue.

    ii. the link of the node is NULL.iii. TOP points to that node.

    c. If TOP is NOT NULLi. the link of TOP points to the new node.

    ii. TOP points to that node.6. DELETE DATA FROM queue

    a. If TOP is NULLi. the queue is empty

    b. If TOP is NOT NULLi. the link of TOP is the current TOP.

    ii. the pervious TOP is popped from queue.6. The queue represented by linked list is traversed to display its content.

    Program:

    #include#include#includeclass queue{int queue1[5];int rear,front;public:queue(){rear=-1;front=-1;}void insert(int x){if(rear>4){cout

  • EC6312 OOPS and Data Structures Lab 69Ms.M.Abinaya, Lect/IT

    {if(front==rear){cout

  • EC6312 OOPS and Data Structures Lab 70Ms.M.Abinaya, Lect/IT

    }return(0);}

    Output:

    1.insert2.delete3.display4.exitEnter ur choice1enter the element5inserted 5Enter ur choice1enter the element6inserted 6Enter ur choice1enter the element9inserted 9Enter ur choice35 6 9Enter ur choice2deleted 5Enter ur choice36 9Enter ur choice4

    Result :

    Thus the program for Queue ADT operations using Linked List hasbeen executed and verified successfully.

  • EC6312 OOPS and Data Structures Lab 71Ms.M.Abinaya, Lect/IT

    BINARY SEARCH TREE

    Aim:To write a program for binary search tree.

    Algorithm:

    1. Declare function create(),search(),delete(),Display().2. Create a structure for a tree contains left pointer and right pointer.3. Insert an element is by checking the top node and the leaf node and the

    operation will be performed.4. Deleting an element contains searching the tree and deleting the item.5. display the Tree elements.

    Program:#include#includeclass bintree{typedef struct bst{int data;struct bst *left,*right;}node;node *root,*New,*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);};

  • EC6312 OOPS and Data Structures Lab 72Ms.M.Abinaya, Lect/IT

    void bintree::create(){New=new node;New->left=NULL;New->right=NULL;coutNew->data;if(root==NULL)root=New;elseinsert(root,New);}void bintree::insert(node *root,node *New){if(New->datadata){if(root->left==NULL)root->left=New;elseinsert(root->left,New);}if(New->data>root->data){if(root->right==NULL)root->right=New;elseinsert(root->right,New);}}void bintree::display(){if(root==NULL)cout

  • EC6312 OOPS and Data Structures Lab 73Ms.M.Abinaya, Lect/IT

    {inorder(temp->left);cout

  • EC6312 OOPS and Data Structures Lab 74Ms.M.Abinaya, Lect/IT

    {int key;coutkey;if(key==root->data){bintree();// assigning a value NULL to root}elsedel(root,key);}void bintree::del(node *root,int key){node *temp_succ;if(root==NULL)coutright!=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;coutright==NULL){if(parent->left==temp)parent->left=temp->left;elseparent->right=temp->left;temp=NULL;delete temp;

  • EC6312 OOPS and Data Structures Lab 75Ms.M.Abinaya, Lect/IT

    coutright!=NULL){if(parent->left==temp)parent->left=temp->right;elseparent->right=temp->right;temp=NULL;delete temp;coutright==NULL){if(parent->left==temp)parent->left=NULL;elseparent->right=NULL;cout

  • EC6312 OOPS and Data Structures Lab 76Ms.M.Abinaya, Lect/IT

    tr.create();cout

  • EC6312 OOPS and Data Structures Lab 77Ms.M.Abinaya, Lect/IT

    yenter the element 12

    do you want to enter more elements(y/n)yenter the element 11

    do you want to enter more elements(y/n)yenter the element 13

    do you want to enter more elements(y/n)n

    1.create2.search3.delete4.display

    enter your choice 4

    the tree is: 7 8 9 10 11 12 131.create2.search3.delete4.display

    enter your choice 2

    enter the element which you want to search 13the 13 element is presentparent of node 13 is 121.create2.search3.delete4.display

    enter your choice 3

    enter the element you wish to delete 12the 12 element is present now deleted it!

  • EC6312 OOPS and Data Structures Lab 78Ms.M.Abinaya, Lect/IT

    1.create2.search3.delete4.display

    enter your choice 4

    the tree is : 7 8 9 10 11 131.create2.search3.delete4.display

    enter your choice 3

    enter the element you wish to delete 11the 11 element is present now deleted it!1.create2.search3.delete4.display

    enter your choice 4

    the tree is : 7 8 9 10 131.create2.search3.delete4.display

    enter your choice 3

    enter the element you wish to delete 7the 7 element is present now deleted it!1.create2.search3.delete4.display

    enter your choice 4

  • EC6312 OOPS and Data Structures Lab 79Ms.M.Abinaya, Lect/IT

    the tree is : 8 9 10 131.create2.search3.delete4.display

    enter your choice 5

    Result:

    Thus the Program for Binary Search Tree has been executed and verifiedsuccessfully.

    HEAP SORT

    Aim:To write a c program to perform heap sort.

    Algorithm:

    1. Get the size of the array from the user.2. Get the elements to be sorted.3. Sorting is performed when we call the heap sort function.4. Now the array is contained with sorted elements.5. Display the sorted elements.

    Program:

    #include#include#include#define MAX 10class heap{private:int arr[MAX];int n;public:Heap();void insert(int num);

  • EC6312 OOPS and Data Structures Lab 80Ms.M.Abinaya, Lect/IT

    void makespace();void heapsort();void display();};heap::heap(){n=0;for(int i=0;i

  • EC6312 OOPS and Data Structures Lab 81Ms.M.Abinaya, Lect/IT

    int j;if(i==1)j=-1;elsej=1;if(i>2&&arr[2]>arr[1])j=2;while(j>=0&&temp

  • EC6312 OOPS and Data Structures Lab 82Ms.M.Abinaya, Lect/IT

    obj.heapsort();cout

  • EC6312 OOPS and Data Structures Lab 83Ms.M.Abinaya, Lect/IT

    QUICK SORT

    Aim:To write a program to perform quick sort.

    Algorithm:

    1. Get the value of how many no. to be sorted.2. Get the elements from the user.3. Two function quicksort() and swap(). Quick sort to perform sorting.4. Swap() is just to rearrange the values.5. Quick sort algorithm works by partitioning the array to be sorted, then

    recursively sorting each partition.6. Display the sorted value.

    Program:# include # include void swap(long &,long &);void quick_sort(long [],int,int);main( ){clrscr( );const int array_size=10;long array[array_size]={0};cout

  • EC6312 OOPS and Data Structures Lab 84Ms.M.Abinaya, Lect/IT

    }cout

  • EC6312 OOPS and Data Structures Lab 85Ms.M.Abinaya, Lect/IT

    }}

    Output:

    *********************** Quick Sort ******************

    * Array size = 10* Data Type = longEnter the array :

    Element[0] = 6Element[1] = 4Element[2] = 2Element[3] = 8Element[4] = 4Element[5] = 1Element[6] = 3Element[7] = 9Element[8] = 0Element[9] = 7

    Sorted Array :

    Element[0] = 0Element[1] = 1Element[2] = 2Element[3] = 3Element[4] = 4Element[5] = 4Element[6] = 6Element[7] = 7Element[8] = 8Element[9] = 9

    **********************************************************

    Result:

    Thus the program for quick sort has been executed and verifiedsuccessfully.