Data Structures & Algorithms Lab - Using 'C' - Solutions 1

download Data Structures & Algorithms Lab - Using 'C' - Solutions 1

of 38

Transcript of Data Structures & Algorithms Lab - Using 'C' - Solutions 1

  • 7/29/2019 Data Structures & Algorithms Lab - Using 'C' - Solutions 1

    1/38

    Data Structures and Algorithms Using C

    Data Structures and Algorithms Lab Using C

    Problems & Solutions

    Note: -

    The following programs are Compiled and Executed in Borland C++

    ver.5.02.

    Page 1

  • 7/29/2019 Data Structures & Algorithms Lab - Using 'C' - Solutions 1

    2/38

    Data Structures and Algorithms Using C

    1. Write a program to determine the Factorial and Binomial Co-efficient of a

    number.

    #include

    #include

    float factorial(int);

    void main(){

    int x,r;

    float b;

    clrscr();printf("\n INTEGER NUMBER = ");

    scanf("%d",&x);

    printf("\n R VALUE = ");

    scanf("%d",&r);clrscr();

    printf("\n FACTORIAL OF A NUMBER");printf("\n ---------------------");

    printf("\n AN INTEGER NUMBER = %2d",x);

    printf("\n FACTORIAL OF %2d IS = %5.1f",x,factorial(x));

    printf("\n\n BINOMIAL CO-EFFICIENT");printf("\n ---------------------");

    b = factorial(x)/(factorial(r)*factorial(x-r));

    printf("\n BINOMIAL CO-EFFICIENT OF %1dC%1d IS = %5.2f",x,r,b);getch();

    }//main end

    float factorial(int n)

    {

    if (n

  • 7/29/2019 Data Structures & Algorithms Lab - Using 'C' - Solutions 1

    3/38

    Data Structures and Algorithms Using C

    2. Write a program to Add two Polynomials, A and B.

    #include#include

    struct pol{float coef;

    int exp;

    };

    void newterm(float,int);

    struct pol terms1[10],terms2[10],terms3[20];

    int af,al,bf,bl,cf,cl,i,n,n1,p,q,d,k=1;

    float c;

    void main(){

    clrscr();

    printf("\nNo.of terms in polynomial A : ");

    scanf("%d",&n);

    for(i=1;i

  • 7/29/2019 Data Structures & Algorithms Lab - Using 'C' - Solutions 1

    4/38

    Data Structures and Algorithms Using C

    p=1,q=1,cf=1;

    al=n,bl=n1;

    while((p

  • 7/29/2019 Data Structures & Algorithms Lab - Using 'C' - Solutions 1

    5/38

    Data Structures and Algorithms Using C

    printf("\n Polynomial B");

    printf("\n 6X^8 + 5X^2 + 2X^1 - 7\n");

    printf("\n Added Polynomial");printf("\n ----------------\n");

    while(cf

  • 7/29/2019 Data Structures & Algorithms Lab - Using 'C' - Solutions 1

    6/38

    Data Structures and Algorithms Using C

    3. Create a Stack and Add elements into the Stack. Delete elements from the

    same Stack.

    #include

    #include

    int stack[50],item;

    int ch,n=0,top,i;

    void main()

    {

    void create();

    void add();void display();

    void delete1();

    do{

    clrscr();printf("\nSTACK OPERATIONS");

    printf("\n----------------");

    printf("\n Your Choice(1-Create 2-Add 3-Delete 4-Display 5-Exit:");

    scanf("%d",&ch);

    if (ch==1)

    {printf("\n Maximum Size of the Stack : ");

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

    switch(ch)

    {

    case 1:create();getch();break;case 2:add();getch();break;

    case 3:delete1();getch();break;

    case 4:display();getch();break;case 5:break;

    default :printf("\n INVALID CHOICE. TRYAGAIN...");

    getch();}

    } while(ch!=5);

    }/*main end*/

    void create()

    Page 6

  • 7/29/2019 Data Structures & Algorithms Lab - Using 'C' - Solutions 1

    7/38

    Data Structures and Algorithms Using C

    {

    printf("\n CREATING A STACK");

    printf("\n ----------------");top=0;

    printf("\n Stack Creation Over");

    return;}

    void add(){

    printf("\n ADDING ELEMENT INTO A STACK");

    printf("\n ---------------------------");

    printf("\n Item that has to be inserted : ");scanf("%d",&item);

    if (top == n)

    { printf("\n STACK FULL CONDITION");

    getch();return;

    }

    top++;stack[top]=item;

    printf("\n Item %d has been inserted into Stack",item);

    printf("\n Items of the Stack are :\n");

    for(i=1;i

  • 7/29/2019 Data Structures & Algorithms Lab - Using 'C' - Solutions 1

    8/38

    Data Structures and Algorithms Using C

    printf("\n Item %d has been deleted from Stack",item);

    printf("\n Items of the Stack are :\n");

    for(i=1;i

  • 7/29/2019 Data Structures & Algorithms Lab - Using 'C' - Solutions 1

    9/38

    Data Structures and Algorithms Using C

    4. Create a Queue and Add elements into the Queue. Delete elements from the

    same Queue.

    #include

    #include

    int queue[50],item;

    int ch,n=0,front,rear,i;

    void main()

    {

    void create();

    void add();void delete1();

    void display();

    do{

    clrscr();printf("\nQUEUE OPERATIONS");

    printf("\n----------------");

    printf("\n Your Choice(1-Create 2-Add 3-Delete 4-Display 5-Exit:");

    scanf("%d",&ch);

    if (ch==1)

    {printf("\n Maximum Size of the Queue : ");

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

    switch(ch)

    {

    case 1:create();getch();break;case 2:add();getch();break;

    case 3:delete1();getch();break;

    case 4:display();getch();break;case 5:break;

    default :printf("\n INVALID CHOICE. TRYAGAIN...");

    getch();}

    }while(ch!=5);

    }/*main end*/

    void create()

    Page 9

  • 7/29/2019 Data Structures & Algorithms Lab - Using 'C' - Solutions 1

    10/38

    Data Structures and Algorithms Using C

    {

    printf("\n CREATING A QUEUE");

    printf("\n ----------------");front=0;rear=0;

    printf("\n Queue Creation Over");

    return;}

    void add(){

    printf("\n ADDING ELEMENT INTO A QUEUE");

    printf("\n ---------------------------");

    printf("\n Item that has to be inserted : ");scanf("%d",&item);

    if (rear == n)

    { printf("\n QUEUE FULL CONDITION");

    getch();return;

    }

    rear++;queue[rear]=item;

    printf("\n Item %d has been inserted into Queue",item);

    printf("\n Items of the Queue are :\n");

    for(i=front+1;i

  • 7/29/2019 Data Structures & Algorithms Lab - Using 'C' - Solutions 1

    11/38

    Data Structures and Algorithms Using C

    front++;

    item=queue[front];

    front++;printf("\n Item %d has been deleted from Queue",item);

    printf("\n Items of the Queue are :\n");

    for(i=front;i

  • 7/29/2019 Data Structures & Algorithms Lab - Using 'C' - Solutions 1

    12/38

    Data Structures and Algorithms Using C

    5. To Traverse a Binary Tree in

    a. In-order form

    b. Pre-order form

    c. Post-order form

    #include#include

    #include

    struct treerec

    {

    struct treerec *lchild;char data;

    struct treerec *rchild;

    };

    struct treerec *item;

    struct treerec *rdata[50];

    main()

    {

    int i,n;clrscr();

    printf("\n No. of Numbers : ");

    scanf("%d",&n);printf("\n Enter Data of the Tree : \n");

    for(i=1;idata);

    rdata[i]->lchild=NULL;rdata[i]->rchild=NULL;

    }

    for(i=1;ilchild = rdata[2*i];

    rdata[i]->rchild = rdata[2*i+1];}

    item=rdata[1];

    Page 12

  • 7/29/2019 Data Structures & Algorithms Lab - Using 'C' - Solutions 1

    13/38

    Data Structures and Algorithms Using C

    clrscr();

    printf("\n TREE TRAVERSALS");

    printf("\n ---------------");printf("\n GIVEN DATA IN TREE");

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

    for(i=1;idata);

    printf("\n\n");printf("\n INORDER TREE TRAVERSAL");

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

    inorder(item);

    printf("\n PREORDER TREE TRAVERSAL");printf("\n -----------------------\n\t");

    preorder(item);

    printf("\n POSTORDER TREE TRAVERSAL");

    printf("\n ------------------------\n\t");postorder(item);

    getch();

    }/*main end*/

    inorder(cnode)

    struct treerec *cnode;

    {if (cnode!=NULL)

    { inorder(cnode->lchild);if (cnode->data!='-') printf("%c",cnode->data);

    inorder(cnode->rchild);

    }

    return(1);}

    preorder(cnode)struct treerec *cnode;

    {

    if (cnode!=NULL){

    if (cnode->data!='-') printf("%c",cnode->data);

    preorder(cnode->lchild);preorder(cnode->rchild);

    }

    return(1);

    Page 13

  • 7/29/2019 Data Structures & Algorithms Lab - Using 'C' - Solutions 1

    14/38

    Data Structures and Algorithms Using C

    }

    postorder(cnode)struct treerec *cnode;

    {

    if (cnode!=NULL){

    postorder(cnode->lchild);

    postorder(cnode->rchild);if (cnode->data!='-') printf("%c",cnode->data);

    }

    return(1);}

    Page 14

  • 7/29/2019 Data Structures & Algorithms Lab - Using 'C' - Solutions 1

    15/38

    Data Structures and Algorithms Using C

    6. Create a Singly Linked List. Insert elements into the Singly Linked List and

    Delete elements from the same Singly Linked List.

    #include

    #include

    #include

    struct node{

    int no;

    struct node *next;

    };

    struct node *head,*ptr;

    int i,ch;

    void main(){

    void create();

    void add();void display();

    void delete1();

    do

    { clrscr();printf("\n LINKED LIST OPERATIONS");

    printf("\n ----------------------");

    printf("\n\t 1 - CREATION");

    printf("\n\t 2 - ADDITION");printf("\n\t 3 - DELETION");

    printf("\n\t 4 - EXIT");

    printf("\n\n Enter your Choice : ");scanf("%d",&ch);

    switch(ch){

    case 1 : {

    create();display();getch();

    break;

    }

    Page 15

  • 7/29/2019 Data Structures & Algorithms Lab - Using 'C' - Solutions 1

    16/38

    Data Structures and Algorithms Using C

    case 2 : {

    add();display();

    getch();break;

    }

    case 3 : {

    delete1();display();

    getch();break;

    }

    case 4 : break;

    default: printf("\n\t Invalid Choice. TryAgain.");

    getch();

    }/*end switch*/

    }while(ch!=4);

    }/*main end*/

    void create()

    {

    head=NULL;printf("\n A Number (Type 999 to quit) : ");

    scanf("%d",&i);

    while(i!=999)

    {

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

    ptr->no=i;ptr->next=head;

    head=ptr;

    printf("\n A Number (Type 999 to quit) : ");scanf("%d",&i);

    }

    return;

    }/*end create*/

    Page 16

  • 7/29/2019 Data Structures & Algorithms Lab - Using 'C' - Solutions 1

    17/38

    Data Structures and Algorithms Using C

    void add()

    {

    struct node *pnode;int ch;

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

    printf("\n A Number (Type 999 to quit) : ");scanf("%d",&ptr->no);

    printf("\nPlace ahead of (Press 0 if New Item is Last s: ");

    scanf("%d",&ch);

    if (ch==0)

    {

    ptr->next=head;head=ptr;

    }

    else

    {pnode=head;

    while ((pnode->no!=ch) && (pnode))

    pnode=pnode->next;

    ptr->next=pnode->next;pnode->next=ptr;

    }

    return;

    }/*end add*/

    void display()

    {

    getch();clrscr();

    printf("\n\t Contents of Linked List");

    printf("\n\t -----------------------\n");ptr=head;

    while(ptr){

    printf("\n\t Number = %d\n",ptr->no);

    ptr=ptr->next;}

    return;

    }/*end display*/

    void delete1()

    Page 17

  • 7/29/2019 Data Structures & Algorithms Lab - Using 'C' - Solutions 1

    18/38

    Data Structures and Algorithms Using C

    {

    int i;

    char flag,flag1;struct node *pnode;

    printf("\n The Element to be Deleted : ");

    scanf("%d",&i);ptr=head;

    if (ptr->no==i){

    head=ptr->next;

    flag='y';

    }else

    {

    pnode=ptr;

    ptr=ptr->next;

    while((ptr->no!=i) && (ptr)){

    pnode=ptr;

    ptr=ptr->next;

    }

    if(ptr)

    pnode->next=ptr->next;

    flag1='y';}

    if ((flag=='n') || (flag1=='n'))

    printf("\nKey %2d is not found in the Linked List",i);

    return;

    }

    Page 18

  • 7/29/2019 Data Structures & Algorithms Lab - Using 'C' - Solutions 1

    19/38

    Data Structures and Algorithms Using C

    7. Sort a given set of numbers using Bubble Sort technique.

    #include#include

    void main(){

    int i,j,n,pass;

    char change;float temp,x[20];

    clrscr();

    printf("\n No. of numbers : ");

    scanf("%d",&n);

    for(i=1;i

  • 7/29/2019 Data Structures & Algorithms Lab - Using 'C' - Solutions 1

    20/38

    Data Structures and Algorithms Using C

    }/*end if*/

    }/*end for*/for(i=1;i

  • 7/29/2019 Data Structures & Algorithms Lab - Using 'C' - Solutions 1

    21/38

    Data Structures and Algorithms Using C

    8. Write a program to search for a particular number in a given set of numbers

    using Binary Search technique.

    #include

    #include

    void main()

    {

    int f[20],i,j,n,k,l,u,m,temp;char done;

    clrscr();

    printf("\n Enter the no. of numbers : ");

    fflush(stdin);scanf("%d",&n);

    for(i=1;i

  • 7/29/2019 Data Structures & Algorithms Lab - Using 'C' - Solutions 1

    22/38

    Data Structures and Algorithms Using C

    printf("\n\n SORTED LIST");

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

    for(i=1;i

  • 7/29/2019 Data Structures & Algorithms Lab - Using 'C' - Solutions 1

    23/38

    Data Structures and Algorithms Using C

    9. Given a String,

    a. Find the length of the String

    b. Concatenate with another String

    #include#include

    #include

    void main()

    {

    char str1[35],str2[35],str3[70];int i,j;

    for(i=0;i

  • 7/29/2019 Data Structures & Algorithms Lab - Using 'C' - Solutions 1

    24/38

    Data Structures and Algorithms Using C

    10. Write a program to delete a portion of a given String.

    #include#include

    void main()

    {char str[55],str1[55];

    int i,j,s,e;

    printf("\n\n Enter the Text : ");gets(str);

    printf("\n Starting - Ending position of text deleted");

    scanf("%d%d",&s,&e);

    clrscr();printf("\n STRING MANIPULATION");

    printf("\n -------------------\n");

    printf("\n\n Given Text");

    printf("\n ----------");printf("\n %s\n",str);

    printf("\n\n Starting-Ending Position of Text to be Deleted(S-E):%d-%d",s,e);printf("\n\n String After Deletion");

    printf("\n ---------------------\n ");

    j=0;

    for(i=0;str[i]!='\0';i++)

    if ((i(e-1))){

    str1[j]=str[i];j++;}

    str1[j]='\0';

    for(i=0;str1[i]!='\0';i++)

    printf("%c",str1[i]);

    getch();

    }/*main end*/

    Page 24

  • 7/29/2019 Data Structures & Algorithms Lab - Using 'C' - Solutions 1

    25/38

    Data Structures and Algorithms Using C

    11. Count the number of characters in a given String and reverse the String.

    #include#include

    #include

    void main()

    {

    char x,str[55];int i,charcnt=0,wordcnt=0,chcnt=0;

    printf("\n Enter the Text : ");

    gets(str);

    printf("\n Enter the Character : ");scanf("%c",&x);

    for(i=0;str[i]!='\0';i++)

    { charcnt++;

    if

    ((str[i]==x)||(str[i]==toupper(x))||(toupper(str[i])==x))

    chcnt++;

    if (str[i]==' ')

    wordcnt++;

    }

    clrscr();printf("\n\t STRING MANIPULATION");printf("\n\t -------------------\n");

    printf("\n\t GIVEN TEXT");

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

    printf("\n\t %s\n",str);printf("\n\t OCCURENCES OF CHARACTER %c = %d",x,chcnt);

    printf("\n\n\t NO. OF WORDS = %d\n\n",wordcnt+1);

    printf("\n\t REVERSED TEXT");printf("\n\t -------------\n\n\t");

    for(i=charcnt;i>=0;i--)printf("%c",str[i]);

    getch();

    }/*main end*/

    Page 25

  • 7/29/2019 Data Structures & Algorithms Lab - Using 'C' - Solutions 1

    26/38

    Data Structures and Algorithms Using C

    12. Given an Employee number, Employee name, Department and Basic pay,

    prepare a pay slip for that Employee using Files.

    #include

    #include

    struct emp

    {

    int eno;char ename[30];

    char dept[15];

    float basic;

    };

    main()

    {

    struct emp employee;

    FILE *fp;float da,hra,cca,pf,lic,gp,np;

    int n,i;

    fp=fopen("emp.dat","w");

    clrscr();printf("\n Enter the no. of Employees : ");

    scanf("%d",&n);

    for(i=1;i

  • 7/29/2019 Data Structures & Algorithms Lab - Using 'C' - Solutions 1

    27/38

    Data Structures and Algorithms Using C

    while(!feof(fp))

    {

    printf("\n%s",employee.ename);clrscr();

    da = 0.08 * employee.basic;

    hra = 0.05 * employee.basic;cca = 0.03 * employee.basic;

    pf = 0.06 * employee.basic;

    lic = 0.10 * employee.basic;gp = employee.basic + hra + da + cca;

    np = gp - (pf + lic);

    printf("\n PAYSLIP");

    printf("\n -------\n\n");printf("\n Employee No. : %3d",employee.eno);

    printf("\n Employee Name : %s",employee.ename);

    printf("\n Department : %s",employee.dept);

    printf("\n Basic Pay :(Rs.)%8.2f\n\n",employee.basic);printf("\n Allowances");

    printf("\n HRA :(Rs.)%6.2f",hra);printf("\n DA :(Rs.)%6.2f",da);

    printf("\n CCA :(Rs.)%6.2f\n\n",cca);

    printf("\n Gross Pay :(Rs.)%6.2f\n\n",gp);

    printf("\n Deductions");printf("\n LIC :(Rs.)%6.2f\n\n",lic);

    printf("\n Net Pay :(Rs.)%6.2f",np);

    getch();fread(&employee,sizeof(struct emp),1,fp);

    }

    fclose(fp);

    }/*main end*/

    Page 27

  • 7/29/2019 Data Structures & Algorithms Lab - Using 'C' - Solutions 1

    28/38

    Data Structures and Algorithms Using C

    13. Determine the Permutations of a given String.

    #include#include

    #include

    void main()

    {void prem(char a[50],int n,int k);

    int n,k=0;

    char a[50];clrscr();

    printf("Enter the String : ");

    gets(a);n=strlen(a);

    clrscr();

    printf("\n OUTPUT");printf("\n ------\n\n");

    printf("\n Generation of Permutations");

    printf("\n --------------------------\n\n");

    printf("\n Given String : %s\n",a);printf("\n\n Permutations of Given String are \n\n");

    perm(a,k,n);

    getch();}/*main end*/

    void perm(ar,k1,n1)

    int k1,n1;char ar[50];

    {

    int i;char j,b[10];

    if(k1==n1)

    {printf("\t%s",ar);

    }

    strcpy(b,ar);for(i=k1;i

  • 7/29/2019 Data Structures & Algorithms Lab - Using 'C' - Solutions 1

    29/38

    Data Structures and Algorithms Using C

    14. Write a program to generate the Fibonacci Series.

    #include#include

    #include

    void main()

    {int j,i0,i1,i2,n;

    clrscr();

    printf("\n\n");

    printf("\n The no. of Terms that have to be Generated is : ");scanf("%d",&n);

    i0=-1;

    i1=1;

    clrscr();printf("\n\n Generation of Fibonacci Sequence");

    printf("\n --------------------------------\n\n");printf("\n The Fibonacci Sequence for the %d Values is found to be :",n);

    printf("\n\n\n");

    for(j=1;j

  • 7/29/2019 Data Structures & Algorithms Lab - Using 'C' - Solutions 1

    30/38

    Data Structures and Algorithms Using C

    15. Determine the Sparse Transpose Matrix of a given Matrix.

    #include#include

    #include

    main()

    {

    struct sparse{

    int row;

    int col;

    int val;};

    struct sparse a[50],b[50];

    int i,j,m;

    clrscr();printf("\n Enter the No. of Rows, Columns, Values of Sparse Matrix :\n");

    scanf("%d%d%d",&a[0].row,&a[0].col,&a[0].val);

    b[0].row=a[0].col;

    b[0].col=a[0].row;b[0].val=a[0].val;

    printf("\n Please enter the corresponding Rows, Columns and Values :\n");

    for(i=1;i

  • 7/29/2019 Data Structures & Algorithms Lab - Using 'C' - Solutions 1

    31/38

    Data Structures and Algorithms Using C

    clrscr();

    printf("\n SPARSE MATRIX TRANSPOSE");printf("\n -----------------------\n");

    printf("\n Given Matrix A :");

    printf("\n ----------------\n\n");

    for(i=0;i

  • 7/29/2019 Data Structures & Algorithms Lab - Using 'C' - Solutions 1

    32/38

    Data Structures and Algorithms Using C

    16. Write a program to convert an Infix Expression to Postfix Expression.

    #include#include

    #include

    void display_status(char stack[], int top, char ch, char exp[], int k);

    /*** Main Program ***/

    void main(){

    void change_screen();

    /*** Variable Declaration Part ***/

    char infix[100], stack[100], exp[100];

    int i, top=0, k=0,len;

    /*** Printing Heading and Initialization Process ***/

    gotoxy(26,2); printf("Infix to Postfix Conversion");

    gotoxy(15,4);

    printf("Infix Expression : ");fflush(stdin);scanf("%s",&infix);

    len=strlen(infix); infix[len]=')';stack[0]='('; infix[len+1]='\0';

    /*** Process Converting Infix to Postfix ***/

    gotoxy(10,6); printf("Symbol Scanned");gotoxy(30,6); printf("Stack Content");

    gotoxy(50,6); printf("Expression");

    gotoxy(10,7); for(i=1;i

  • 7/29/2019 Data Structures & Algorithms Lab - Using 'C' - Solutions 1

    33/38

    Data Structures and Algorithms Using C

    case'/':if(stack[top]=='/'||stack[top]=='*'

    ||stack[top]=='^')exp[k++]=stack[top--];

    stack[++top]=infix[i]; break;

    case'+':

    case'-':

    if(stack[top]=='/'||stack[top]=='*'||

    stack[top]=='^'||stack[top]=='+'||stack[top]=='-')

    exp[k++]=stack[top--];stack[++top]=infix[i]; break;

    case'(':stack[++top]=infix[i]; break;

    case')':while(stack[top]!='(')

    exp[k++]=stack[top--];if(stack[top]=='(')

    top--; break;

    default:if(infix[i]!=')'||infix[i]!=' ')exp[k++]=infix[i]; break;

    }

    display_status(stack,top,infix[i],exp,k);

    }

    exp[k]='\0';

    /*** Displaying the Result ***/

    gotoxy(15, wherey()+2); textcolor(15);

    printf("Postfix Expression : %s",exp); getch();

    }/*main end*/

    void change_screen(){

    int i;

    getch(); clrscr();gotoxy(10,6); printf("Symbol Scanned");

    gotoxy(30,6); printf("Stack Context");

    gotoxy(50,6); printf("Expression");

    gotoxy(10,7);

    Page 33

  • 7/29/2019 Data Structures & Algorithms Lab - Using 'C' - Solutions 1

    34/38

    Data Structures and Algorithms Using C

    for(i=1;i22)

    {

    change_screen();

    r=wherey();r++;

    }

    gotoxy(18,r); printf("%c",ch);gotoxy(36,r);

    for(i=0;i

  • 7/29/2019 Data Structures & Algorithms Lab - Using 'C' - Solutions 1

    35/38

    Data Structures and Algorithms Using C

    17. Write a program that implements Depth First Search technique.

    #include#include

    int adj[100][100],n;int visit[100];

    void dfs();

    void main()

    {

    int i,j,var;printf("\n\n DEPTH FIRST SEARCH");

    printf("\n ------------------");

    printf("\n Enter the no. of vertices in the graph : ");

    scanf("%d",&n);fflush(stdin);

    printf("\n Enter the values of Adjacency Matrix : \n");

    for(i=0;i

  • 7/29/2019 Data Structures & Algorithms Lab - Using 'C' - Solutions 1

    36/38

    Data Structures and Algorithms Using C

    printf("\t %d",adj[i][j]);

    }

    }

    printf("\n\n");

    printf("\n The Vertex for which DFS has to be done %d \n",var);printf("\n The Vertices Obtained after DFS \n");

    dfs(var-1);

    printf("\n");

    getch();

    return;

    }/*main end*/

    /*recursive procedure*/

    void dfs(v)

    int v;

    {int j;

    visit[v]=1;

    printf("\t %d",v+1);

    for(j=0;j

  • 7/29/2019 Data Structures & Algorithms Lab - Using 'C' - Solutions 1

    37/38

    Data Structures and Algorithms Using C

    18. Determine the Connected Components of an Adjacency Matrix.

    #include#include

    int adjacent[100][100],m;int view[100];

    void main()

    {

    void component();

    void trap();

    int i,j;

    printf("\n");clrscr();

    printf("\n\t\t\t CONNECTED COMPONENTS\n");printf("\n ENTER THE NO OF VERTICES IN THE GRAPH ");

    scanf("%d",&m);

    fflush(stdin);

    printf("\n\n THE NUMBER OF VERTICES ARE FOUND TO BE %d \n",m);printf("\n ENTER THE VALUES OF THE ADJACENCY MATRIX\n");

    for(i=0;i

  • 7/29/2019 Data Structures & Algorithms Lab - Using 'C' - Solutions 1

    38/38

    Data Structures and Algorithms Using C

    for(j=0;j