Ds & oops Lab Manual

download Ds & oops Lab Manual

of 66

Transcript of Ds & oops Lab Manual

  • 7/29/2019 Ds & oops Lab Manual

    1/66

    AKSHAYA COLLEGE OF ENGINEERING AND

    TECHNOLOGY,

    Coimbatore

    NAME :

    SECTION :

    BRANCH :

    II YEAR ECE (III SEMESTER)

    DATA STRUCTURES AND OBJECT ORIENTED

    PROGRAMMING LABOBSERVATION

  • 7/29/2019 Ds & oops Lab Manual

    2/66

    2 Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual

    T.Abirama Sundari-AP/ECE

    AKSHAYA COLLEGE OF ENGINEERING AND TECHNOLOGY

    DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB

    II YEAR / III SEM - ECE

    LIST OF EXPERIMENTS

    Ex No NAME OF THE EXPERIMENT Page No Marks Sign

    1 Programs for C++ Concepts(Ex 1.1 to Ex1.5)

    2 Array implementation of List Abstract Data Type (ADT)

    3 Linked list implementation of List ADT

    4 Cursor implementation of List ADT

    55.1 Stack ADT - Array implementations

    5.1 Stack linked list implementations

    6

    6.1Implement any Stack Application using array implementation of

    Stack

    6.2Implement any Stack Application using linked list

    implementation of Stack

    77.1 Queue ADT Array implementations

    7.2 Queue ADT linked list implementations

    8 Search Tree ADT - Binary Search Tree

    9 Heap Sort

    10 Quick Sort

  • 7/29/2019 Ds & oops Lab Manual

    3/66

    3 Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual

    T.Abirama Sundari-AP/ECE

    Ex. No. 1.1

    Date : STUDENT DETAILS USING CLASSES AND OBJECT

    AIM:

    To write a C++ program to display the student details using classes and object as array.

    ALGORITHM:

    Step 1: Create a class as student.Step 2: Declare the data members rollno, name, mark1, mark2, mark3, total and

    average.Step 3: Declare the member functions as getdata() anddisplaydata().

    3.1 getdata() method used to get the student details.3.2 displaydata() method used to display the student details.

    Step 4: In the main, create an object array for the student class using the followingsyntax:

    Classname objectname[size];Step 5: Get the number of students.Step 6: In getdata() method, get the student details using for loop.Step 7: In displaydata() method, display the student details using for loop.Step 8: Use the following syntax to call the member functions from the main

    Objectname.methodname();

    PROGRAM:#include#include#includeclass student{

    int rno;char name[20];

    int m1,m2,m3,t;float avg;public:void getdata();void displaydata();

    };

  • 7/29/2019 Ds & oops Lab Manual

    4/66

    4 Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual

    T.Abirama Sundari-AP/ECE

    void student :: getdata(){

    coutrno;coutname;coutm1;coutm2;coutm3;t=m1+m2+m3; avg=t/3;

    }void student :: displaydata(){

    cout

  • 7/29/2019 Ds & oops Lab Manual

    5/66

    5 Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual

    T.Abirama Sundari-AP/ECE

    OUTPUT :

    Enter the range of the student 2Enter the roll no:1Enter the name:maryEnter the mark 1:89Enter the mark 2:89Enter the mark 3:78Enter the roll no:2Enter the name:jimEnter the mark 1:67Enter the mark 2:78Enter the mark 3:89Enter the roll no:2Enter the name:jackEnter the mark 1:78Enter the mark 2:89

    Enter the mark 3:67

    **************************************************Student Details:

    **************************************************Rollno Name Mark1 Mark2 Mark3 Total Avg

    1 mary 89 89 78 256 852 jim 67 78 89 234 783 jack 78 89 67 234 78

    RESULT:

  • 7/29/2019 Ds & oops Lab Manual

    6/66

    6 Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual

    T.Abirama Sundari-AP/ECE

    Ex:No:1.2

    Date : POINTER TO DATA MEMBER

    AIM:

    To write a C++ program to find the area of rectangle using pointer to data member.

    ALGORITHM:

    Step 1: Create a class as rectangle.Step 2: Declare the data members a, b, *x,*y.Step 3: Declare the member functions as getdata() andarea().

    3.1 In the getdata() function, get the length and breadth of the rectangle and store theiraddress to the pointer variable.

    3.2 In the area() function, display the area of the rectangle.Step 4: In the main, create an object for the rectangle class using the following syntax:

    Classname objectname;Step 5: Call the getdata() and area() function using the following syntax:

    Objectname.methodname();

    PROGRAM:#include#includeclass rectangle

    {int a,b,*x,*y;public:void getdata();void area();};void rectangle :: getdata(){couta;coutb;x=&a;y=&b;}

    void rectangle :: area()

  • 7/29/2019 Ds & oops Lab Manual

    7/66

    7 Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual

    T.Abirama Sundari-AP/ECE

    {cout

  • 7/29/2019 Ds & oops Lab Manual

    8/66

    8 Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual

    T.Abirama Sundari-AP/ECE

    Ex:No:1.3

    Date : FUNCTION OVERLOADING

    AIM:

    To write a C++ program to find the volume of cube, rectangle and cylinder usingfunction overloading.

    ALGORITHM:

    Step 1: Create a class as shape.Step 2: Declare the data members as a, l, b, h, andr.Step 3: Declare the member function as volume with different arguments.Step 4: Volume function calculates the volume of cube, cylinder and rectangle based on theparameter passed to it.Step 5: In the main, create the object for the shape class.Step 6: Call the function using objectname.functionname();

    PROGRAM:

    #include#includeclass shape{int a,l,b,h;float r;public:

    void volume(int);void volume(int,int,int);void volume(float,int);};void shape :: volume(int a){cout

  • 7/29/2019 Ds & oops Lab Manual

    9/66

    9 Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual

    T.Abirama Sundari-AP/ECE

    {shape s;int a1,l1,b1,h1,h2;float r1;clrscr();cout

  • 7/29/2019 Ds & oops Lab Manual

    10/66

    10 Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual

    T.Abirama Sundari-AP/ECE

    Ex:No:1.4Date : MATRIX CLASS USING DEFAULT ARGUMENT,STATIC DATA MEMBERS AND

    FRIEND FUNCTION

    AIM:

    To write a C++ program to perform matrix manipulation using static variable, defaultargument and friend function.

    ALGORITHM:

    Step 1: Declare the class as Matrix.Step 2: Declare the data member as r, c and**x.Step 3: Declare the member function as

    Matrix(int r1=2,int c1=2);

    void get();

    void put();

    friend Matrix add(Matrix,Matrix);friend Matrix mul(Matrix,Matrix);

    Step 4: Member function with default argument is used to initialize the value of thematrix.

    Step 5: get() function is used to get the values of two matrices.Step 6: add() and mul() function are used to perform addition and multiplication of the

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

    display the result.

    PROGRAM:

    #include#include#includeclass 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);

    };

  • 7/29/2019 Ds & oops Lab Manual

    11/66

    11 Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual

    T.Abirama Sundari-AP/ECE

    matrix::matrix(int r1,int c1){

    r=r1;c=c1;x=new int*[r];for(int i=0;i

  • 7/29/2019 Ds & oops Lab Manual

    12/66

    12 Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual

    T.Abirama Sundari-AP/ECE

    c.x[i][j]=c.x[i][j]+a.x[i][k]*(b.x[k][j]);}

    return c;}

    void main(){clrscr();matrix a,b,c1,d1;a.get();b.get();cout

  • 7/29/2019 Ds & oops Lab Manual

    13/66

    13 Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual

    T.Abirama Sundari-AP/ECE

    Ex:No:1.5

    Date : COMPLEX NUMBER USING OBJECT AS ARGUMENT

    AIM:

    To write a C++ program to add two complex numbers using object as argument.

    ALGORITHM:

    Step 1: Create a class as complex.Step 2: Declare the data members as real andimg.Step 3: Declare the member functions as

    void getdata()

    void show()void sum(complex c1,complex c2)

    Step 4: getdata() method is used to get the values .Step 5: show() method is used to display the values.

    Step 6: sum() method is used to perform addition operation using object as argument.Step 7: In main, create the object for Complex class.Step 8: Call the function using objectname.functionname();

    PROGRAM:#include#include

    class complex{

    int real;int img;

    public:void getdata(){

    coutreal>>img;

    }void show(){

    cout

  • 7/29/2019 Ds & oops Lab Manual

    14/66

    14 Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual

    T.Abirama Sundari-AP/ECE

    real=c1.real+c2.real;img=c1.img+c2.img;

    }

    void main(){clrscr();complex c1,c2,c3;c1.getdata();c2.getdata();c3.add(c1,c2);cout

  • 7/29/2019 Ds & oops Lab Manual

    15/66

    15 Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual

    T.Abirama Sundari-AP/ECE

    Ex.No1.6

    Date : CONSTRUCTOR OVERLOADING

    AIM:

    To write a C++ program to display the account number and balance using constructoroverloading.

    ALGORITHM:

    Step 1: Create a class as Account.Step 2: Declare the data members as accountid andbalance.Step 3: Declare the member functions as

    void Account() implies default constructorvoid Account(float) implies one argument constructor

    void Account(int,float) implies two argument constructor

    void moneytransfer(Account,float)

    void display()Step 4: In main, create the object for Account class.Step 5: While creating the object without argument, the default constructor is called.Step 6: While creating the object with one argument, constructor with one argument is

    called and value is assigned to the accoundid.Step 7: Call the display() function to display the account details.Step 8: While creating the object with two arguments, constructor with two arguments is

    called and value is assigned to the accoundid and balance.Step 9: Call the display() function to display the account details.Step 10: Call the money transfer() function to transfer the money and display the balance

    after money transfer.

    PROGRAM:#include#include

    class Account{

    int accid;float balance;

    public:Account();Account(float );Account(int ,float);void moneytransfer(Account&,float);void display();

  • 7/29/2019 Ds & oops Lab Manual

    16/66

    16 Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual

    T.Abirama Sundari-AP/ECE

    };

    Account::Account(){

    accid=1;balance=1000.0;

    }

    Account::Account(float y){

    accid=2;balance=y;

    }

    Account::Account(int x,float z){

    accid=x;balance=z;

    }

    void Account::display(){

    cout

  • 7/29/2019 Ds & oops Lab Manual

    17/66

    17 Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual

    T.Abirama Sundari-AP/ECE

    ac1.display();ac2.display();ac3.display();

    ac3.moneytransfer(ac1,a);

    cout

  • 7/29/2019 Ds & oops Lab Manual

    18/66

    18 Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual

    T.Abirama Sundari-AP/ECE

    Ex no: 2 ARRAY IMPLEMENTATION OF LIST

    Date :

    AIM: To develop a C++ program for implementing list abstract data type using array.

    ALGORITHM:

    STEP 1: Start the program and include the necessary header files for the programSTEP 2: Declare the necessary variables.STEP 3: Create the main function and declare the necessary operations to be performed on the lisSTEP 4: Define create() function such that it is called whenever the array has be created.STEP 5: Define insert() function such that it is called whenever the array elements to are to

    inserted into the listSTEP 6: Define the display() function such that it is called whenever the array elements has to

    displayed

    STEP 7: Define the search() function such that it is called whenever an array element has tosearched and its position will be returned.

    STEP 8: Define the delete() function such that it is called whenever an array element has todeleted from the list.

    STEP 9: Define the reverse() function such that it is called whenever the array elements has todisplayed in reverse form.

    STEP 10: Get the choice from the user and call the necessary functions wherever needed.STEP 11: Stop the program.

    PROGRAM:

    #include#include#include#define MAX 20int list[MAX];

    class arr_list{

    public:int create();

    void display(int);void reverse(int);int search(int);void delet(int);

    };

  • 7/29/2019 Ds & oops Lab Manual

    19/66

    19 Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual

    T.Abirama Sundari-AP/ECE

    int arr_list::create(){

    int i,n;clrscr();cout

  • 7/29/2019 Ds & oops Lab Manual

    20/66

    20 Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual

    T.Abirama Sundari-AP/ECE

    cout

  • 7/29/2019 Ds & oops Lab Manual

    21/66

    21 Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual

    T.Abirama Sundari-AP/ECE

    void main(){

    int choice,len,position;clrscr();arr_list a;do{

    cout

  • 7/29/2019 Ds & oops Lab Manual

    22/66

    22 Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual

    T.Abirama Sundari-AP/ECE

    ------------------How many elements you want in the list? 3Enter the element number1: 10Enter the element number2: 20Enter the element number3: 30

    List is successfully created!!!Press any key to continue.ENTER THE CHOICE: 2

    DISPLAY LIST------------------------------------

    The list is 10 20 30Press any key to continue.ENTER THE CHOICE: 3

    SEARCH LIST------------------------------------

    Enter the number you want to search: 20The given number is at position 1Press any key to continue.ENTER THE CHOICE: 4

    REVERSE LIST------------------------------------

    The reverse list is 30 20 10Press any key to continue..ENTER THE CHOICE: 5

    DELETE LIST------------------

    ------------------Enter the number you want to search : 20The given number is at position 1Press any key to continue.The given element is now deleted..We put -1 to indicate the empty locationENTER THE CHOICE: 2

    DISPLAY LIST------------------------------------

    The list is 10 30

    ENTER THE CHOICE: 6

    RESULT:

  • 7/29/2019 Ds & oops Lab Manual

    23/66

    23 Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual

    T.Abirama Sundari-AP/ECE

    Ex no: 3 LINKED LIST IMPLEMENTATION OF LIST ADT

    Date :

    AIM:

    To develop a C++ program for implementing list abstract data type using linked list implementat

    ALGORITHM:

    STEP 1: Start the program and include the necessary header files for the programSTEP 2: Declare the necessary variables.STEP 3: Create the main function and declare the necessary operations to be performed on the lisSTEP 4: Define create() function such that it is called whenever the array has be created.STEP 5: Define insert() function such that it is called whenever the array elements to are to

    inserted into the list.

    STEP 6: Define the printlist() function such that it is called whenever the array elements has to bedisplayed

    STEP 7: Define the find() function such that it is called whenever an array element has to besearched and its position will be returned.

    STEP 8: Define the remove() function such that it is called whenever an array element has to bedeleted from the list.

    STEP 9: Define the reverse() function such that it is called whenever the array elements has to bedisplayed in reverse form.

    STEP 10: Define the findprev() function such that it is called whenever an array element has to bsearched and its previous position will be returned.

    STEP 11: Get the choice from the user and call the necessary functions wherever needed.

    STEP 12: Stop the program.

    PROGRAM:#include#include#include

    template

    class list

    {struct node

    {t element;node*next;node(t E=0,node*n=NULL)

  • 7/29/2019 Ds & oops Lab Manual

    24/66

    24 Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual

    T.Abirama Sundari-AP/ECE

    {element=E;next=n;

    }};t x;node *head;node *current;

    public:list(){

    head=new node;current=head;

    };void create();void insert();void remove();void find();

    void printlist();int findprev(t);

    };

    template

    void list::create(){

    int n,i;coutn;

    for(i=1;i

  • 7/29/2019 Ds & oops Lab Manual

    25/66

    25 Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual

    T.Abirama Sundari-AP/ECE

    while(p!=NULL){

    coutnext->element==x){

    current=q;return 1;

    }return 0;

    }template

    void list::remove(){

    node*p;coutx;

    if(findprev(x)){

    p=current->next;current->next=p->next;delete p;

    }else{

    cout

  • 7/29/2019 Ds & oops Lab Manual

    26/66

    26 Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual

    T.Abirama Sundari-AP/ECE

    cin>>ch;coutx;

    switch(ch){

    case 1:p=new node(x,head->next);head->next=p;break;

    case 2:coutpos;for(q=head;inext,i++);

    if(q){

    p=new node(x,q->next);

    q->next=p;}else{

    coutnext);

    p=new node(x,q->next);q->next=p;}

    }

    templatevoid list::find(){

    coutx;node*q;

    q=head->next;

    while(q!=NULL&&q->element!=x)q=q->next;

    if(q)

  • 7/29/2019 Ds & oops Lab Manual

    27/66

    27 Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual

    T.Abirama Sundari-AP/ECE

    cout

  • 7/29/2019 Ds & oops Lab Manual

    28/66

    28 Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual

    T.Abirama Sundari-AP/ECE

    OUTPUT:MENU:

    1. CREATE2. DISPLAY3. PRINTLIST4. DELETE5. FIND6. EXIT

    ENTER THE OPERATION TO BE PERFORMED: 1ENTER HOW MANY NODES TO CREATE? 2ENTER THE NODE1: 10ENTER THE NODE2: 20

    ENTER THE OPERATION TO BE PERFORMED: 3ELEMENTS IN THE LIST

    10 20ENTER THE OPERATION TO BE PERFORMED: 2

    INSERT AT1. FIRST2. ANYWHERE3. LAST

    ENTER THE CHOICE :1ENTER THE ELEMENT TO BE INSERTED 40ENTER THE OPERATION TO BE PERFORMED: 3ELEMENTS IN THE LIST

    40 10 20ENTER THE OPERATION TO BE PERFORMED: 2INSERT AT

    1. FIRST2. ANYWHERE3. LAST

    ENTER THE CHOICE :2ENTER THE ELEMENT TO BE INSERTED 50ENTER THE OPERATION TO BE PERFORMED: 2ELEMENTS IN THE LIST

    40 50 10 20ENTER THE OPERATION TO BE PERFORMED: 2INSERT AT

    1. FIRST

    2. ANYWHERE3. LAST

    ENTER THE CHOICE :3ENTER THE ELEMENT TO BE INSERTED 30ENTER THE OPERATION TO BE PERFORMED: 3ELEMENTS IN THE LIST

  • 7/29/2019 Ds & oops Lab Manual

    29/66

    29 Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual

    T.Abirama Sundari-AP/ECE

    40 50 10 20 30ENTER THE OPERATION TO BE PERFORMED: 4ENTER THE ELEMENT TO BE DELETED : 10ELEMENT DELETED!!!ENTER THE OPERATION TO BE PERFORMED: 3ELEMENTS IN THE LIST

    40 50 20 30ENTER THE OPERATION TO BE PERFORMED: 5ENTER THE ELEMENT TO BE SEARCHED : 30ELEMENTS FOUND !!!ENTER THE OPERATION TO BE PERFORMED : 6

    RESULT:

  • 7/29/2019 Ds & oops Lab Manual

    30/66

    30 Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual

    T.Abirama Sundari-AP/ECE

    Ex no: 4 CURSOR IMPLEMENTATION OF LIST ADT

    Date :

    AIM:

    To develop a C++ program for implementing list abstract data type using cursor implementation.

    ALGORITHM:

    STEP 1: Start the program and include the necessary header files for the programSTEP 2: Declare the necessary variables.STEP 3: Create the main function and declare the necessary operations to be performed on the list.STEP 4: Define create() function such that it is called whenever the array has be created.STEP 5: Define the display() function such that it is called whenever the array elements has to be displaySTEP 6: Define the search() function such that it is called whenever an array element has to be searched

    its position will be returned.

    STEP 7: Define the delete() function such that it is called whenever an array element has to be deleted frthe list.

    STEP 8: Get the choice from the user and call the necessary functions wherever needed.STEP 9: Stop the program.

    PROGRAM:#include#include#include#define MAX 20

    int List[MAX];void main(){

    int choice,len,position;int create();char ans;clrscr();void display(int);int search(int);void delet(int);do

    {cout

  • 7/29/2019 Ds & oops Lab Manual

    31/66

    31 Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual

    T.Abirama Sundari-AP/ECE

    cout

  • 7/29/2019 Ds & oops Lab Manual

    32/66

    32 Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual

    T.Abirama Sundari-AP/ECE

    void display(int n){

    int i;clrscr();cout

  • 7/29/2019 Ds & oops Lab Manual

    33/66

    33 Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual

    T.Abirama Sundari-AP/ECE

    4. DELETE5. QUIT

    ENTER THE CHOICE: 1How many elements you want in the list? 3Enter the element number1: 10Enter the element number2: 20Enter the element number3: 30

    List is successfully created!!!ENTER THE CHOICE: 2The list is 10 20 30ENTER THE CHOICE: 3Enter the number you want to search: 20The given number is at position 1ENTER THE CHOICE: 4Enter the number you want to search : 30The given number is at position 2The element is now deleted..ENTER THE CHOICE: 2The list is 10 20ENTER THE CHOICE: 5

    RESULT:

  • 7/29/2019 Ds & oops Lab Manual

    34/66

    34 Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual

    T.Abirama Sundari-AP/ECE

    Ex no: 5.1 ARRAY IMPLEMENTATION OF STACK ADT

    Date :

    AIM:

    To develop a C++ program for implementing stack abstract data type using array implementation.

    ALGORITHM:

    STEP 1: Start the program and include the necessary header files for the programSTEP 2: Declare the necessary variables and the structure for the stack.STEP 3: Create the main function and declare the necessary operations to be performed on the stack.STEP 4: Define the isfull() function such that it is used to check whether the stack is full or not.STEP 5: Define the isempty() function such that it is used to check whether the stack is empty.STEP 6: Define the display() function such that it is called whenever the elements in a stack has to be

    displayed

    STEP 7: Define the push() function such that it is called whenever an array element has to be inserted intthe stack.

    STEP 8: Define the pop() function such that it is called whenever an array element has to be deleted fromthe stack.

    STEP 9: Get the choice from the user and call the necessary functions wherever needed.STEP 10: Stop the program.

    PROGRAM:

    #include

    #include#include#define size 5struct stack{

    int s[size];int top;

    }st;class arr_stack{

    public:

    int stfull();int stempty();void push(int);int pop();void display();

    };

  • 7/29/2019 Ds & oops Lab Manual

    35/66

    35 Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual

    T.Abirama Sundari-AP/ECE

    int arr_stack::stfull(){

    if(st.top>=size-1){

    return 1;}else{

    return 0;}

    }void arr_stack::push(int item){

    st.top++;st.s[st.top]=item;

    }int arr_stack::stempty(){

    if (st.top==-1){

    return 1;}else{

    return 0;}

    }int arr_stack::pop(){

    int item;item=st.s[st.top];st.top--;return(item);

    }void arr_stack::display(){

    int i;

    if(stempty()){

    cout

  • 7/29/2019 Ds & oops Lab Manual

    36/66

    36 Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual

    T.Abirama Sundari-AP/ECE

    cout

  • 7/29/2019 Ds & oops Lab Manual

    37/66

    37 Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual

    T.Abirama Sundari-AP/ECE

    break;case 4: exit(0);}

    coutans;}while(ans==1);

    getch();}

    OUTPUT:

    MENU:1. PUSH2. POP3. DISPLAY4. QUIT

    ENTER THE CHOICE: 1

    Enter the item to be pushed : 10ENTER THE CHOICE: 1Enter the item to be pushed : 15ENTER THE CHOICE: 1Enter the item to be pushed : 25ENTER THE CHOICE: 3The stack is 10 15 25ENTER THE CHOICE: 2The popped element is 25ENTER THE CHOICE: 3The stack is 10 15

    ENTER THE CHOICE: 4

    RESULT:

  • 7/29/2019 Ds & oops Lab Manual

    38/66

    38 Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual

    T.Abirama Sundari-AP/ECE

    Ex no: 5.2 LINKED LIST IMPLEMENTATION OF STACK ADT

    Date :

    AIM: To develop a C++ program for implementing stack abstract data type using linked lis

    implementation.

    ALGORITHM:

    STEP 1: Start the program and include the necessary header files for the programSTEP 2: Declare the necessary variables and the structure for the stack.STEP 3: Create the main function and declare the necessary operations to be performed on the stack.STEP 4: Define the isfull() function such that it is used to check whether the stack is full or not.STEP 5: Define the isempty() function such that it is used to check whether the stack is empty.STEP 6: Define the display() function such that it is called whenever the elements in a stack has to be

    displayed

    STEP 7: Define the push() function such that it is called whenever an array element has to be inserted intthe stack.

    STEP 8: Define the pop() function such that it is called whenever an array element has to be deleted fromthe stack.

    STEP 9: Get the choice from the user and call the necessary functions wherever needed.STEP 10: Stop the program.

    PROGRAM:#include#include

    #include#includestruct node{

    int info;struct node*link;

    }*top=NULL;

    class st_list{

    public:

    void push();void pop();void display();

    };void st_list::push(){

  • 7/29/2019 Ds & oops Lab Manual

    39/66

    39 Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual

    T.Abirama Sundari-AP/ECE

    struct node*tmp;int pushed_item;tmp=(struct node*)malloc(sizeof(struct node));coutpushed_item;tmp->info=pushed_item;tmp->link=top;top=tmp;

    }void st_list::pop(){

    struct node*tmp;

    if(top==NULL)cout

  • 7/29/2019 Ds & oops Lab Manual

    40/66

    40 Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual

    T.Abirama Sundari-AP/ECE

    while(1){

    cout

  • 7/29/2019 Ds & oops Lab Manual

    41/66

    41 Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual

    T.Abirama Sundari-AP/ECE

    Ex no: 6 ARRAY IMPLEMENTATION OF STACK ADT - APPLICATION

    Date :

    AIM:To develop a C++ program for implementing application of stack abstract data type using array

    implementation.

    ALGORITHM:

    STEP 1: Start the program and include the necessary header files for the programSTEP 2: Declare the necessary variables and the structure for the stack.STEP 3: Create the main function and declare the necessary operations to be performed on the stack.STEP 4: Define the infix() function such that it is used to display the expression in infix format.STEP 5: Define the prefix() function such that it is used to display the expression in prefix format.STEP 6: Define the postfix() function such that it is called whenever the elements in a stack has to be

    displayed in the postfix format.

    STEP 7: Define the push() function such that it is called whenever an array element has to be inserted intthe stack.

    STEP 8: Define the pop() function such that it is called whenever an array element has to be deleted fromthe stack.

    STEP 9: Get the choice from the user and call the necessary functions wherever needed.STEP 10: Stop the program.

    PROGRAM:

    #include

    #include#include

    char inf[40],post[40];int top=0,st[20];

    class infix{

    public:void postfix();void push(int);char pop();

    };

    void infix::postfix(){

    int i,j=0;for(i=0;inf[i]!='\0';i++){

    switch(inf[i])

  • 7/29/2019 Ds & oops Lab Manual

    42/66

    42 Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual

    T.Abirama Sundari-AP/ECE

    {case '+': while(st[top]>=1)

    post[j++]=pop();push(1);break;

    case '-': while(st[top]>=1)post[j++]=pop();push(2);break;

    case '*': while(st[top]>=3)post[j++]=pop();push(3);break;

    case '/': while(st[top]>=3)post[j++]=pop();push(4);break;

    case '^': while(st[top]>=4)

    post[j++]=pop();push(5);break;

    case '(': push(0);break;

    case ')': while(st[top]!=0)post[j++]=pop();top--;break;

    default: post[j++]=inf[i];}

    }while(top>0)post[j++]=pop();cout

  • 7/29/2019 Ds & oops Lab Manual

    43/66

    43 Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual

    T.Abirama Sundari-AP/ECE

    {case 1: e='+';

    break;case 2: e='-';

    break;case 3: e='*';

    break;case 4: e='/';

    break;case 5: e='^';

    break;}

    return(e);}

    void main(){

    clrscr();infix i;

    coutinf;i.postfix();getch();

    }

    OUTPUT:

    ENTER THE INFIX EXPRESSION:a*b/c+(d*e)-fpostfix expression is:ab*c/de*+f-

    RESULT:

  • 7/29/2019 Ds & oops Lab Manual

    44/66

    44 Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual

    T.Abirama Sundari-AP/ECE

    Ex no: 7.1 ARRAY IMPLEMENTATION OF QUEUE ADT

    Date :

    AIM: To develop a C++ program for implementing queue abstract data type using array implementat

    ALGORITHM:

    STEP 1: Start the program and include the necessary header files for the programSTEP 2: Declare the necessary variables and the structure for the queue.STEP 3: Create the main function and declare the necessary operations to be performed on the

    queue.STEP 4: Define the display() function such that it is called whenever the elements in a queue has

    be displayed.STEP 5: Define the insert() function such that it is called whenever an array element has to be

    inserted into the queue.

    STEP 6: Define the delete() function such that it is called whenever an array element has to bedeleted from the queue.

    STEP 7: Get the choice from the user and call the necessary functions wherever neededSTEP 8: Stop the program.

    PROGRAM:

    #include#include#include

    #define MAX 5int queue_arr[MAX];int rear=-1;int front=-1;

    class arr_q{

    public:void insert();void del();void display();

    };

    void arr_q::insert(){

    int item;if(rear==MAX-1)

  • 7/29/2019 Ds & oops Lab Manual

    45/66

    45 Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual

    T.Abirama Sundari-AP/ECE

    {cout

  • 7/29/2019 Ds & oops Lab Manual

    46/66

    46 Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual

    T.Abirama Sundari-AP/ECE

    cout

  • 7/29/2019 Ds & oops Lab Manual

    47/66

    47 Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual

    T.Abirama Sundari-AP/ECE

    OUTPUT:PROGRAM FOR ARRAY IMPLEMENTATION OF QUEUE...

    1.Insert2.Delete3.Display4.Quit

    Enter your choice(1-4): 2

    Queue underflow

    PROGRAM FOR ARRAY IMPLEMENTATION OF QUEUE...

    1.Insert2.Delete3.Display4.Quit

    Enter your choice(1-4): 1

    Enter the element for adding in queue:23

    PROGRAM FOR ARRAY IMPLEMENTATION OF QUEUE...

    1.Insert2.Delete3.Display4.Quit

    Enter your choice(1-4): 1

    Enter the element for adding in queue:34

    PROGRAM FOR ARRAY IMPLEMENTATION OF QUEUE...

    1.Insert2.Delete3.Display4.Quit

    Enter your choice(1-4): 3Queue is:

    23 34

    PROGRAM FOR ARRAY IMPLEMENTATION OF QUEUE...

  • 7/29/2019 Ds & oops Lab Manual

    48/66

    48 Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual

    T.Abirama Sundari-AP/ECE

    1.Insert2.Delete3.Display4.Quit

    Enter your choice(1-4): 2

    Element deleted from queue is:23

    PROGRAM FOR ARRAY IMPLEMENTATION OF QUEUE...

    1.Insert2.Delete3.Display4.Quit

    Enter your choice(1-4): 3

    Queue is:

    34

    PROGRAM FOR ARRAY IMPLEMENTATION OF QUEUE...

    1.Insert2.Delete3.Display4.Quit

    Enter your choice(1-4): 4

    RESULT:

  • 7/29/2019 Ds & oops Lab Manual

    49/66

    49 Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual

    T.Abirama Sundari-AP/ECE

    Ex no: 7.2 LINKED LIST IMPLEMENTATION OF QUEUE ADT

    Date :

    AIM:To develop a C++ program for implementing queue abstract data type using linked list

    implementation.

    ALGORITHM:

    STEP 1: Start the program and include the necessary header files for the programSTEP 2: Declare the necessary variables and the structure for the queue.STEP 3: Create the main function and declare the necessary operations to be performed on the queue.STEP 4: Define the display() function such that it is called whenever the elements in a queue has to be

    displayed.STEP 5: Define the insert() function such that it is called whenever element has to be inserted into the

    queue.

    STEP 6: Define the delete() function such that it is called whenever element has to be deleted from thequeue.

    STEP 7: Get the choice from the user and call the necessary functions wherever neededSTEP 8: Stop the program.

    PROGRAM :

    #include#include#include

    #includestruct node{

    int info;struct node * link;

    }*front=NULL,*rear=NULL;

    class list_q{

    public:void insert();

    void del();void display();

    };void list_q::insert(){

    struct node*tmp;

  • 7/29/2019 Ds & oops Lab Manual

    50/66

    50 Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual

    T.Abirama Sundari-AP/ECE

    int added_item;tmp=(struct node*)malloc(sizeof(struct node));coutadded_item;tmp->info=added_item;tmp->link=NULL;

    if(front==NULL)front=tmp;

    elserear->link=tmp;rear=tmp;

    }void list_q::del(){

    struct node*tmp;if(front==NULL){

    cout

  • 7/29/2019 Ds & oops Lab Manual

    51/66

    51 Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual

    T.Abirama Sundari-AP/ECE

    cout

  • 7/29/2019 Ds & oops Lab Manual

    52/66

    52 Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual

    T.Abirama Sundari-AP/ECE

    OUTPUT:1.INSERT

    2.DELETE

    3.DISPLAY

    4.QUIT

    ENTER YOUR CHOICE(1-4):1

    Input the element for adding in queue:4

    PROGRAM FOR LINKED LIST IMPLEMENTATION OF QUEUE...

    1.INSERT

    2.DELETE

    3.DISPLAY

    4.QUIT

    ENTER YOUR CHOICE(1-4):3

    QUEUE ELEMENTS :

    2

    4

    PROGRAM FOR LINKED LIST IMPLEMENTATION OF QUEUE...

    1.INSERT

    2.DELETE

    3.DISPLAY

    4.QUIT

    ENTER YOUR CHOICE(1-4):2

  • 7/29/2019 Ds & oops Lab Manual

    53/66

    53 Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual

    T.Abirama Sundari-AP/ECE

    DELETED ELEMENT IS:2

    PROGRAM FOR LINKED LIST IMPLEMENTATION OF QUEUE...

    1.INSERT

    2.DELETE

    3.DISPLAY

    4.QUIT

    ENTER YOUR CHOICE(1-4):3

    QUEUE ELEMENTS :

    4

    PROGRAM FOR LINKED LIST IMPLEMENTATION OF QUEUE...

    1.INSERT

    2.DELETE

    3.DISPLAY

    4.QUIT

    ENTER YOUR CHOICE(1-4):4

    RESULT:

  • 7/29/2019 Ds & oops Lab Manual

    54/66

    54 Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual

    T.Abirama Sundari-AP/ECE

    SEARCH TREE ADT - BINARY SEARCH TREE

    EX NO: 8

    DATE:

    AIM:

    To write a program to implement search tree ADT using Binary Search Tree

    ALGORITHM:

    Step 1: Start the process. Step 2: Initialize and declare variables. Step 3: Construct the Tree Step 4: Data values are given which we call a key and a binary search tree Step 5: To search for the key in the given binary search tree, start with the root node and

    Compare the key with the data value of the root node. If they match, return theroot pointer.

    Step 6: If the key is less than the data value of the root node, repeat the process by usingthe left subtree.

    Step 7: Otherwise, repeat the same process with the right subtree until either a match isfound or the subtree under consideration becomes an empty tree.

    Step 8: Terminate

    PROGRAM:

    #include

    #include

    #include#include

    struct tree

    {

    int data;

    struct tree *lchild;

    struct tree *rchild;

    }*t,*temp;

    int element;

    class bst

    {

    public:

    // int element;

    void inorder(struct tree *);

  • 7/29/2019 Ds & oops Lab Manual

    55/66

    55 Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual

    T.Abirama Sundari-AP/ECE

    void preorder(struct tree *);

    void postorder(struct tree *);

    struct tree * create(struct tree *, int);

    struct tree * find(struct tree *, int);

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

    struct tree * del(struct tree *, int);

    struct tree * findmin(struct tree *);

    struct tree * findmax(struct tree *);

    };

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

    {

    t=(struct tree *)malloc(sizeof(struct tree));

    t->data=element;

    t->lchild=NULL;

    t->rchild=NULL;return t;

    }

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

    {

    if(t==NULL)

    return NULL;

    if(elementdata)

    return(find(t->lchild,element));

    else if(element>t->data)return(find(t->rchild,element));

    else

    return t;

    }

    struct tree * bst :: findmin(struct tree *t)

    {

    if(t==NULL)

    return NULL;

    else if(t->lchild==NULL)

    return t;

    else

    return(findmin(t->lchild));

    }

  • 7/29/2019 Ds & oops Lab Manual

    56/66

    56 Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual

    T.Abirama Sundari-AP/ECE

    struct tree* bst :: findmax(struct tree *t)

    {

    if(t!=NULL)

    {

    while(t->rchild!=NULL)

    t=t->rchild;

    }

    return t;

    }

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

    {

    if(t==NULL)

    {

    t=(struct tree *)malloc(sizeof(struct tree));t->data=element;

    t->lchild=NULL;

    t->rchild=NULL;

    return t;

    }

    else

    {

    if(elementdata)

    {

    t->lchild=insert(t->lchild,element);}

    else if(element>t->data)

    {

    t->rchild=insert(t->rchild,element);

    }

    else if(element==t->data)

    {

    printf("element already present\n");

    }

    return t;

    }

    }

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

  • 7/29/2019 Ds & oops Lab Manual

    57/66

  • 7/29/2019 Ds & oops Lab Manual

    58/66

    58 Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual

    T.Abirama Sundari-AP/ECE

    return;

    else

    {

    printf("\t%d",t->data);

    preorder(t->lchild);

    preorder(t->rchild);

    }

    }

    void bst :: postorder(struct tree *t)

    {

    if(t==NULL)

    return;

    else

    {

    postorder(t->lchild);postorder(t->rchild);

    printf("\t%d",t->data);

    }

    }

    void main()

    {

    int ch;

    bst obj;

    do{

    printf("\n\t\t\tBINARY SEARCH TREE");

    printf("\n\t\t\t****** ****** ****");

    printf("\nMain Menu\n");

    printf("\n1.Create\n2.Insert\n3.Delete\n4.Find\n5.FindMin\n6.FindMax");

    printf("\n7.Inorder\n8.Preorder\n9.Postorder\n10.Exit\n");

    printf("\nEnter ur choice :");

    scanf("%d",&ch);

    switch(ch)

    {

    case 1:

    printf("\nEnter the data:");

    scanf("%d",&element);

    t=obj.create(t,element);

  • 7/29/2019 Ds & oops Lab Manual

    59/66

    59 Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual

    T.Abirama Sundari-AP/ECE

    obj.inorder(t);

    break;

    case 2:

    printf("\nEnter the data:");

    scanf("%d",&element);

    t=obj.insert(t,element);

    obj.inorder(t);

    break;

    case 3:

    printf("\nEnter the data:");

    scanf("%d",&element);

    t=obj.del(t,element);

    obj.inorder(t);

    break;

    case 4:

    printf("\nEnter the data:");scanf("%d",&element);

    temp=obj.find(t,element);

    if(temp->data==element)

    printf("\nElement %d is at %d",element,temp);

    else

    printf("\nElement is not found");

    break;

    case 5:

    temp=obj.findmin(t);

    printf("\nMix element=%d",temp->data);break;

    case 6:

    temp=obj.findmax(t);

    printf("\nMax element=%d",temp->data);

    break;

    case 7:

    obj.inorder(t);

    break;

    case 8:

    obj.preorder(t);

    break;

    case 9:

    obj.postorder(t);

    break;

  • 7/29/2019 Ds & oops Lab Manual

    60/66

    60 Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual

    T.Abirama Sundari-AP/ECE

    case 10:

    exit(0);

    }

    }while(ch

  • 7/29/2019 Ds & oops Lab Manual

    61/66

    61 Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual

    T.Abirama Sundari-AP/ECE

    Enter ur choice :2

    Enter the data:43

    12 43

    BINARY SEARCH TREE

    ****** ****** ****Main Menu1.Create2.Insert3.Delete4.Find5.FindMin6.FindMax7.Inorder8.Preorder9.Postorder

    10.ExitEnter ur choice :8

    12 23 45 32 65 56

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

    Main Menu

    1.Create2.Insert3.Delete

    4.Find5.FindMin6.FindMax7.Inorder8.Preorder9.Postorder10.Exit

    Enter ur choice :932 56 65 45 23 12

    RESULT:

  • 7/29/2019 Ds & oops Lab Manual

    62/66

    62 Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual

    T.Abirama Sundari-AP/ECE

    HEAP SORT

    EX NO: 9

    DATE:

    AIM:

    To write a C++ program to implement Heap sort.

    ALGORITHM:

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

    PROGRAM:

    #include

    #include

    #include

    class heaps

    {public:

    void heapsort(int x[],int);

    };void heaps :: heapsort(int x[],int arr)

    {

    int i,m;

    int lc,rc,mc,rt,temp;rt=(arr-1)/2;

    for(m=rt;m>=0;m--)

    {for(i=rt;i>=0;i--)

    {

    lc=(2*i)+1;

    rc=(2*i)+2;if((lc

  • 7/29/2019 Ds & oops Lab Manual

    63/66

    63 Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual

    T.Abirama Sundari-AP/ECE

    mc=lc;

    }else

    {

    if(rc>arr)mc=lc;

    else

    mc=rc;}

    if(x[i]x[i];

    }

    for(i=n;i>1;i--){

    obj.heapsort(x,i-1);

    }cout

  • 7/29/2019 Ds & oops Lab Manual

    64/66

    64 Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual

    T.Abirama Sundari-AP/ECE

    OUTPUT:

    Enter the size:5

    Enter 5 elements:9067127501

    sorted elements:112677590

    RESULT:

  • 7/29/2019 Ds & oops Lab Manual

    65/66

    65 Akshaya College of Engineering and Technology-ECE Department-DS&OOPS Lab Manual

    T.Abirama Sundari-AP/ECE

    QUICK SORT

    EX NO: 10

    DATE:

    AIM:

    To write a program in C++ to implement Quick sort.

    ALGORITHM:

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

    each partition.

    Step 6: Display the sorted value.PROGRAM:

    #include

    #include

    #include

    int i,j,n,pi,a[20];class quicksort

    {

    public:void qsort(int a[],int ft,int lt);

    void swap(int a[],int i,int j);};void quicksort :: qsort(int a[],int lb,int ub)

    { if(lb

  • 7/29/2019 Ds & oops Lab Manual

    66/66

    qsort(a,lb,j-1);

    qsort(a,j+1,ub);}

    }

    void quicksort :: swap(int a[],int i,int j){

    int t;

    t=a[i];a[i]=a[j];

    a[j]=t;

    }

    void main(){

    int n,i,a[20];

    quicksort obj;clrscr();

    coutn;

    cout