PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

download PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

of 146

Transcript of PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    1/146

    K.L.N. COLLEGE OF ENGINEERING

    POTTAPALAYAM.P.O, SIVAGANGAI -630611.

    DEPARTMENT OF INFORMATION TECHNOLOGY

    CS6212-

    PROGRAMMING AND DATA STRUCTURE LABORATORY I

    I YEAR / II SEMESTER

    REGULATION 2013

    PREPARED BY

    Dr.G.RAMESH Professor/IT

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    2/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    Ex. No: 1 ODD OR EVEN

    Date :

    AIM:

    To write a C program find whether the given number is odd or even.

    ALGORITHM:

    Step 1 : Start.

    Step 2 : Read num, c.

    Step 3 : if(num= = 0), then print it is neither odd or even

    otherwise go to step-4.

    Step 4 : else calculate c=num%2.

    Step 5 : if(c= =0), then print the given number is even,

    else print the given number is odd.

    Step 6 : Stop.

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    3/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    4/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    PROGRAM:

    #include

    #include

    void main()

    {

    clrscr();

    int num,c;

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

    scanf("%d",&num);

    if(num= =0)

    printf("it is neither odd nor

    even"); else{

    c=num%2;

    if(c= =0)

    printf("\nThe given no. is

    even"); else

    printf("\nThe given no. is odd");

    }

    getch();

    }

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    5/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    OUTPUT:

    RESULT:

    Thus the program was executed and output is verified successfully.

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    6/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    Ex. No: 2 BIGGEST OF THREE NUMBERS

    Date :

    AIM:

    To write a C program to find the biggest of three numbers.

    ALGORITHM:

    Step 1 : Start.

    Step 2 : Read a, b, c,max.

    Step 3 : if(a>b), then if(a>c), then max = a,

    otherwise max = c, else go to step-4.

    Step 4 :- if(b>c), then max = b, else max = c.

    Step 5 : Print max is the biggest value.

    Step 6 : Stop.

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    7/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    PROGRAM:

    #include

    #include

    void main()

    {

    clrscr();

    int a,b,c,max;

    printf("\nEnter the three nos\t:");

    scanf("%d%d%d",&a,&b,&c);

    if(a>b)

    {

    if(a>c)

    max = a;

    else max

    = c;

    }

    else if(b>c)max = b;

    else

    max = c;

    printf("%d is the biggest value",max);

    getch();

    }

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    8/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    OUTPUT:

    RESULT:

    Thus the program was executed and output is verified successfully.

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    9/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    Ex. No: 3 ARMSTRONG OR NOT

    Date :

    AIM:

    To write a C program to find whether the given number is Armstrong or not.

    ALGORITHM:

    Step 1 : Start.

    Step 2 : Read n,t,s,r.

    Step 3 : Let t=n & s=0.

    Step 4 : while(n>0), calculate r =n%10, s=s+(r*r*r) & n=n/10.

    Step 5 : if(t = = s), print t is an amstrong number , else print

    t is not an amstrong

    number. Step 6 : Stop.

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    10/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    PROGRAM:

    #include

    #include

    void main()

    {

    clrscr();

    int n,t,s,r;

    printf("\n Enter the

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

    t=n;

    s=0;

    while(n>0)

    {

    r=n%10;

    s=s+(r*r*r);

    n=n/10;

    }

    if(t= =s)

    printf("\n %d is an amstrong

    number",t); else

    printf("\n %d is not an amstrong number

    ",t); getch();

    }

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    11/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    OUTPUT:

    RESULT:

    Thus the program was executed and output is verified successfully.

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    12/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    Ex. No: 4 PERFECT OR NOT

    Date :

    AIM:

    To write a C program to find whether the given number is perfect or not.

    ALGORITHM:

    Step 1 : Start.

    Step 2 : Read num,temp=1,i=0,c=0,k.

    Step 3 : while(temp

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    13/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    PROGRAM:

    #include

    #include

    void main()

    {

    clrscr();

    int num,temp=1,i=0,c=0,k;

    printf("Enter the no. :");

    scanf("%d",&num);

    while(temp

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    14/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    OUTPUT:

    RESULT:

    Thus the program was executed and output is verified successfully.

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    15/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    Ex. No: 5 PALINDROME OR NOT

    Date :

    AIM:

    To write a C program to find the given string is palindrome or not.

    ALGORITHM:

    Step 1 : Start.

    Step 2 : Read input, copy, condition, length, i.

    Step 3 : To find length of string use length=strlen(input).

    Step 4 : for(i=0;i

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    16/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    PROGRAM:

    #include

    #include

    #include

    #include

    void main()

    {

    char input[50],copy[50];

    int condition,length,i;

    clrscr();

    printf("Enter the input :");

    scanf("%s",input);

    length = strlen(input);

    for (i=0; i

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    17/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    OUTPUT:

    RESULT:

    Thus the program was executed and output is verified successfully.

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    18/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    Ex. No: 6 SUM OF DIGITS

    Date :

    AIM:

    To write a C program to print the sum of digits of a given number.

    ALGORITHM:

    Step 1 : Start.

    Step 2 : Read num, sum=0, rem.

    Step 3 : while(num>0), rem = num%10,sum =sum+rem,num=num/10

    Step 4 : Print sum.

    Step 5: Stop.

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    19/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    PROGRAM:

    #include

    #include

    void main()

    {

    long int num, rem,

    sum=0; clrscr();

    printf("Enter the Number:

    "); scanf("%ld", &num);

    while(num>0)

    {

    rem = num%10;

    sum = sum + rem;

    num = num/10;

    }

    printf("The Sum of digits of the Given Number is: %ld

    ",sum); getch();

    }

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    20/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    OUTPUT:

    RESULT:

    Thus the program was executed and output is verified successfully.

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    21/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    Ex. No: 7 FIBONACCI SERIES

    Date :

    AIM:

    To write a C program to print the Fibonacci Series.

    ALGORITHM:-

    Step 1 : Start.

    Step 2 : Read l=-1,j=1,n,k,i.

    Step 3 : for(i=0,i

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    22/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    PROGRAM:

    #include

    #include

    void main()

    {

    int l = -1,j = 1,i,n,k;

    clrscr();

    printf("Enter the number of terms:

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

    printf("\n\t\t\t FIBONACCI SERIES\t\t\t\n

    "); for(i = 0; i

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    23/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    OUTPUT:

    RESULT:

    Thus the program was executed and output is verified successfully.

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    24/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    Ex. No: 8 PRIME NUMBER OR NOT

    Date :

    AIM:

    To write a C program that prints the given number is prime or not.

    ALGORITHM:

    Step 1 : Start.

    Step 2 : Read num,temp=2,k.

    Step 3 : While(temp

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    25/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    PROGRAM:

    #include

    #include

    void main()

    {

    long int num, temp =2,

    k; clrscr();

    printf("Enter the number:

    "); scanf("%ld",&num);

    while(temp

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    26/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    OUTPUT:

    RESULT:

    Thus the program was executed and output is verified successfully.

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    27/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    Ex. No: 9 SUM OF ARRAY ELEMENTS

    Date :

    AIM:

    To write a C program to print the sum of array of a given number.

    ALGORITHM:

    Step 1: Start.

    Step 2: Read a[5],b[5],c[5], i, j, k.

    Step 3: Get values for Array a[5].

    Step 4: Get values for Array b[5].

    Step 5: Array c[5] is Sum of Array a[5] and b[5].

    Step 6: Print Array c[5].

    Step 7: Stop.

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    28/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    PROGRAM:

    #include

    #include

    void main()

    {

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

    clrscr();

    printf("\n\t Enter 5 numbers in array

    1:\n\t\t"); for(i=0;i

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    29/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    OUTPUT:

    RESULT:

    Thus the program was executed and output is verified successfully.

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    30/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    Ex. No: 10 CALL BY VALUE

    Date :

    AIM:

    To write a C program that calls a function by call by value.

    ALGORITHM:

    Step-1 : Start.

    Step -2 : Read a,b.

    Step -3 : Print a,b.

    Step -4 : call swap(a,b)

    Step -5 : Print a,b

    Step -6 : Stop

    Swap(a, b)

    Step -1 : Start swap(a,b).

    Step -2 : temp = a, a = b, b =temp.

    Step -3 : Print a,b.

    Step -4 : Return.

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    31/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    PROGRAM:

    #include

    #include

    void swap(int,int);

    void main(){

    int a,b;

    clrscr();

    printf("\n\t Enter a value for A: ");

    scanf("%d",&a);

    printf("\n\t Enter a value for B: ");

    scanf("%d",&b);

    printf("\n\n The values before swap - in

    main:\n"); printf("\n\t Now the value for A: %d

    ",a); printf("\n\t Now the value for B: %d ",b);

    swap(a,b);

    printf("\n\n The values after swap - in

    main:\n"); printf("\n\t Now the value for A: %d

    ",a); printf("\n\t Now the value for B: %d ",b);getch();

    }

    void swap(int a,int

    b){ int temp;

    temp=a;

    a=b;

    b=temp;

    printf("\n\n The values after swap-in swap function

    :\n"); printf("\n\t Now the value for A: %d ",a);

    printf("\n\t Now the value for B: %d ",b);

    }

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    32/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    OUTPUT:

    Enter a value for A: 345

    Enter a value for B: 789

    The values before swap - in main:

    Now the value for A: 345

    Now the value for B: 789

    The values after swap-in swap function :

    Now the value for A: 789

    Now the value for B: 345

    The values after swap - in main:

    Now the value for A: 345

    Now the value for B: 789

    RESULT:

    Thus the program was executed and output is verified successfully.

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    33/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    Ex. No: 11 CALL BY REFERENCE

    Date :

    AIM:

    To write a C program that calls a function by call by reference.

    ALGORITHM:

    Step -1 : Start.

    Step -2 : Read a,b.

    Step -3 : Print a,b.

    Step -4 : call function (&a,&b)

    Step -5 : Print a,b

    Step -6 : Stop

    function(&a, &b)

    Step -1 : Start function(*a,*b).

    Step -2 : temp =* a, *a =*b,*b =temp.

    Step -3 : Print a,b.

    Step -4 : Return.

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    34/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    PROGRAM:

    #include

    #include

    void function(int *, int *);

    void main(){

    int a,b;

    clrscr();

    printf("\n\t Enter a value for A: ");

    scanf("%d",&a);

    printf("\n\t Enter a value for B: ");

    scanf("%d",&b);

    printf("\n\n The values before swap

    are:\n"); printf("\n\t Now the value for A:

    %d ",a); printf("\n\t Now the value for B:

    %d ",b); function(&a,&b);

    printf("\n\n The values after swap are:\n");

    printf("\n\t Now the value for A: %d ",a);

    printf("\n\t Now the value for B: %d ",b);getch();

    }

    void function(int *a,int *b){

    int temp;

    temp = *a;

    *a=*b;

    *b=temp;

    return;

    }

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    35/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    OUTPUT:

    RESULT:

    Thus the program was executed and output is verified successfully.

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    36/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    Ex. No: 12 FACTORIAL

    Date :

    AIM:

    To write a C program to print the Factorial of a given number.

    ALGORITHM:

    Step -1 : Start.

    Step -2 : Read num,output.

    Step -3 : output =fact(num).

    Step -4: Definition of fact(num)

    Step -5 : Read fact =1

    Step -6 : While(num>0),fact =fact*num,num--.

    Step -7 : Return fact.

    Step -8 : Print fact

    Step -9: Stop.

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    37/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    PROGRAM:

    #include

    #include long

    int fact(long int n);

    void main(){

    clrscr();

    long int num, output;

    printf("Enter the number:

    "); scanf("%ld",&num);

    output = fact(num);

    printf("\n\n\n\t\tThe Factorial of the number is:

    %ld",output); getch();

    }

    long int fact(long int

    num){ long int fact = 1;

    while(num>0){

    fact = fact*num;num--;

    }

    return fact;

    }

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    38/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    OUTPUT:

    RESULT:

    Thus the program was executed and output is verified successfully.

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    39/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    Ex. No: 13 MATRIX ADDITION

    Date :

    AIM:

    To write a C program to add two matrices.

    ALGORITHM:

    Step -1 : Start.

    Step -2 : Declare a two dimensional array

    Step -3 : Read two matrices

    Step -4 : Use nested for loop

    Step -5: Add two matrices

    Step -6 : store the answer in array variable

    Step -7 : Print result.

    Step -8: Stop.

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    40/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    PROGRAM:

    #include

    #include

    void main ( ) {

    int a[2][2],b[2][2],s[2][2];

    int i,j;

    clrscr ();

    printf("enter first matrix: \n");

    for ( i=1; i

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    41/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    }

    printf("\n");

    }getch ();

    }

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    42/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    OUTPUT:

    RESULT:

    Thus the program was executed and output is verified successfully.

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    43/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    Ex. No: 14 MATRIX MULTIPLICATION

    Date :

    AIM:

    To write a C program to multiply two matrices.

    ALGORITHM:

    Step -1 : Start.

    Step -2 : Declare a two dimensional array

    Step -3 : Read two matrices

    Step -4: Use nested for loop

    Step -5: Multiply two matrices

    Step -6 : store the answer in array variable

    Step -7 : Print result.

    Step -8: Stop.

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    44/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    PROGRAM:

    #include

    #include

    void main( ) {

    int a[2][2], b[2][2],s[2][2];

    int i,j,k;

    printf("Enter first matrix:\n" );

    for( i=1;i

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    45/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    for (j=1;j

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    46/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    OUTPUT:

    RESULT:

    Thus the program was executed and output is verified successfully.

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    47/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    Ex. No: 15 PALINDROME

    Date :

    AIM:

    To write a C program for palindrome checking.

    ALGORITHM:

    Step -1 : Start.

    Step -2 : Declare a two array variables.

    Step -3 : Read a string

    Step -4 : Use string handling functions

    Step -5: Compare the given two strings are same.

    Step -6 : Display the message palindrome or not.

    Step -7: Stop.

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    48/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    PROGRAM:

    #include

    #include

    #include

    void main(){

    char a[100], b[100];

    printf("Enter the string to check if it is a

    palindrome\n"); gets(a);

    strcpy(b,a);

    strrev(b);

    if( strcmp(a,b) == 0 )

    printf("Entered string is a palindrome.\n");

    else

    printf("Entered string is not a

    palindrome.\n"); getch();

    }

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    49/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    OUTPUT:

    RESULT:

    Thus the program was executed and output is verified successfully.

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    50/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    Ex. No: 16 FUNCTION POINTER

    Date :

    AIM:

    To write a C program for Function pointer.

    ALGORITHM:

    Step -1 : Start.

    Step -2 : Declare a pointer variable as function.

    Step -3 : Assigning address of function to pointer variable

    Step -4 : Define a function definition as show()

    Step -5: Call a function.

    Step -6 : Display the message.

    Step -7: Stop.

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    51/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    PROGRAM:

    #include

    #include

    int show();

    void main(){

    int (*fnPtr)();

    clrscr();

    fnPtr=show;

    printf("Address of function :%u",show);

    (*fnPtr)();

    getch();

    }

    int show(){

    printf("\nFunction called using

    pointer!"); return 0;

    }

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    52/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    OUTPUT:

    RESULT:

    Thus the program was executed and output is verified successfully.

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    53/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    Ex. No: 17 FUNCTION WITH VARIABLE NUMBER OF ARGUMENTS

    Date :

    AIM:

    To write a C program for Function with variable number of arguments

    ALGORITHM:

    Step -1 : Start.

    Step -2 : Include a header file called stdarg.h

    Step -3 : Define a function as fun() with variable number of arguments.

    Step -4 : Call the functions related to stdarg header file.

    Step -5: Passing variable number of arguments as function arguments.

    Step -6: Display the result.

    Step -7: Stop.

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    54/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    PROGRAM:

    #include

    #include

    void fun(char *msg,

    ...); int main(){

    clrscr();

    fun("Sarav", 1, 4, 7, 11,

    0); getch();

    return 0;

    }

    void fun(char *msg, ...){

    va_list ptr;

    int num;

    va_start(ptr, msg);

    num = va_arg(ptr,

    int); printf("%d",

    num); printf("\n");

    num = va_arg(ptr,int); printf("%d",

    num); printf("\n");

    num = va_arg(ptr,

    int); printf("%d",

    num); va_end(ptr);

    }

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    55/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    OUTPUT:

    RESULT:

    Thus the program was executed and output is verified successfully.

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    56/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    Ex. No: 18 NESTED STRUCTURES

    Date :

    AIM:

    To write a C program to use structures to print name and address of student.

    ALGORITHM:

    Step -1 : Start.

    Step -2 : Read Structure address, student.

    Step -3 : Read structure members name, doorno, strtname, place, city.

    Step -4 : Print structure members name, doorno, strtname, place, city.

    Step -5 : Stop.

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    57/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    PROGRAM:

    #include

    #include

    struct addr{

    int doorno;

    char strtname[15];

    char place[20];

    char city[20];

    };

    struct student{

    char name[15];

    struct addr address;

    }s1;

    void main(){

    clrscr();

    printf("\n\t\t Enter Student Name:

    "); gets(s1.name);

    printf("\n\t Enter Address: \n");

    printf("\n\t\t Enter Door number :

    "); gets(s1.address.doorno);

    printf("\n\t\t Enter Street name : ");

    gets(s1.address.strtname);

    printf("\n\t\t Enter Place : ");

    gets(s1.address.place);

    printf("\n\t\t Enter City : ");

    gets(s1.address.city);

    printf("\n\n\n\t\t Student Name: ");

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    58/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    puts(s1.name);

    printf("\n\t Address: \n");

    printf("\n\t\t Door number : ");

    puts(s1.address.doorno);

    printf("\n\t\t Street name : ");

    puts(s1.address.strtname);

    printf("\n\t\t Place : ");

    puts(s1.address.place);

    printf("\n\t\t City : ");

    puts(s1.address.city);

    getch();

    }

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    59/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    OUTPUT:

    RESULT:

    Thus the program was executed and output is verified successfully.

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    60/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    Ex. No: 19 FILE HANDLING-SEQUENTIAL ACCESS

    Date :

    AIM:

    To write a C program for file handling.

    ALGORITHM:

    Step -1 : Start.

    Step -2 : Open a file in write mode and read mode.

    Step -3 : Read a string and end with # symbol.

    Step -4 : use putc() function to write the given message in file.

    Step -5: use getc() function for read the given message as one by one character from

    file.

    Step -6: Display the message in the window read from file.

    Step -7: Stop.

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    61/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    PROGRAM:

    #include

    #include

    void main(){

    char c;

    FILE *fp= fopen("write.c","w");

    printf("Enter contents to store in file (Enter # at

    end):\n"); while((c=getchar())!='#')

    putc(c,fp);

    fclose(fp);

    fp = fopen("write.c","r");

    while (c!= EOF){

    putchar(c); c

    = getc(fp);

    }

    fclose(fp);

    getch();

    }

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    62/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    OUTPUT:

    RESULT:

    Thus the program was executed and output is verified successfully.

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    63/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    Ex. No: 20 FILE HANDLING-RANDOM ACCESS (FTELL() AND REWIND())

    Date :

    AIM:

    To write a C program random access in file handling.

    ALGORITHM:

    Step -1: Start.

    Step -2 : Declare two character array.

    Step -3 : create a file in write mode.

    Step -4 : check EOF is reached or not.

    Step -5: use the function for handling the file.

    Step -6: Display the output.

    Step -7: Stop.

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    64/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    PROGRAM:

    #include

    #include

    #define BUFLEN 6

    void main(){

    char msg[] = "abcdefghijklmnopqrstuvwxyz";

    char buf[BUFLEN];

    FILE *fp;

    clrscr();

    if((fp = fopen("TEXT.TXT", "w")) == NULL){

    fprintf(stderr, "Error opening file.");

    exit(1);

    }

    if(fputs(msg, fp) == EOF){

    fprintf(stderr, "Error writing to

    file."); exit(1);

    }

    fclose(fp);

    // Now open the file for reading.

    if((fp = fopen("TEXT.TXT", "r")) ==

    NULL){ fprintf(stderr, "Error opening

    file."); exit(1);

    }

    printf("\nImmediately after opening, position = %ld", ftell(fp));

    // Read in 5 characters.

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    65/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    fgets(buf, BUFLEN, fp);

    printf("\nAfter reading in %s, position = %ld", buf, ftell(fp));

    //Read in the next 5 characters.

    fgets(buf, BUFLEN, fp);

    printf("\n\nThe next 5 characters are %s, and position now = %ld",buf, ftell(fp));

    //Rewind the stream.

    rewind(fp);

    printf("\n\nAfter rewinding, the position is back at %ld",ftell(fp));

    //

    Read in 5 characters.

    fgets(buf, BUFLEN, fp);

    printf("\nReading starts at the beginning again: %s\n", buf);

    fclose(fp);

    getch();

    }

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    66/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    OUTPUT:

    RESULT:

    Thus the program was executed and output is verified successfully.

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    67/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    Ex. No: 21 FILE HANDLING-ERROR HANDLING

    Date :

    AIM:

    To write a C program for error handling in files.

    ALGORITHM:

    Step -1 : Start.

    Step -2 : Open a file in open mode..

    Step -3 : Read a string

    Step -4 : Use Error handling functions for handle the errors.

    Step -5: Close the file stream.

    Step -6: Stop.

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    68/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    PROGRAM:

    #include

    #include

    #include

    extern int errno ;

    void main (){

    int errnum;

    FILE * pf;

    clrscr();

    pf = fopen ("filesss.txt", "rb");

    if (pf == NULL) {

    errnum = errno;

    fprintf(stderr,"Value of errno: %d\n",errno);

    perror("Error printed by perror");

    fprintf(stderr,"Error opening file: %s\n",strerror(errnum));

    }

    else {

    fclose (pf);

    }

    getch();

    }

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    69/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    OUTPUT:

    RESULT:

    Thus the program was executed and output is verified successfully.

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    70/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    Ex. No: 22 ARRAY IMPLEMENTATION OF LIST ADT

    Date:

    AIM:

    To implement the List ADT using arrays.

    ALGORITHM:

    Step 1: Start.

    Step 2: Declare the necessary functions for implementation.

    Step 3: Get the input from the user and store it an array.

    Step 4: In Insertion, half of the elements to be shifted upwards and in deletion half of the

    elements to be shifted downwards.

    Step 5: Display the output using an

    array. Step 6: Stop.

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    71/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    PROGRAM:

    #include

    #include

    #define MAX 10

    void create();

    void insert();

    void deletion();

    void search();

    void display();

    int a,b[20], n, p, e, f, i, pos;

    void main()

    {

    clrscr();

    int ch;

    char g='y';

    do

    {

    printf("\n main Menu");

    printf("\n 1.Create \n 2.Delete \n 3.Search \n 4.Insert \n 5.Display\n 6.Exit

    \n"); printf("\n Enter your Choice");

    scanf("%d",

    &ch); switch(ch)

    {

    case 1:

    create();

    break;

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    72/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    case 2:

    deletion();

    break;

    case 3:

    search();

    break;

    case 4:

    insert();

    break;

    case 5:

    display();

    break;

    case 6:

    exit();

    break;

    default:

    printf("\n Enter the correct choice:");

    }

    printf("\n Do u want to continue:::");

    scanf("\n%c", &g);

    }

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

    getch();

    }

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    73/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    void create()

    {

    printf("\n Enter the number of

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

    for(i=0;i=n)

    {

    printf("\n Invalid Location::");

    }

    else

    {

    for(i=pos+1;i

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    74/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    }

    }

    void search()

    {

    printf("\n Enter the Element to be searched:");

    scanf("%d", &e);

    for(i=0;i=n)

    {

    printf("\n invalid Location::");

    }

    else

    {

    for(i=MAX-1;i>=pos-1;i--)

    {

    b[i+1]=b[i];

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    75/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    }

    printf("\n Enter the element to insert::\n");

    scanf("%d",&p);

    b[pos]=p;

    n++;

    }

    printf("\n The list after insertion::\n");

    display();

    }

    void display()

    {

    printf("\n The Elements of The list ADT

    are:"); for(i=0;i

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    76/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    OUTPUT:

    Main Menu

    1.Create

    2.Delete

    3.Search

    4.Insert

    5.Display

    6.Exit

    Enter your Choice: 1

    Enter the number of elements: 4

    Enter the elements:

    10

    20

    30

    40

    Do u want to continue(y/n): y

    main Menu1.Create

    2.Delete

    3.Search

    4.Insert

    5.Display

    6.Exit

    Enter your Choice: 2

    Enter the element to delete:20

    Elements after deletion: 10

    30 40

    Do u want to continue(y/n): y

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    77/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    main Menu

    1.Create

    2.Delete

    3.Search

    4.Insert

    5.Display

    6.Exit

    Enter your Choice: 3

    Enter the element to search: 100

    Element not found

    Do u want to continue(y/n): y

    main Menu

    1.Create

    2.Delete

    3.Search

    4.Insert

    5.Display

    6.Exit

    Enter your Choice: 4

    Enter the element to insert: 15

    Enter the position to insert:2

    Elements after insertion:

    10

    15

    30

    40

    Do u want to continue(y/n): y

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    78/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    main Menu

    1.Create

    2.Delete

    3.Search

    4.Insert

    5.Display

    6.Exit

    Enter your Choice: 4

    RESULT:

    Thus, a C program of list ADT using arrays was implemented successfully.

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    79/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    Ex. No: 23 SINGLY LINKED LIST

    Date:

    AIM:

    To write a C program to implement singly linked list.

    ALGORITHM:

    Step 1: Start

    Step 2: Creation: Get the number of elements, and create the nodes having structures

    DATA, LINK and store the element in Data field, link them together to form

    a linked list.

    Step 3: Insertion: Get the number to be inserted and create a new node store the value in

    DATA field. And insert the node in the required position.

    Step 4: Deletion: Get the number to be deleted. Search the list from the beginning and locate the

    node then delete the node.

    Step 5: Display: Display all the nodes in the list.

    Step 6: Stop.

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    80/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    PROGRAM:

    #include

    #include

    #include

    #define NULL 0

    typedef struct list

    {

    int no;

    struct list

    *next; }LIST;

    LIST *p,*t,*h,*y,*ptr,*pt;

    void create( void );

    void insert( void );

    void delet( void );

    void display ( void );

    int j,pos,k=1,count;

    void main()

    {

    int n,i=1,opt;

    clrscr();

    p = NULL;

    printf("%d",sizeof(LIST));

    printf( "Enter the no of nodes :\n "

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

    count = n;

    while( i

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    81/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    create();

    i++;

    }

    printf("\nEnter your option:\n");

    printf("1.Insert \t 2.Delete \t 3.Display \t

    4.Exit\n"); do

    {

    scanf("%d",&opt);

    switch( opt )

    {

    case 1:

    insert();

    count++;

    break;

    case 2:

    delet();

    count--;

    if ( count == 0 )

    {

    printf("\n List is empty\n");

    }

    break;

    case 3:

    printf("List elements

    are:\n"); display();

    break;

    }

    printf("\nEnter your option

    \n"); }while( opt != 4 );

    getch();

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    82/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    }

    void create ( )

    {

    if( p== NULL )

    {

    p = ( LIST * ) malloc ( sizeof ( LIST ) );

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

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

    p->next = NULL;

    h = p;

    }

    else

    {

    t= ( LIST * ) malloc (sizeof( LIST

    )); printf( "\nEnter the element" );

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

    t->next = NULL;

    p->next = t;

    p = t;

    }

    }

    void insert()

    {

    t=h;

    p = ( LIST * ) malloc ( sizeof(LIST) );

    printf("Enter the element to be inserted:\n");

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

    printf("Enter the position to insert:\n");

    scanf( "%d",&pos );

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    83/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    if( pos == 1 )

    {

    h = p; h-

    >next = t;

    }

    else

    {

    for(j=1;jnext;

    p->next = t->next;

    t->next = p;

    t=p;

    }

    }

    void delet(){

    printf("Enter the position to delete:\n");

    scanf( "%d",&pos );

    if( pos == 1 )

    {

    h = h->next ;

    }

    else

    {

    t= h; for(j=1;jnext;

    pt=t->next->next;

    free(t->next); t-

    >next= pt;

    }

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    84/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    }

    void display()

    {

    t= h;

    while( t->next != NULL )

    {

    printf("\t%d",t->no);

    t = t->next;

    }

    printf( "\t %d\t",t->no );

    }

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    85/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    OUTPUT:

    Enter the no of nodes : 3

    Enter the element:1

    Enter the element 2

    Enter the element 3

    Enter your option:

    1.Insert 2.Delete 3.Display 4.Exit

    3

    List elements

    are: 1 2 3

    Enter your option 1

    Enter the element to be inserted:

    12

    Enter the position to insert: 1

    Enter your option 3

    List elements

    are: 12 1 2 3

    Enter your option 1

    Enter the element to be inserted:

    13

    Enter the position to insert: 3

    Enter your option 1

    Enter the element to be inserted:

    14

    Enter the position to insert:6

    Enter your option 3

    List elements are:

    12 1 13 2 3 14

    Enter your option 2

    Enter the position to delete:1

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    86/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    Enter your option 3

    List elements are:

    1 13 2 3 14

    Enter your option 2

    Enter the position to delete:3

    Enter your option 3

    List elements

    are: 1 13 3 14

    Enter your option 2

    Enter the position to delete:4

    Enter your option 3

    List elements

    are: 1 13 3

    Enter your option: 6

    RESULT:

    Thus, a C program for Singly Linked List was implemented successfully.

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    87/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    Ex. No: 24 DOUBLY LINKED LIST

    Date:

    AIM:

    To write a C program to implement doubly linked list with Insert, Delete and Display

    operations.

    ALGORITHM:

    Step 1: Start

    Step 2: Creation: Get the number of elements to create the list. Then create the node having the

    Structure: BLINK, DATA , FLINK and store the elements in Data field. Link them

    together to form a doubly linked list.

    Step 3: Insertion: Get the number to be Inserted, create a new node to store the value. Search the

    list and insert the node in its right position.

    Step 4: Deletion: Get the number to be deleted. Search the list from the beginning and try to

    locate node p with DATA. If found then delete the node.

    Step 5: FLINK Ps previous node to Ps Next node. BLINK Ps Next node to Ps Previous node

    else display Data not Found.

    Step 6: Display: Display all the nodes in the list.

    Step 7: Stop.

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    88/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    PROGRAM :

    #include

    #include

    #include

    #define NULL 0

    typedef struct list

    {

    int no;

    struct list *next;

    struct list *pre;

    }LIST;

    LIST *p,*t,*h;

    void create( void );

    void insert( void );

    void delet( void );

    void display ( void );

    int j,pos,k=1,count;

    void main()

    {

    int n,i=1,opt;

    clrscr();

    p = NULL;

    printf( "Enter the no of nodes :\n "

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

    count = n;

    while( i

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    89/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    { create();

    i++;

    }

    printf("\nEnter your option:\n");

    printf("1.Insert \t 2.Delete \t 3.Display \t

    4.Exit\n"); do

    {

    scanf("%d",&opt);

    switch( opt ){ case

    1:

    insert();

    count++;

    break;

    case 2:

    delet();

    count--;

    if ( count == 0 )

    {

    printf("\n List is empty\n");

    }

    break;

    case 3:

    printf("List elements

    are:\n"); display();

    break;

    }

    printf("\nEnter your option

    \n"); }while( opt != 4 );

    getch();

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    90/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    }

    void create ( )

    {

    if( p == NULL )

    {

    p = ( LIST * ) malloc ( sizeof ( LIST ) );

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

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

    p->next = NULL; p-

    >pre = NULL;

    h = p;

    }

    else

    {

    t= ( LIST * ) malloc (sizeof( LIST

    )); printf( "\nEnter the element" );

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

    t->next = NULL;p->next = t; t-

    >pre = p;

    p = t;

    }

    }

    void insert()

    {

    t=h;

    p = ( LIST * ) malloc ( sizeof(LIST) );

    printf("Enter the element to be

    insrted:\n"); scanf("%d",&p->no);

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    91/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    printf("Enter the position to insert:\n");

    scanf( "%d",&pos );

    if( pos == 1 )

    {

    h = p; h-

    >next = t;

    t->pre = h;

    h->pre = NULL;

    }

    else

    {

    for(j=1;jnext;

    p->next = t-

    >next; t->next =

    p; p->pre = t;

    }

    }

    void delet()

    {

    printf("Enter the position to delete:\n");

    scanf( "%d",&pos );

    if( pos == 1 )

    {

    h = h->next ; h-

    >pre = NULL;

    }

    else

    {

    t= h;

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    92/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    for(j=1;jnext;

    t->next = t->next-

    >next; t->next->pre = t;

    free( t->next );

    }

    }

    void display()

    {

    t= h;

    while( t->next != NULL )

    {

    printf("%d\n",t-

    >no); t = t->next;

    }

    printf( "%d",t->no );

    }

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    93/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    OUTPUT:

    Enter the no of nodes: 3

    Enter the element3

    Enter your option:

    1.Insert 2.Delete 3.Display 4.Exit

    3

    List elements

    are: 1 2 3

    Enter your option 1

    Enter the element to be inserted:22

    Enter the position to insert:1

    Enter your option 3

    List elements are:

    22 1 2 3

    Enter your option 1

    Enter the element to be inserted:

    11

    Enter the position to insert:5

    Enter your option 3

    List elements

    are: 22 1 2 3 11

    Enter your option

    2

    Enter the position to delete:

    1

    Enter your option

    3

    List elements

    are: 1 2 3 11

    Enter your option 4

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    94/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    RESULT:

    Thus a C program for Doubly Linked List was implemented successfully.

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    95/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    Ex. No: 25 STACK USING ARRAY

    Date:

    AIM:

    To write a C program to implement Stack operations such as push, pop and display using

    array.

    ALGORITHM:

    Step 1: Start.

    Step 2: Initialy top = -1;

    Step 3: push operation increases top by one and writes pushed element to storage[top];

    Step 4: pop operation checks that top is not equal to -1 and decreases top variable by

    1; Step 5: display operation checks that top is not equal to -1 and returns storage[top];

    Step 6: Stop.

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    96/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    PROGRAM:

    #include

    #include

    #include

    #define size 5

    int item;

    int s[10];

    int top;

    void display()

    {

    int i;

    if(top==-1)

    {

    printf("\nstack is empty");

    return;

    }

    printf("\nContent of stack

    is:\n"); for(i=0;i

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    97/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    }

    void pop()

    {

    if(top==-1)

    {

    printf("\nstack is empty");

    return;

    }

    printf("\nDeleted item is:

    %d",s[top]); top--;

    }

    void main()

    {

    int ch;

    top=-1;

    clrscr();

    printf("\n1.push\t\t2.pop\n3.display\t4.exit\n");

    do{

    printf("\nEnter your

    choice:\n"); scanf("%d",&ch);

    switch(ch)

    {

    case 1:// printf("Enter

    item:\n"); //scanf("%d",&item);

    push();

    break;

    case 2:

    pop(); break;

    case 3: display();

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    98/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    break;

    case 4: exit(0);

    default: printf("\nWrong entry ! try

    again"); }}while(ch

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    99/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    OUTPUT:

    1.push 2.pop

    3.display 4.exit

    Enter your choice:

    1

    Enter item:

    100

    Enter your choice:

    1

    Enter item:

    200

    Enter your choice:

    1

    Enter item:

    300

    Enter your choice:

    2

    Deleted item is: 300Enter your choice:

    3

    Content of stack is:

    100 200

    Enter your choice:4

    RESULT:

    Thus a C program for Stack using array was implemented successfully.

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    100/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    Ex. No: 26 STACK USING LINKED LIST

    Date:

    AIM:

    To write a C program to implement Stack operations such as push, pop and display using

    linked list.

    ALGORITHM:

    Step 1: Start.

    Step 2: push operation inserts an element at the front.

    Step 4: pop operation deletes an element at the front of the list;

    Step 5: display operation displays all the elements in the list.

    Step 6: Stop.

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    101/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    PROGRAM:

    #include "stdio.h"

    #include "stdlib.h"

    #include "conio.h"

    void pop();

    void push(int value);

    void display(); struct

    node

    {

    int data;

    struct node *link;

    };

    struct node *top=NULL,*temp;

    void main()

    {

    int choice,data;

    while(1) //infinite loop is used to insert/delete infinite number of elements in stack

    {

    printf("\n1.Push\n2.Pop\n3.Display\n4.Exit\n");

    printf("\nEnter ur choice:");

    scanf("%d",&choice);

    switch(choice)

    {

    case 1: //To push a new element into stack

    printf("Enter a new element :");

    scanf("%d",&data);

    push(data);

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    102/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    break;

    case 2: // pop the element from

    stack pop();

    break;

    case 3: // Display the stack elements

    display();

    break;

    case 4: // To exit

    exit(0);

    }

    }

    getch();

    //return 0;

    }

    void display()

    {

    temp=top;

    if(temp==NULL)

    {

    printf("\nStack is empty\n");

    }

    printf("\n The Contents of the Stack

    are..."); while(temp!=NULL)

    {

    printf(" %d ->",temp-

    >data); temp=temp->link;

    }

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    103/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    }

    void push(int data)

    {

    temp=(struct node *)malloc(sizeof(struct node)); // creating a space for the new

    element. temp->data=data;

    temp->link=top;

    top=temp;

    display();

    }

    void pop()

    {

    if(top!=NULL)

    {

    printf("The poped element is %d",top->data);

    top=top->link;

    }

    else

    {

    printf("\nStack Underflow");

    }

    display();

    }

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    104/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    OUTPUT:

    1.Push

    2.Pop

    3.Display

    4.Exit

    Enter ur choice:1

    Enter a new element :10

    The Contents of the Stack are... 10 -

    > 1.Push

    2.Pop

    3.Display

    4.Exit

    Enter ur choice:1

    Enter a new element :20

    The Contents of the Stack are... 20 -> 10 ->

    1.Push

    2.Pop

    3.Display

    4.Exit

    Enter ur choice:1

    Enter a new element :30

    The Contents of the Stack are... 30 -> 20 -> 10 ->

    1.Push

    2.Pop

    3.Display

    4.Exit

    Enter ur choice:2

    The poped element is 30

    The Contents of the Stack are... 20 -> 10 ->

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    105/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    1.Push

    2.Pop

    3.Display

    4.Exit

    Enter ur choice:3

    The Contents of the Stack are... 20 -> 10 ->

    1.Push

    2.Pop

    3.Display

    4.Exit

    Enter ur choice:4

    RESULT:

    Thus a C program for Stack using linked list was implemented successfully.

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    106/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    Ex. No: 27 QUEUE USING ARRAY

    Date:

    AIM:

    To write a C program to implement Queue operations such as enqueue, dequeue and

    display using array.

    ALGORITHM:

    Step 1: Start.

    Step 2: Initialize front=0; rear=-1.

    Step 3: Enqueue operation moves a rear by one position and inserts a element at the rear.

    Step 4: Dequeue operation deletes a element at the front of the list and moves the front by

    one position

    Step 5: Display operation displays all the element in the list.

    Step 6: Stop.

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    107/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    PROGRAM:

    #include

    #include

    #define SIZE 5 /* Size of Queue */

    int Q[SIZE],f=0,r=-1; /* Global declarations */

    Qinsert(int elem)

    { /* Function for Insert operation */

    if( Qfull())

    printf("\n\n Overflow!!!!\n\n");

    else

    {

    ++r;

    Q[r]=elem;

    }

    }

    int Qdelete()

    { /* Function for Delete operation */

    int elem;

    if(Qempty()){ printf("\n\nUnderflow!!!!\n\n");

    return(-1); }

    else

    {

    elem=Q[f];

    f=f+1;

    return(elem);

    }

    }

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    108/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    /* Function to Check Queue Full */

    if(r==SIZE-1) return 1;

    return 0;

    /* Function to Check Queue Empty */

    if(f > r) return 1;

    return 0;

    /* Function to display status of Queue */

    int i;

    if(Qempty()) printf(" \n Empty Queue\n");

    else

    {

    printf("Front->");for(i=f;i

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    109/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    printf("\n ### Queue Operations using Arrays###

    \n\n"); printf("\n Press 1-Insert, 2-Delete,3-Display,4-

    Exit\n"); printf("\n Your option ? ");

    scanf("%d",&opn);

    switch(opn)

    {

    case 1: printf("\n\nRead the element to be Inserted

    ?"); scanf("%d",&elem);

    Qinsert(elem); break;

    case 2: elem=Qdelete();

    if( elem != -1)

    printf("\n\nDeleted Element is %d

    \n",elem); break;

    case 3: printf("\n\nStatus of Queue\n\n");

    display(); break;

    case 4: printf("\n\n Terminating \n\n"); break;

    default: printf("\n\nInvalid Option !!! Try Again !! \n\n");

    break;

    }

    printf("\n\n\n\n Press a Key to Continue . . . ");

    getch();

    }while(opn != 4);

    getch();

    }

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    110/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    OUTPUT:

    ### Queue Operations using Arrays###

    Press 1-Insert, 2-Delete,3-Display,4-

    Exit Your option ? 1

    Read the element to be Inserted ?100

    Press a Key to Continue . . .

    ### Queue Operations using Arrays###

    Press 1-Insert, 2-Delete,3-Display,4-

    Exit Your option ? 1

    Read the element to be Inserted ?200

    Press a Key to Continue . . .

    ### Queue Operations using Arrays###

    Press 1-Insert, 2-Delete,3-Display,4-

    Exit Your option ? 1

    Read the element to be Inserted ?300

    Press a Key to Continue . . .

    ### Queue Operations using Arrays###

    Press 1-Insert, 2-Delete,3-Display,4-Exit Your option ? 2

    Deleted Element is 100

    Press a Key to Continue . . .

    ### Queue Operations using Arrays###

    Press 1-Insert, 2-Delete,3-Display,4-Exit

    Your option ? 3

    Status of Queue

    Front->200 300

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    111/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    Ex. No: 28 QUEUE USING LINKED LIST

    Date:

    AIM:

    To write a C program to implement Queue operations such as enqueue, dequeue and

    display using linked list.

    ALGORITHM:

    Step 1: Start.

    Step 2: enqueue operation inserts an element at the rear of the list.

    Step 4: dequeue operation deletes an element at the front of the list.

    Step 5: display operation display all the element in the list.

    Step 6: Stop.

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    112/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    PROGRAM:

    #include

    #include

    struct node

    {

    int info;

    struct node *link;

    }*front = NULL, *rear = NULL;

    void insert();

    void delet();

    void display();

    int item;

    void main()

    {

    int

    ch; do

    {

    printf("\n\n1.\tEnqueue\n2.\tDequeue\n3.\tDisplay\n4.\tExit\n");

    printf("\nEnter your choice: ");

    scanf("%d",

    &ch); switch(ch)

    {

    case 1:

    insert();

    break;

    case 2:

    delet();

    break;

    case 3:

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    113/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    display();

    break;

    case 4:

    exit(0);

    default:

    printf("\n\nInvalid choice. Please try again...\n");

    }

    } while(1);

    getch();

    }

    void insert()

    {

    printf("\n\nEnter ITEM:

    "); scanf("%d", &item);

    if(rear == NULL)

    {

    rear = (struct node *)malloc(sizeof(struct

    node)); rear->info = item;

    rear->link = NULL;

    front = rear;

    }

    else

    {

    rear->link = (struct node *)malloc(sizeof(struct

    node)); rear = rear->link;

    rear->info = item;

    rear->link = NULL;

    }

    }

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    114/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    void delet()

    {

    struct node *ptr;

    if(front == NULL)

    printf("\n\nQueue is

    empty.\n"); else

    {

    ptr = front;

    item = front->info;

    front = front-

    >link; free(ptr);

    printf("\nItem deleted: %d\n", item);

    if(front == NULL)

    rear = NULL;

    }

    }

    void display()

    {

    struct node *ptr = front;

    if(rear == NULL)

    printf("\n\nQueue is

    empty.\n"); else

    {

    printf("\n\n");

    while(ptr != NULL)

    {

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

    ptr = ptr->link;

    }

    }}

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    115/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    OUTPUT:

    1. Enqueue

    2. Dequeue

    3. Display

    4. Exit

    Enter your choice: 1

    Enter ITEM: 12

    1. Enqueue

    2.

    Dequeue

    3.

    Display

    4.

    Exit

    Enter your choice: 1

    Enter ITEM: 15

    1. Enqueue

    2. Dequeue

    3. Display

    4. Exit

    Enter your choice: 1

    Enter ITEM: 20

    1. Enqueue

    2. Dequeue

    3. Display

    4.

    Exit

    Enter your choice: 2

    Item deleted: 12

    1.

    Enqueue

    2. Dequeue

    3. Display

    4. Exit

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    116/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    Enter your choice:3

    15 20

    1. Enqueue

    2. Dequeue

    3. Display

    4. Exit

    Enter your choice:4

    RESULT:

    Thus a C program for Queue using linked list was implemented successfully.

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    117/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    Ex. No: 29 INFIX TO POSTFIX EXPRESSION USING STACK

    Date:

    AIM:

    To write a C program to implement the convertion of infix to postfix expression using

    Stack.

    ALGORITHM:

    Step 1: Start.

    Step 2: Create a stack to store operand and operator.

    Step 3: In Postfix notation the operator follows the two operands and in the infix notation the

    operator is in between the two operands.

    Step 4: Consider the sum of A and B. Apply the operator + to the operands A and B and write

    the sum as A+B is INFIX. + AB is PREFIX. AB+ is POSTFIX

    Step 5: Get an Infix Expression as input and evaluate it by first converting it to postfix and then

    evaluating the postfix expression.

    Step 6: The expressions with in innermost parenthesis must first be converted to postfix so that

    they can be treated as single operands. In this way Parentheses can be successivelyeliminated until the entire expression is converted.

    Step 7: The last pair of parentheses to be opened with in a group of parentheses encloses the first

    expression with in that group to be transformed. This last-in first-out immediately

    suggests the use of Stack. Precedence plays an important role in the transforming infix to

    postfix.

    Step 8: Stop.

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    118/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    PROGRAM:

    #include

    #include

    #include

    #define MAX 20

    int top=-1;

    char pop();

    char stack[MAX];

    void push(char item);

    int prcd(char symbol){

    switch(symbol){

    case '+':

    case '-':return

    2; break;

    case '*':

    case '/':return

    4; break;

    case '^':

    case '$':return

    6; break;

    case '(':

    case ')':

    case '#':return

    1; break;

    }

    }

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    119/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    int isoperator(char

    symbol){ switch(symbol){

    case '+':

    case '-':

    case '*':

    case '/':

    case '^':

    case '$':

    case '(':

    case ')':return

    1; break;

    default:

    return 0;

    }

    }

    void convertip(char infix[],char postfix[]){

    int i,symbol,j=0;

    stack[++top]='#';

    for(i=0;i

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    120/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    }

    pop();//pop out (.

    }

    else{

    if(prcd(symbol)>prcd(stack[top]))

    push(symbol);

    else{

    while(prcd(symbol)

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    121/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    getch();

    }

    void push(char item){

    top++;

    stack[top]=item;

    }

    char pop(){

    char a;

    a=stack[top];

    top--;

    return a;

    }

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    122/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    OUTPUT:

    Enter the valid infix string:

    (a+b)*c

    The corresponding postfix string

    is: ab+c*

    RESULT:

    Thus a C program to implement the conversion of infix to postfix expression using Stack.

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    123/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    Ex. No: 30 EVALUATION OF POSTFIX EXPRESSION USING STACK

    Date:

    AIM:

    To write a C program to implement the evaluation of postfix expression using Stack.

    ALGORITHM:

    Step-1: Start.

    Step 2: Scan the Postfix string from left to

    right. Step 3: Initialize an empty stack.

    Step 4: If the scanned character is an operand, add it to the stack. If the scanned character is an

    operator, there will be atleast two operands in the stack.

    Step 5:If the scanned character is an Operator, then we store the top most element of the

    stack(topStack) in a variable temp. Pop the stack. Now evaluate topStack(Operator)temp.

    Let the result of this operation be retVal. Pop the stack and Push retVal into the stack.

    Repeat this step till all the characters are scanned.

    Step 6: After all characters are scanned, we will have only one element in the stack. Return

    topStack.

    Step 7: Stop.

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    124/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    PROGRAM:

    #include

    #include

    struct

    stack{ int

    top; float

    a[50]; }s;

    void main(){

    char pf[50];

    float d1,d2,d3;

    int i;

    clrscr();

    s.top=-1;

    printf("\n\n Enter the postfix

    expression:"); gets(pf);

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

    switch(pf[i]){

    case '0':

    case '1':

    case '2':

    case '3':

    case '4':

    case '5':

    case '6':

    case '7':

    case '8':

    case '9':

    s.a[++s.top]=pf[i]-'0';

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    125/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    break;

    case '+':

    d1=s.a[s.top--];

    d2=s.a[s.top--];

    s.a[++s.top]=d1+d2;

    break;

    case '-':

    d2=s.a[s.top--];

    d1=s.a[s.top--];

    s.a[++s.top]=d1-d2;

    break;

    case '*':

    d2=s.a[s.top--];

    d1=s.a[s.top--];

    s.a[++s.top]=d1*d2;

    break;

    case '/':

    d2=s.a[s.top--];d1=s.a[s.top--];

    s.a[++s.top]=d1/d2;

    break;

    }

    }

    printf("\n\n The value of expression

    is%f",s.a[s.top]); getch();

    }

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    126/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    OUTPUT:

    Enter the postfix expression: 56*7-

    The value of expression is 23.000000

    RESULT:

    Thus a C program to implement the evaluation of postfix expression using Stack was

    implemented successfully.

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    127/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    Ex. No: 31 LINEAR SEARCH

    Date:

    AIM:

    To write a C program to implement the concept of linear search.

    ALGORITHM:

    Step 1: Start with the first item in the list.

    Step 2: Compare the current item to the target

    Step 3: If the current value matches the target then we declare victory and stop.

    Step 4: If the current value is less than the target then set the current item to be the next item and

    repeat from 2.

    Step 5: Stop.

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    128/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    PROGRAM:

    #include

    #include

    void main() {

    int arr[20]; int

    i,size,sech;

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

    printf("Enter total no. of elements :

    "); scanf("%d",&size);

    for(i=0; i

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    129/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    OUTPUT:

    -- Linear Search --

    Enter total no. of elements : 5

    Enter 1 element : 10

    Enter 2 element : 4

    Enter 3 element : 2

    Enter 4 element : 17

    Enter 5 element : 100

    Enter the element to be searched: 17

    Element exits in the list at position : 4

    RESULT:

    Thus a C program for the concept of linear search was implemented successfully.

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    130/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    Ex. No: 32 BINARY SEARCH

    Date:

    AIM:

    To write a C program to implement the concept of binary search.

    ALGORITHM:

    Step 1: Set the list to be the whole list

    Step 2: Find the middle value of the list

    Step 3: If the middle value is equal to the target then we declare victory and stop.

    Step 4: If the middle item is less than the target, then we set the new list to be the upper half

    of the old list and we repeat from step 2 using the new list.

    Step 5: If the middle value is greater than the target, then we set the new list to be the bottom half

    of the list, and we repeat from step 2 with the new list.

    Step 6: Stop.

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    131/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    PROGRAM:

    #include

    #include

    void main(){

    int n,i,search,f=0,low,high,mid,a[20];

    clrscr();

    printf("Enter the n value:");

    scanf("%d",&n);

    for(i=1;i

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    132/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    }

    if(f==0)

    printf("not present");

    getch();

    }

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    133/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    OUTPUT:

    Enter the n value: 5

    Enter the number in ascending order a[1]=10

    Enter the number in ascending order a[2]=8

    Enter the number in ascending order a[3]=9

    Enter the number in ascending order a[4]=24

    Enter the number in ascending order a[5]=1

    Enter the search element:9

    obtained in the position 3:

    RESULT:

    Thus a C program for the concept of binary search was implemented successfully.

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    134/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    Ex. No: 33 BUBBLE SORT

    Date:

    AIM:

    To write a C program to implement the concept of bubble sort.

    ALGORITHM:

    Step 1: Start.

    Step 2: Repeat Steps 3 and 4 for i=1 to 10

    Step 3: Set j=1

    Step 4: Repeat while j

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    135/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    PROGRAM:

    #include

    #include

    void main(){

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

    printf("Enter the total integers you want to enter (make it less than

    100):\n"); scanf("%d",&n);

    printf("Enter the %d integer array elements:\n",n);

    for(i=0;i

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    136/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    OUTPUT:

    Enter the total integers you want to enter (make it less than

    100): 5

    Enter the 5 integer array elements:

    99 87 100 54 150

    The sorted numbers are: 54 87 99 100 150

    RESULT:

    Thus a C program for the concept of bubble sort was implemented successfully.

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    137/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    Ex. No: 34 MERGE SORT

    Date:

    AIM:

    To write a C program to implement the concept of merge sort.

    ALGORITHM:

    Step 1: Start.

    Step 2: First you divide the number of elements by 2 and seperate them as

    two. Step 3: Divide those two which are divided by 2.

    Step 4: Divide them until you get a single element.

    Step 5: Start comparing the starting two pair of elements with each other and place them

    in ascending order.

    Step 6: When you combine them compare them so that you make sure they are sorted.

    Step 7: When all the elements are compared the array will be surely sorted in an ascending

    order. Step 8: Stop.

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    138/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    PROGRAM:

    #include

    #include

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

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

    void main(){

    int arr[30];

    int i,size;

    printf("\n\t------- Merge sorting method -------

    \n\n"); printf("Enter total no. of elements : ");

    scanf("%d",&size);

    for(i=0; i

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    139/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    void merge(int arr[],int min,int mid,int

    max){ int tmp[30];

    int i,j,k,m;

    j=min;

    m=mid+1;

    for(i=min; j

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    140/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    OUTPUT:

    ------- Merge sorting method -------

    Enter total no. of elements : 6

    Enter 1 element : 100

    Enter 2 element : 99

    Enter 3 element : 75

    Enter 4 element : 155

    Enter 5 element : 11

    Enter 6 element : 43

    ------- Merge sorted elements -------

    11 43 75 99 100 155

    RESULT:

    Thus a C program for the concept of merge sort was implemented successfully.

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    141/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    Ex. No: 35 QUICK SORT

    Date:

    AIM:

    To write a C program to implement the concept of Quick sort.

    ALGORITHM:

    Step 1: Start.

    Step 2: Choose any element of the array to be the pivot.

    Step 3: Divide all other elements (except the pivot) into two partitions.

    o All elements less than the pivot must be in the first partition.o Allelementsgreate r thanthe pivot mustbe in the secondpartition.

    Step 4: Use recursion to sort both partitions.

    Step 5: Join the first sorted partition, the pivot, and the second sorted partition.

    Step 6: Stop.

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    142/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    PROGRAM:

    #include

    #include

    void qsort(int arr[20], int fst, int last);

    void main(){

    int arr[30];

    int i,size;

    printf("Enter total no. of the elements : ");

    scanf("%d",&size);

    printf("Enter total %d elements :

    \n",size); for(i=0; i

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    143/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    if(i

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    144/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    OUTPUT:

    Enter total no. of the elements: 5

    Enter total 5 elements:

    11

    97

    54

    10

    100

    Quick sorted elements are as :

    10 11 54 97 100

    RESULT:

    Thus a C program for the concept of Quick sort was implemented successfully.

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    145/146

    WWW.CSEITQUESTIONS.BLOGSPOT.IN

    WWW.CSEITQUESTIONS.BLOGSPOT.IN V+ TEAM

  • 7/25/2019 PROGRAMMING AND DATA STRUCTURE LAB-I MANUAL

    146/146