computer lab manual expt 2-10

Upload: ram-kumar

Post on 29-May-2018

234 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/9/2019 computer lab manual expt 2-10

    1/52

    EX.NO:5 ARRAY IMPLEMENTATION OF LIST ADT

    DATE :

    AIM:

    To Write a C++ Program for array implementation of list ADT.

    ALGORITHM:Step1: Create nodes first,last,next,prev and cur then set the value as NULL.

    Step 2: Read the list operation type.

    step 3: If operation type is create then process the following steps.

    1. Allocate memory for node cur.

    2. Read data in cur's data area.

    3. Assign cur node as NULL.

    4. Assign first=last=cur.

    Step 4: If operation type is Insert then process the following steps.

    1. Allocate memory for node cur.

    2. Read data in cur's data area.

    3. Read the position the Data to be insert.4. Availability of the position is true then assing cur's node as first and first=cur.

    5. If availability of position is false then do following steps.

    1. Assign next as cur and count as zero.

    2. Repeat the following steps until count less than postion.

    1 .Assign prev as next

    2. Next as prev of node.

    3. Add count by one.

    4. If prev as NULL then display the message INVALID POSITION.

    5. If prev not qual to NULL then do the following steps.

    1. Assign cur's node as prev's node.

    2. Assign prev's node as cur.

    Step5: If operation type is delete then do the following steps.

    1. Read the position .

    2. Check list is Empty .If it is true display the message List empty.

    3. If position is first.

    1. Assign cur as first.

    2. Assign First as first of node.

    3. Reallocate the cur from memory.

    4. If position is last.

    1. Move the current node to prev.

    2. cur's node as Null.

    3. Reallocate the Last from memory.

    4. Assign last as cur.

    5. If position is enter Mediate.

    1. Move the cur to required postion.

    2. Move the Previous to cur's previous position

    3. Move the Next to cur's Next position.

    4. Now Assign previous of node as next.

    5. Reallocate the cur from memory.

  • 8/9/2019 computer lab manual expt 2-10

    2/52

    step 6: If operation is traverse.

    1. Assign current as first.

    2. Repeat the following steps untill cur becomes NULL.

    PROGRAM

    #include#include

    #include

    void create();

    void insert();

    void deletion();

    void search();

    void display();

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

    void main()

    {

    int c;char g='y';

    clrscr();

    do

    {

    cout

  • 8/9/2019 computer lab manual expt 2-10

    3/52

    coutn;

    for(i=0;i>b[i];}}

    void deletion()

    {

    coutd;

    for(i=0;i

  • 8/9/2019 computer lab manual expt 2-10

    4/52

    Output

    Main Menu

    1.Create

    2.Delete

    3.Search

    4.Insert

    5.Display

    6.Exit

    Enter your choice

    1

    Enter the number

    2

    3

    4

    Do u want to continue

    n

    Result:

    Thus, the array implementation of list ADT program has been written and executed

    successfully.

  • 8/9/2019 computer lab manual expt 2-10

    5/52

    EX.NO:6 Linked list implementation of List ADT

    DATE :

    AIM:

    To Write a C++ Program for linked list implementation of list ADT.

    Algorithm:

    Step1: Create nodes first,last,next,prev and cur then set the value as NULL.

    Step 2: Read the list operation type.

    step 3: If operation type is create then process the following steps.

    1. Allocate memory for node cur.

    2. Read data in cur's data area.

    3. Assign cur link as NULL.

    4. Assign first=last=cur.

    Step 4: If operation type is Insert then process the following steps.

    1. Allocate memory for node cur.

    2. Read data in cur's data area.3. Read the position the Data to be inserting.

    4. Availability of the position is true then assign cur's link as first and first=cur.

    5. If availability of position is false then do following steps.

    1. Assign next as cur and count as zero.

    2. Repeat the following steps until count less than position.

    1 .Assign prev as next

    2. Next as prev of link.

    3. Add count by one.

    4. If prev as NULL then display the message INVALID POSITION.

    5. If prev not qual to NULL then do the following steps.

    1. Assign cur's link as prev's link.

    2. Assign prev's link as cur.

    Step5: If operation type is delete then do the following steps.

    1. Read the position .

    2. Check list is Empty .If it is true display the message List empty.

    3. If position is first.

    1. Assign cur as first.

    2. Assign First as first of link.

    3. Reallocate the cur from memory.

    4. If position is last.

    1. Move the current node to prev.

    2. cur's link as Null.

    3. Reallocate the Last from memory.

    4. Assign last as cur.

    5. If position is enter Mediate.

    1. Move the cur to required position.

    2. Move the Previous to cur's previous position

    3. Move the Next to cur's Next position.

    4. Now assign previous of link as next.

  • 8/9/2019 computer lab manual expt 2-10

    6/52

    5. Reallocate the cur from memory.

    step 6: If operation is traverse.

    1. Assign current as first.

    2. Repeat the following steps until cur becomes NULL.

    Program

    #include#include

    #include

    class list

    {

    struct node

    {

    int data;

    node *link;

    }*p;

    public:

    void inslast(int);void insbeg(int);

    void insnext(int,int);

    void delelement(int);

    void delbeg();

    void dellast();

    void disp();

    int seek(int);

    list(){p=NULL;}

    ~list();

    };

    void list::inslast(int x)

    {

    node *q,*t;

    if(p==NULL)

    {

    p=new node;

    p->data=x;

    p->link=NULL;

    }

    else

    {

    q=p;

    while(q->link!=NULL)

    q=q->link;

    t=new node;

    t->data=x;

    t->link=NULL;

    q->link=t;

  • 8/9/2019 computer lab manual expt 2-10

    7/52

    }

    coutlink=q;

    coutlink;

    delete q;

    return;

    }

    r=q;

    while(q!=NULL)

    {

    if(q->data==x)

    {

    r->link=q->link;

    delete q;

    return;

    }

    r=q;

    q=q->link;

    }

    cout

  • 8/9/2019 computer lab manual expt 2-10

    8/52

    q=p;

    if(q==NULL)

    {

    coutlink;

    q->link=NULL;

    return;

    }

    list::~list()

    {

    node *q;

    if(p==NULL) return;

    while(p!=NULL)

    {

    q=p->link;

    delete p;

    p=q;

    }

    }

  • 8/9/2019 computer lab manual expt 2-10

    9/52

    void list::disp()

    {

    node *q;

    q=p;

    if(q==NULL){

    coutlink=temp1;

    }

    temp=temp->link;

    }

    cout

  • 8/9/2019 computer lab manual expt 2-10

    10/52

    temp=p;

    int position=0;

    while(temp!=NULL)

    {

    if(temp->data==value)

    return position+1;else

    {

    temp=temp->link;

    position=position+1;

    }

    }

    cout

  • 8/9/2019 computer lab manual expt 2-10

    11/52

    case 3:

    coutp;

    l.insnext(v,p);

    break;

    default:

    cout

  • 8/9/2019 computer lab manual expt 2-10

    12/52

    l.disp();

    break;

    case 4:

    clrscr();

    l.disp();

    coutv;

    cout

  • 8/9/2019 computer lab manual expt 2-10

    13/52

    Output:

    Singly Linked List

    1.Create

    2.Insert

    3.Delete

    4.ExitEnter Your Choice : 1

    Enter The Data: 10

    10

    1.Create

    2.Insert

    3.Delete

    4.Exit

    Enter Your Choice : 2

    Enter The Data: 30Enter The Position: 1

    30

    10

    1.Create

    2.Insert

    3.Delete

    4.Exit

    Enter Your Choice : 3

    Enter The Position : 2

    List Is Empty

    Result:

    Thus, the linked list implementation of list ADT for singly linked list program has been

    written and executed successfully.

  • 8/9/2019 computer lab manual expt 2-10

    14/52

    EX.NO:7 Cursor implementation of List ADT

    DATE :

    AIM:

    To Write a C++ Program for Cursor implementation of list ADT.

    Algorithm:

    Step1: Create nodes first,last,next,prev and cur then set the value as NULL.

    Step 2: Read the list operation type.

    step 3: If operation type is create then process the following steps.

    1. Allocate memory for node cur.

    2. Read data in cur's data area.

    3. Assign cur link as NULL.

    4. Assign first=last=cur.

    Step 4: If operation type is Insert then process the following steps.

    1. Allocate memory for node cur.

    2. Read data in cur's data area.3. Read the position the Data to be inserting.

    4. Availability of the position is true then assign cur's link as first and first=cur.

    5. If availability of position is false then do following steps.

    1. Assign next as cur and count as zero.

    2. Repeat the following steps until count less than position.

    1 .Assign prev as next

    2. Next as prev of link.

    3. Add count by one.

    4. If prev as NULL then display the message INVALID POSITION.

    5. If prev not qual to NULL then do the following steps.

    1. Assign cur's link as prev's link.

    2. Assign prev's link as cur.

    Step5: If operation type is delete then do the following steps.

    1. Read the position .

    2. Check list is Empty .If it is true display the message List empty.

    3. If position is first.

    1. Assign cur as first.

    2. Assign First as first of link.

    3. Reallocate the cur from memory.

    4. If position is last.

    1. Move the current node to prev.

    2. cur's link as Null.

    3. Reallocate the Last from memory.

    4. Assign last as cur.

    5. If position is enter Mediate.

    1. Move the cur to required position.

    2. Move the Previous to cur's previous position

    3. Move the Next to cur's Next position.

    4. Now assign previous of link as next.

  • 8/9/2019 computer lab manual expt 2-10

    15/52

    5. Reallocate the cur from memory.

    step 6: If operation is traverse.

    1. Assign current as first.

    2. Repeat the following steps until cur becomes NULL.

    Program

    #include

    #include

    #include

    #define max 5

    struct node

    {

    int data;

    int next;

    };

    typedef struct node NODE;

    int avail,list = -1;

    NODE curs[max];

    void initial()

    {

    int i;

    avail = 0;

    for(i=0;i

  • 8/9/2019 computer lab manual expt 2-10

    16/52

    if(n==max-1) avail = -1;

    else avail=n+1;

    cout

  • 8/9/2019 computer lab manual expt 2-10

    17/52

    int item,i;

    coutitem;

    //cout

  • 8/9/2019 computer lab manual expt 2-10

    18/52

    if(curs[list].next==-1)

    {

    curs[list].next=avail;

    avail=list;

    list=-1;

    }}

    void delend()

    {

    int i,prev;

    i=list;

    while(curs[i].next!=-1)

    {

    prev=i;

    i=curs[i].next;

    }

    curs[prev].next=-1;curs[i].next=avail;

    avail=i;

    curs[avail].data=0;

    if(curs[list].next==-1)

    {

    curs[list].next=avail;

    avail=list;

    list=-1;

    }

    }

    void delint()

    {

    int pos,i,count,prev;

    coutpos;

    //cout

  • 8/9/2019 computer lab manual expt 2-10

    19/52

    if(curs[list].next==-1)

    {

    curs[list].next=avail;

    avail=list;

    list=-1;

    }}

    void main()

    {

    int ch;

    clrscr();

    do

    {

    cout

  • 8/9/2019 computer lab manual expt 2-10

    20/52

    if(list==-1) cout

  • 8/9/2019 computer lab manual expt 2-10

    21/52

    3

    enter the element 4

    4

    enter the element 55

    LIST

    1. Create

    2. Insert at begin

    3. Insert at end

    4. Insert at intermediate

    5. Delete at begin

    6. Delete at end

    7. Delete at intermediate

    8. Display9. Exit

    Enter your choice:8

    8

    Cursor space:

    Avail = -1 List = 0

    ______________

    DATA NEXT

    ______________

    0 1

    _______________

    2 2

    _______________

    3 3

    _______________

    4 4

    _______________

    5 -1

    _______________

    LIST

    1. Create

    2. Insert at begin

    3. Insert at end

    4. Insert at intermediate

    5. Delete at begin

    6. Delete at end

    7. Delete at intermediate

    8. Display

  • 8/9/2019 computer lab manual expt 2-10

    22/52

    9. Exit

    Enter your choice:6

    6

    LIST

    1. Create2. Insert at begin

    3. Insert at end

    4. Insert at intermediate

    5. Delete at begin

    6. Delete at end

    7. Delete at intermediate

    8. Display

    9. Exit

    Enter your choice:8

    8

    Cursor space:

    Avail = 4 List = 0

    ______________

    DATA NEXT

    ______________

    0 1

    _______________

    2 2

    _______________

    3 3

    _______________

    4 -1

    _______________

    0 -1

    _______________

    LIST

    1. Create

    2. Insert at begin

    3. Insert at end

    4. Insert at intermediate

    5. Delete at begin

    6. Delete at end

    7. Delete at intermediate

    8. Display

    9. Exit

    Enter your choice:2

    2

  • 8/9/2019 computer lab manual expt 2-10

    23/52

    Enter the item to be inserted: 6

    6

    LIST

    1. Create

    2. Insert at begin3. Insert at end

    4. Insert at intermediate

    5. Delete at begin

    6. Delete at end

    7. Delete at intermediate

    8. Display

    9. Exit

    Enter your choice:8

    8

    Cursor space:

    Avail = -1 List = 0

    ______________

    DATA NEXT

    ______________

    0 4

    _______________

    2 2

    _______________

    3 3

    _______________

    4 -1

    _______________

    6 1

    _______________

    LIST

    1. Create

    2. Insert at begin

    3. Insert at end

    4. Insert at intermediate

    5. Delete at begin

    6. Delete at end

    7. Delete at intermediate

    8. Display

    9. Exit

    Enter your choice:7

    7

  • 8/9/2019 computer lab manual expt 2-10

    24/52

    Enter the position: 2

    2

    LIST

    1. Create

    2. Insert at begin3. Insert at end

    4. Insert at intermediate

    5. Delete at begin

    6. Delete at end

    7. Delete at intermediate

    8. Display

    9. Exit

    Enter your choice:8

    8

    Cursor space:

    Avail = 1 List = 0

    ______________

    DATA NEXT

    ______________

    0 4

    _______________

    0 -1

    _______________

    3 3

    _______________

    4 -1

    _______________

    6 2

    _______________

    LIST

    1. Create

    2. Insert at begin

    3. Insert at end

    4. Insert at intermediate

    5. Delete at begin

    6. Delete at end

    7. Delete at intermediate

    8. Display

    9. Exit

    Enter your choice:3

    3

    Enter the item to be inserted: 9

  • 8/9/2019 computer lab manual expt 2-10

    25/52

    9

    LIST

    1. Create

    2. Insert at begin

    3. Insert at end4. Insert at intermediate

    5. Delete at begin

    6. Delete at end

    7. Delete at intermediate

    8. Display

    9. Exit

    Enter your choice:8

    8

    Cursor space:

    Avail = -1 List = 0

    ______________

    DATA NEXT

    ______________

    0 4

    _______________

    9 -1

    _______________

    3 3

    _______________

    4 1

    _______________

    6 2

    _______________

    LIST

    1. Create

    2. Insert at begin

    3. Insert at end

    4. Insert at intermediate

    5. Delete at begin

    6. Delete at end

    7. Delete at intermediate

    8. Display

    9. Exit

    Enter your choice:5

    5

    LIST

    1. Create

  • 8/9/2019 computer lab manual expt 2-10

    26/52

    2. Insert at begin

    3. Insert at end

    4. Insert at intermediate

    5. Delete at begin

    6. Delete at end

    7. Delete at intermediate8. Display

    9. Exit

    Enter your choice:8

    8

    Cursor space:

    Avail = 4 List = 0

    ______________

    DATA NEXT

    ______________0 2

    _______________

    9 -1

    _______________

    3 3

    _______________

    4 1

    _______________

    0 -1

    _______________

    LIST

    1. Create

    2. Insert at begin

    3. Insert at end

    4. Insert at intermediate

    5. Delete at begin

    6. Delete at end

    7. Delete at intermediate

    8. Display

    9. Exit

    Enter your choice:4

    4

    Enter the item to be inserted: 21

    21

    Enter the position of the item: 2

    2

  • 8/9/2019 computer lab manual expt 2-10

    27/52

    LIST

    1. Create

    2. Insert at begin

    3. Insert at end

    4. Insert at intermediate

    5. Delete at begin6. Delete at end

    7. Delete at intermediate

    8. Display

    9. Exit

    Enter your choice:8

    8

    Cursor space:

    Avail = -1 List = 0

    ______________DATA NEXT

    ______________

    0 2

    _______________

    9 -1

    _______________

    3 4

    _______________

    4 1

    _______________

    21 3

    _______________

    LIST

    1. Create

    2. Insert at begin

    3. Insert at end

    4. Insert at intermediate

    5. Delete at begin

    6. Delete at end

    7. Delete at intermediate

    8. Display

    9. Exit

    Enter your choice:9

    Result

    Thus, the cursor implementation of list ADT for singly linked list program has been

    written and executed successfully.

  • 8/9/2019 computer lab manual expt 2-10

    28/52

    EX.NO:8 Stack ADT- Array Implementation

    DATE:

    AIM:

    To Write a C++ Program to Stack ADT implementation using array

    Algorithm:

    Step 1: Define a stack size.

    Step 2: Read the stack operation.

    Step 3: Read the stack element.

    Step 4: Check the stack operation is Push or Pop.

    Step 5: If operation is push then check the stack status.

    i. If stack status is over flow we cant push the element in to stack.

    ii. Otherwise we can add the data into stack .

    iii. Move top to next position.

    Program

    #include

    #include

    #include

    //using namespace std;

    class stack

    {

    int stk[5];

    int top;

    public:

    stack()

    { top=-1;

    }

    void push(int x)

    {

    if(top > 4)

    {

    cout

  • 8/9/2019 computer lab manual expt 2-10

    29/52

    }

    cout

  • 8/9/2019 computer lab manual expt 2-10

    30/52

    OUTPUT

    1.push 2.pop 3.display 4.exit

    Enter ur choice2

    stack under flow

    1.push 2.pop 3.display 4.exitEnter ur choice1

    enter the element2

    inserted2

    1.push 2.pop 3.display 4.exit

    Enter ur choice1

    enter the element3

    inserted3

    1.push 2.pop 3.display 4.exit

    Enter ur choice2deleted3

    1.push 2.pop 3.display 4.exit

    Enter ur choice1

    enter the element5

    inserted5

    1.push 2.pop 3.display 4.exit

    Enter ur choice3

    5 2

    1.push 2.pop 3.display 4.exit

    Enter ur choice4

    Result:

    Thus, the stack ADT- array implementation program has been written and executed

    successfully.

  • 8/9/2019 computer lab manual expt 2-10

    31/52

    EX.NO:9 Stack ADT- Linked list Implementation

    DATE :

    AIM:

    To Write a C++ Program to Stack ADT implementation using linked list

    Alogarithm :

    Step 1: create a list.

    i) Create a new empty node top.

    ii) Read the stack element and store it in top's data area.

    iii) Assign top's link part as NULL (i.e. top->link=NULL).

    iv) Assign temp as top (i.e. temp=top).

    Step 2: Read next stack operation.

    i) If it is Create then go to step1.

    ii) If it is Push then it process following steps

    a) Check Main memory for node creation.

    b) Create a new node top.c) Read the stack element and store it in top's data area.

    d) Assign top's link part as temp (i.e. top->link=temp).

    e) Assign temp as top (i.e. temp=top).

    iii) If it is pop then it process following steps

    a) If top is NULL then display stack is empty.

    b) Otherwise assign top as temp (i.e. top=temp, bring the top to top position)

    c) Assign temp as temp's link. (i.e. temp=temp->link, bring the temp to top's previous

    position).

    d) Delete top from memory.

    iv) If it is traverse then process the following steps

    a) Bring the top to stacks top position(i.e. top=temp)

    b) Repeat until top becomes NULL

    i) Display the top's data.

    ii) Assign top as top's link (top=top->link).

    Program

    #include

    #include

    #include

    //using namespace std;

    class node

    {

    public:

    class node *next;

    int data;

    };

    class stack : public node

  • 8/9/2019 computer lab manual expt 2-10

    32/52

    {

    node *head;

    int tos;

    public:

    stack()

    { tos=-1;

    }

    void push(int x)

    {

    if (tos < 0 )

    {

    head =new node;

    head->next=NULL;

    head->data=x;

    tos ++;

    }else

    {

    node *temp,*temp1;

    temp=head;

    if(tos >= 4)

    {

    cout next;

    temp1=new node;

    temp->next=temp1;

    temp1->next=NULL;

    temp1->data=x;

    }

    }

    void display()

    {

    node *temp;

    temp=head;

    if (tos < 0)

    {

    cout

  • 8/9/2019 computer lab manual expt 2-10

    33/52

    cout next;}

    temp->next=NULL;

    }

    };

    main()

    {

    Clrscr();

    stack s1;

    int ch;

    while(1)

    {

    cout ch;

    switch(ch)

    {

    case 1: cout ch;

    s1.push(ch);

    break;

    case 2: s1.pop();break;

    case 3: s1.display();

    break;

    case 4: exit(0);

    }

    }

    return (0);

    }

  • 8/9/2019 computer lab manual expt 2-10

    34/52

    Output

    1.PUSH 2.POP 3.DISPLAY 4.EXIT

    enter ru choice:1

    enter a element23

    1.PUSH 2.POP 3.DISPLAY 4.EXITenter ru choice:1

    enter a element67

    1.PUSH 2.POP 3.DISPLAY 4.EXIT

    enter ru choice:3

    23 67

    1.PUSH 2.POP 3.DISPLAY 4.EXIT

    enter ru choice:2

    1.PUSH 2.POP 3.DISPLAY 4.EXIT

    enter ru choice:323

    1.PUSH 2.POP 3.DISPLAY 4.EXIT

    enter ru choice:2

    1.PUSH 2.POP 3.DISPLAY 4.EXIT

    enter ru choice:2

    stack under flow

    1.PUSH 2.POP 3.DISPLAY 4.EXIT

    enter ru choice:4

    Result:

    Thus, the stack ADT- linked list implementation program has been written and executedsuccessfully.

  • 8/9/2019 computer lab manual expt 2-10

    35/52

    EX.NO:10.1 Oueue ADT- Array Implementation

    DATE:

    AIM:

    To Write a C++ Program to Queue ADT implementation using array

    Algorithm

    Step 1: Initialize the queue variables front =0 and rear = -1

    Step 2: Read the queue operation type.

    Step 3: Check the queue operations status.

    i). If it is Insertion then do the following steps

    1. Check rear < queue_size is true increment the rear by one and read the queueelement and also display queue. otherwise display the queue is full.

    2. Go to step2.ii). If it is deletion then do the following steps

    1. Check rear< front is true then display the queue is empty.2. Move the elements to one step forward (i.e. move to previous index ).3. Decreases the rear value by one (rear=rear-1).4. Display queue5. Go to step2.

    Program:

    #include

    #include

    #include

    //using namespace std;

    class queue{

    int queue1[5];

    int rear,front;

    public:

    queue()

    {

    rear=-1;

    front=-1;

    }

    void insert(int x)

    {if(rear > 4)

    {

    cout

  • 8/9/2019 computer lab manual expt 2-10

    36/52

    cout

  • 8/9/2019 computer lab manual expt 2-10

    37/52

    OUTPUT

    1.insert 2.delet 3.display 4.exit

    Enter ur choice1

    enter the element21

    inserted21

    1.insert 2.delet 3.display 4.exit

    Enter ur choice1

    enter the element22

    inserted22

    1.insert 2.delet 3.display 4.exit

    Enter ur choice1

    enter the element16

    inserted16

    1.insert 2.delet 3.display 4.exitEnter ur choice3

    21 22 16

    1.insert 2.delet 3.display 4.exit

    Enter ur choice2

    deleted21

    1.insert 2.delet 3.display 4.exit

    Enter ur choice3

    22 16

    1.insert 2.delet 3.display 4.exit

    Enter ur choice

    Result:

    Thus, the queue ADT- array implementation program has been written and executed

    successfully.

  • 8/9/2019 computer lab manual expt 2-10

    38/52

    EX.NO:10.2 Oueue ADT- Linked list Implementation

    DATE:

    AIM:

    To Write a C++ Program to Queue ADT implementation using linked list

    Algorithm

    Step 1: Initialize the queue variables front =0 and rear = -1

    Step 2: Read the queue operation type.

    Step 3: Check the queue operations status.

    i). If it is Insertion then do the following steps

    3. Check rear not equal to null is true increment the rear by one and read the queueelement and also display queue. otherwise display the queue is full.

    4. Go to step2.ii). If it is deletion then do the following steps

    6. Check rear< front is true then display the queue is empty.7. Move the elements to one step forward (i.e. move to previous index ).8. Decreases the rear value by one (rear=rear-1).9. Display queue10.Go to step2.

    Program

    #include

    #include

    #include

    //using namespace std;

    class node

    {public:

    class node *next;

    int data;

    };

    class queue : public node

    {

    node *head;

    int front,rare;

    public:

    queue(){

    front=-1;

    rare=-1;

    }

    void push(int x)

    {

    if (rare < 0 )

  • 8/9/2019 computer lab manual expt 2-10

    39/52

    {

    head =new node;

    head->next=NULL;

    head->data=x;

    rare ++;

    }else

    {

    node *temp,*temp1;

    temp=head;

    if(rare >= 4)

    {

    cout next;

    temp1=new node;

    temp->next=temp1;

    temp1->next=NULL;

    temp1->data=x;

    } }

    void display()

    {

    node *temp;

    temp=head;

    if (rare < 0)

    {

    cout

  • 8/9/2019 computer lab manual expt 2-10

    40/52

    return;

    }

    if(front == rare)

    {

    front = rare =-1;

    head=NULL;return;

    }

    front++;

    head=head->next;

    }

    };

    main()

    {

    Clrscr();

    queue s1;

    int ch;while(1)

    {

    cout ch;

    switch(ch)

    {

    case 1:

    cout ch;

    s1.push(ch); break;

    case 2: s1.pop();break;

    case 3: s1.display();break;

    case 4: exit(0);

    }

    }

    return (0);

    }

  • 8/9/2019 computer lab manual expt 2-10

    41/52

    OUTPUT

    1.PUSH 2.POP 3.DISPLAY 4.EXIT

    enter ru choice:1

    enter a element23

    1.PUSH 2.POP 3.DISPLAY 4.EXIT

    enter ru choice:1

    enter a element54

    1.PUSH 2.POP 3.DISPLAY 4.EXIT

    enter ru choice:3

    23 54

    1.PUSH 2.POP 3.DISPLAY 4.EXIT

    enter ru choice:2

    1.PUSH 2.POP 3.DISPLAY 4.EXITenter ru choice:2

    1.PUSH 2.POP 3.DISPLAY 4.EXIT

    enter ru choice:2

    queue under flow

    1.PUSH 2.POP 3.DISPLAY 4.EXIT

    enter ru choice:4

    Result

    Thus, the queue ADT- linked list implementation program has been written and executed

    successfully.

  • 8/9/2019 computer lab manual expt 2-10

    42/52

    EX.NO:11 Search Tree ADT Binary Search Tree

    DATE:

    AIM:

    To Write a C++ Program to to perform Insert, Delete, Search an element into a binary

    search tree

    Algorithm

    y Create a new instance of BinaryTree.y Create a new instance of TreeTest and select BinaryTree instance in the objecty bench as the parameter in the constructor.y Call the populate method of TreeTest instance.y Inspect the BinaryTree. Its attributes are a left subtree, a right subtree and a data

    item.

    Program

    #include

    #include

    #include

    //using namespace std;

    void insert(int,int );

    void delte(int);

    void display(int);

    int search(int);

    int search1(int,int);

    int tree[40],t=1,s,x,i;

    main()

    {

    clrscr();

    int ch,y;

    for(i=1;i> ch;

    switch(ch)

    {

    case 1:

    cout ch;

    insert(1,ch);

    break;

  • 8/9/2019 computer lab manual expt 2-10

    43/52

    case 2:

    cout x;

    y=search(1);

    if(y!=-1) delte(y);

    else cout

  • 8/9/2019 computer lab manual expt 2-10

    44/52

    else if(tree[2*x]==-1)

    { tree[x]=tree[2*x+1];

    tree[2*x+1]=-1;

    }

    else if(tree[2*x+1]==-1)

    { tree[x]=tree[2*x];tree[2*x]=-1;

    }

    else

    {

    tree[x]=tree[2*x];

    delte(2*x);

    }

    t--;

    }

    int search(int s){

    if(t==1)

    {

    cout

  • 8/9/2019 computer lab manual expt 2-10

    45/52

    {

    if(t==1)

    {

    cout

  • 8/9/2019 computer lab manual expt 2-10

    46/52

    OUTPUT

    1.INSERT

    2.DELETE

    3.DISPLAY

    4.SEARCH

    5.EXITEnter your choice:3

    no element in tree:

    0123456789011121314151617181920212223242526272829303132

    1. INSERT

    2.DELETE

    3.DISPLAY

    4.SEARCH

    5.EXIT

    Enter your choice:1

    Enter the element to insert 10

    1.INSERT

    2.DELETE

    3.DISPLAY

    4.SEARCH

    5.EXIT

    Enter your choice: 4

    Enter the element to search: 10

    10 is in 1 position1.INSERT

    2.DELETE

    3.DISPLAY

    4.SEARCH

    5.EXIT

    Enter your choice:5

    Result:

    Thus, the t program to perform Insert, Delete, Search an element into a binary tree has

    been written and executed successfully.

  • 8/9/2019 computer lab manual expt 2-10

    47/52

    EX.NO:12 Heap Sort

    DATE:

    AIM:

    To Write a C++ Program to perform heap sort.

    Algorithm:

    Step I: The user inputs the size of the heap(within a specified limit).The program generates

    a corresponding binary tree with nodes having randomly generated key Values.

    Step II: Build Heap Operation

    Step III: Remove maximum element:The program removes the largest element of the

    heap(the root) by swapping it with the last element.

    Step IV: The program executes Heapify(new root) so that the resulting tree satisfies the

    heap property.

    Step V: Goto step III till heap is empty

    Program:

    #include

    #include

    class sorting

    {

    private:

    int n,size;

    double *minheap;

    public:

    void insert_minheap(double);double delete_one_minheap();

    void input();

    void output();

    };

    void sorting::insert_minheap(double n)

    {

    if(size>=9)

    {

    cout

  • 8/9/2019 computer lab manual expt 2-10

    48/52

    double t=minheap[k];

    minheap[k]=minheap[k/2];

    minheap[k/2]=t;

    k/=2;

    }else

    break;

    }

    }

    double sorting::delete_one_minheap()

    {

    if(size

  • 8/9/2019 computer lab manual expt 2-10

    49/52

    k=newk;

    }

    }

    return val;

    }

    void sorting::input()

    {

    coutn;

    minheap=new double[n+1];

    /********** Construct a heap with the input elements *******/

    size=0;

    cout

  • 8/9/2019 computer lab manual expt 2-10

    50/52

    Output

    Enter how many numbers you are going to enter for sorting :5

    Now enter the elements

    9

    1

    82

    6

    The sorted numbers are:

    1 2 6 8 9

    Result:

    Thus, the heap sort program has been written and executed successfully.

  • 8/9/2019 computer lab manual expt 2-10

    51/52

    EX.NO:12 Quick Sort

    DATE:

    AIM:

    To Write a C++ Program to perform quick sort.

    Algorithm

    y Pick an element, called a pivot, from the list.y Reorder the list so that all elements which are less than the pivot come before the

    pivot and so that all elements greater than the pivot come after it (equal values can

    go either way). After this partitioning, the pivot is in its final position. This is called

    the partition operation.

    y Recursively sort the sub-list of lesser elements and the sub-list of greater elementsProgram

    #include#include

    class QuiSort

    {

    int i,j,pivot;

    public:

    int n,a[20];

    void quick(int a[],int left,int right);

    void swap(int a[],int i,int j);

    };

    void QuiSort :: quick(int a[],int first,int last)

    { if(first

  • 8/9/2019 computer lab manual expt 2-10

    52/52

    }

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

    {

    int temp;

    temp=a[i];

    a[i]=a[j];a[j]=temp;

    }

    void main()

    {

    QuiSort obj;

    clrscr();

    cout