DS II-I LAB MANUAL

Embed Size (px)

Citation preview

  • 7/30/2019 DS II-I LAB MANUAL

    1/51

    /*Recursive program to compute the nth Fibonacci number*/

    #include

    #include

    int fib(int); /*Function prototype*/

    void main()

    {

    int n,f;

    clrscr();

    printf("Enter the term: ");

    scanf("%d",&n);

    f=fib(n-1); /*Function call*/

    printf("%dth term of Fibonacci series: %d\n",n,f);

    getch();

    }

    /*Function definition*/

    int fib(int n)

    {

    if(n==0||n==1) /*Base criteria*/

    return n;

    else

    return fib(n-1)+fib(n-2); /*Recursive Function call*/

    }

    /*Recursive program to compute Factorial of an integer*/

    #include

    #include

    long int factorial(int); /*Function prototype*/

    void main()

    {

    int n;

    long int f;

    clrscr();

    printf("Enter the number: ");

    scanf("%d",&n);

    f=factorial(n); /*Function call*/

    printf("Factorial of %d: %ld\n",n,f);

  • 7/30/2019 DS II-I LAB MANUAL

    2/51

    getch();

    }

    /*Function definition*/

    long int factorial(int n)

    {

    if(n==0||n==1) /*Base criteria*/

    return 1;

    else

    return n*factorial(n-1); /*Recursive Function call*/

    }

    /*Recursive program for calculation of GCD(n,m)*/

    #include

    #include

    int gcd(int,int); /*Function prototype*/

    void main()

    {

    int n,m,g;

    clrscr();

    printf("Enter two numbers: ");

    scanf("%d%d",&n,&m);g=gcd(n,m); /*Function call*/

    printf("GCD of %d & %d: %d\n",n,m,g);

    getch();

    }

    /*Function definition*/

    int gcd(int n,int m)

    {

    if(n==0) /*Base criteria*/return m;

    if(m==0) /*Base criteria*/

    return n;

    return gcd(m,n%m); /*Recursive Function call*/

    }

  • 7/30/2019 DS II-I LAB MANUAL

    3/51

    /*Recursive program for Towers of Hanoi: N disks are to be transferred from peg S to peg D with peg A

    as the intermediate peg*/

    #include

    #include

    void transfer(int,int,int,int); /*Function prototype*/

    void main()

    {

    int n;

    clrscr();

    printf("Enter number of disks: ");

    scanf("%d",&n);

    transfer(n,1,3,2); /*Function call*/

    getch();

    }

    void transfer(int n,int s,int d,int a) /*Function definition*/

    {

    if(n==0) /*Base criteria*/

    printf("Move disk %d from %d to %d\n",n,s,d);

    else

    {

    transfer(n-1,s,a,d); /*Recursive Function call*/

    printf("Move disk %d from %d to %d\n",n,s,d);

    transfer(n-1,a,d,s); /*Recursive Function call*/

    }

    }

    /* Program to perform linear search using recursive & non-recursive functions*/

    #include

    #include

    int rlinear_search(int [],int,int,int); /*Function prototype*/

    int ilinear_search(int [],int,int); /*Function prototype*/

    void main()

    {

    int n,key,i,pos,ch;

    int a[100];

    clrscr();

    printf("Enter size of the list: ");

    scanf("%d",&n);

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

  • 7/30/2019 DS II-I LAB MANUAL

    4/51

    for(i=0;i=n) /*Base criteria*/

    return -1;

    if(i

  • 7/30/2019 DS II-I LAB MANUAL

    5/51

    void main()

    {

    int n,key,i,pos,ch;

    int a[100];

    clrscr();

    printf("Enter size of the list: ");

    scanf("%d",&n);

    printf("Enter %d elements in ascending order\n",n);for(i=0;iub) /*Base criteria*/

    return -1;

    else

    {

    mid=(lb+ub)/2;

    if(a[mid]==key)

    return mid;

    else if(key

  • 7/30/2019 DS II-I LAB MANUAL

    6/51

    {

    mid=(i+j)/2;

    if(key==a[mid])

    return mid;

    else if(key

  • 7/30/2019 DS II-I LAB MANUAL

    7/51

    /*Program to perform Fibonacci Search*/

    #include

    #include

    int fibonacci_search(int [],int,int); /*Function prototype*/

    int fib(int); /*Function prototype*/

    void main()

    {int a[100],n,key,i,pos;

    clrscr();

    printf("Enter size of the list: ");

    scanf("%d",&n);

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

    for(i=0;i

  • 7/30/2019 DS II-I LAB MANUAL

    8/51

    }

    }

    else

    {

    if(q==1)

    p=0;

    else

    {p=p+r; q=q-r; r=r-q;

    }

    }

    }

    if(key==a[p])

    return p;

    printf("Key not found\n");

    return -1;

    }

    int fib(int n) /*Function definition*/

    {

    if(n==0 || n==1) /*Base criteria*/

    return n;

    else

    return fib(n-1)+fib(n-2); /*Recursive Function call*/

    }

    /*Program to implement Bubble Sort in ascending order*/

    #include

    #include

    void bubble_sort(int [],int); /*Function prototype*/

    void swap(int *,int *); /*Function prototype*/

    void main()

    {

    int a[100],n,i;

    clrscr();

    printf("Enter size of the list: ");

    scanf("%d",&n);

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

    for(i=0;i

  • 7/30/2019 DS II-I LAB MANUAL

    9/51

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

    getch();

    }

    void bubble_sort(int a[],int n) /*Function definition*/

    {

    int i,j;

    for(i=0;i

  • 7/30/2019 DS II-I LAB MANUAL

    10/51

    void quick_sort(int a[],int lb,int ub) /*Function definition*/

    {

    int i,j,pivot;

    if(lb

  • 7/30/2019 DS II-I LAB MANUAL

    11/51

    void insertion_sort(int a[],int n) /*Function definition*/

    {

    int i,temp,pos;

    for(i=1;i0&&a[pos-1]>temp)

    {a[pos]=a[pos-1];

    --pos;

    }

    a[pos]=temp;

    }

    }

    /*Program to implement Heap Sort in ascending order*/

    #include

    #include

    void build_heap(int [],int); /*Function prototype*/void heap_sort(int [],int); /*Function prototype*/

    void main()

    {

    int a[100],n,i;

    clrscr();

    printf("Enter size of the list: ");

    scanf("%d",&n);

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

    for(i=0;i

  • 7/30/2019 DS II-I LAB MANUAL

    12/51

    while(s>0 && a[f]0;i--)

    {

    temp=a[i];

    a[i]=a[0];

    f=0;

    if(i==1)

    s=-1;else

    s=1;

    if(i>2 && a[2]>a[1])

    s=2;

    while(s>0 && a[s]>temp)

    {

    a[f]=a[s];

    f=s;

    s=2*f+1;

    if(s+1a[s])

    s++;

    if(s>i-1)

    s=-1;

    }

    a[f]=temp;

    }

    }

    /*Program to implement Radix Sort in ascending order*/

    #include

    #include

    void radix_sort(int [],int); /*Function prototype*/

    void main()

    {

    int a[100],n,i;

    clrscr();

    printf("Enter size of the list: ");

    scanf("%d",&n);

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

    for(i=0;i

  • 7/30/2019 DS II-I LAB MANUAL

    13/51

    printf("Enter a[%d]: ",i);

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

    }

    printf("\nElements before sorting: ");

    for(i=0;i

  • 7/30/2019 DS II-I LAB MANUAL

    14/51

    /*Program to implement Merge Sort in ascending order*/

    #include

    #include

    void merge_sort(int [],int,int); /*Function prototype*/

    void merge(int[],int,int,int); /*Function prototype*/

    void main()

    {

    int a[100],n,i;

    clrscr();

    printf("Enter size of the list: ");

    scanf("%d",&n);

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

    for(i=0;i

  • 7/30/2019 DS II-I LAB MANUAL

    15/51

    b[k++]=a[i];

    i++;

    }

    else

    {

    b[k++]=a[j];

    j++;

    }}

    while(i

  • 7/30/2019 DS II-I LAB MANUAL

    16/51

    case 2:

    x=pop(); /*Function call*/

    printf("\nPopped element: %d\n",x);

    break;

    case 3:

    display(); /*Function call*/

    break;

    case 4:exit(0);

    }

    printf("Do you want to continue (1-YES/2-NO): ");

    scanf("%d",&ch);

    }while(ch==1);

    getch();

    }

    void push(int x) /*Function definition*/

    {

    if(isfull()) /*Function call*/

    {

    printf("Stack is full\n");

    return;

    }

    top++;

    stack[top]=x;

    }

    int pop(void) /*Function definition*/

    {

    int x;

    if(isempty()) /*Function call*/{

    printf("Stack is empty\n");

    exit(0);

    }

    x=stack[top];

    top--;

    return x;

    }

    int isempty(void) /*Function definition*/

    {if(top==-1)

    return 1;

    else

    return 0;

    }

    int isfull(void) /*Function definition*/

    {

    if(top==MAXSTK-1)

    return 1;

  • 7/30/2019 DS II-I LAB MANUAL

    17/51

    else

    return 0;

    }

    void display() /*Function definition*/

    {

    int i;

    if(isempty()) /*Function call*/{

    printf("Stack is empty\n");

    return;

    }

    printf("Elements of stack: ");

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

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

    printf("\n");

    }

    OUTPUT

    1.PUSH

    2.POP

    3.DISPLAY

    4.QUIT

    Enter your choice: 1

    Enter an element: 10

    Do you want to continue (1-YES/2-NO): 1

    1.PUSH

    2.POP

    3.DISPLAY4.QUIT

    Enter your choice: 1

    Enter an element: 20

    Do you want to continue (1-YES/2-NO): 1

    1.PUSH

    2.POP

    3.DISPLAY

    4.QUIT

    Enter your choice: 3

    Elements of stack: 20 10

    Do you want to continue (1-YES/2-NO): 1

    1.PUSH

    2.POP

    3.DISPLAY

    4.QUIT

    Enter your choice: 2

    Popped element: 20

    Do you want to continue (1-YES/2-NO): 1

    1.PUSH

    2.POP

    3.DISPLAY

  • 7/30/2019 DS II-I LAB MANUAL

    18/51

    4.QUIT

    Enter your choice: 3

    Elements of stack: 10

    Do you want to continue (1-YES/2-NO): 2

  • 7/30/2019 DS II-I LAB MANUAL

    19/51

    6) aii) Write a program that implements stack (its operations) using arrays.

    /*Program to implement stack using arrays*/

    #include

    #include

    #include

    #define MAXSTK 100

    typedef struct

    {

    int top;

    int items[MAXSTK];

    }stack;

    void push(stack *); /*Function prototype*/

    int pop(stack *); /*Function prototype*/

    void display(stack *); /*Function prototype*/

    int isempty(stack *); /*Function prototype*/int isfull(stack *); /*Function prototype*/

    void main()

    {

    stack s;

    int ch,x;

    clrscr();

    s.top=-1;

    do

    {

    printf("1.PUSH\n2.POP\n3.DISPLAY\n4.QUIT\n");

    printf("Enter your choice: ");

    scanf("%d",&ch);

    switch(ch)

    {

    case 1: push(&s); /*Function call*/

    break;

    case 2:

    x=pop(&s); /*Function call*/

    printf("\nPopped element: %d\n",x);

    break;

    case 3: display(&s); /*Function call*/

    break;case 4: exit(0);

    }

    printf("Do you want to continue (1-YES/2-NO): ");

    scanf("%d",&ch);

    }while(ch==1);

    getch();

    }

    void push(stack *s) /*Function definition*/

    {

    int x;

  • 7/30/2019 DS II-I LAB MANUAL

    20/51

    if(isfull(s)) /*Function call*/

    {

    printf("Stack is full\n");

    return;

    }

    printf("Enter an element: ");

    scanf("%d",&x);

    s->top++;s->items[s->top]=x;

    }

    int pop(stack *s) /*Function definition*/

    {

    int x;

    if(isempty(s)) /*Function call*/

    {

    printf("Stack is empty\n");

    exit(0);

    }

    x=s->items[s->top];s->top--;

    return x;

    }

    int isempty(stack *s) /*Function definition*/

    {

    if(s->top==-1)

    return 1;

    else

    return 0;

    }

    int isfull(stack *s) /*Function definition*/

    {

    if(s->top==MAXSTK-1)

    return 1;

    else

    return 0;

    }

    void display(stack *s) /*Function definition*/

    {

    int i;

    if(isempty(s)) /*Function call*/

    {

    printf("Stack is empty\n");

    return;

    }

    printf("Elements of stack: ");

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

    printf("%d ",s->items[i]);

    printf("\n");

    }

  • 7/30/2019 DS II-I LAB MANUAL

    21/51

    OUTPUT

    1.PUSH

    2.POP

    3.DISPLAY

    4.QUIT

    Enter your choice: 1

    Enter an element: 10

    Do you want to continue (1-YES/2-NO): 11.PUSH

    2.POP

    3.DISPLAY

    4.QUIT

    Enter your choice: 1

    Enter an element: 20

    Do you want to continue (1-YES/2-NO): 1

    1.PUSH

    2.POP

    3.DISPLAY

    4.QUIT

    Enter your choice: 3

    Elements of stack: 20 10

    Do you want to continue (1-YES/2-NO): 1

    1.PUSH

    2.POP

    3.DISPLAY

    4.QUIT

    Enter your choice: 2

    Popped element: 20

    Do you want to continue (1-YES/2-NO): 1

    1.PUSH

    2.POP3.DISPLAY

    4.QUIT

    Enter your choice: 2

    Popped element: 10

    Do you want to continue (1-YES/2-NO): 1

    1.PUSH

    2.POP

    3.DISPLAY

    4.QUIT

    Enter your choice: 3

    Stack is emptyDo you want to continue (1-YES/2-NO): 2

  • 7/30/2019 DS II-I LAB MANUAL

    22/51

    6) b) Write a program that implements stack (its operations) using linked list.

    /*Program to implement stack using Linked List*/

    #include

    #include

    #include

    struct link

    {

    int info;

    struct link *next;

    };

    typedef struct link node;

    node *start=NULL;

    void push(); /*Function prototype*/

    int pop(); /*Function prototype*/

    void display(); /*Function prototype*/

    int isempty(); /*Function prototype*/

    void main()

    {

    int ch,x;

    clrscr();

    do

    {

    printf("1.PUSH\n2.POP\n3.DISPLAY\n4.QUIT\n");

    printf("Enter your choice: ");

    scanf("%d",&ch);

    switch(ch)

    {

    case 1:

    push(); /*Function call*/

    break;

    case 2:

    x=pop(); /*Function call*/

    printf("\nPopped element: %d\n",x);

    break;

    case 3:

    display(); /*Function call*/

    break;

    case 4:exit(0);

    }

    printf("Do you want to continue (1-YES/2-NO): ");

    scanf("%d",&ch);

    }while(ch==1);

    getch();

    }

    void push() /*Function definition*/

    {

    node *p;

  • 7/30/2019 DS II-I LAB MANUAL

    23/51

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

    if(p==NULL)

    {

    printf("Out of memory space");

    exit(0);

    }

    printf("Enter an element: ");

    scanf("%d",&p->info);p->next=start;

    start=p;

    }

    int pop() /*Function definition*/

    {

    int x;

    node *temp;

    temp=start;

    if(isempty())

    {

    printf("Stack is empty\n");exit(0);

    }

    x=start->info;

    start=start->next;

    free(temp);

    return x;

    }

    int isempty() /*Function definition*/

    {

    if(start==NULL)

    return 1;else

    return 0;

    }

    void display() /*Function definition*/

    {

    node *temp;

    if(isempty())

    {

    printf("Stack is empty\n");

    return;

    }temp=start;

    printf("Elements of stack: ");

    while(temp->next!=NULL)

    {

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

    temp=temp->next;

    }

    printf("%d\n",temp->info);

    }

  • 7/30/2019 DS II-I LAB MANUAL

    24/51

    OUTPUT

    1.PUSH

    2.POP

    3.DISPLAY

    4.QUIT

    Enter your choice: 3

    Stack is empty

    Do you want to continue (1-YES/2-NO): 11.PUSH

    2.POP

    3.DISPLAY

    4.QUIT

    Enter your choice: 1

    Enter an element: 10

    Do you want to continue (1-YES/2-NO): 1

    1.PUSH

    2.POP

    3.DISPLAY

    4.QUIT

    Enter your choice: 1

    Enter an element: 20

    Do you want to continue (1-YES/2-NO): 1

    1.PUSH

    2.POP

    3.DISPLAY

    4.QUIT

    Enter your choice: 3

    Elements of stack: 20->10

    Do you want to continue (1-YES/2-NO): 1

    1.PUSH

    2.POP3.DISPLAY

    4.QUIT

    Enter your choice: 2

    Popped element: 20

    Do you want to continue (1-YES/2-NO): 1

    1.PUSH

    2.POP

    3.DISPLAY

    4.QUIT

    Enter your choice: 3

    Elements of stack: 10Do you want to continue (1-YES/2-NO): 2

    7) a) Write a program that uses Stack operations to convert infix expression into postfix expression.

    /*Program to convert infix to postfix expression*/

    #include

    #include

    #include

    #include

    #define MAXSTK 100

    int top=-1;

    char stack[MAXSTK];

  • 7/30/2019 DS II-I LAB MANUAL

    25/51

    void push(char);

    char pop(void);

    int isp(char);

    int icp(char);

    void main()

    {

    char infix[100],postfix[100],ch,ch1;int i,j;

    clrscr();

    printf("Enter infix expression: ");

    gets(infix);

    push('(');

    for(i=0,j=0;infix[i]!='\0';i++)

    {

    ch=infix[i];

    if(isalpha(ch))

    postfix[j++]=ch;

    else

    {

    if(ch==')')

    {

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

    {

    ch1=pop();

    postfix[j++]=ch1;

    }

    top--;

    }

    else

    {while(isp(stack[top])>icp(ch))

    {

    ch1=pop();

    postfix[j++]=ch1;

    }

    push(ch);

    }

    }

    }

    postfix[j++]='\0';

    printf("\nPostfix Expression: %s\n",postfix);getch();

    }

    void push(char x)

    {

    top++;

    stack[top]=x;

    }

    char pop(void)

    {

  • 7/30/2019 DS II-I LAB MANUAL

    26/51

    char x;

    x=stack[top];

    top--;

    return x;

    }

    int isp(char c)

    {switch(c)

    {

    case '(': return 0;

    case '*':

    case '/': return 2;

    case '+':

    case '-': return 1;

    }

    }

    int icp(char c)

    {

    switch(c)

    {

    case '(': return 4;

    case '*':

    case '/': return 2;

    case '+':

    case '-': return 1;

    }

    }

    7) b) Write a program that implements Queue (its operations) using arrays./*Program to implement Queue using arrays*/

    #include

    #include

    #include

    #define MAXQUE 100

    int front=0,rear=-1;

    int que[MAXQUE];

    void insert(int);

    int delet(void);

    void display(void);int isempty(void);

    int isfull(void);

    void main()

    {

    int ch,x;

    clrscr();

    do

    {

    printf("1.INSERT\n2.DELETE\n3.DISPLAY\n4.QUIT\n");

  • 7/30/2019 DS II-I LAB MANUAL

    27/51

    printf("Enter your choice: ");

    scanf("%d",&ch);

    switch(ch)

    {

    case 1:

    printf("Enter an element: ");

    scanf("%d",&x);

    insert(x);break;

    case 2:

    x=delet();

    printf("\nDeleted element: %d\n",x);

    break;

    case 3:

    display();

    break;

    case 4:

    exit(0);

    }

    printf("Do you want to continue (1-YES/2-NO): ");

    scanf("%d",&ch);

    }while(ch==1);

    getch();

    }

    void insert(int x)

    {

    if(isfull())

    {

    printf("Queue is full\n");

    return;}

    rear++;

    que[rear]=x;

    }

    int delet(void)

    {

    int x;

    if(isempty())

    {

    printf("Queue is empty\n");exit(0);

    }

    x=que[front];

    front++;

    return x;

    }

    int isempty(void)

    {

    if(front>rear)

  • 7/30/2019 DS II-I LAB MANUAL

    28/51

    return 1;

    else

    return 0;

    }

    int isfull(void)

    {

    if(rear==MAXQUE-1)return 1;

    else

    return 0;

    }

    void display()

    {

    int i;

    if(isempty())

    {

    printf("Queue is empty\n");

    return;

    }

    printf("Elements of Queue: ");

    for(i=front;i

  • 7/30/2019 DS II-I LAB MANUAL

    29/51

    printf("1.INSERT\n2.DELETE\n3.DISPLAY\n4.QUIT\n");

    printf("Enter your choice: ");

    scanf("%d",&ch);

    switch(ch)

    {

    case 1:

    insert();

    break;case 2:

    x=delet();

    printf("\nDeleted element: %d\n",x);

    break;

    case 3:

    display();

    break;

    case 4:

    exit(0);

    }

    printf("Do you want to continue (1-YES/2-NO): ");

    scanf("%d",&ch);

    }while(ch==1);

    getch();

    }

    void insert()

    {

    node *p;

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

    if(p==NULL)

    {

    printf("Out of memory space");exit(0);

    }

    printf("Enter an element: ");

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

    p->next=NULL;

    if(rear==NULL)

    {

    front=rear=p;

    }

    else

    {rear->next=p;

    rear=p;

    }

    }

    int delet()

    {

    int x;

    node *temp;

    if(isempty())

    {

  • 7/30/2019 DS II-I LAB MANUAL

    30/51

    printf("QUEUE is empty\n");

    exit(0);

    }

    temp=front;

    x=temp->info;

    if(front==rear)

    front=rear=NULL;

    elsefront=front->next;

    free(temp);

    return x;

    }

    int isempty()

    {

    if(front==NULL&&rear==NULL)

    return 1;

    else

    return 0;

    }

    void display()

    {

    node *temp;

    if(isempty())

    {

    printf("QUEUE is empty\n");

    return;

    }

    temp=front;

    printf("Elements of Queue: ");while(temp->next!=NULL)

    {

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

    temp=temp->next;

    }

    printf("%d\n",temp->info);

    }

  • 7/30/2019 DS II-I LAB MANUAL

    31/51

    8) a) /*Program to create singly linked list*/

    #include

    #include

    #include

    struct list

    {

    int info;struct list *next;

    };

    typedef struct list node;

    node *start=NULL;

    void createlist(void);

    void display(void);

    void main()

    {

    clrscr();

    createlist();

    display();

    getch();

    }

    void createlist()

    {

    node *p,*ptr;

    int ch;

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

    if(p==NULL)

    {printf("Out of memory");

    exit(0);

    }

    printf("Enter the element: ");

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

    p->next=NULL;

    start=ptr=p;

    printf("Do you want to continue (1-YES/2-NO): ");

    scanf("%d",&ch);

    while(ch==1)

    {p=(node *)malloc(sizeof(node));

    if(p==NULL)

    {

    printf("Out of memory");

    exit(0);

    }

    printf("Enter the element: ");

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

    p->next=NULL;

    ptr->next=p;

  • 7/30/2019 DS II-I LAB MANUAL

    32/51

    ptr=p;

    printf("Do you want to continue (1-YES/2-NO): ");

    scanf("%d",&ch);

    }

    }

    void display()

    {node *ptr;

    if(start==NULL)

    {

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

    return;

    }

    ptr=start;

    printf("Elements of Linked List: ");

    while(ptr->next!=NULL)

    {

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

    ptr=ptr->next;

    }

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

    }

    8) b) /*Program to perform insertion operation on a singly linked list*/

    #include

    #include

    #include

    struct list{

    int info;

    struct list *next;

    };

    typedef struct list node;

    node *start=NULL;

    void createlist(void);

    void display(void);

    void insert(int);

    void main()

    {

    int x,ch;

    clrscr();

    createlist();

    display();

    do

    {

    printf("Enter element to be inserted: ");

  • 7/30/2019 DS II-I LAB MANUAL

    33/51

    scanf("%d",&x);

    insert(x);

    display();

    printf("Do you want to continue(1-YES/2-NO): ");

    scanf("%d",&ch);

    }while(ch==1);

    getch();

    }

    void createlist()

    {

    node *p,*ptr;

    int ch;

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

    if(p==NULL)

    {

    printf("Out of memory");

    exit(0);

    }

    printf("Enter the element: ");

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

    p->next=NULL;

    start=ptr=p;

    printf("Do you want to continue (1-YES/2-NO): ");

    scanf("%d",&ch);

    while(ch==1)

    {

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

    if(p==NULL)

    {

    printf("Out of memory");exit(0);

    }

    printf("Enter the element: ");

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

    p->next=NULL;

    ptr->next=p;

    ptr=p;

    printf("Do you want to continue (1-YES/2-NO): ");

    scanf("%d",&ch);

    }

    }

    void display()

    {

    node *ptr;

    if(start==NULL)

    {

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

    return;

    }

    ptr=start;

  • 7/30/2019 DS II-I LAB MANUAL

    34/51

    printf("Elements of Linked List: ");

    while(ptr->next!=NULL)

    {

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

    ptr=ptr->next;

    }

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

    }

    void insert(int x)

    {

    node *p,*curr,*prev;

    int ch,pos,i;

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

    p->info=x;

    p->next=NULL;

    printf("Enter your choice 1.Beginning 2.End 3.Middle: ");

    scanf("%d",&ch);

    if(ch==1)

    {

    p->next=start;

    start=p;

    }

    else if(ch==2)

    {

    curr=start;

    while(curr->next!=NULL)

    curr=curr->next;

    curr->next=p;

    }

    else if(ch==3){

    printf("Enter the position: ");

    scanf("%d",&pos);

    curr=start;

    for(i=1;inext;

    }

    prev->next=p;

    p->next=curr;}

    else

    printf("Entered wrong option\n");

    }

    8) c) /*Program to perform deletion operation on a singly linked list*/

    #include

    #include

    #include

  • 7/30/2019 DS II-I LAB MANUAL

    35/51

    struct list

    {

    int info;

    struct list *next;

    };

    typedef struct list node;

    node *start=NULL;void createlist(void);

    void display(void);

    int delet(void);

    void main()

    {

    int x,ch;

    clrscr();

    createlist();

    display();

    do

    {

    x=delet();

    printf("Deleted element: %d\n",x);

    display();

    printf("Do you want to continue(1-YES/2-NO): ");

    scanf("%d",&ch);

    }while(ch==1);

    }

    void createlist()

    {

    node *p,*ptr;int ch;

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

    if(p==NULL)

    {

    printf("Out of memory");

    exit(0);

    }

    printf("Enter the element: ");

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

    p->next=NULL;

    start=ptr=p;printf("Do you want to continue (1-YES/2-NO): ");

    scanf("%d",&ch);

    while(ch==1)

    {

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

    if(p==NULL)

    {

    printf("Out of memory");

    exit(0);

    }

  • 7/30/2019 DS II-I LAB MANUAL

    36/51

    printf("Enter the element: ");

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

    p->next=NULL;

    ptr->next=p;

    ptr=p;

    printf("Do you want to continue (1-YES/2-NO): ");

    scanf("%d",&ch);

    }}

    void display()

    {

    node *ptr;

    if(start==NULL)

    {

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

    return;

    }

    ptr=start;

    printf("Elements of Linked List: ");

    while(ptr->next!=NULL)

    {

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

    ptr=ptr->next;

    }

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

    }

    int delet()

    {

    node *p,*curr,*prev;int ch,pos,i,x;

    printf("Enter your choice to delete 1.Beginning 2.End 3.Middle: ");

    scanf("%d",&ch);

    if(ch==1)

    {

    p=start;

    x=start->info;

    start=start->next;

    free(p);

    }

    else if(ch==2){

    curr=start;

    while(curr->next!=NULL)

    {

    prev=curr;

    curr=curr->next;

    }

    prev->next=NULL;

    x=curr->info;

    free(curr);

  • 7/30/2019 DS II-I LAB MANUAL

    37/51

    }

    else if(ch==3)

    {

    printf("Enter the position: ");

    scanf("%d",&pos);

    curr=start;

    for(i=1;inext;

    }

    prev->next=curr->next;

    x=curr->info;

    free(curr);

    }

    else

    printf("Entered wrong option\n");

    return x;

    }

    9) b) /*Program to reverse elements of a singly linked list*/

    #include

    #include

    #include

    struct list

    {

    int info;

    struct list *next;

    };

    typedef struct list node;

    node *start=NULL;

    void createlist(void);

    void display(void);

    void reverselist(void);

    void main()

    {

    clrscr();createlist();

    display();

    reverselist();

    display();

    getch();

    }

    void createlist()

    {

    node *p,*ptr;

  • 7/30/2019 DS II-I LAB MANUAL

    38/51

    int ch;

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

    if(p==NULL)

    {

    printf("Out of memory");

    exit(0);

    }

    printf("Enter the element: ");scanf("%d",&p->info);

    p->next=NULL;

    start=ptr=p;

    printf("Do you want to continue (1-YES/2-NO): ");

    scanf("%d",&ch);

    while(ch==1)

    {

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

    if(p==NULL)

    {

    printf("Out of memory");

    exit(0);

    }

    printf("Enter the element: ");

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

    p->next=NULL;

    ptr->next=p;

    ptr=p;

    printf("Do you want to continue (1-YES/2-NO): ");

    scanf("%d",&ch);

    }

    }

    9) c) /*Program to store a Polynomial expression using linked list*/

    #include

    #include

    #include

    struct poly

    {

    int exp;

    int coe;

    struct poly *next;};

    typedef struct poly node;

    node *start=NULL;

    void createpolynomial();

    void display();

    void main()

    {

    clrscr();

  • 7/30/2019 DS II-I LAB MANUAL

    39/51

    createpolynomial();

    display();

    getch();

    }

    void createpolynomial()

    {

    node *p,*ptr;int ch,count=0;

    do

    {

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

    printf("Enter coefficient & exponent: ");

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

    p->next=NULL;

    count++;

    if(count==1)

    {

    start=p;

    ptr=p;

    }

    else

    {

    ptr->next=p;

    ptr=p;

    }

    printf("Do you want to enter more terms(1-YES/2-NO): ");

    scanf("%d",&ch);

    }while(ch==1);

    }

    void display()

    {

    node *ptr;

    ptr=start;

    if(ptr==NULL)

    printf("No Polynomial expression\n");

    else

    {

    while(ptr->next!=NULL)

    {

    printf("%dX^%d+",ptr->coe,ptr->exp);ptr=ptr->next;

    }

    printf("%d\n",ptr->coe);

    }

    }

    9) d) /*Program to represent the given Sparse matrix using arrays*/

    #include

    #include

  • 7/30/2019 DS II-I LAB MANUAL

    40/51

    #include

    void createsparse(int [][3]);

    void displaysparse(int [][3]);

    void main()

    {

    int smat[100][3];clrscr();

    createsparse(smat);

    displaysparse(smat);

    getch();

    }

    void createsparse(int smat[][3])

    {

    int i;

    printf("Enter number of rows of sparse matrix: ");

    scanf("%d",&smat[0][0]);

    printf("Enter number of columns of sparse matrix: ");

    scanf("%d",&smat[0][1]);

    printf("Enter number of nonzero elements of sparse matrix: ");

    scanf("%d",&smat[0][2]);

    for(i=1;i

  • 7/30/2019 DS II-I LAB MANUAL

    41/51

    int col;

    int value;

    struct sparse *next;

    };

    typedef struct sparse node;

    node *start=NULL;

    void createsparse();void displaysparse();

    void main()

    {

    clrscr();

    createsparse();

    displaysparse();

    getch();

    }

    void createsparse()

    {

    node *p,*curr;

    int r,c,v,i;

    printf("Enter number of rows of sparse matrix: ");

    scanf("%d",&r);

    printf("Enter number of columns of sparse matrix: ");

    scanf("%d",&c);

    printf("Enter number of nonzero elements of sparse matrix: ");

    scanf("%d",&v);

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

    p->next=NULL;

    p->row=r;p->col=c;

    p->value=v;

    start=p;

    curr=p;

    for(i=1;irow);

    printf("Enter the column number: ");

    scanf("%d",&p->col);printf("Enter the value: ");

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

    p->next=NULL;

    curr->next=p;

    curr=p;

    }

    }

    void displaysparse()

    {

  • 7/30/2019 DS II-I LAB MANUAL

    42/51

    node *curr,*prev;

    if(start==NULL)

    printf("Sparse matrix is empty\n");

    else

    {

    curr=start;

    printf("Sparse matrix\n");

    while(curr!=NULL){

    printf("%d %d %d\n",curr->row,curr->col,curr->value);

    curr=curr->next;

    }

    }

    }

    11) a) /*Program to create Binary Search Tree*/

    #include

    #include

    #include

    struct tree

    {

    int info;

    struct tree *left;

    struct tree *right;

    };

    typedef struct tree node;

    node *root=NULL;

    void create_tree(int);void display_tree();

    void main()

    {

    int n;

    clrscr();

    printf("Enter number of elements in BST: ");

    scanf("%d",&n);

    create_tree(n);

    display_tree(node *);

    getch();}

    void create_tree(int n)

    {

    int i,flag;

    node *p,*curr,*prev;

    for(i=1;i

  • 7/30/2019 DS II-I LAB MANUAL

    43/51

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

    p->left=p->right=NULL;

    if(i==1)

    root=p;

    else

    {

    curr=root;

    flag=1;while(curr!=NULL)

    {

    prev=curr;

    if(p->infoinfo)

    curr=curr->left;

    else if(p->info>curr->info)

    curr=curr->right;

    else

    {

    flag=0;

    break;

    }

    }

    if(flag)

    {

    if(p->infoinfo)

    prev->left=p;

    else

    prev->right=p;

    }

    }

    }

    }

    void display_tree(node *t)

    {

    if(t==NULL)

    return;

    display_tree(t->left);

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

    display_tree(t->right);

    }

    11) b) /*Program to insert a node into Binary Search Tree*/

    #include

    #include

    #include

    struct tree

    {

    int info;

    struct tree *left;

    struct tree *right;

  • 7/30/2019 DS II-I LAB MANUAL

    44/51

    };

    typedef struct tree node;

    node *root=NULL;

    void insert(int);

    void display_tree(node *);

    void main(){

    int n,ch;

    clrscr();

    do

    {

    printf("Enter element to be inserted into BST: ");

    scanf("%d",&n);

    insert(n);

    display_tree(root);

    printf("\nDo you want to continue(1-YES/2-NO): ");

    scanf("%d",&ch);

    }while(ch==1);

    getch();

    }

    void insert(int n)

    {

    int flag;

    node *p,*curr,*prev;

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

    p->info=n;

    p->left=p->right=NULL;

    if(root==NULL)root=p;

    else

    {

    curr=root;

    flag=1;

    while(curr!=NULL)

    {

    prev=curr;

    if(p->infoinfo)

    curr=curr->left;

    else if(p->info>curr->info)curr=curr->right;

    else

    {

    flag=0;

    free(p);

    break;

    }

    }

    if(flag)

    {

  • 7/30/2019 DS II-I LAB MANUAL

    45/51

    if(p->infoinfo)

    prev->left=p;

    else

    prev->right=p;

    }

    }

    }

    void display_tree(node *t)

    {

    if(t==NULL)

    return;

    display_tree(t->left);

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

    display_tree(t->right);

    }

    11) c) /*Program to delete a node from Binary Search Tree*/

    #include

    #include

    #include

    struct tree

    {

    int info;

    struct tree *left;

    struct tree *right;

    };

    typedef struct tree node;node *root=NULL;

    void create_tree(int);

    node * delet_tree(node *,int);

    void display_tree();

    void main()

    {

    int n,f;

    node *t;

    clrscr();

    printf("Enter number of elements in BST: ");scanf("%d",&n);

    create_tree(n);

    display_tree(node *);

    printf("Enter element to be deleted: ");

    scanf("%d",&ele);

    t=delet_tree(ele);

    if(t!=NULL)

    display_tree(root);

    /* else

    printf("Element is not present in the BST\n");*/

  • 7/30/2019 DS II-I LAB MANUAL

    46/51

    getch();

    }

    void create_tree(int n)

    {

    int i,flag;

    node *p,*curr,*prev;

    for(i=1;iinfo);

    p->left=p->right=NULL;

    if(i==1)

    root=p;

    else

    {

    curr=root;

    flag=1;

    while(curr!=NULL)

    {

    prev=curr;

    if(p->infoinfo)

    curr=curr->left;

    else if(p->info>curr->info)

    curr=curr->right;

    else

    {

    flag=0;

    break;

    }}

    if(flag)

    {

    if(p->infoinfo)

    prev->left=p;

    else

    prev->right=p;

    }

    }

    }

    }

    node * delet_tree(node *t,int x)

    {

    node *temp;

    if(t==NULL)

    printf("\nElement is not found in the BST\n");

    else

    {

    if(xinfo)

    t->left=delet_tree(t->left,x);

  • 7/30/2019 DS II-I LAB MANUAL

    47/51

    else if(x>t->info)

    t->right=delet_tree(t->right,x);

    }

    }

    void display_tree(node *t)

    {

    if(t==NULL)return;

    display_tree(t->left);

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

    display_tree(t->right);

    }

    12)a) /*Dijkstras Algorithm*/

    #include

    #include

    #define SIZE 20

    int adj[SIZE][SIZE];

    int length[SIZE],set[SIZE];

    int path[SIZE];

    int i,j,k,n;

    void dijkstra(int);

    void displaypath(int);

    int searchmin();

    void main(){

    int s;

    clrscr();

    printf("Enter the no. of vertices: ");

    scanf("%d",&n);

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

    for(i=1;i

  • 7/30/2019 DS II-I LAB MANUAL

    48/51

    dijkstra(s);

    printf("Distance matrix\n");

    displaypath(s);

    getch();

    }

    void displaypath(int s)

    {for(i=1;i

  • 7/30/2019 DS II-I LAB MANUAL

    49/51

    length[s]=0; //Source vertex is implicitly enumerated with length as 0

    /*ITERATION*/

    flag=0; //flag is for controlling the iteration

    while(!flag)

    {

    j=searchmin(); //Find a vertex j which has min. distance among those

    //vetices not yet enumerated for the shortest path

    set[j]=1; //Vertex j is enumeratedfor(i=1;i

  • 7/30/2019 DS II-I LAB MANUAL

    50/51

    void displaypath(); //Function Prototype

    void main()

    {

    clrscr();

    printf("Enter the no. of vertices: ");

    scanf("%d",&n);

    printf("Enter the values of Adjacency matrix\n");for(i=0;i

  • 7/30/2019 DS II-I LAB MANUAL

    51/51

    {

    for(j=0;j