data struct

Upload: parveen-swami

Post on 05-Apr-2018

230 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/31/2019 Data Struct

    1/45

    1

    Assignment No. :- 1Algorithm and Programming for

    LINEAR SEARCH USING C PROGRAMMING

    Algorithm :

    LINEAR(DATA,N,ITEM,LOC)

    1. Set DATA [N+1] = ITEM2. Set LOC = 1

    3. while DATA[LOC] != ITEM

    Set LOC = LOC + 1

    4. If LOC = N + 1

    Set LOC = 0

    5. Exit

    Program :

    #include

    #includevoid main()

    {

    int a[10],i,n,m,c=0;

    printf("Enter the size of an array");

    scanf("%d",&n);

    printf("\nEnter the elements of the array");

    for(i=0;i

  • 7/31/2019 Data Struct

    2/45

    2

    Output:

    Enter the size of an array

    8

    Enter the elements of the array

    1 2 3 4 5 6 7 8

    Enter the number to be search : 6

    Result: The number is found at place 6.

    Enter the number to be search : 9

    Result: The number is not in the list

  • 7/31/2019 Data Struct

    3/45

    3

    Assignment No. :- 2Algorithm and Programming for

    BINARY SEARCH USING C PROGRAMMING

    Algorithm:

    BINARY(DATA,LB,UB,ITEM,LOC)

    1.Set BEG = LB,END = UB and MID = INT(BEG+END/2)2.Repeat Steps 3 and 4,while BEG

  • 7/31/2019 Data Struct

    4/45

    4

    if(m==a[mid]){

    c=1;

    break;

    }

    else if(m

  • 7/31/2019 Data Struct

    5/45

    5

    Assignment No. :- 3Algorithm and Programming for

    BUBBLE SORT USING C PROGRAMMING

    ALGORITHM :

    BUBBLE(A,N)

    1. Repeat 2 & 3 for K=1 to N-12. Set PTR = 13. While PTR a[PTR+1]

    Then INTERCHANGE

    (b)PTR = PTR + 1

    EXIT

    PROGRAM:

    #include#include

    void main()

    {

    int s,temp,i,j,a[20];

    clrscr();

    printf("\nEnter size of the array: ");

    scanf("%d",&s);

    printf("\nEnter %d elements in to the array:",s);

    for(i=0;i

  • 7/31/2019 Data Struct

    6/45

  • 7/31/2019 Data Struct

    7/45

    7

    Assignment No. :- 4Algorithm and Programming for

    SELECTION SORT USING C PROGRAMMINGALGORITHM :

    for i = 1:n,

    k = i

    for j = i+1:n, if a[j] < a[k], k = j

    invariant: a[k] smallest of a[i..n]swap a[i,k]

    invariant: a[1..i] in final position

    end

    PROGRAM :

    #include

    #include

    void main()

    {

    int s,i,j,temp,a[20];clrscr();

    printf("\nEnter size of the array :");

    scanf("%d",&s);

    printf("\nEnter %d elements in to the array:");

    for(i=0;i

  • 7/31/2019 Data Struct

    8/45

    8

    OUTPUT :

    Enter size of the array : 7

    Enter elements in to the array :

    14

    25

    7

    65

    43

    21

    89

    The array after sorting is:

    7 14 21 25 43 65 89

  • 7/31/2019 Data Struct

    9/45

    9

    Assignment No. :- 5(a)Algorithm and Programming for

    MATRIX ADDITION USING C PROGRAMMINGALGORITHM :

    int A[n][n], B[n][n], C[n][n]; //--> declaring 3 matrices in a common format.

    int i=j=1; //---> declaring variables.i and j values changes from 1 to 3 in the pgm.

    for(i=1; i

  • 7/31/2019 Data Struct

    10/45

    10

    printf("\n");

    for(j=0;j

  • 7/31/2019 Data Struct

    11/45

    11

    OUTPUT :

    Enter the First matrix2 3

    5 7

    Enter the Second matrix4 6

    5 4

    The Addition of two matrix is 6 9

    10 11

  • 7/31/2019 Data Struct

    12/45

    12

    Assignment No. :- 05(b)Algorithm and Programming for

    MATRIX SUBTRACTION USING C PROGRAMMINGALGORITHM :

    int A[n][n], B[n][n], C[n][n]; //--> declaring 3 matrices in a common format.

    int i=j=1; //---> declaring variables.i and j values changes from 1 to 3 in the program.

    for(i=1; i

  • 7/31/2019 Data Struct

    13/45

    13

    printf("\n");

    for(j=0;j

  • 7/31/2019 Data Struct

    14/45

    14

    OUTPUT :

    Enter the First matrix8 9

    5 7

    Enter the Second matrix4 6

    5 4

    The Subtraction of two matrix is 4 3

    0 3

  • 7/31/2019 Data Struct

    15/45

    15

    Assignment No. :- 05(c)Algorithm and Programming for

    TRANSPOSE OF A MATRIX USING C PROGRAMMINGALGORITHM :

    1. for (int i = 0; i < 4; i++) {2. for (int j = i + 1; j < 4; j++) {3. int save = matrix[i][j];4. matrix[i][j] = matrix[j][i];5. matrix[j][i] = save;6. }7. }

    PROGRAM :

    #include

    #includevoid main()

    {

    int a[10][10],b[10][10],i,j,k=0,m,n;

    printf("\nEnter the row and column of matrix");

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

    printf("\nEnter the First matrix->");

    for(i=0;i

  • 7/31/2019 Data Struct

    16/45

    16

    }

    printf("\n\nTraspose of a matrix is -> ");

    for(i=0;i

  • 7/31/2019 Data Struct

    17/45

    17

    OUTPUT :

    Enter the row and column of matrix 2 2

    Enter the First matrix

    5 7

    9 8

    Traspose of a matrix is

    5 9

    7 8

  • 7/31/2019 Data Struct

    18/45

    18

    Assignment No. :- 05(d)Algorithm and Programming for

    MATRIX MULTIPLICATION USING C PROGRAMMING

    ALGORITHM :

    void mm_mul (int A[N][N], int B[N][N], int C[N][N])

    {

    int i, j, k;

    int sum;for (i = 0; i < N; i++)

    {

    for (j = 0; j < N; j++) {

    sum = 0;

    for (k = 0; k < N; k++) {

    sum += A[i][k] * B[k][j];

    }

    C[i][j] = sum;

    }

    }

    }

    PROGRAM :

    # include

    # include

    void main()

    {

    int a[3][3],i,j,k,b[3][3],c[3][3];

    printf("Enter matrix A:");

    for(i=0;i

  • 7/31/2019 Data Struct

    19/45

    19

    }

    printf("Multiplication of the Matrices:\n");

    for(i=0;i

  • 7/31/2019 Data Struct

    20/45

    20

    OUTPUT:

    Enter matrix A :

    1 1 1

    1 1 1

    1 1 1

    Enter matrix B :

    2 2 2

    2 2 2

    2 2 2

    Multiplication of the Matrices :

    6 6 6

    6 6 6

    6 6 6

  • 7/31/2019 Data Struct

    21/45

    21

    Assignment No. :- 06Algorithm and Programming for

    Stack operations (Inserting ,deleting traversing)

    Algorithm :-

    int main()

    {

    int j = 10;

    int l;

    /* allocating a stack */

    stackinit(j);

    /* fill the stack with 10 items */

    for(l=0;l= 10)

    {

    break;

    exit(1);

    }

    }

    /* pop out all items of the stack */

    int k;

    for(k = 10;k>=0;k--)

    {

    pop(k);

    if(k

  • 7/31/2019 Data Struct

    22/45

    22

    int choice;

    char ch;

    do

    {

    clrscr();

    printf("1. Push\n2. Pop\n3. Traverse\n Enter your choice....");

    scanf("%d",&choice);

    switch(choice)

    {

    case 1: push();

    break;

    case 2: printf("the deleted element is::: ",pop());

    break;

    case 3: traverse();

    break;

    default:printf("wrong choice entered");

    }

    printf("Do you want to continue(y/n)....");

    scanf("%c", &ch);

    }

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

    }

    void push()

    {

    int item;

    if(top==maxsize-1)

    printf("overflow");

    else

    {

    printf("Enter the item to be inserted....");

    scanf("%d", &item);top=top+1;

    stack[top]=item;

    }

    }

    int pop()

    {

    int item;

    if (top==-1)

    printf("underflow");

    else

    {item=stack[top];

    top=top-1;

    }

    return item;

    }

    void traverse()

  • 7/31/2019 Data Struct

    23/45

    23

    {

    int i;

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

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

    }

  • 7/31/2019 Data Struct

    24/45

    24

    Output :-

    1. Push2. Pop3. Traverse

    Enter your choice. 1

    Enter the item to be inserted. 5

    Do you want to continue(Y/N). N

  • 7/31/2019 Data Struct

    25/45

    25

    Assignment No. :- 07Algorithm and Programming for

    Stack as Linked List

    Algorithm :-

    PUSH( )

    1. t = newnode( )

    2. Enter info to be inserted3. Read n

    4. tinfo = n

    5. tnext = top

    6. top = t

    7. Return

    POP( )

    1. If (top = NULL)

    Print underflow

    Return

    2. x = top

    3. top = top next

    4. delnode(x)

    5. Return

    Program:-

    #include

    #includestruct stack

    {

    int no;

    struct stack *next;

    }

    *start=NULL;

    typedefstruct stack st;

    void push();

    int pop();

    void display();

    void main()

    {

    charch;

    int choice,item;

    do

  • 7/31/2019 Data Struct

    26/45

    26

    {

    clrscr();

    printf("\n 1: push");

    printf("\n 2: pop");

    printf("\n 3: display");

    printf("\n Enter your choice");

    scanf("%d",&choice);

    switch (choice)

    {

    case 1: push();

    break;

    case 2: item=pop();

    printf("The delete element in %d",item);

    break;

    case 3: display();

    break;

    default : printf("\n Wrong choice");

    };

    printf("\n do you want to continue(Y/N)");

    fflush(stdin);

    scanf("%c",&ch);

    }

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

    }

    void push()

    {

    st *node;node=(st *)malloc(sizeof(st));

    printf("\n Enter the number to be insert");

    scanf("%d",&node->no);

    node->next=start;

    start=node;

    }

    int pop()

    {

    st *temp;

    temp=start;

    if(start==NULL)

    {

    printf("stack is already empty");

    getch();

    exit();

  • 7/31/2019 Data Struct

    27/45

    27

    }

    else

    {

    start=start->next;

    free(temp);

    }

    return(temp->no);

    }

    void display()

    {

    st *temp;

    temp=start;

    while(temp->next!=NULL)

    {

    printf("\nno=%d",temp->no);

    temp=temp->next;

    }

    printf("\nno=%d",temp->no);}

  • 7/31/2019 Data Struct

    28/45

    28

    Assignment No. :- 08Algorithm and Programming for

    Implementation of Queue as Link List

    Algorithm:-

    CREATE

    1. t = new node

    2. Enter info to be inserted

    3. Read n

    4. t info = n

    5. t next = front

    6. front = t

    INSERTION

    1. r next = t

    2. t next = NULL

    3. Return

    DELETION

    1. x = front

    2. front = front next

    3. delnode(x)

    4. Return

    DISPLAY

    1. If (front = NULL)

    Print empty queue

    Return

    Else

    P = start

    Repeat until (p< > NULL)

    Print p info

    P = p next

    Return

    Program:-

    #include

    #include

    struct queue

    {int no;

    struct queue *next;}

    *start=NULL;

  • 7/31/2019 Data Struct

    29/45

    29

    void add();

    int del();

    void traverse();

    void main()

    { int ch;

    charchoice;

    do

    { clrscr();

    printf("----1. add\n");

    printf("----2. delete\n");

    printf("----3. traverse\n");

    printf("----4. exit\n");

    printf("Enter your choice\n");

    scanf("%d",&ch);

    switch(ch)

    { case 1: add();

    break;

    case 2: printf("the deleted element is\n%d",del());

    break;

    case 3: traverse();

    break;

    case 4: return;

    default : printf("wrong choice\n"); };

    fflush(stdin);

    scanf("%c",&choice); }

    while(choice!=4); }

    void add(){ struct queue *p,*temp;

    temp=start;

    p=(struct queue*)malloc(sizeof(struct queue));

    printf("Enter the data");

    scanf("%d",&p->no);

    p->next=NULL;

    if(start==NULL)

    { start=p; }

    else

    { while(temp->next!=NULL)

    { temp=temp->next; }

    temp->next=p; }

    }

    int del()

    { struct queue *temp;

  • 7/31/2019 Data Struct

    30/45

    30

    int value;

    if(start==NULL)

    { printf("queue is empty");

    getch();

    return(0); }

    else

    { temp=start;

    value=temp->no;

    start=start->next;

    free(temp); }

    return(value); }

    void traverse()

    {

    struct queue *temp;

    temp=start;

    while(temp->next!=NULL)

    {

    printf("no=%d",temp->no);

    temp=temp->next;}

    printf("no=%d",temp->no);

    getch();

    }

  • 7/31/2019 Data Struct

    31/45

    31

    Output :-

    1. Add2. Delete3. Traverse4. Exit

    Enter your choice. 2

    Enter the item to be deleted. 5

    The item is deleted i.e. . 5

    Do you want to continue(Y/N). N

  • 7/31/2019 Data Struct

    32/45

    32

    Assignment No. :- 09Algorithm and Programming for

    Finding Factorial of any number

    Algorithm:-

    1. Take a number as a input from the user.

    2. initialize a variable fact=1

    3. fact=fact*number

    4. number=number-1

    5. repeat step 3 to 5 if number>0

    6. print the value of fact variable

    Program:-

    #include

    #include

    void main()

    {

    int c, n, fact = 1;

    printf("Enter a number to calculate it's factorial\n");

    scanf("%d",&n);

    for( c = 1 ; c

  • 7/31/2019 Data Struct

    33/45

    33

    Output :-

    Enter a number to calculate it's factorial 5

    Factorial of 5 = 120

  • 7/31/2019 Data Struct

    34/45

    34

    Assignment No. :- 10Algorithm and Programming for

    Finding Fibonacci Series in C

    Algorithm:-

    Fib(n) = fib(n-1) + fib(n-2)

    Stop conditions: n = 2 || n = 1

    Program:-

    #include

    int fib(int n);

    int main()

    {

    int n, answer;

    printf("Enter number upto where fibonacci is to find: ");

    scanf(%d,&n);

    printf("\n\n");

    answer = fib(n);

    Printf(Answeris the " , n );

    return 0;

    }

    int fib (int n)

    {

    cout

  • 7/31/2019 Data Struct

    35/45

    35

    Output :-

    Enter number upto where fibonacci is to find: 3

    Answer is the 0 1 2

  • 7/31/2019 Data Struct

    36/45

    36

    Assignment No. :- 11Algorithm and Programming for

    Swapping two nos. by Call by Value Method

    Algorithm :-

    swap(x1,y1)

    {

    int z1;z1=x1;

    x1=y1;

    y1=z1;

    printf(x=%d y=%d,x1,y1);

    getch();

    }

    Program :-

    #include

    #include

    void main()

    {

    int x,y,swap(intx,inty);

    printf("enter value of x & y:");

    scanf("%d%d",&x,&y);

    swap(&x,&y);

    printf(x=%d y=%d,x,y);

    }

    swap(x1,y1)

    {

    int z1;

    z1=x1;

    x1=y1;

    y1=z1;

    printf(x=%d y=%d,x1,y1);

    getch();

    }

  • 7/31/2019 Data Struct

    37/45

    37

    Output :-

    Enter value of x and y:

    10

    20

    Values after swapping are

    x1=20 y1=10

  • 7/31/2019 Data Struct

    38/45

    38

    Assignment No. :- 12Algorithm and Programming for

    Swapping two nos. by Call by Reference Method

    Algorithm :-

    swap(x1,y1)

    {

    int z1;z1=*x1;

    *x1=*y1;

    *y1=z1;

    printf(Values after swapping are\n*x=%d *y=%d,x1,y1);

    getch();

    }

    Program :-

    #include

    #include

    void main()

    {

    int x,y,swap(intx,inty);

    printf("Enter value of x & y:");

    scanf("%d%d",&x,&y);

    swap(&x,&y);

    printf(x=%d y=%d,x,y);

    }

    swap(x1,y1)

    {

    int z1;

    z1=*x1;

    *x1=*y1;

    *y1=z1;

    printf(Values after swapping are\n*x=%d *y=%d,x1,y1);

    getch();

    }

  • 7/31/2019 Data Struct

    39/45

    39

    Output :-

    Enter value of x and y:

    10

    20

    Values after swapping are

    x1=20 y1=10

  • 7/31/2019 Data Struct

    40/45

  • 7/31/2019 Data Struct

    41/45

    41

    quick_sort(a,l,h);

    printf("Sorted Array :\n ");

    output(a,n);

    }

    void quick_sort(int a[],int l, int h)

    {

    int temp,key,low,high;

    low=l;

    high=h;

    key=a[(low+high)/2];

    do

    {

    while(key>a[low])

    {

    low++;

    }

    while(key

  • 7/31/2019 Data Struct

    42/45

    42

    Output :-

    How many elements in the array: 5

    Enter the elements : 55 28 82 298 477

    Sorted Array: 28 55 82 298 477

  • 7/31/2019 Data Struct

    43/45

    43

    Assignment No. :- 14

    Algorithm and Programming for

    INSERTION SORT.

    #include

    #include

    void main()

    {

    int a[100],n,k,i,j,temp;

    clrscr();

    printf("how many elements: ");

    scanf("%d",&n);

    printf("enter the element of array\n");

    for(i=0;i

  • 7/31/2019 Data Struct

    44/45

    44

    }

    printf("element of array after sorting are : \n");

    for(i=0;i

  • 7/31/2019 Data Struct

    45/45

    OUTPUT:

    How many elements:

    6

    Enter the elements of array:

    6

    4

    9

    2

    8

    1

    Elements of array after sorting are:

    1

    2

    4

    6

    8

    9