Top Banner

of 26

Lab Manual Dsa

Jun 03, 2018

Download

Documents

Tanzeem Syed
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 8/12/2019 Lab Manual Dsa

    1/26

    LAB MANUAL

    SUBJECT:-DATASTRUCTURES

  • 8/12/2019 Lab Manual Dsa

    2/26

    Theem College of Engineering,BoisarSession:-2013-2014

    Semester: Third (Information Technology)

    LIST OF DATA STRUCTURES & ALGORITHMICS Lab

    1. Write a program for implementation of Fabonacii Series.

    2. Write a program for displaying following pattern.

    1

    4 9 16

    7 8 6 93. Write a program for displaying following pattern.

    * * * *

    * * *

    *

    4. Write a program for implementation of stack using push ( ) and pop( ) methods.

    5. Write a program to implement.

    6. Write a program for implementation of

    7. Write a program for implementation of binary search.

    8. Write a program to perform Quick sort.

    9. Write a program to perform the following operations in

    10. Write a program for implementation of singly linked list

    11. Write a program to perform preorder, inorder and post order traversal of binary search

    tree .

    12. Write a program to solve postfix expression using stack.

    FACULTY NAME:-

    Prof. Syed Tanzeem

  • 8/12/2019 Lab Manual Dsa

    3/26

    EX.NO:

    CONVERT INFIX TO POSTFIX EXPRESSION

    Aim: - Write a program to solve postfix expression using stack.

    Theory:-

    Infix Expression:

    The arithmetic is an expression in which opearator is present in between two operands called as InfixExpressions. Ex :( a*b)

    Postfix Expression:

    The arithmetic is an expression in which operator is present after operands called as Postfix Expression.

    Ex: ab*

    Algorithms:-

    Infixtopostfix(P,I ) :

    Description: Here I is an arithmetic expression written in infix notation and P is theequivalent postfix expression generated by this algorithm.

    1. Push ( left parenthesis onto stack.

    2. Add ) right parenthesis to the end of expression I.

    3. Scan I from left to right and repeat step 4 for each element of I

    until the stack becomes empty.

    4. If the scanned element is:

    (a) an operand then add it to P.

    (b) a left parenthesis then push it onto stack.

    (c) an operator then:

    (i) Pop from stack and add to P each operator

    which has the same or higher precedence then

    the scanned operator.

    (ii) Add newly scanned operator to stack.

    (d) a right parenthesis then:

    (i) Pop from stack and add to P each operator

  • 8/12/2019 Lab Manual Dsa

    4/26

    until a left parenthesis is encountered.

    (ii) Remove the left parenthesis. [End of Step 4 If][End of step 3 For Loop]

    5. Exit.Program code:-

    #include

    #define SIZE 50 /* Size of Stack */

    #include

    char s[SIZE];

    int top = -1; /* Global declarations */

    push(char elem) { /* Function for PUSH operation */

    s[++top] = elem;

    }

    char pop() { /* Function for POP operation */

    return (s[top--]);

    }

    int pr(char elem) { /* Function for precedence */

    switch (elem) {

    case '#':

    return 0;

    case '(':

    return 1;

    case '+':

    case '-':

    return 2;

    case '*':

    case '/':

    return 3;

    }

    }

  • 8/12/2019 Lab Manual Dsa

    5/26

    main() { /* Main Program */

    char infx[50], pofx[50], ch, elem;

    int i = 0, k = 0;

    printf("\n\nRead the Infix Expression ? ");

    scanf("%s", infx);

    push('#');

    while ((ch = infx[i++]) != '\0') {

    if (ch == '(')

    push(ch);

    else if (isalnum(ch))

    pofx[k++] = ch;

    else if (ch == ')') {while (s[top] != '(')

    pofx[k++] = pop();

    elem = pop(); /* Remove ( */

    } else { /* Operator */

    while (pr(s[top]) >= pr(ch))

    pofx[k++] = pop();

    push(ch);

    }

    }

    while (s[top] != '#') /* Pop from stack till empty */

    pofx[k++] = pop();

    pofx[k] = '\0'; /* Make pofx as valid string */

    printf("\n\nGiven Infix Expn: %s\n\nPostfix Expn: %s\n", infx, pofx);

    }

  • 8/12/2019 Lab Manual Dsa

    6/26

    Output:-

    Assignments:

    i)Write a program for infix expression:(a*b)+c/e.

    Viva Questions:

    1)What are the steps to convert infix expression to equivalent postfix expression?

    2)Which data structure used for this purpose?

    3)How to decide precedence of operators?

  • 8/12/2019 Lab Manual Dsa

    7/26

    EX. NO : 7

    INSERTION IN AVL TREE

    AIM :

    To write a C program to perform insertion in an AVL tree.

    ALGORITHM :

    Step 1: Read the elements to be inserted into an AVL tree one at a time.

    Step 2: Insert each element into an initially empty AVL tree based on the BST tree property.

    Step 3: After inserting an element check whether the balance property is maintained.If not then

    Step 3.1 :Identify the node at which the balance is lost.

    Step 3.2: Find the kind of insertion . If it is left-left or right right then perform asingle , .

    rotation.

    Step 3.3: If it is left-right or right-left then perform a double rotation.

    Step 4: Repeat steps 2 and 3 till all the elements are inserted.

    Step 5: Display the elements present in the AVL tree according to inorder traversal.

    PROGRAM:

    #include

    #include

    #include

    typedef struct node *pos;

    typedef struct node *avltree;

    avltree t;

    struct node

    {

    int element;

  • 8/12/2019 Lab Manual Dsa

    8/26

    avltree left;

    avltree right;

    int height;

    };

    avltree insert(int,avltree);

    int max(int,int);

    int height(pos p);

    pos singlerotatewithleft(pos);

    pos singlerotatewithright(pos);

    pos doublerotatewithleft(pos); pos doublerotatewithright(pos);

    void display(avltree);

    void main()

    {

    int ch,ele;

    clrscr();

    t=NULL;

    do

    {

    printf("\nMENU\n");

    printf("\n1. Insertion\n");

    printf("\n2. Display\n");

    printf("\n3. Exit\n");

    printf("\nEnter your choice:");

    scanf("%d",&ch);

    switch(ch)

  • 8/12/2019 Lab Manual Dsa

    9/26

    {

    case 1: printf("\nEnter the element to be inserted:");

    scanf("%d",&ele);

    t=insert(ele,t);

    printf("\nThe element is inserted");

    break;

    case 2: printf("\nThe elements in the AVL TREE are:");

    display(t);

    break;

    case 3: exit(0);

    break;

    }

    }while(chelement=x;t->height=0;

    t->left=t->right=NULL;

  • 8/12/2019 Lab Manual Dsa

    10/26

    }

    }

    else

    if(xelement)

    {

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

    if(height(t->left)-height(t->right)==2)

    if(xleft->element)

    t=singlerotatewithleft(t);

    else

    t=doublerotatewithleft(t);

    }

    else

    if(x>t->element)

    {

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

    if(height(t->right)-height(t->left)==2)

    if(x>t->left->element)

    t=singlerotatewithright(t);

    else

    t=doublerotatewithright(t);

    }

    t->height=max(height(t->left),height(t->right))+1;

    return t;

    }

  • 8/12/2019 Lab Manual Dsa

    11/26

    int height(pos p)

    {

    if(p==NULL)

    return -1;

    else

    return p->height;

    }

    int max(int p,int q)

    {

    if(p>q)

    return p;

    else

    return q;

    }

    pos singlerotatewithleft(pos k2)

    {

    pos k1;

    k1=k2->left;

    k2->left=k1->right;

    k1->right=k2;

    k2->height=max(height(k2->left),height(k2->right))+1;

    k1->height=max(height(k1->left),k2->height)+1;

    return k1;

    }

    pos singlerotatewithright(pos k1)

  • 8/12/2019 Lab Manual Dsa

    12/26

    {

    pos k2;

    k2=k1->right;

    k1->right=k2->left;

    k2->left=k1;

    k1->height=max(height(k1->left),height(k1->right))+1;

    k2->height=max(k1->height,height(k2->right))+1;

    return k2;

    }

    pos doublerotatewithleft(pos k3)

    {

    k3->left=singlerotatewithright(k3->left);

    return singlerotatewithleft(k3);

    }

    pos doublerotatewithright(pos k1)

    {

    k1->right=singlerotatewithleft(k1->right);

    return singlerotatewithright(k1);

    }

    void display(avltree t)

    {

    if(t!=NULL)

    {

    display(t->left);

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

  • 8/12/2019 Lab Manual Dsa

    13/26

    display(t->right);

    }

    }

    OUTPUT:

    MENU

    1. Insertion

    2. Display

    3. ExitEnter your choice:1Enter the element to be inserted:10

    The element is inserted

    MENU

    1. Insertion

    2. Display

    3. ExitEnter your choice:2The elements in the AVL TREE are: 10

    MENU

    1. Insertion

    2. Display

    3. ExitEnter your choice:1Enter the element to be inserted:20

    The element is inserted

    MENU

    1. Insertion

    2. Display

  • 8/12/2019 Lab Manual Dsa

    14/26

    3. ExitEnter your choice: 3

    RESULT:

    Thus the C program to implement AVL tree is executed.

  • 8/12/2019 Lab Manual Dsa

    15/26

    EX. NO: 6

    BINARY SEARCH TREE

    AIM:

    To write a C program to perform various operations on a binary search tree

    ALGORITHM:

    CREATION OF BINARY SEARCH TREE:

    Step 1: Enter the number of elements in the tree n

    Step 2: Enter the n elements into the binary search tree

    Step 3: As each element is put into the tree check whether tree is binary

    SEARCH TREE PROPERTY IS MAINTAINED:

    Step 1: Enter the element to be inserted x

    Step 2: If x< root, Keep moving towards the left sub tree

    Step 3: If x>root, Keep moving towards right sub tree

    Step 4: Perform steps 2 and 3 for the sub trees until the exact location of insertion is

    Found

    Step 5: Insert at that location and display the new tree using inorder traversal

    DELETION OF A NODE:

    Step 1: Enter the element to be deleted x

    Step 2: If X has no children then it can be deleted immediately

    Step 3: If x has no child then adjust the parents pointer by passing the node

    Step 4: If the node has two children then replace the data in the node with the minimum

    Data in the right sub tree of that node and then delete the node recursively

    Step 5: Display the new tree using inorder traversal

  • 8/12/2019 Lab Manual Dsa

    16/26

    FINDING THE MINIMUM AND MAXIMUM

    Step 1: More towards the left sub tree to find the minimum

    Step 2: More towards the right sub tree to find the maximum

    PROGRAM :

    #include#include#include#define NULL 0struct treenode

    {

    };int element;struct treenode *left;struct treenode *right;

    typedef struct treenode *position,*searchtree;searchtree insert(int x,searchtree t){

    if(t==NULL)

    { t=(struct treenode*)malloc(sizeof(struct treenode));if(t==NULL)

    exit(0);else{

    t->element=x;t->left=t->right=NULL;

    }}elseif(xelement)

    t->left=insert(x,t->left);elseif(x>t->element)

    t->right=insert(x,t->right);return t;

    } position findmin(searchtree t){

    if(t==NULL)

  • 8/12/2019 Lab Manual Dsa

    17/26

    return NULL;

    elseif(t->left==NULL)

    return t;elsereturn findmin(t->left);

    } position findmax(searchtree t){

    if(t==NULL)return NULL;

    elseif(t->right==NULL)

    return t;else

    return findmax(t->right);}

    searchtree rem(int x,searchtree t){

    position temp;if(t==NULL)

    printf("\nElement not found");elseif(xelement)

    t->left=rem(x,t->left);elseif(x>t->element)

    t->right=rem(x,t->right);elseif(t->left&&t->right){

    temp=findmin(t->right);t->element=temp->element;t->right=rem(t->element,t->right);

    }else

    { temp=t;if(t->left==NULL)

    t=t->right;elseif(t->right==NULL)

    t=t->left;free(temp);

    }

  • 8/12/2019 Lab Manual Dsa

    18/26

    return t;}

    void intrav(searchtree head){if(head==NULL)

    return;if(head->left!=NULL)

    intrav(head->left); printf("%d\t",head->element);

    if(head->right!=NULL)intrav(head->right);

    }

    void main(){int n,i,dat,ch;searchtree t=NULL;

    position node;clrscr();

    printf("Enter no of elements:\n");scanf("%d",&n);

    printf("Enter the elements:\n");for(i=1;i

  • 8/12/2019 Lab Manual Dsa

    19/26

    break;case 2:printf("\n Enter the node to be deleted:");

    scanf("%d",&dat);t=rem(dat,t);

    break;case 3:node=findmin(t);

    printf("\nThe minimum element is %d",node->element); break;

    case 4:node=findmax(t); printf("\nThe maximum element is %d",node->element); break;

    case 5:intrav(t); break;

    case 6:exit(0);}}while(ch!=6);getch();

    }

    OUTPUT:

    Enter no of elements:

    3

    Enter the elements:

    10

    6

    2

    2 6 10

    ****MENU****

    Enter 1 -> Insert a node

    2 -> Delete a node

  • 8/12/2019 Lab Manual Dsa

    20/26

    3 -> Find Minimum

    4 -> Find Maximum

    5 -> Display(Inorder Traversal

    6 -> Exit

    Enter your choice:3

    The minimum element is 2

    ****MENU****

    Enter 1 -> Insert a node

    2 -> Delete a node

    3 -> Find Minimum

    4 -> Find Maximum

    5 -> Display(Inorder Traversal

    6 -> Exit

    Enter your choice:6

    RESULT:

    Thus the C program to implement binary search tree is executed.

  • 8/12/2019 Lab Manual Dsa

    21/26

    Practical Number:

    Aim: - Write a program to perform Quick sort.

    .Theory:-

    Quicksort is a divide and conquer algorithm . Quicksort first divides a large list into two smaller sub-lists:the low elements and the high elements. Quicksort can then recursively sort the sub-lists.

    The steps are:

    1. Pick an element, called a pivot , from the list.2. Reorder the list so that all elements with values less than the pivot come before the pivot, while

    all elements with values greater than the pivot come after it (equal values can go either way).After this partitioning, the pivot is in its final position. This is called the partition operation.

    3. Recursively apply the above steps to the sub-list of elements with smaller values and separately

    the sub-list of elements with greater values.The base case of the recursion are lists of size zero or one, which never need to be sorted.

    Complexities:

    Worst-case: O(N 2)

    This happens when the pivot is the smallest (or the largest) element.Then one of the partitions is empty, and we repeat recursively the procedure for N-1 elements.

    Best -case:O(NlogN) The best case is when the pivot is the median of the array,and then the left and the right part will have same size.

    There are logN partitions, and to obtain each partitions we do N comparisons(and not more than N/2 swaps). Hence the complexity is O(NlogN)

    Average Case:O(NlogN)

    Algorithms:

    Quick Sort ( A, BEG, END ):

    Description: Here A is an unsorted array. BEG is the lower bound and END is the upper bound.

    1. If (BEG < END) Then

    2. X = Partition (A, BEG, END)3. Call Quick Sort (A, BEG, X 1)

    4. Call Quick Sort (A, X + 1, END)

    [End of If]

    5.Exit

    http://en.wikipedia.org/wiki/Divide_and_conquer_algorithmhttp://en.wikipedia.org/wiki/Divide_and_conquer_algorithmhttp://en.wikipedia.org/wiki/Divide_and_conquer_algorithmhttp://en.wikipedia.org/wiki/List_(computing)http://en.wikipedia.org/wiki/List_(computing)http://en.wikipedia.org/wiki/List_(computing)http://en.wikipedia.org/wiki/Recursion_(computer_science)http://en.wikipedia.org/wiki/Recursion_(computer_science)http://en.wikipedia.org/wiki/Recursion_(computer_science)http://en.wikipedia.org/wiki/Recursion_(computer_science)http://en.wikipedia.org/wiki/Recursion_(computer_science)http://en.wikipedia.org/wiki/Recursion_(computer_science)http://en.wikipedia.org/wiki/Recursion_(computer_science)http://en.wikipedia.org/wiki/List_(computing)http://en.wikipedia.org/wiki/Divide_and_conquer_algorithm
  • 8/12/2019 Lab Manual Dsa

    22/26

    Partition ( A, BEG, END ):

    Description: Here A is an unsorted array. BEG is the lower bound, END is the upper bound.

    1. Set LOC = BEG

    2. Repeat While (True)

    3. Repeat While (A[LOC] = A[BEG]) and (LOC != BEG)10. BEG = BEG + 1

    [End of While Loop]

    11. If (LOC == BEG) Then

    12. Return LOC

    [End of If]

    13. Interchange A[LOC] and A[BEG]

    14. Set LOC = BEG

    [End of While Loop]

    15.Exit.

    Program code:-

    #include

    #include

    void quicksort(int [10],int,int);

    void main(){

    int x[20],size,i;

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

    scanf("%d",&size);

    printf("Enter %d elements: ",size);

  • 8/12/2019 Lab Manual Dsa

    23/26

    for(i=0;i

  • 8/12/2019 Lab Manual Dsa

    24/26

    }

    }

    temp=x[pivot];

    x[pivot]=x[j];

    x[j]=temp;

    quicksort(x,first,j-1);

    quicksort(x,j+1,last);

    }

    }

    Output:-

    Assignments:

    i)Write a program to sort 15 elements using quick sort.

    Viva Questions:

    1. What is logic for quick sort?2. How it is efficient?3. Which Data Structure is used to perform quick sort?

  • 8/12/2019 Lab Manual Dsa

    25/26

    EX. NO:

    SINGLY LINKED LIST

    AIM:

    To write a C program to perform various operations on a Singly linked list

    #include#include#includetypedef struct node *list_ptr;struct node{int data;list_ptr next;};

    list_ptr insert(list_ptr first,int p,int element);void main(){list_ptr first;first=NULL;clrscr();first=(list_ptr)malloc(sizeof(*first));first->data=10;first->next=NULL;

    printf("data in list=%d",first->data);int p,element;

    printf("enter the position and the element");

    scanf("%d%d",&p,&element);first=insert(first,p,element);list_ptr temp2;temp2=first;while(temp2!=NULL){

    printf("data=%d\n",temp2->data);temp2=temp2->next;}getch();}list_ptr insert(list_ptr first,int p,int element){list_ptr temp,node;node=(list_ptr)malloc(sizeof(*node));

    node->data=element;int i=1;if(first==NULL){node->next=NULL;

  • 8/12/2019 Lab Manual Dsa

    26/26

    first=node;}else{

    temp=first;if(p==1){node->next=first;first=node;}else{while(temp->next!=NULL && i!=p-1){temp=temp->next;i++;}node->next=temp->next;temp->next=node;}}

    return(first);}