34482550 IGNOU MCA 2nd Semster Data Structure Lab Record Solved MCSL 025

Embed Size (px)

Citation preview

  • 8/6/2019 34482550 IGNOU MCA 2nd Semster Data Structure Lab Record Solved MCSL 025

    1/74

    AIM:

    Product of two matrices

    PROGRAM

    #include

    #define MAX 10

    void main(){int a[MAX][MAX],b[MAX][MAX],c[MAX][MAX],i,j,k,m,n,p,q;

    clrscr();

    printf("Enter the order of the first matrix\n");

    scanf("%d %d",&m,&n);

    printf("Enter the order of the second matrix\n");

    scanf("%d %d",&p,&q);

    if(n!=p)

    {printf("The matrix can not be multiplied\n");

    }

    else{printf("Enter the elements of the first matrix\n");

    for(i=0;i

  • 8/6/2019 34482550 IGNOU MCA 2nd Semster Data Structure Lab Record Solved MCSL 025

    2/74

    Enter the order of the second matrix

    3 2

    Enter the elements of the first matrix

    1 2

    3 4

    5 6

    Enter the elements of the second matrix

    1 2 3

    4 5 6

    The resultant matrix on multiplication is

    22 28

    49 64

    AIM:

    Program to accept 10 strings as input and print them in lexicographic orderALGORITHM:

    9. Start

    10. Enter 10 strings to an array of strings

    11. Set i = 0, repeat steps 4-7 until i=10

    12. Set j =i+1

    13. If i th string in the array is greater than j th string, then inter-change them, using a

    temporary string variable.

    14. Increment j, repeat step 5,6 untill j> 10

    15. Increment i

    16. Print the strings

    17. Stop

    PROGRAM:

    #include

    void main()

    {

    char str[10][10],t[10];

    int i,j;

    clrscr();

    for(i=0;i

  • 8/6/2019 34482550 IGNOU MCA 2nd Semster Data Structure Lab Record Solved MCSL 025

    3/74

    {

    strcpy(t,str[i]);

    strcpy(str[i],str[j]);

    strcpy(str[j],t);

    }}

    printf("The strings in lexicographical order is\n");

    for(i=0;i

  • 8/6/2019 34482550 IGNOU MCA 2nd Semster Data Structure Lab Record Solved MCSL 025

    4/74

    char s1[50],s2[50];

    clrscr();

    printf("Enter 2 strings\n");

    gets(s1);

    gets(s2);

    l1=strlen(s1);

    l2=strlen(s2);

    while(i

  • 8/6/2019 34482550 IGNOU MCA 2nd Semster Data Structure Lab Record Solved MCSL 025

    5/74

    30. Set i=0, repeatedly copy the ith character of first string to a third string, till len1

    31. Set J=0, repeatedly copy the jth character of string 2 to the ith position onwards of 3rd

    string till len 2

    32. Increment i and j

    33. Print the 3rd string

    34. Stop

    PROGRAM:

    #include#include

    void main()

    {

    char *s1, *s2, *s3;

    int length, len1=0,len2=0, i, j;

    clrscr();

    s1=(char*)malloc(20* sizeof(s1));

    s2=(char*)malloc(20* sizeof(s2));

    printf("Enter first string\n");

    gets(s1);

    len1=strlen(s1);

    printf("Enter second string\n");

    gets(s2);

    len2=strlen(s2);

    length=len1+len2;

    s3= (char*)malloc((length+2)* sizeof(s3));

    for(i=0;i

  • 8/6/2019 34482550 IGNOU MCA 2nd Semster Data Structure Lab Record Solved MCSL 025

    6/74

    students, based on marks. Program should print mark list in ascending order of their rank.

    ALGORITHM:

    35. Start

    36. Read the number of students (n)

    37. Repeat steps 4, n times

    38. Read enrolment no., name, total mark39. Sort the names based on marks

    40. Print the mark list with their ranks

    41. Stop

    PROGRAM:

    #include

    #include

    struct stud

    {

    int roll;

    int mark;

    char name[15];

    };main()

    {

    struct stud S[10], t;

    int i, j, n;

    clrscr();

    printf("Enter numbers of students\n");

    scanf("%d",&n);for(i=0;i

  • 8/6/2019 34482550 IGNOU MCA 2nd Semster Data Structure Lab Record Solved MCSL 025

    7/74

    Enter numbers of students

    3

    Enter roll no., name, mark

    1

    VINU50

    Enter roll no., name, mark2

    GEETHA

    60

    Enter roll no., name, mark

    3

    RAMU

    55

    Rank List

    Roll No Name Mark Rank

    2 GEETHA 60 1

    1 VINU 50 2

    3 RAMU 55 3

    AIM:

    Program to multiply two sparse matrices

    ALGORITHM:

    42. Start

    43. Enter two matrices by reading volumes

    44. Make them into 3 tuple from by calling (convsp)45. Set i=1, repeat i loop till i< number of non zeros in the first matrix (M1)

    46. Set j=1, repeat j lp[ till jx number of non zeros in the second matrix (M2)

    47. If M[i] [1]=M2 [j][0], then set

    48. K1=M1[i] [o], K2=M2[j] [o]

    49. Sum +=M1[i][2] * M2[j] [2]

    50. Print the product matrix

    51. Stop

    PROGRAM:

    /*to multiply two sparce matrices***/

    #include#include

    /*function to convert to a sparse matrix ***/

    struct sp

    {

    int row,col;

    int mat[10][10];

    int sp[50][3];

  • 8/6/2019 34482550 IGNOU MCA 2nd Semster Data Structure Lab Record Solved MCSL 025

    8/74

    };

    /* to convert the entered matrix to sparce form,by eleminating zero values***/

    int convsp(struct sp *M )

    {

    int e=1,i,j;

    printf("enter number of rows and columns in the matrix");

    scanf("%d%d",&M->row,&M->col);

    printf("enter the matrix");

    for(i=0;irow;++i)

    for(j=0;jcol;++j)

    {scanf("%d",&M->mat[i][j]);

    if(M->mat[i][j]!=0)

    {

    M->sp[e][0]=i;

    M->sp[e][1]=j;

    M->sp[e][2]=M->mat[i][j];e++;

    }//end if

    }/*end j loop*/

    M->sp[0][0]=M->row; //store number of rows to first(row,col) of sparse

    M->sp[0][1]=M->col; //store number of cols to first row,second col

    M->sp[0][2]=e-1; //store total number of non zero values to 3rd column

    return M->sp[0][2]; //return total number of non zero elements

    } /*to multiply the 2 matrix**/

    mult(struct sp M1,int e1, struct sp M2,int e2)

    {int sum[10][10],i,j,k1,k2;

    for(i=0;i

  • 8/6/2019 34482550 IGNOU MCA 2nd Semster Data Structure Lab Record Solved MCSL 025

    9/74

    printf("\n");

    }

    } /*to print sparse matrix ***/

    void printsp(int n,struct sp matx)

    {

    int i,j;

    for(i=0;i

  • 8/6/2019 34482550 IGNOU MCA 2nd Semster Data Structure Lab Record Solved MCSL 025

    10/74

    2 2 1

    Enter number of rows and columns in the matrix

    3 3

    Enter the matrix

    0 1 0

    1 0 0

    1 0 1

    SPARSE MATRIX2

    3 3 4

    0 1 1

    1 0 1

    2 0 1

    2 2 1

    Product matrix

    0 1 01 0 0

    2 1 1

    AIM:

    To enter a paragraph of text as input. Make a list of words and the number of references of each

    word in the paragraph as output.

    ALGORITHM:

    52. Start53. Enter a paragraph of text

    54. Repeat steps 4-5 until In is met

    55. Read char by char, until space, then store this word to a new array, which is used to store

    the different words in the para.

    56. Repeat step 4 and the new word compared with the word stored in new array before. If

    same, then increment number of occurrence, else store as next word.

    57. Print the new array of words and the corresponding number of occurrences.

    PROGRAM:

    #include

    #includestruct paragraph

    {

    char words[15];

    int occ;

    }p[50];

    void main()

  • 8/6/2019 34482550 IGNOU MCA 2nd Semster Data Structure Lab Record Solved MCSL 025

    11/74

    {

    int i=0,j=0,k=0,flag=0;

    char w[15],ch=0;

    clrscr();

    strcpy(w," ");

    printf("Enter the paragraph\n");

    while(ch!='\n')

    {

    flag=0;

    strcpy(p[j].words,"");

    p[j].occ=1;

    ch=getchar();

    w[i]=ch;

    i++;

    if(ch==' ')

    {w[i]='\0';

    for(k=0;k

  • 8/6/2019 34482550 IGNOU MCA 2nd Semster Data Structure Lab Record Solved MCSL 025

    12/74

    God 1

    bless 1

    you 1

    children 2

    good 1

    be 1

    AIM:

    Program for the creation of Singly Linked list. Also to delete an element from the list

    ALGORITHM:

    58. Start

    59. Create a node

    60. Read the dates, till =1 is entered

    61. Point the next pointer to the next node, if end is reached (-1), next pointer point to Null

    62. StopDeletion

    63. If list is empty, print list is empty

    64. else if first node is to be deleted, then head point to the second node and free list

    65. else find the node to be deleted and that node is freed

    66. End

    PROGRAM:

    /*Creation and deletion of linked list*/

    #define NULL 0

    struct student

    {char name[15];

    int roll_no;

    struct student *next;

    }*stud,*first;

    /*creation of list*/

    list_create(struct student *s1)

    {

    printf("Enter roll number:-1 to terminate\n");

    scanf("%d",&s1->roll_no);

    if(s1->roll_no!=-1){

    printf("Enter name: ");

    scanf("%s",s1->name);

    s1->next=(struct student*)malloc(sizeof(struct student));

    list_create(s1->next);

    }

    else

  • 8/6/2019 34482550 IGNOU MCA 2nd Semster Data Structure Lab Record Solved MCSL 025

    13/74

    {

    s1->next=NULL;

    return;

    }

    }

    /*Display the list */

    display_list(struct student *s1)

    {

    if(first->next==NULL)

    {

    printf("List is empty");

    getch();

    return 0;

    }

    while(s1->next)

    {printf("%d\t%s\n",s1->roll_no,s1->name);

    s1=s1->next;

    }

    getch();

    return 0;

    }

    /*Delete from list */

    delete_element(struct student *start)

    {

    struct student *temp,*t;int roll,flag=0;

    if(first->next==NULL)

    {

    printf("List empty");

    getch();

    return 0;

    }

    printf("Enter rollnumber to delete\n");

    scanf("%d",&roll);

    if(start->roll_no==roll){

    temp=start->next;

    free(start);

    first=temp;

    return 0;

    }

  • 8/6/2019 34482550 IGNOU MCA 2nd Semster Data Structure Lab Record Solved MCSL 025

    14/74

    /* any other node */

    t=start;/*store previous node t*/

    start=start->next; /*point next node */

    while(start)

    {

    if(start->roll_no==roll)

    {

    temp=start->next;

    free(start);

    t->next=temp;

    return;

    }

    t=t->next;

    start=start->next;

    flag=1;

    }if(flag==1)

    {

    printf("Rollnumber not found");

    getch();

    return(0);

    }

    }

    main()

    {

    int choice=0;clrscr();

    stud=(struct student*)malloc(sizeof(struct student));

    first=stud;

    first->next=NULL;

    while(choice!=4)

    {

    clrscr();

    printf("MENU\n\n");

    printf("Create....1\n\nDisplay...2\n\nDelete...3\n\nExit...4\n\n");

    printf("Enter choice\n");scanf("%d",&choice);

    switch(choice)

    {

    case 1:

    list_create(stud);

    break;

  • 8/6/2019 34482550 IGNOU MCA 2nd Semster Data Structure Lab Record Solved MCSL 025

    15/74

    case 2:

    display_list(stud);

    break;

    case 3:

    delete_element(stud);

    break;

    case 4: break;

    }

    }

    getch();

    return 0;

    }

    OUTPUT:

    MENU

    67. create

    68. display69. delete

    70. Exit

    Enter choice : 1

    Enter roll number : 12

    Enter name : Anu

    Enter roll number : -1

    return to main menu

    AIM:

    Implementation of doubly linked listALGORITHM:

    Creation

    71. Define data structure with a data field and one left pointer and one right pointer

    72. Allocate space for first node (malloc)

    73. Read the value to the node

    74. Left pointer of first node points to Null and right pointer points to next node

    75. Repeat above steps for the number of nodes right ptr. of last node points to NULL

    Deletion

    76. If list is empty, display message

    77. If first node is to be deleted, then left pointer of second node points to NULL and free firstnode

    78. Else search for the node to be deleted and free the node

    PROGRAM:

    /* doubly linked list*/

    #define NULL 0

    struct student

  • 8/6/2019 34482550 IGNOU MCA 2nd Semster Data Structure Lab Record Solved MCSL 025

    16/74

    {

    int roll_no;

    char name[15];

    struct student *right,*left;

    }*stud;

    /* Creation of list */

    create(struct student *s)

    {

    int rol_no;

    printf("Enter rollnumber -1 to terminate : ");

    scanf("%d",&s->roll_no);

    if(s->roll_no!=-1)

    {

    printf("Enter name: ");

    scanf("%s",s->name);

    s->right=(struct student*)malloc(sizeof(struct student));s->right->left=s;

    s->right->right=NULL;

    create(s->right);

    }

    else

    s->right=NULL;

    }

    /*traverse the list */

    void trav_right(struct student *s)

    {struct student *stud=s;

    if(s->right==NULL)

    {

    printf("List empty");

    getch();

    return;

    }

    while(s->right)

    {

    printf("\n Roll No. %d\t\tName: %s",s->roll_no,s->name);s=s->right;

    }

    } /*deleting element */

    del_element()

    {

    int rol_no;

  • 8/6/2019 34482550 IGNOU MCA 2nd Semster Data Structure Lab Record Solved MCSL 025

    17/74

    struct student *t,*start,*temp;

    printf("\n\nEnter rollnumber to delete:\n");

    scanf("%d",&rol_no);

    /* if first rollnumber is to delete*/

    if(stud->roll_no==rol_no)

    {

    t=stud;

    stud=stud->right;

    stud->left=NULL;

    free(t);

    return;

    }

    /*any other rollnumber */

    temp=stud;

    while(temp->right!=NULL)

    {if(temp->right->roll_no==rol_no)

    {

    t=temp->right;

    temp->right=t->right;

    temp->right->left=temp;

    free(t);

    }

    temp=temp->right;

    }

    } /*main function***/main()

    {

    clrscr();

    stud=(struct student*)malloc(sizeof(struct student));

    create(stud);

    trav_right(stud);

    del_element();

    trav_right(stud);

    getch();

    return;}

    OUTPUT:

    Enter roll number, -1 to stop

    15

    Enter Name : ANU

    Enter roll number : 16

  • 8/6/2019 34482550 IGNOU MCA 2nd Semster Data Structure Lab Record Solved MCSL 025

    18/74

    Enter Name : Veena

    Enter roll number : 1

    roll number = 15, Name = Anu

    roll number = 16, Name = Veena

    Enter roll number to delete : 15

    roll number =16, Name Veena

    _______________________________________________________________

    AIM:

    Implementation of circularly linked list

    ALGORITHM

    79. Start

    80. Define a data structure with fields data and a pointer

    81. allocate memory space for the first node and read data head

    left=NULL, head

    right=value82. Repeat above steps for all nodes

    83. For the tail node; headright=first (start node)

    Insertion

    84. If list is empty or new element is before start, then insert new element at start

    85. If new is after last element and set the new as last node and point its right node to first

    86. Else insert at proper place

    Deletion

    87. If list empty, element cant be deleted

    88. If element is in first node, then start is second node

    89. Else find position of element and delete90. Stop

    PROGRAM:

    struct list

    {

    int num;

    struct list *next;

    }*first,*start;

    /*create list*/

    void create(struct list *start){

    int n;

    printf("Enter number(-1 to exist):");scanf("%d",&n);

    if(n!=-1)

    {

    start->next=(struct list*)malloc(sizeof(struct list));

    start->num=n;

    create(start->next);

    }

  • 8/6/2019 34482550 IGNOU MCA 2nd Semster Data Structure Lab Record Solved MCSL 025

    19/74

    else

    start->next=first;

    }

    /*Display list*/

    void display(struct list *start){

    while(start->next){

    if(start->next!=first)

    {

    printf("\n%d",start->num);

    start=start->next;

    }

    }

    }

    /*Deleting element*/

    del(struct list *start)

    {

    int ele;struct list *t, *temp;

    printf("\n\nEnter element to delete\n");

    scanf("%d",&ele);

    /*Delete first element*/if(start->num==ele)

    {

    t=start->next;

    free(start);

    first=t;

    return;

    }

    /*Delete other nodes*/t=start;

    start=start->next;

    while(start)

    {

    if(start->num==ele)

    {

    temp=start->next;

    free(start);

    t->next=temp;

    return;}

    t=t->next;start=start->next;

    }

    }

    main()

    {

    clrscr();

    first=start;

    create(start);

  • 8/6/2019 34482550 IGNOU MCA 2nd Semster Data Structure Lab Record Solved MCSL 025

    20/74

    display(start);

    del(start);

    display(first);

    getch();

    return;}

    OUTPUT:

    Enter number : -1 to exit 25

    Enter number : -1 to exit 35

    Enter number

    List

    25

    35

    Enter element to delete : 35

    List

    25

    AIM

    Program that accepts two singly linked lists A and B as input and print the common elements to

    A and B

    ALGORITHM:

    91. Start

    92. Declare data structure for the list

    93. Create the lists A and B by reading necessary values to both

    94. Repeat steps 5-7 till Anext is NULL

    95. Repeat steps 6-7 till Bnext is NULL

    96. If Avalue=Bvalue then print it.

    97. Set flag 1

    98. If flag=0 print no common elements

    99. Stop

    PROGRAM:

    # include

    # include

    struct student {int roll no; struct student *next;} *s1, *s2;

    /*creation of linked list*/

    List-create (struct student *S1)

    {

    print f (enter roll number) ; Scan f(%d, & s1roll no.);

    if (s1roll no 1= -1) /* -1 is entered to stop*/

    {

    sinext (struct student*) malloc (size of (struct students));

  • 8/6/2019 34482550 IGNOU MCA 2nd Semster Data Structure Lab Record Solved MCSL 025

    21/74

    list-create (s1next);

    }

    else s1next = NULL

    return

    }

    /*display the list*/

    void display list (struct sudent *s1)s

    {

    if (snext l== NULL)

    {

    print f ( %d \n, s1roll no);

    display list (s1next)

    }

    }

    /* function to print common elements */

    common-elements (styruct student *s1, struct student * s2)

    (

    int flag = 0

    struct student * temp s2; /* temp is to store the initial node of s2*)

    print f (common elements \n);

    while (s1lnext 1=NULL) /*repeat till the end of s1*/

    {

    while (s2next 1=NULL)/* This loop is repeated that many times the number of nodes in s1x s2

    */

    {

    if (s1roll no==s2roll no.)

    {

    flag=1; print f (%d \n, s1roll no.);

    /*flag is set to show that an equal roll number is met*/

    }

    s2=s2next;

    }

    s1=s1next; s2=templ /* s2 is to be started again from beginning when s1=s1next */

    }

    if (flag=0)print f (no common elements)`

    }

    main ( )

    {

    s1=(struct student*) malloc (struct student));

    s2=(struct student*) mulloc (size of (structstudent));

  • 8/6/2019 34482550 IGNOU MCA 2nd Semster Data Structure Lab Record Solved MCSL 025

    22/74

    /*create the s1 list */

    list-create (s1);

    print f (elements in first list \n); display- list (s1)

    get ch ( )

    print f (enter elements to second list);

    list-create (s2); /* creating second list */

    print f(elements in second list \n) display-list (s2);

    /*printing common elements */

    common-element (s1, s2) get ch( );

    }

    OUTPUT

    enter roll number : 12

    enter roll number : 13

    enter roll number : 14

    enter roll number : -1

    elements in the first list :12

    13

    14

    enter elements to second list

    enter roll number : 25

    enter roll number : 14

    enter roll number : 26

    enter roll number : 13

    enter roll number : -1

    Elements in the second list25

    14

    26

    13

    Common elements

    13

    14

    __________________________________________________________________________

    AIM:Sort the elements of the list in ascending order and accept an integer as input and insert this

    integer into the singly linked list at the appropriate position.

    ALGORITHM:

    100.START

    101.Create a list by reading values

    102.Sort the list

  • 8/6/2019 34482550 IGNOU MCA 2nd Semster Data Structure Lab Record Solved MCSL 025

    23/74

    103.Read the value to insert into the list

    104.Compromise this value with the values in list

    105.If the number is less than first value in list

    106.Then insert it at the beginning, else

    107.Repeat steps till listvalue less than the entered number

    108.If the number is less than any volumes insert just before value which is greater

    109.Else insert the value at the end of list

    110.If duplicate print duplicate

    111.STOP

    PROGRAM:

    /* to sort a linked list and insert an element at the proper position*/

    #include

    #include

    struct student

    {

    int rollno;

    struct student *next;

    }*stud,*first;

    /*******creation of list********/

    void list_create(struct student *s1)

    {

    clrscr();

    printf("enter roll number-1 to stop"); scanf("%d",&s1->rollno);

    if(s1->rollno!=-1)

    {

    // printf("enter name"); scanf("%s",s1->name);s1->next=(struct student*)malloc(sizeof(struct student));

    list_create(s1->next);

    }

    else s1->next=NULL;

    return;

    }

    /*****display the list **********/

    void display_list(struct student *s1)

    {

    if(s1->next!=NULL){

    printf("%d\n",s1->rollno);

    // printf("%d\t%s",s1->next->rollno,s1->next->name);

    // printf("%d\t%s",s1->next->nextrollno,s1->next->next->name);

    display_list(s1->next);

    }

  • 8/6/2019 34482550 IGNOU MCA 2nd Semster Data Structure Lab Record Solved MCSL 025

    24/74

    }

    /********sort list ***********/

    void sort_list(struct student *s1)

    {

    struct student *temp,*t,*t1;

    if(s1->next==NULL)

    return;

    t=s1;

    while(t)

    {

    t1=t->next;

    while(t1->next!=NULL)

    {if(t->rollno>t1->rollno)

    {temp->rollno=t->rollno;

    t->rollno=t1->rollno;

    t1->rollno=temp->rollno;}t1=t1->next;}

    t=t->next;}

    }

    /**inserting an element to list*/

    insert_element(struct student *s1)

    { int r; struct student* temp,*prev;

    printf("enter rollnumber to insert"); scanf("%d",&r);

    //to insert before the first node

    if(rrollno)

    { temp=(struct student*)malloc(sizeof(struct student));temp->rollno=r;

    temp->next=s1;

    first=temp; return;

    }

    /*to insert in between any node*/

    while(s1->next)

    {

    if(s1->rollno next;}

    else

    {temp=(struct student*)malloc(sizeof(struct student));

    temp->rollno=r;

    temp->next=prev->next;

    prev->next=temp;

    break;}

  • 8/6/2019 34482550 IGNOU MCA 2nd Semster Data Structure Lab Record Solved MCSL 025

    25/74

    }

    /*to insert after last node*/

    if(s1->next==NULL && r>s1->rollno)

    {

    temp=(struct student*)malloc(sizeof(struct student));

    temp->rollno=r;

    temp->next=prev->next;

    prev->next=temp;

    }}

    /********searching for an element in the list ********/

    /*struct student* search(struct student *start, int rn)

    {

    if(start->next==NULL)

    return (NULL);

    if(start->next->rollno==rn)

    return(start);else

    search(start->next,rn);

    return NULL;

    }*/

    /********* delete element from list *************/

    /*struct student* delete_element(struct student *start)

    {

    struct student *temp,*t; int roll;

    printf("enter rollnumber to delete"); scanf("%d",&roll);

    if(start->rollno==roll){

    temp=start->next;

    free(start);

    start=temp;

    }

    else

    {

    t=search(start,roll);

    if(t==NULL)

    printf("roll number not found\n");else

    { temp=t->next->next; free(t->next); t->next=temp; }

    }return(start);

    }*/

    /*********main ***********/

    main()

  • 8/6/2019 34482550 IGNOU MCA 2nd Semster Data Structure Lab Record Solved MCSL 025

    26/74

    {

    clrscr();

    first=(struct student*)malloc(sizeof(struct student));

    stud=(struct student*)malloc(sizeof(struct student));

    first=stud;// first->next=NULL;

    list_create(stud);

    display_list(stud);

    //stud=delete_element(stud);

    printf("\nsorted list\n");

    sort_list(stud);

    display_list(stud);

    insert_element(stud);

    display_list(first);

    getch();

    return;

    }OUTPUT:

    Enter roll number-1 to stop

    10 68 74 1 22 99 4 3

    Sorted list

    1

    3

    4

    10

    22

    6874

    99

    Enter rollnumber to insert

    15 1 3 4 10

    AIM:

    Reverse an input string using stack

    ALGORITHM:

    112.START

    113.Create the data structure for stack, Repeat steps 3,4, until \n

    114.Read a string char by char

    115.Push the char to stack

    116.Repeat steps 6,7 until stack is empty

    117.Pop a character from stack

    118.Print the car

    119.STOP

  • 8/6/2019 34482550 IGNOU MCA 2nd Semster Data Structure Lab Record Solved MCSL 025

    27/74

    PROGRAM:

    /* to reverse a string using stack */

    #include

    #include

    struct list

    {

    char ch;

    struct list *next;

    }*top=NULL;

    /*store string to stak*/

    push(char s)

    {

    struct list* t;

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

    t->ch=s;

    t->next=top;top=t;

    }

    /*display reverse string*/

    display()

    {

    struct list *tmp;

    tmp=top;

    while(tmp!=NULL){

    printf("%c",tmp->ch);

    tmp=tmp->next;}

    }

    main()

    {

    char c;

    clrscr();

    printf("enter a string\n");

    while(c!='\n')

    {

    c=getchar();push(c); }

    printf("reversed string is\n");

    display();

    getch();

    }

    OUTPUT:

  • 8/6/2019 34482550 IGNOU MCA 2nd Semster Data Structure Lab Record Solved MCSL 025

    28/74

    Enter a string : IGNOU

    Reversed string is

    UONGI

    AIM:

    Program to implement multiple stack in a single array

    ALGORITHM:

    120.START

    121.Declare an array of size 20

    122.Set t1=0 (beginning of 1st stack) t2 = 10 (beginning of 2nd)

    123.Read the value to push to stack, and the stack number

    124.Push the value to the entered stack number

    125.If stack size is < 10 then print stack overflow

    126.To pop a value-enter stack number

    127.If stack size

  • 8/6/2019 34482550 IGNOU MCA 2nd Semster Data Structure Lab Record Solved MCSL 025

    29/74

    scanf("%d",&val);

    push(sno,val);

    break;

    case 2:

    printf("enter the stack no:\n");

    scanf("%d",&sno);

    pop(sno);

    break;

    case 3:

    exit();

    }

    }

    getch();

    }

    void push(int sno,int val)

    {switch(sno)

    {

    case 1:

    if((++t1)==10)

    printf("stack 1:overflow\n");

    else

    a[t1]=val;

    break;

    case 2:

    if(++t2==20)printf("stack 2:overflow\n");

    else

    a[t2]=val;

    break;

    }

    }

    void pop(int sno)

    {

    switch(sno)

    {case 1:

    if(t1

  • 8/6/2019 34482550 IGNOU MCA 2nd Semster Data Structure Lab Record Solved MCSL 025

    30/74

    t1--;

    }

    break;

    case 2:

    if(t2

  • 8/6/2019 34482550 IGNOU MCA 2nd Semster Data Structure Lab Record Solved MCSL 025

    31/74

    # include

    # define MAX 5

    int deque_arr[MAX];

    int left = -1;

    int right = -1;

    main()

    {

    int choice;

    printf("1.Input restricted dequeue\n");

    printf("2.Output restricted dequeue\n");

    printf("Enter your choice : ");

    scanf("%d",&choice);

    switch(choice)

    {

    case 1 :input_que();

    break;

    case 2:

    output_que();

    break;

    default:

    printf("Wrong choice\n");

    }/*End of switch*/

    }/*End of main()*/

    input_que(){

    int choice;

    while(1)

    {

    printf("1.Insert at right\n");

    printf("2.Delete from left\n");

    printf("3.Delete from right\n");

    printf("4.Display\n");

    printf("5.Quit\n");

    printf("Enter your choice : ");scanf("%d",&choice);

    switch(choice)

    {

    case 1:

    insert_right();

    break;

  • 8/6/2019 34482550 IGNOU MCA 2nd Semster Data Structure Lab Record Solved MCSL 025

    32/74

    case 2:

    delete_left();

    break;

    case 3:

    delete_right();

    break;

    case 4:

    display_queue();

    break;

    case 5:

    exit();

    default:

    printf("Wrong choice\n");

    }/*End of switch*/

    }/*End of while*/

    }/*End of input_que() */output_que()

    {

    int choice;

    while(1)

    {

    printf("1.Insert at right\n");

    printf("2.Insert at left\n");

    printf("3.Delete from left\n");

    printf("4.Display\n");

    printf("5.Quit\n");printf("Enter your choice : ");

    scanf("%d",&choice);

    switch(choice)

    {

    case 1:

    insert_right();

    break;

    case 2:

    insert_left();

    break;case 3:

    delete_left();

    break;

    case 4:

    display_queue();

    break;

  • 8/6/2019 34482550 IGNOU MCA 2nd Semster Data Structure Lab Record Solved MCSL 025

    33/74

    case 5:

    exit();

    default:

    printf("Wrong choice\n");

    }/*End of switch*/

    }/*End of while*/

    }/*End of output_que() */

    insert_right()

    {

    int added_item;

    if((left == 0 && right == MAX-1) || (left == right+1))

    {

    printf("Queue Overflow\n");

    return;

    }

    if (left == -1) /* if queue is initially empty */{

    left = 0;

    right = 0;

    }

    else

    if(right == MAX-1) /*right is at last position of queue */

    right = 0;

    else

    right = right+1;

    printf("Input the element for adding in queue : ");scanf("%d", &added_item);

    deque_arr[right] = added_item ;

    }/*End of insert_right()*/

    insert_left()

    {

    int added_item;

    if((left == 0 && right == MAX-1) || (left == right+1))

    {

    printf("Queue Overflow \n");

    return;}

    if (left == -1)/*If queue is initially empty*/

    {

    left = 0;

    right = 0;

    }

  • 8/6/2019 34482550 IGNOU MCA 2nd Semster Data Structure Lab Record Solved MCSL 025

    34/74

  • 8/6/2019 34482550 IGNOU MCA 2nd Semster Data Structure Lab Record Solved MCSL 025

    35/74

    if(right == 0)

    right=MAX-1;

    else

    right=right-1;

    }/*End of delete_right() */

    display_queue()

    {

    int front_pos = left,rear_pos = right;

    if(left == -1)

    {

    printf("Queue is empty\n");

    return;

    }

    printf("Queue elements :\n");

    if( front_pos

  • 8/6/2019 34482550 IGNOU MCA 2nd Semster Data Structure Lab Record Solved MCSL 025

    36/74

    1.Insert at right

    2.Delete from left

    3.Delete from right

    4.Display

    5.Quit

    Enter your choice : 1

    Input the element for adding in queue : 12

    1.Insert at right

    2.Delete from left

    3.Delete from right

    4.Display

    5.Quit

    Enter your choice : 1

    Input the element for adding in queue : 24

    1.Insert at right

    2.Delete from left3.Delete from right

    4.Display

    5.Quit

    Enter your choice : 2

    Element deleted from queue is : 12

    1.Insert at right

    2.Delete from left

    3.Delete from right

    4.Display

    5.QuitEnter your choice : 4

    Queue elements :

    24

    1.Insert at right

    2.Delete from left

    3.Delete from right

    4.Display

    5.Quit

    Enter your choice : 3

    Element deleted from queue is : 241.Insert at right

    2.Delete from left

    3.Delete from right

    4.Display

    5.Quit

    Enter your choice : 4

  • 8/6/2019 34482550 IGNOU MCA 2nd Semster Data Structure Lab Record Solved MCSL 025

    37/74

    Queue is empty

    1.Insert at right

    2.Delete from left

    3.Delete from right

    4.Display

    5.Quit

    Enter your choice :5

    1.Input restricted dequeue

    2.Output restricted dequeue

    Enter your choice : 2

    1.Insert at right

    2.Insert at left

    3.Delete from left

    4.Display

    5.Quit

    Enter your choice : 1Input the element for adding in queue : 12

    1.Insert at right

    2.Insert at left

    3.Delete from left

    4.Display

    5.Quit

    Enter your choice : 2

    Input the element for adding in queue : 24

    1.Insert at right

    2.Insert at left3.Delete from left

    4.Display

    5.Quit

    Enter your choice : 4

    Queue elements :

    24 12

    1.Insert at right

    2.Insert at left

    3.Delete from left

    4.Display5.Quit

    Enter your choice : 3

    Element deleted from queue is : 24

    1.Insert at right

    2.Insert at left

    3.Delete from left

  • 8/6/2019 34482550 IGNOU MCA 2nd Semster Data Structure Lab Record Solved MCSL 025

    38/74

    4.Display

    5.Quit

    Enter your choice : 5

    AIM:

    Implement a Dequeue using pointers

    PROGRAM:

    /*to implemnet a dequ using linked list **/

    #include

    #include

    struct node

    {

    int data;

    struct node *link;};void addqatend(struct node**,struct node**,int);

    void addqatbeg(struct node**,struct node**,int);

    delqatbeg(struct node**,struct node**);

    delqatend(struct node**,struct node**);

    main()

    {

    struct node*front,*rear;

    int item;

    front=rear=NULL;

    addqatend(&front,&rear,11);addqatend(&front,&rear,81);

    addqatend(&front,&rear,16);

    addqatend(&front,&rear,45);

    clrscr();

    q_display(front);

    printf("\nnumber of elements in the que=%d",count(front));

    printf("\nitems taken from q\n");

    item=delqatbeg(&front,&rear);

    printf("%d ",item);

    printf("\nafter deletion\n");q_display(front);

    getch();

    }

    /*adds a new element at end **/

    void addqatend(struct node **f,struct node **r,int item)

    {

  • 8/6/2019 34482550 IGNOU MCA 2nd Semster Data Structure Lab Record Solved MCSL 025

    39/74

    struct node *q;

    q=(struct node*)malloc(sizeof(struct node));

    q->data=item;

    q->link=NULL;

    /**if q empty**/

    if(*f==NULL)

    *f=q;

    else

    (*r)->link=q;

    *r=q;

    }

    /*add at begin**/

    void addqatbeg(struct node** f,struct node** r,int item)

    {

    struct node *q;

    int t;q=(struct node*)malloc(sizeof(struct node));

    q->data=item;

    q->link=NULL;

    /**if q empty**/

    if(*f==NULL)

    *f=*r=q;

    else

    q->link=*f;

    *r=*f;

    *f=q;}

    /*remove from front ***/

    delqatbeg(struct node** f,struct node** r)

    {

    struct node *q; int item;

    if(*f==NULL)

    printf("q empty");

    else

    q=*f;item=q->data;

    *f=q->link; free(q);/*if q becom empty after delet*/

    if(*f==NULL)

    *r=NULL;

    return item;

    }

    /*remove from rear end ***/

  • 8/6/2019 34482550 IGNOU MCA 2nd Semster Data Structure Lab Record Solved MCSL 025

    40/74

    delqatend(struct node** f,struct node** r)

    {

    struct node *q,*rleft,*temp; int item;

    temp=*f;

    if(*r==NULL)

    printf("q empty");

    else

    /*traverse q to find the prevous element adrs*/

    while(temp!=*r)

    {rleft=temp; temp=temp->link;}

    /*delete the node*/

    q=*r;item=q->data;

    free(q);

    *r=rleft;

    (*r)->link=NULL;

    /*if q becom empty after delet*/if(*r==NULL)

    *f=NULL;

    return item;

    }

    /*to display**/

    q_display(struct node *q)

    {

    printf("\nfront->");

    while(q!=NULL)

    {if(q->link==NULL)

    printf("

  • 8/6/2019 34482550 IGNOU MCA 2nd Semster Data Structure Lab Record Solved MCSL 025

    41/74

    front->11 81 16 81 16

  • 8/6/2019 34482550 IGNOU MCA 2nd Semster Data Structure Lab Record Solved MCSL 025

    42/74

    clrscr();

    add();

    display();

    getch();

    }

    OUTPUT:

    Enter numbers(5 only)

    num=1

    num=2

    num=3

    num=4

    num=5

    The numbers in reverse order are:

    5

    4

    32

    1

    AIM:

    Implement a queue using two stacks

    ALGORITHM:

    137.START

    138.Declare 2 stacks

    139.Set top=top1=front=rear=0140.While top0)

    {

    scanf("%d",&n);

    stak1[top]=n;

  • 8/6/2019 34482550 IGNOU MCA 2nd Semster Data Structure Lab Record Solved MCSL 025

    43/74

    top++;

    }

    while(top0)

    n=stak2[top1];

    top1--;

    }/*display elements*/

    void display()

    {

    int i=top1;

    while(i>0)

    {

    printf("\n%d",stak2[i]);

    i++;

    }

    }main()

    {

    printf("\nEnter 10 numbers\n");

    add();

    display();

    del();

    printf("Elements in the que after deleting first ele\n");

    display();

    del();

    printf("\nElements after deleting second ele\n");display();

    getch();

    }

    OUTPUT:

    Enter 10 numbers to the stuk

    1 2 3 4 5 6 7 8 9 0

  • 8/6/2019 34482550 IGNOU MCA 2nd Semster Data Structure Lab Record Solved MCSL 025

    44/74

    Elements in the que:

    1 2 3 4 5 6 7 8 9 0

    Elements in the que after deleting first element

    2 3 4 5 6 7 8 9 0

    Elements after deleting 2nd element

    3 4 5 6 7 8 0

    AIM:

    Insert 15,25,2,4,3,1,50 into an initially empty AVL tree

    PROGRAM:

    /*Program for insertion in AVL tree*/

    #include

    #include

    typedef enum { FALSE ,TRUE } bool;struct node

    {

    int info;

    int balance;

    struct node *lchild;

    struct node *rchild;

    };

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

    struct node* search(struct node *,int);

    main(){

    bool ht_inc;

    int info ;

    int choice;

    struct node *root = (struct node *)malloc(sizeof(struct node));

    root = NULL;

    clrscr();

    while(1)

    {

    printf("1.Insert\n");printf("2.Display\n");

    printf("3.Quit\n");

    printf("Enter your choice : ");

    scanf("%d",&choice);

    switch(choice)

    {

  • 8/6/2019 34482550 IGNOU MCA 2nd Semster Data Structure Lab Record Solved MCSL 025

    45/74

    case 1:

    printf("Enter the value to be inserted : ");

    scanf("%d", &info);

    if( search(root,info) == NULL )

    root = insert(info, root, &ht_inc);

    else

    printf("Duplicate value ignored\n");

    break;

    case 2:

    if(root==NULL)

    {

    printf("Tree is empty\n");

    continue;

    }

    printf("Tree is :\n");

    display(root, 1);printf("\n\n");

    printf("Inorder Traversal is: ");

    inorder(root);

    printf("\n");

    break;

    case 3:

    exit(1);

    default:

    printf("Wrong choice\n");

    }/*End of switch*/}/*End of while*/

    }/*End of main()*/

    struct node* search(struct node *ptr,int info)

    {

    if(ptr!=NULL)

    if(info < ptr->info)

    ptr=search(ptr->lchild,info);

    else if( info > ptr->info)

    ptr=search(ptr->rchild,info);

    return(ptr);}/*End of search()*/

    struct node *insert (int info, struct node *pptr, int *ht_inc)

    {

    struct node *aptr;

    struct node *bptr;

    if(pptr==NULL)

  • 8/6/2019 34482550 IGNOU MCA 2nd Semster Data Structure Lab Record Solved MCSL 025

    46/74

    {

    pptr = (struct node *) malloc(sizeof(struct node));

    pptr->info = info;

    pptr->lchild = NULL;

    pptr->rchild = NULL;

    pptr->balance = 0;

    *ht_inc = TRUE;

    return (pptr);

    }

    if(info < pptr->info)

    {

    pptr->lchild = insert(info, pptr->lchild, ht_inc);

    if(*ht_inc==TRUE)

    {

    switch(pptr->balance)

    {case -1: /* Right heavy */

    pptr->balance = 0;

    *ht_inc = FALSE;

    break;

    case 0: /* Balanced */

    pptr->balance = 1;

    break;

    case 1: /* Left heavy */

    aptr = pptr->lchild;

    if(aptr->balance == 1){

    printf("Left to Left Rotation\n");

    pptr->lchild= aptr->rchild;

    aptr->rchild = pptr;

    pptr->balance = 0;

    aptr->balance=0;

    pptr = aptr;

    }

    else

    {printf("Left to right rotation\n");

    bptr = aptr->rchild;

    aptr->rchild = bptr->lchild;

    bptr->lchild = aptr;

    pptr->lchild = bptr->rchild;

    bptr->rchild = pptr;

  • 8/6/2019 34482550 IGNOU MCA 2nd Semster Data Structure Lab Record Solved MCSL 025

    47/74

    if(bptr->balance == 1 )

    pptr->balance = -1;

    else

    pptr->balance = 0;

    if(bptr->balance == -1)

    aptr->balance = 1;

    else

    aptr->balance = 0;

    bptr->balance=0;

    pptr=bptr;

    }

    *ht_inc = FALSE;

    }/*End of switch */

    }/*End of if */

    }/*End of if*/if(info > pptr->info)

    {

    Pptr->rchild = insert(info, pptr->rchild, ht_inc);

    if(*ht_inc==TRUE)

    {

    switch(pptr->balance)

    {

    case 1: /* Left heavy */

    pptr->balance = 0;

    *ht_inc = FALSE;break;

    case 0: /* Balanced */

    pptr->balance = -1;

    break;

    case -1: /* Right heavy */

    aptr = pptr->rchild;

    if(aptr->balance == -1)

    {

    printf("Right to Right Rotation\n");

    pptr->rchild= aptr->lchild;aptr->lchild = pptr;

    pptr->balance = 0;

    aptr->balance=0;

    pptr = aptr;

    }

    else

  • 8/6/2019 34482550 IGNOU MCA 2nd Semster Data Structure Lab Record Solved MCSL 025

    48/74

    {

    printf("Right to Left Rotation\n");

    bptr = aptr->lchild;

    aptr->lchild = bptr->rchild;

    bptr->rchild = aptr;

    pptr->rchild = bptr->lchild;

    bptr->lchild = pptr;

    if(bptr->balance == -1)

    pptr->balance = 1;

    else

    pptr->balance = 0;

    if(bptr->balance == 1)

    aptr->balance = -1;

    else

    aptr->balance = 0;bptr->balance=0;

    pptr = bptr;

    }/*End of else*/

    *ht_inc = FALSE;

    }/*End of switch */

    }/*End of if*/

    }/*End of if*/

    return(pptr);

    }/*End of insert()*/display(struct node *ptr,int level)

    {

    int i;

    if ( ptr!=NULL )

    {

    display(ptr->rchild, level+1);

    printf("\n");

    for (i = 0; i < level; i++)

    printf(" ");

    printf("%d", ptr->info);display(ptr->lchild, level+1);

    }/*End of if*/

    }/*End of display()*/

    inorder(struct node *ptr)

    {

  • 8/6/2019 34482550 IGNOU MCA 2nd Semster Data Structure Lab Record Solved MCSL 025

    49/74

    if(ptr!=NULL)

    {

    inorder(ptr->lchild);

    printf("%d ",ptr->info);

    inorder(ptr->rchild);

    }

    }/*End of inorder()*/

    OUTPUT

    1.Insert

    2.Display

    3.Quit

    Enter your choice : 1

    Enter the value to be inserted : 25

    1.Insert

    2.Display

    3.QuitEnter your choice : 1

    Enter the value to be inserted : 2

    1.Insert

    2.Display

    3.Quit

    Enter your choice : 1

    Enter the value to be inserted : 4

    1.Insert

    2.Display

    3.QuitEnter your choice : 1

    Enter the value to be inserted : 3

    Right to Left Rotation

    1.Insert

    2.Display

    3.Quit

    Enter your choice : 1

    Enter the value to be inserted : 1

    Left to Left Rotation

    1.Insert2.Display

    3.Quit

    Enter your choice : 1

    Enter the value to be inserted : 50

    1.Insert

    2.Display

  • 8/6/2019 34482550 IGNOU MCA 2nd Semster Data Structure Lab Record Solved MCSL 025

    50/74

    3.Quit

    Enter your choice : 2

    Tree is :

    50

    25

    15

    4

    3

    2

    1

    Inorder Traversal is: 1 2 3 4 15 25 50

    1.Insert

    2.Display

    3.Quit

    Enter your choice :3

    AIM:

    Implement Dijkstras algorithm

    PROGRAM:

    /* Program of shortest path between two node in graph using Djikstra algorithm */

    #include

    #define MAX 10

    #define TEMP 0

    #define PERM 1

    #define infinity 9999struct node

    {

    int predecessor;

    int dist; /*minimum distance of node from source*/

    int status;

    };

    int adj[MAX][MAX];

    int n;

    void main()

    {int i,j;

    int source,dest;

    int path[MAX];

    int shortdist,count;

    clrscr();

    create_graph();

  • 8/6/2019 34482550 IGNOU MCA 2nd Semster Data Structure Lab Record Solved MCSL 025

    51/74

    printf("The adjacency matrix is :\n");

    display();

    while(1)

    {

    printf("Enter source node(0 to quit) : ");

    scanf("%d",&source);

    printf("Enter destination node(0 to quit) : ");

    scanf("%d",&dest);

    if(source==0 || dest==0)

    exit(1);

    count = findpath(source,dest,path,&shortdist);

    if(shortdist!=0)

    {

    printf("Shortest distance is : %d\n", shortdist);

    printf("Shortest Path is : ");

    for(i=count;i>1;i--)printf("%d->",path[i]);

    printf("%d",path[i]);

    printf("\n");

    }

    else

    printf("There is no path from source to destination node\n");

    }/*End of while*/

    }/*End of main()*/

    create_graph()

    {int i,max_edges,origin,destin,wt;

    printf("Enter number of vertices : ");

    scanf("%d",&n);

    max_edges=n*(n-1);

    for(i=1;i n || destin > n || origin

  • 8/6/2019 34482550 IGNOU MCA 2nd Semster Data Structure Lab Record Solved MCSL 025

    52/74

  • 8/6/2019 34482550 IGNOU MCA 2nd Semster Data Structure Lab Record Solved MCSL 025

    53/74

    if( newdist < state[i].dist )

    {

    state[i].predecessor = current;

    state[i].dist = newdist;

    }

    }

    }/*End of for*/

    /*Search for temporary node with minimum distand make it current node*/

    min=infinity;

    current=0;

    for(i=1;i1;i--)

    {

    u=path[i];

    v=path[i-1];

    *sdist+= adj[u][v];}

    return (count);

    }/*End of findpath()*/

    OUTPUT

    Enter number of vertices : 4

    Enter edge 1(0 0 to quit) : 1 2

  • 8/6/2019 34482550 IGNOU MCA 2nd Semster Data Structure Lab Record Solved MCSL 025

    54/74

    Enter weight for this edge : 1

    Enter edge 2(0 0 to quit) : 2 3

    Enter weight for this edge : 2

    Enter edge 3(0 0 to quit) : 3 4

    Enter weight for this edge : 3

    Enter edge 4(0 0 to quit) : 4 1

    Enter weight for this edge : 4

    Enter edge 5(0 0 to quit) : 0 0

    The adjacency matrix is :

    0 1 0 0

    0 0 2 0

    0 0 0 3

    4 0 0 0

    Enter source node(0 to quit) : 1

    Enter destination node(0 to quit) : 4

    Shortest distance is : 6Shortest Path is : 1->2->3->4

    Enter source node(0 to quit) :0

    AIM:

    Implement Kruskals algorithm

    PROGRAM

    /* Program for creating a minimum spanning tree from Kruskal's algorithm */

    #include

    #define MAX 20struct edge

    {

    int u;

    int v;

    int weight;

    struct edge *link;

    }*front = NULL;

    int father[MAX]; /*Holds father of each node */

    struct edge tree[MAX]; /* Will contain the edges of spanning tree */

    int n; /*Denotes total number of nodes in the graph */int wt_tree=0; /*Weight of the spanning tree */

    int count=0; /* Denotes number of edges included in the tree */

    /* Functions */

    void make_tree();

    void insert_tree(int i,int j,int wt);

    void insert_pque(int i,int j,int wt);

  • 8/6/2019 34482550 IGNOU MCA 2nd Semster Data Structure Lab Record Solved MCSL 025

    55/74

    struct edge *del_pque();

    main()

    {

    int i;

    clrscr();

    create_graph();

    make_tree();

    printf("Edges to be included in spanning tree are :\n");

    for(i=1;i n || origin

  • 8/6/2019 34482550 IGNOU MCA 2nd Semster Data Structure Lab Record Solved MCSL 025

    56/74

    }

    }/*End of create_graph()*/

    void make_tree()

    {

    struct edge *tmp;

    int node1,node2,root_n1,root_n2;

    while( count < n-1) /*Loop till n-1 edges included in the tree*/

    {

    tmp=del_pque();

    node1=tmp->u;

    node2=tmp->v;

    printf("n1=%d ",node1);

    printf("n2=%d ",node2);

    while( node1 > 0)

    {

    root_n1=node1;node1=father[node1];

    }

    while( node2 >0 )

    {

    root_n2=node2;

    node2=father[node2];

    }

    printf("rootn1=%d ",root_n1);

    printf("rootn2=%d\n",root_n2);

    if(root_n1!=root_n2){

    insert_tree(tmp->u,tmp->v,tmp->weight);

    wt_tree=wt_tree+tmp->weight;

    father[root_n2]=root_n1;

    }

    }/*End of while*/

    }/*End of make_tree()*/

    /*Inserting an edge in the tree */

    void insert_tree(int i,int j,int wt)

    {printf("This edge inserted in the spanning tree\n");

    count++;

    tree[count].u=i;

    tree[count].v=j;

    tree[count].weight=wt;

    }/*End of insert_tree()*/

  • 8/6/2019 34482550 IGNOU MCA 2nd Semster Data Structure Lab Record Solved MCSL 025

    57/74

    /*Inserting edges in the priority queue */

    void insert_pque(int i,int j,int wt)

    {

    struct edge *tmp,*q;

    tmp = (struct edge *)malloc(sizeof(struct edge));

    tmp->u=i;

    tmp->v=j;

    tmp->weight = wt;

    /*Queue is empty or edge to be added has weight less than first edge*/

    if( front == NULL || tmp->weight < front->weight )

    {

    tmp->link = front;

    front = tmp;

    }

    else

    {q = front;

    while( q->link != NULL && q->link->weight weight )

    q=q->link;

    tmp->link = q->link;

    q->link = tmp;

    if(q->link == NULL) /*Edge to be added at the end*/

    tmp->link = NULL;

    }/*End of else*/

    }/*End of insert_pque()*/

    /*Deleting an edge from the priority queue*/struct edge *del_pque()

    {

    struct edge *tmp;

    tmp = front;

    printf("Edge processed is %d->%d %d\n",tmp->u,tmp->v,tmp->weight);

    front = front->link;

    return tmp;

    }/*End of del_pque()*/

    OUTPUT:

    Enter number of nodes : 5Enter edge 1(0 0 to quit): 1 2

    Enter weight for this edge : 2

    Enter edge 2(0 0 to quit): 2 3

    Enter weight for this edge : 3

    Enter edge 3(0 0 to quit): 3 4

    Enter weight for this edge : 4

  • 8/6/2019 34482550 IGNOU MCA 2nd Semster Data Structure Lab Record Solved MCSL 025

    58/74

    Enter edge 4(0 0 to quit): 4 5

    Enter weight for this edge : 5

    Enter edge 5(0 0 to quit): 5 6

    Enter weight for this edge : 6

    Invalid edge!

    Enter edge 5(0 0 to quit): 5 1

    Enter weight for this edge : 6

    Enter edge 6(0 0 to quit): 0 0

    Edge processed is 1->2 2

    n1=1 n2=2 rootn1=1 rootn2=2

    This edge inserted in the spanning tree

    Edge processed is 2->3 3

    n1=2 n2=3 rootn1=1 rootn2=3

    This edge inserted in the spanning tree

    Edge processed is 3->4 4

    n1=3 n2=4 rootn1=1 rootn2=4This edge inserted in the spanning tree

    Edge processed is 4->5 5

    n1=4 n2=5 rootn1=1 rootn2=5

    This edge inserted in the spanning tree

    Edges to be included in spanning tree are :

    1->2

    2->3

    3->4

    4->5

    Weight of this minimum spanning tree is : 14

    AIM:

    Implement linear search

    PROGRAM:

    /*Write a program for linear searching*/

    # include

    main()

    {

    int arr[20],n,i,item;clrscr();

    printf("How many elements you want to enter in the array : ");

    scanf("%d",&n);

    for(i=0; i < n;i++)

    {

  • 8/6/2019 34482550 IGNOU MCA 2nd Semster Data Structure Lab Record Solved MCSL 025

    59/74

    printf("Enter element %d : ",i+1);

    scanf("%d", &arr[i]);

    }

    printf("Enter the element to be searched : ");

    scanf("%d",&item);

    for(i=0;i < n;i++)

    {

    if(item == arr[i])

    {

    printf("%d found at position %d\n",item,i+1);

    break;

    }

    }/*End of for*/

    if(i == n)

    printf("Item %d not found in array\n",item);

    getch();}

    OUTPUT:

    How many elements you want to enter in the array : 5

    Enter element 1 : 45

    Enter element 2 : 98

    Enter element 3 : 75

    Enter element 4 : 86

    Enter element 5 : 42

    Enter the element to be searched : 75

    75 found at position 3

    AIM:

    Implement binary search using pointers:

    PROGRAM:

    /*binary search using pointers*/

    # include

    # include

    void main()

    {int search(int *,int,int,int,int *);

    int arr[]={0,1,2,3,4,5,7,12,53,31,78,87,65,45,100,200};

    int i,j,n=15,temp,num,pos;

    char ans;

    clrscr();

    printf("Do u want to enter values to array automaticaly y/n:");

  • 8/6/2019 34482550 IGNOU MCA 2nd Semster Data Structure Lab Record Solved MCSL 025

    60/74

    scanf("%c",&ans);

    if(ans=='n')

    {

    printf("Enter number of elts, max is 15 :");

    scanf("%d",&n);

    printf("Enter %d elements...\n",n);

    for(i=0;i

  • 8/6/2019 34482550 IGNOU MCA 2nd Semster Data Structure Lab Record Solved MCSL 025

    61/74

    {

    *pos=mid;

    return(1);

    }

    if(*(arr+mid)> num)

    {

    return( search(arr,mid+1,epos,num,pos) );

    }

    if(*(arr+mid) < num)

    {

    return( search(arr,spos,mid-1,num,pos) );

    }

    }

    OUTPUT:

    Do u want to enter values to array automaticaly y/n:y

    Entered array after sorting is...100 87 78 65 53 45 31 12 7 5 4 3 2 1 0

    Enter the number to be searched:5

    Entered number 5 found at position 10

    Search again y/n :n

    AIM:

    Implement Quick sort

    PROGRAM:

    /****QUICK SORT ***********/#include

    #include

    int n;

    qsort(int b[],int left, int right)

    {

    int i,j,p,tmp,finished,k;

    if(right>left)

    {

    i=left;

    j=right;p=b[left];

    printf("the partitioning element is :%d\n",p);

    finished=0;

    while(!finished)

    {

    do

  • 8/6/2019 34482550 IGNOU MCA 2nd Semster Data Structure Lab Record Solved MCSL 025

    62/74

    {

    ++i;

    }while((b[i]left))

    --j;

    if(j

  • 8/6/2019 34482550 IGNOU MCA 2nd Semster Data Structure Lab Record Solved MCSL 025

    63/74

    qsort(a,l,r);

    printf("result\n");

    for(i=0;i

  • 8/6/2019 34482550 IGNOU MCA 2nd Semster Data Structure Lab Record Solved MCSL 025

    64/74

    heap_sort();

    printf("Sorted list is :\n");

    display();

    getch();

    }/*End of main()*/

    display()

    { int i;

    for(i=0;i

  • 8/6/2019 34482550 IGNOU MCA 2nd Semster Data Structure Lab Record Solved MCSL 025

    65/74

    i=0; /*Since every time we have to replace root with last*/

    /*Exchange last element with the root */

    temp=arr[i];

    arr[i]=arr[last];

    arr[last]=temp;

    left=2*i+1; /*left child of root*/

    right=2*i+2;/*right child of root*/

    while( right < last)

    {

    if( arr[i]>=arr[left] && arr[i]>=arr[right] )

    return;

    if( arr[right]

  • 8/6/2019 34482550 IGNOU MCA 2nd Semster Data Structure Lab Record Solved MCSL 025

    66/74

    Enter element 6 : 7

    Enter element 7 : 3

    Enter element 8 : 2

    Enter element 9 : 1

    Enter element 10 : 0

    Entered list is :

    5 6 4 8 9 7 3 2 1 0

    Heap is :

    9 8 7 5 6 4 3 2 1 0

    Sorted list is :

    0 1 2 3 4 5 6 7 8 9

    AIM:

    Implement Merge sort

    PROGRAM:

    /* Program of sorting using merge sort through recursion*/

    #include

    #define MAX 20

    int array[MAX];

    main()

    {

    int i,n;

    clrscr();

    printf("Enter the number of elements : ");

    scanf("%d",&n);for(i=0;i

  • 8/6/2019 34482550 IGNOU MCA 2nd Semster Data Structure Lab Record Solved MCSL 025

    67/74

    {

    int mid;

    if( low != high )

    {

    mid = (low+high)/2;

    merge_sort( low , mid );

    merge_sort( mid+1, high );

    merge( low, mid, high );

    }

    }/*End of merge_sort*/

    merge( int low, int mid, int high )

    {

    int temp[MAX];

    int i = low;

    int j = mid +1 ;

    int k = low ;

    while( (i

  • 8/6/2019 34482550 IGNOU MCA 2nd Semster Data Structure Lab Record Solved MCSL 025

    68/74

    88 456 754 785 956

    PROGRAM

    /* Program of sorting using merge sort without recursion*/

    #include

    #define MAX 30

    main()

    {

    int arr[MAX],temp[MAX],i,j,k,n,size,l1,h1,l2,h2;

    clrscr();

    printf("Enter the number of elements : ");

    scanf("%d",&n);

    for(i=0;i

  • 8/6/2019 34482550 IGNOU MCA 2nd Semster Data Structure Lab Record Solved MCSL 025

    69/74

    }

    while(i

  • 8/6/2019 34482550 IGNOU MCA 2nd Semster Data Structure Lab Record Solved MCSL 025

    70/74

    AIM:

    Implement Bubble sort

    PROGRAM:

    /* Program of sorting using bubble sort */

    #include

    #define MAX 20

    main()

    {

    int arr[MAX],i,j,k,temp,n,xchanges;

    clrscr();

    printf("Enter the number of elements : ");

    scanf("%d",&n);

    for (i = 0; i < n; i++)

    {

    printf("Enter element %d : ",i+1);

    scanf("%d",&arr[i]);}

    printf("Unsorted list is :\n");

    for (i = 0; i < n; i++)

    printf("%d ", arr[i]);

    printf("\n");

    /* Bubble sort*/

    for (i = 0; i < n-1 ; i++)

    {

    xchanges=0;for (j = 0; j arr[j+1])

    {

    temp = arr[j];

    arr[j] = arr[j+1];

    arr[j+1] = temp;

    xchanges++;

    }/*End of if*/

    }/*End of inner for loop*/if(xchanges==0) /*If list is sorted*/

    break;

    printf("After Pass %d elements are : ",i+1);

    for (k = 0; k < n; k++)

    printf("%d ", arr[k]);

    printf("\n");

  • 8/6/2019 34482550 IGNOU MCA 2nd Semster Data Structure Lab Record Solved MCSL 025

    71/74

    }/*End of outer for loop*/

    printf("Sorted list is :\n");

    for (i = 0; i < n; i++)

    printf("%d ", arr[i]);

    printf("\n");

    getch();

    }/*End of main()*/

    OUTPUT:

    Enter the number of elements : 4

    Enter element 1 : 45

    Enter element 2 : 88

    Enter element 3 : 75

    Enter element 4 : 6

    Unsorted list is :

    45 88 75 6

    After Pass 1 elements are : 45 75 6 88After Pass 2 elements are : 45 6 75 88

    After Pass 3 elements are : 6 45 75 88

    Sorted list is :

    6 45 75 88

    AIM:

    Implement Topological sort

    PROGRAM:

    /* Program for topological sorting */#include

    #define MAX 20

    int n,adj[MAX][MAX];

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

    main()

    {

    int i,j=0,k;

    int topsort[MAX],indeg[MAX];

    clrscr();

    create_graph();printf("The adjacency matrix is :\n");

    display();

    /*Find the indegree of each node*/

    for(i=1;i

  • 8/6/2019 34482550 IGNOU MCA 2nd Semster Data Structure Lab Record Solved MCSL 025

    72/74

    if( indeg[i]==0 )

    insert_queue(i);

    }

    while(front

  • 8/6/2019 34482550 IGNOU MCA 2nd Semster Data Structure Lab Record Solved MCSL 025

    73/74

    else

    adj[origin][destin]=1;

    }/*End of for*/

    }/*End of create_graph()*/

    display()

    {

    int i,j;

    for(i=1;i

  • 8/6/2019 34482550 IGNOU MCA 2nd Semster Data Structure Lab Record Solved MCSL 025

    74/74

    int indegree(int node)

    {

    int i,in_deg=0;

    for(i=1;i