Data Structure Programs-Final Rishabh Jain

download Data Structure Programs-Final Rishabh Jain

of 48

Transcript of Data Structure Programs-Final Rishabh Jain

  • 8/2/2019 Data Structure Programs-Final Rishabh Jain

    1/48

    PROGRAM -1

    Write a Program to Implement STACK.

    -: INSERTION :-

    #include

    #include

    #include

    int push(int [ ],int &,int);

    void display(int [ ],int);

    const int size=50;

    void main( )

    {

    int stack[size],item,top=-1,res;

    char ch=y;

    clrscr();

    while(ch==y||ch==Y)

    {

    coutitem;

    res=push(stack,top,item);

    if(res == -1)

    {

    cout

  • 8/2/2019 Data Structure Programs-Final Rishabh Jain

    2/48

    cout

  • 8/2/2019 Data Structure Programs-Final Rishabh Jain

    3/48

    The output produced by above program is shown below.

    Symbolsignifies top.

    Enter ITEM for insertion: 7

    The stack now is:

    7

    Want to insert more elements? (y/n)... y

    Enter ITEM for insertion: 9

    The stack now is:

    9

    7

    Want to insert more elements? (y/n)... y

    Enter ITEM for insertion: 9

    The stack now is:

    5

    9

    7

    Want to insert more elements? (y/n)... n

    The above program calls function push() for pushing element intostack-array. This function firstly tests for STACK-FULL condition and if it

    is full, it returns -1, otherwise, it successfully inserts element into stack

    and returns 0.

    3

  • 8/2/2019 Data Structure Programs-Final Rishabh Jain

    4/48

    Rishabh Jain (0114IT071070)

    4

  • 8/2/2019 Data Structure Programs-Final Rishabh Jain

    5/48

    -: DELETION :-

    #include

    #include

    #include

    int pop(int [ ],int &);

    int push(int [ ],int &,int);

    void display(int [ ],int);

    const int size=50;

    void main( )

    {

    int stack[size],item, top= -1,res;

    char ch=y;

    clrscr();

    while(ch==y||ch==Y)

    {

    coutitem;

    res=push(stack ,top, item);

    if(res== -1)

    {

    cout

  • 8/2/2019 Data Structure Programs-Final Rishabh Jain

    6/48

    cin>>ch;

    }

    cout

  • 8/2/2019 Data Structure Programs-Final Rishabh Jain

    7/48

    {

    Top++;

    stack[top]=ele;

    }

    return 0;

    }

    int pop(int stack[], int &top)

    {

    int ret;

    if(top== -1)

    return -1;

    else

    {

    ret=stack[top];

    top--;

    }

    return ret;

    }

    void display(int stack[],int top)

    {

    if(top== -1)

    return;

    cout

  • 8/2/2019 Data Structure Programs-Final Rishabh Jain

    8/48

    The output produced by above program is shown below.

    Symbolsignifies top.

    The stack now is:

    9

    5

    8

    7

    Want to delete more elements? (y/n)... y

    Element deleted is: 9

    The stack now is:

    5

    8

    7

    Want to delete more elements? (y/n)... y

    Element deleted is: 5

    The stack now is:

    8

    7

    Want to insert more elements? (y/n)... n

    Rishabh Jain (0114IT071070)

    PROGRAM- 2

    8

  • 8/2/2019 Data Structure Programs-Final Rishabh Jain

    9/48

    Write a Program to Implement QUEUE?

    -: INSERTION:-

    #include

    #include

    #include

    int insert_in_Q(int [ ],int);

    void display(int [ ], int, int);

    const int size=50;

    int queue[size], front= -1, rear= -1;

    void main()

    {

    int item, res;

    char ch=y;

    clrscr();

    while(ch==y||ch==Y)

    {

    coutitem;

    res=insert_in_Q(queue, item);

    if(res == -1)

    {

    cout

  • 8/2/2019 Data Structure Programs-Final Rishabh Jain

    10/48

    display(queue, front, rear);

    coutch;

    }

    getch();

    }

    int insert_in_Q(int queue[ ], int ele)

    {

    if(rear==size-1)

    return -1;

    else if(rear== -1)

    {

    front=rear=0;

    queue[rear]=ele;

    }

    else

    {

    rear++;

    queue[rear]=ele;

    }

    return 0;

    }

    void display(int queue[ ],int top)

    {

    Rishabh Jain (0114IT071070)

    if(front == -1)

    10

  • 8/2/2019 Data Structure Programs-Final Rishabh Jain

    11/48

    return;

    for(int i=top-1;i>=0;i--)

    cout

  • 8/2/2019 Data Structure Programs-Final Rishabh Jain

    12/48

    The output produced by above program is shown below.

    Enter ITEM for insertion: 3

    Now the Queue (Front.......to.......Rear) is:

    3

    Want to insert more elements? (y/n)... y

    Enter ITEM for insertion: 6

    Now the Queue (Front.......to.......Rear) is:

    3 6

    Want to insert more elements? (y/n)... y

    Enter ITEM for insertion: 8

    Now the Queue (Front.......to.......Rear) is:

    3 6 8

    Want to insert more elements? (y/n)... n

    12

  • 8/2/2019 Data Structure Programs-Final Rishabh Jain

    13/48

    Rishabh Jain (0114IT071070)

    -: DELETION:-

    #include

    #include

    #include

    int remove(int [ ], int);

    int insert(int [ ],int);

    void display(int [ ], int, int);

    const int size=50;

    int queue[size], front= -1, rear= -1;

    void main( )

    {

    int item, res;

    char ch=y;

    clrscr( );

    while(ch==y||ch==Y)

    {

    coutitem;

    res=insert(queue, item);

    if(res== -1)

    {

    cout

  • 8/2/2019 Data Structure Programs-Final Rishabh Jain

    14/48

    exit(1);

    }

    cout

  • 8/2/2019 Data Structure Programs-Final Rishabh Jain

    15/48

    }

    getch();

    }

    int insert_in_Q(int queue[ ], int ele) Rishabh Jain (0114IT071070)

    {

    if(rear==size-1)

    return -1;

    else if(rear== -1)

    {

    front=rear=0;

    queue[rear]=ele;

    }

    else

    {

    rear++;

    queue[rear]=ele;

    }

    return 0;

    }

    int remove(int queue[ ])

    {

    int ret;

    if(front== -1)

    return -1;

    else

    {

    15

  • 8/2/2019 Data Structure Programs-Final Rishabh Jain

    16/48

    ret=queue[front];

    if(front==rear)

    front=rear=-1;

    else Rishabh Jain (0114IT071070)

    front++;

    }

    return ret;

    }

    void display(int stack[],int top)

    {

    if(front == -1)

    return;

    for(int i=top-1;i>=0;i--)

    cout

  • 8/2/2019 Data Structure Programs-Final Rishabh Jain

    17/48

    Rishabh Jain (0114IT071070)

    The Output Produced By Above Program Is Shown Below.

    Now the Queue (Front.......to.......Rear) is:

    7 9 4 2

    Want to delete more elements? (y/n)... y

    Element deleted is: 7

    Now the Queue (Front.......to.......Rear) is:

    9 4 2

    Want to delete more elements? (y/n)... y

    Element deleted is: 9

    Now the Queue (Front.......to.......Rear) is:

    4 2

    Want to delete more elements? (y/n)... n

    17

  • 8/2/2019 Data Structure Programs-Final Rishabh Jain

    18/48

    Rishabh Jain (0114IT071070)

    PROGRAM- 3

    Write a Program to Implement LINKED LIST.

    -: INSERTION:-

    #include

    #include

    #include

    struct node

    {

    int info;

    Node *next;

    } *start, *newptr, *save, *ptr;

    Node *create_new_node(int);

    void insert_beg(node *);

    void display(node *);

    void main()

    {

    start=NULL;

    int inf;

    char ch=y;

    while(ch==y||ch==Y)

    {

    18

  • 8/2/2019 Data Structure Programs-Final Rishabh Jain

    19/48

    clrscr();

    coutinf;Rishabh Jain (0114IT071070)

    cout

  • 8/2/2019 Data Structure Programs-Final Rishabh Jain

    20/48

    getch();

    }

    node *create_new_node(int n)Rishabh Jain (0114IT071070)

    {

    ptr=new node;

    ptr-> info=n;

    ptr->next=NULL;

    return ptr;

    }

    void insert_beg(node *np)

    {

    if(start==NULL)

    start=np;

    else

    {

    save=start;

    start=np;

    np->next=save;

    }

    }

    void display(node *np)

    {

    while(np!=NULL)

    {

    cout

  • 8/2/2019 Data Structure Programs-Final Rishabh Jain

    21/48

    np=np->next;

    }

    cout 7 -> !!

    Press Y to enter more nodes, N to exit...n

    21

  • 8/2/2019 Data Structure Programs-Final Rishabh Jain

    22/48

    Rishabh Jain (0114IT071070)

    -: DELETION:-

    #include

    #include

    #include

    struct node

    {

    int info;

    node *next;

    } *start, *newptr, *save, *ptr, *rear;

    node *create_new_node(int);

    void insert(node *);

    void display(node *);

    void delnode();

    void main()

    {

    start=rear=NULL;

    int inf;

    char ch=y;

    while(ch==y||ch==Y)

    {

    coutinf;

    cout

  • 8/2/2019 Data Structure Programs-Final Rishabh Jain

    23/48

    newptr=create_new_node(inf);

    if(newptr==NULL)

    { Rishabh Jain (0114IT071070)

    cout

  • 8/2/2019 Data Structure Programs-Final Rishabh Jain

    24/48

    ptr->next=NULL;

    return ptr;

    }

    Rishabh Jain (0114IT071070)

    void insert(node *np)

    {

    if(start==NULL)

    start=np;

    rear=np;

    else

    {

    rear->next=np;

    rear=np;

    }

    }

    void delnode()

    {

    if(start==NULL)

    cout

  • 8/2/2019 Data Structure Programs-Final Rishabh Jain

    25/48

    void display(node *np)

    {

    while(np!=NULL)Rishabh Jain (0114IT071070)

    {cout

  • 8/2/2019 Data Structure Programs-Final Rishabh Jain

    26/48

    Rishabh Jain (0114IT071070)

    The output produced by above program is.

    The list now is:

    6 -> 7 -> 1 -> 4 -> 9 -> 3 -> 5 -> 2 -> !!!

    Want to delete first node? (y/n)...y

    The list now is:

    7 -> 1 -> 4 -> 9 -> 3 -> 5 -> 2 -> !!!

    Want to delete first node? (y/n)...y

    The list now is:

    1 -> 4 -> 9 -> 3 -> 5 -> 2 -> !!!

    Want to delete first node? (y/n)...n

    26

  • 8/2/2019 Data Structure Programs-Final Rishabh Jain

    27/48

    Rishabh Jain (0114IT071070)

    PROGRAM- 4

    Write a Program to Implement BUBBLE SORT.

    #include

    #include

    void bubblesort(int [ ],int);

    void main()

    {

    int AR[50],N;

    clrscr();

    coutN;

    cout

  • 8/2/2019 Data Structure Programs-Final Rishabh Jain

    28/48

    } Rishabh Jain (0114IT071070)

    void bubblesort(int AR[ ], int size)

    {int tmp, ctr=0;

    for(int i=0;i

  • 8/2/2019 Data Structure Programs-Final Rishabh Jain

    29/48

    Rishabh Jain (0114IT071070)

    The Output Produced By Above Program Is.

    How many elements do U want to create array with? (max. 50)....4

    Enter Array elements....

    7 3 8 2

    Array after iteration -1 is:

    3 7 8 2

    Array after iteration -2 is:

    3 7 8 2

    Array after iteration -3 is:

    3 7 2 8

    Array after iteration -4 is:

    3 7 2 8

    Array after iteration -5 is:

    3 2 7 8

    Array after iteration -6 is:

    2 3 7 8

    The Sorted array is as shown below...

    2 3 7 8

    Rishabh Jain (0114IT071070)

    29

  • 8/2/2019 Data Structure Programs-Final Rishabh Jain

    30/48

    PROGRAM- 5

    Write a Program to Implement INSERTION SORT.

    #include

    #include

    #include

    void INSsort(int [ ],int);

    void main()

    {

    int AR[50],N;

    clrscr();

    coutN;

    cout

  • 8/2/2019 Data Structure Programs-Final Rishabh Jain

    31/48

    }

    void INSsort(int AR[], int size)

    {

    int tmp,j;

    AR[0]=int_min;

    for(int i=1;i

  • 8/2/2019 Data Structure Programs-Final Rishabh Jain

    32/48

    The Output Produced by Above Program Is.

    How many elements do U want to create array with? (max. 50).....7

    Enter array elements....

    9 6 1 3 4 2 7

    Array after pass - 1 is:

    -32768 9 6 1 3 4 2 7

    Array after pass - 2 is:

    -32768 6 9 1 3 4 2 7

    Array after pass - 3 is:

    -32768 1 6 9 3 4 2 7

    Array after pass - 4 is:

    -32768 1 3 6 9 4 2 7

    Array after pass - 5 is:

    -32768 1 3 4 6 9 2 7

    Array after pass - 6 is:

    -32768 1 2 3 4 6 9 7

    Array after pass - 7 is:

    -32768 1 2 3 4 6 7 9

    The Sorted array is as shown below....

    -32768 1 2 3 4 6 7 9

    Rishabh Jain (0114IT071070)

    PROGRAM- 6

    32

  • 8/2/2019 Data Structure Programs-Final Rishabh Jain

    33/48

    Write a Progrm to Implement SELECTION SORT.

    #include

    #include

    void selsort(int [], int);

    void main()

    {

    int AR[50], N;

    clrscr();

    coutN;

    cout

  • 8/2/2019 Data Structure Programs-Final Rishabh Jain

    34/48

    for(int i=0;j

  • 8/2/2019 Data Structure Programs-Final Rishabh Jain

    35/48

    Enter array elements....

    6 8 2 5 1 3 4

    Array after pass - 1 is:

    1 8 2 5 6 3 4

    Array after pass - 2 is:

    1 2 8 5 6 3 4

    Array after pass - 3 is:

    1 2 3 5 6 8 4

    Array after pass - 4 is:

    1 2 3 4 6 8 5

    Array after pass - 5 is:

    1 2 3 4 5 8 6

    Array after pass - 6 is:

    1 2 3 4 5 6 8

    The Sorted array is as shown below.....

    1 2 3 4 5 6 8

    Rishabh Jain (0114IT071070)

    PROGRAM- 7

    Write a Program to Implement MERGE SORT.

    #include

    35

  • 8/2/2019 Data Structure Programs-Final Rishabh Jain

    36/48

    #include

    void merge(int [], int, int [], int, int []);

    void main()

    {

    int A[50], B[50], C[50], MN=0,M, N;

    clrscr();

    coutM;

    cout

  • 8/2/2019 Data Structure Programs-Final Rishabh Jain

    37/48

    cout

  • 8/2/2019 Data Structure Programs-Final Rishabh Jain

    38/48

    How many elements do U want to create array ? (max. 50)....5

    Enter first Arrays element [ascending]...

    2 5 8 9 12

    How many elements do U want to create array ? (max. 50)....5

    Enter first Arrays element [descending]...

    16 12 10 8 7 3 1

    The Sorted array is as shown below...

    1 2 3 5 7 8 8 9 10 12 12 16

    Rishabh Jain (0114IT071070)

    38

  • 8/2/2019 Data Structure Programs-Final Rishabh Jain

    39/48

    PROGRAM- 8

    Write a program to implement QUICK SORT.

    #include

    #include

    int a[100],n,i,l,h;

    void main()

    { clrscr();

    void input(void);

    input();

    getch();

    }

    void input(void)

    {

    void quicksort(int a[],int l,int h);

    void output(int a[],int n);

    coutn;

    cout

  • 8/2/2019 Data Structure Programs-Final Rishabh Jain

    40/48

    i=0;h=n-1;

    quicksort(a,l,h);

    cout

  • 8/2/2019 Data Structure Programs-Final Rishabh Jain

    41/48

    }

    }

    while(low

  • 8/2/2019 Data Structure Programs-Final Rishabh Jain

    42/48

    Output generated by above program is :

    Enter no. of many elements in the array: 5

    Enter elements:

    4

    3

    6

    2

    5

    Sorted array :

    2 3 4 5 6

    Rishabh Jain (0114IT071070)

    42

  • 8/2/2019 Data Structure Programs-Final Rishabh Jain

    43/48

    PROGRAM- 9

    Write a Program to Implement LINEAR SEARCH.

    #include

    #include

    int Lsearch(int [],int,int);

    void main()

    {

    int AR[50],ITEM,N,index;

    clrscr();

    coutN;

    cout

  • 8/2/2019 Data Structure Programs-Final Rishabh Jain

    44/48

    }

    Int Lsearch(int AR[ ],int size,int item)

    {

    For(int i=0;i

  • 8/2/2019 Data Structure Programs-Final Rishabh Jain

    45/48

    The output produced by the program is.

    Enter desired array size(max. 50)7

    Enter Array elements

    1 5 2 8 6 10 7

    Enter Elements to be searched for6

    Elements found at index : 4,Position :5

    Rishabh Jain (0114IT071070)

    PROGRAM- 10

    45

  • 8/2/2019 Data Structure Programs-Final Rishabh Jain

    46/48

    Write a Program to Implement BINARY SEARCH.

    #include

    #include

    Int Bsearch(int [ ],int,int);

    void main()

    {

    int AR[50],ITEM,N,index;

    clrscr();

    coutN;

    coutAR[i];

    }

    coutITEM;

    index=Bsearch(AR,N,ITEM);

    if ()index==-1)

    cout

  • 8/2/2019 Data Structure Programs-Final Rishabh Jain

    47/48

    {

    int beg,last,mid;

    beg=o;

    last=size-1;

    while(begAR[mid])

    beg=mid+1;

    else

    last=mid-1;

    }

    return -1;

    }

    Rishabh Jain (0114IT071070)

    The Output Produced by The Above Program is.

    47

  • 8/2/2019 Data Structure Programs-Final Rishabh Jain

    48/48

    Enter desired array size (max.50)7

    Enter Array elements (must be sorted in Asc order)

    2 5 7 8 9 10 15

    Enter Element to be searched for8

    Element found at index :3, Position : 4

    Rishabh Jain (0114IT071070)