oops c++ file (1)

Upload: manik-bhalla

Post on 07-Apr-2018

230 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/3/2019 OOPS C++ file (1)

    1/46

    Practical No: 1

    AIM: Implementation of different operation on stack

    #include

    #include

    class stack

    {public:

    int top=-1,a[ ];

    int maxsize=20;

    void push( )

    {int item;

    if(top=maxsize-1){

    cout

  • 8/3/2019 OOPS C++ file (1)

    2/46

    void display( )

    {

    int itemcout

  • 8/3/2019 OOPS C++ file (1)

    3/46

  • 8/3/2019 OOPS C++ file (1)

    4/46

    Practical No: 2

    AIM:To convert a decimal no. into its equivalent binary number

    Source Code

    #include

    #includeclass convert

    {

    int number;public:

    void input();

    void display();void dec_bin();

    };void convert::display()

    {cout

  • 8/3/2019 OOPS C++ file (1)

    5/46

    c1.dec_bin();

    getch();

    }

  • 8/3/2019 OOPS C++ file (1)

    6/46

    OUTPUT

    Enter the number:3

    The decimal number entered is

    3The binary equivalent is

    00000011

  • 8/3/2019 OOPS C++ file (1)

    7/46

    Practical No: 3

    AIM:To Convert Infix Expression To Postfix Expression Using Stack

    Source Code

    #include

    #include

    #include#include

    char stack[50];

    int top=-1;

    void in_to_post(char infix[]);

    void push(char);char pop();

    void main()

    {

    char infix[25];clrscr();

    cout

  • 8/3/2019 OOPS C++ file (1)

    8/46

    char pop()

    {

    char item;if (top==-1)

    {

    cout

  • 8/3/2019 OOPS C++ file (1)

    9/46

    symbol=infix[index];

    switch(symbol)

    {case '(': push(symbol);

    break;

    case ')': temp=pop();while(temp!='(')

    {

    postfix[pos]=temp;pos++;

    temp=pop();

    }

    break;case '+':

    case '-':

    case '*':

    case '/':case '^':

    while(preced(stack[top])>=preced(symbol)){

    temp=pop();

    postfix[pos]=temp;

    pos++;

    }

    push(symbol);break;

    default: postfix[pos++]=symbol;

    break;

    }

    index++;

    }

    while(top>0)

    {temp=pop();

    postfix[pos++]=temp;

    }postfix[pos++]='\0';

    cout

  • 8/3/2019 OOPS C++ file (1)

    10/46

    Output

    Enter the infix expression: a*(b+c)

    The required postfix expression is: abc+*

    Enter the infix expression: a*(b+c)/(e+f)

    The required postfix expression is: abc+ef+/*

    Enter the infix expression: (a+b)/(c*d)*e

    The required postfix expression is: ab+cd*/e*

  • 8/3/2019 OOPS C++ file (1)

    11/46

    Practical No: 4

    AIM:To Implement Various Operation On Singly Linked List At

    Different Positions.

    (i) Traversing (ii) Inserting (iii) Deleting

    class linklist{

    private:

    struct ll{

    int d;

    struct ll *n;

    }*s;

    struct ll *gn(){

    struct ll *nn,*p;int n;

    nn=(struct ll *) malloc(sizeof(struct ll));

    y:coutn;

    if(s==NULL)

    {nn->d=n;

    nn->n=NULL;}else

    {

    p=s;

    while(p!=NULL)

    {if(p->d==n)

    {

    coutd=n;

  • 8/3/2019 OOPS C++ file (1)

    12/46

    nn->n=NULL;

    }

    return nn;}

    Public:

    void an(){

    struct ll *nn,*p;

    nn=gn();if(s==NULL)

    {

    s=nn;

    }else

    {

    p=s;

    while(p->n!=NULL){

    p=p->n;}

    p->n=nn;

    }

    }

    void beg()

    {struct ll *nn,*p;

    nn=gn();

    nn->n=s;s=nn;

    }

    void anap(){

    struct ll *nn,*p;

    int se;

    z:if(s==NULL)

    {

    cout

  • 8/3/2019 OOPS C++ file (1)

    13/46

    while(p!=NULL)

    {

    if(se==p->d){

    coutn=nn;

    }

    }}

    void anbp()

    {struct ll *nn,*p;

    int se,i,j;

    a:i=0;

    if(s==NULL)

    {

    cout

  • 8/3/2019 OOPS C++ file (1)

    14/46

    if(se==p->d)

    {

    coutn;

    p->n=nn;

    }

    }}

    }

    void dl(){

    struct ll *p,*t;

    int i=0,j=1;if(s==NULL)

    {

    cout

  • 8/3/2019 OOPS C++ file (1)

    15/46

    else

    {

    p=s;while(p!=NULL)

    {

    i++;p=p->n;

    }

    p=s;if(i==1)

    {

    free(p);

    s=NULL;cout

  • 8/3/2019 OOPS C++ file (1)

    16/46

    {

    free(p);

    s=NULL;}

    else

    { s=p->n;

    free(p);

    }}

    }

    void dp()

    {struct ll *p,*t;

    int se,i,l,j;

    pd:

    i=0;l=0;

    j=1;if(s==NULL)

    {

    coutd==se){

    cout

  • 8/3/2019 OOPS C++ file (1)

    17/46

    }

    }

    if(p==NULL){

    coutn=NULL;

    free(p);

    }else

    {

    p=s;while(jn;

    j++;

    }t->n=p->n;

    free(p);

    }

    }

  • 8/3/2019 OOPS C++ file (1)

    18/46

  • 8/3/2019 OOPS C++ file (1)

    19/46

    case 3:

    m.anap();

    goto x;case 4:

    m.anbp();

    goto x;case 5:

    m.dl();

    goto x;case 6:

    m.df();

    goto x;

    case 7:m.dp();

    goto x;

    case 8:

    m.dis();goto x;

    case 9:exit(0);

    default:

    goto x;

    }}

    PRE CONDITION

    Enter1 if you want to insert no at the begining

    2 if you want to insert no at the end of the list

    3 if you want to insert node after a particular node4 if you want to insert node before a particular node

    5 if you want to Delete Last Node

    6 if you want to Delete First Node

    7 if you want to Delete Particular Node8 if you want to Display the List

    9 if you want to exit

    1

    POST CONDITION

    Enter No10

    PRE CONDITION

    Enter

    1 if you want to insert no at the begining

  • 8/3/2019 OOPS C++ file (1)

    20/46

    2 if you want to insert no at the end of the list

    3 if you want to insert node after a particular node

    4 if you want to insert node before a particular node5 if you want to Delete Last Node

    6 if you want to Delete First Node

    7 if you want to Delete Particular Node8 if you want to Display the List

    9 if you want to exit

    2

    POST CONDITION

    Enter No25

    PRE CONDITION

    Enter

    1 if you want to insert no at the begining

    2 if you want to insert no at the end of the list3 if you want to insert node after a particular node

    4 if you want to insert node before a particular node5 if you want to Delete Last Node

    6 if you want to Delete First Node

    7 if you want to Delete Particular Node

    8 if you want to Display the List9 if you want to exit

    1

    POST CONDITION

    Enter No5Enter

    1 if you want to insert no at the begining

    2 if you want to insert no at the end of the list3 if you want to insert node after a particular node

    4 if you want to insert node before a particular node

    5 if you want to Delete Last Node

    6 if you want to Delete First Node7 if you want to Delete Particular Node

    8 if you want to Display the List

    9 if you want to exit3

    Enter No after which you want to insert node10

    No FoundEnter No15

    Enter

    1 if you want to insert no at the begining

    2 if you want to insert no at the end of the list

  • 8/3/2019 OOPS C++ file (1)

    21/46

    3 if you want to insert node after a particular node

    4 if you want to insert node before a particular node

    5 if you want to Delete Last Node6 if you want to Delete First Node

    7 if you want to Delete Particular Node

    8 if you want to Display the List9 if you want to exit

    4

    Enter No before which you want to insert node25No Found

    Enter No20

    Enter

    1 if you want to insert no at the begining2 if you want to insert no at the end of the list

    3 if you want to insert node after a particular node

    4 if you want to insert node before a particular node

    5 if you want to Delete Last Node6 if you want to Delete First Node

    7 if you want to Delete Particular Node8 if you want to Display the List

    9 if you want to exit

    8

    List is 5 10 15 20 25Enter

    1 if you want to insert no at the begining

    2 if you want to insert no at the end of the list3 if you want to insert node after a particular node

    4 if you want to insert node before a particular node

    5 if you want to Delete Last Node6 if you want to Delete First Node

    7 if you want to Delete Particular Node

    8 if you want to Display the List9 if you want to exit

    5

    Enter

    1 if you want to insert no at the begining2 if you want to insert no at the end of the list

    3 if you want to insert node after a particular node

    4 if you want to insert node before a particular node5 if you want to Delete Last Node

    6 if you want to Delete First Node

    7 if you want to Delete Particular Node8 if you want to Display the List

    9 if you want to exit

    8

    List is 5 10 15 20

  • 8/3/2019 OOPS C++ file (1)

    22/46

    Enter

    1 if you want to insert no at the begining

    2 if you want to insert no at the end of the list3 if you want to insert node after a particular node

    4 if you want to insert node before a particular node

    5 if you want to Delete Last Node6 if you want to Delete First Node

    7 if you want to Delete Particular Node

    8 if you want to Display the List9 if you want to exit

    6

    Enter

    1 if you want to insert no at the begining2 if you want to insert no at the end of the list

    3 if you want to insert node after a particular node

    4 if you want to insert node before a particular node

    5 if you want to Delete Last Node6 if you want to Delete First Node

    7 if you want to Delete Particular Node8 if you want to Display the List

    9 if you want to exit

    8

    List is 10 15 20

    Enter1 if you want to insert no at the begining

    2 if you want to insert no at the end of the list

    3 if you want to insert node after a particular node4 if you want to insert node before a particular node

    5 if you want to Delete Last Node

    6 if you want to Delete First Node7 if you want to Delete Particular Node

    8 if you want to Display the List

    9 if you want to exit

    7

    PRE CONDITION

    Enter No you want to delete15Enter

    1 if you want to insert no at the begining

    2 if you want to insert no at the end of the list3 if you want to insert node after a particular node

    4 if you want to insert node before a particular node

    5 if you want to Delete Last Node

    6 if you want to Delete First Node

  • 8/3/2019 OOPS C++ file (1)

    23/46

    7 if you want to Delete Particular Node

    8 if you want to Display the List

    9 if you want to exit8

    POST CONDITIONList is 10 20

    Enter

    1 if you want to insert no at the begining2 if you want to insert no at the end of the list

    3 if you want to insert node after a particular node

    4 if you want to insert node before a particular node

    5 if you want to Delete Last Node6 if you want to Delete First Node

    7 if you want to Delete Particular Node

    8 if you want to Display the List

    9 if you want to exit9

  • 8/3/2019 OOPS C++ file (1)

    24/46

    Practical No: 5

    AIM:To accept an integer no. and print using words.

    Source Code

    #include

    #include

    void main(){

    long int n; int i,a[10]; clrscr();

    coutn;

    i=0;

    while(n!=0){

    a[i++]=n%10;

    n=n/10;}

    cout

  • 8/3/2019 OOPS C++ file (1)

    25/46

    getch();

    }

  • 8/3/2019 OOPS C++ file (1)

    26/46

    OUTPUT

    Enter the number: 234412

    The words representation is: Two Three Four Four One Two

    Enter the number: 9876543

    The words representation is: Nine Eight Seven Six Five Four Three

    Enter the number: 543210

    The words representation is: Five Four Three Two One Zero

  • 8/3/2019 OOPS C++ file (1)

    27/46

    Practical No: 6

    AIM:To Traverse A Binary Tree Using

    1. INORDER

    2. PREORDER3. POST ORDER

    Source Code

    // Tree traversal program(inorder, post ,pre)

    #include#include

    struct NODE

    {char info;

    NODE *left_child;NODE *right_child;

    };class traverse

    {

    public:NODE *Binarytree(char *,int,int);

    void output(NODE *,int);

    void preorder(NODE *);void inorder(NODE *);

    void postorder(NODE *);

    };NODE * traverse::Binarytree(char *list,int lower,int upper){

    NODE *node;

    int mid=(lower+upper)/2;node=new(NODE);

    node->info=list[mid];

    if(lower>=upper){

    node->left_child=NULL;

    node->right_child=NULL;

    return(node);}

    if(lowerleft_child=Binarytree(list,lower,mid-1);else

    node->left_child=NULL;

    if(mid+1right_child=Binarytree(list,mid+1,upper);

  • 8/3/2019 OOPS C++ file (1)

    28/46

    else

    node->right_child=NULL;

    return(node);}

    void traverse::output(NODE *t,int level)

    {if(t)

    {

    output(t->right_child,level+1);cout

  • 8/3/2019 OOPS C++ file (1)

    29/46

    traverse btree;

    char list[100];

    int number=0;char info;

    char choice;

    NODE *t=new(NODE);t=NULL;

    cout

  • 8/3/2019 OOPS C++ file (1)

    30/46

    OUTPUT

    Input choice b to break:Input information of the node:A

    Input choice b to break:

    Input information of the node:BInput choice b to break:

    Input information of the node:C

    Input choice b to break:Input information of the node:D

    Input choice b to break:

    Input information of the node:E

    Input choice b to break:Input information of the node:F

    Input choice b to break:

    Input information of the node:G

    Input choice b to break:Input information of the node:H

    Input choice b to break:Input information of the node:J

    Input choice b to break:

    Input information of the node:KInput choice b to break:

    Input information of the node:L

    Input choice b to break:

    Input information of the node:MInput choice b to break:

    Input information of the node:OInput choice b to break: bNumber of elements in the list is 13

    O

    ML

    K

    J

    HG

    F

    ED

    C

    BA

    Preorder Traversal:A,B,D,E,F,C,G,H,J,L,M,K,O

  • 8/3/2019 OOPS C++ file (1)

    31/46

    Inorder Traversal:

    D B F E A G C L J M H O K

    Postorder Traversal:

    D F E B G L M J O K H C A

  • 8/3/2019 OOPS C++ file (1)

    32/46

    Practical No: 7

    AIM:To Sort Array Using Bubble Sort

    Source Code

    #include

    #include

    class sort

    {

    int a[10];public:

    void inputarr(int num);

    void display(int num);void bubble(int num);

    };

    void sort::inputarr(int num){

    int i;

    cout

  • 8/3/2019 OOPS C++ file (1)

    33/46

    }

    void main()

    {int num,i;

    sort s1;

    cout

  • 8/3/2019 OOPS C++ file (1)

    34/46

    OUTPUT

    BUBBLE SORT

    Enter the number of elements6

    Enter the elements in the array

    67

    5543

    21

    6

    9The numbers in the unsorted array are

    67 55 43 21 6 9

    Array after bubble sort is applied

    6 9 21 43 55 67

  • 8/3/2019 OOPS C++ file (1)

    35/46

    Practical No: 8

    AIM:To Sort The Given List Using Quick Sort

    Source Code

    #include

    #include

    class sort

    {int a[10];

    public:

    void inputarr(int num);void display(int num);

    int partition(int left, int right);

    void quick(int left, int right);};

    void sort::inputarr(int num)

    {

    int i;cout

  • 8/3/2019 OOPS C++ file (1)

    36/46

    }

    temp=a[left];

    a[left]=high;a[right]=temp;

    low=a[left];

    while(pivot>low){

    if(right

  • 8/3/2019 OOPS C++ file (1)

    37/46

    }

  • 8/3/2019 OOPS C++ file (1)

    38/46

    OUTPUT

    QUICKSORT PROGRAM

    Enter the number of elements

    5Enter the elements in the array

    2

    31

    5

    8

    The numbers in the unsorted array are2 3 1 5 8

    Array after quicksort is applied

    1 2 3 5 8

  • 8/3/2019 OOPS C++ file (1)

    39/46

    Practical No: 9

    AIM:To Sort The Given List Using Selectig Sort

    Source Code

    #include

    #include

    class sort

    {

    int a[10];public:

    void inputarr(int num);

    void display(int num);void selection(int num);

    };

    void sort::inputarr(int num){

    int i;

    cout

  • 8/3/2019 OOPS C++ file (1)

    40/46

    t=a[i];

    a[i]=a[l];

    a[l]=t;

    }

    }void main()

    {

    int num,i;sort s1;

    cout

  • 8/3/2019 OOPS C++ file (1)

    41/46

    OUTPUT

    SELECTION SORTEnter the number of elements4

    Enter the elements in the array7

    83

    692

    The numbers in the unsorted array are

    7 83 69 2

    Array after selection sort is applied2 7 69 83

  • 8/3/2019 OOPS C++ file (1)

    42/46

    Practical No: 10

    AIM:Implementation of Different Operation on Stack Using Link List

    #include

    #include

    class stack{

    int element;

    stack* next;public:

    stack* push(stack*,int);

    stack* pop(stack*);void stack_display(stack*);

    }*head,object;

    stack* stack::push(stack* head,int key){

    stack* temp,*temp1;

    temp1=head;temp=new stack;

    temp->element=key;

    temp->next=NULL;if(head==NULL)

    head=temp;

    else{

    while(head->next!=NULL)

    head=head->next;

    head->next=temp;head=temp1;

    }

    return head;}

    stack* stack::pop(stack* head)

    {stack* temp;

    if(head!=NULL)

    {temp=head;

    if(head->next==NULL)

    {

  • 8/3/2019 OOPS C++ file (1)

    43/46

    coutnext;coutnext->next;

    head=temp;return head;

    }

    else

    {cout

  • 8/3/2019 OOPS C++ file (1)

    44/46

    cin>>key;

    head=object.push(head,key);

    cout

  • 8/3/2019 OOPS C++ file (1)

    45/46

    OUTPUT:

    Choose the operation to be performed: 1.push 2.pop 3.exit1

    Enter the element to be pushed:12The stack after push operation is:12

    Choose the operation to be performed: 1.push 2.pop 3.exit2

    The pooped element from the stack is:12

    The stack after pop operation is:The stack is empty

    Choose the operation to be performed: 1.push 2.pop 3.exit

    1

    Enter the element to be pushed:12

    The stack after push operation is:12

    Choose the operation to be performed: 1.push 2.pop 3.exit

    1

    Enter the element to be pushed:34

    The stack after push operation is:12->34

    Choose the operation to be performed: 1.push 2.pop 3.exit

    1

    Enter the element to be pushed:56

    The stack after push operation is:12->34->56

    Choose the operation to be performed: 1.push 2.pop 3.exit

    2

    The popped element from the stack is:56The stack after pop operation is:12->34

    Choose the operation to be performed: 1.push 2.pop 3.exit3

  • 8/3/2019 OOPS C++ file (1)

    46/46