DATASTRUCTURE LAB - Copy

download DATASTRUCTURE LAB - Copy

of 53

Transcript of DATASTRUCTURE LAB - Copy

  • 8/7/2019 DATASTRUCTURE LAB - Copy

    1/53

    DATASTRUCTURE

    LAB

    Computer Science And Engineering dept.

    2010

    -11

    Vivek GuptaRoll No. 0905210064

  • 8/7/2019 DATASTRUCTURE LAB - Copy

    2/53

    2 | P a g e

    VIVEK GUPTA ROLL NO. 0905210064

    INDEXS.NO. PROGRAM PAGE NO. SIGNATURE

    1. Array Implementation of Stack 3

    2. Array Implementation of Queues 5

    3. Array implementation of Circular Queue 8

    4. Array Implementation of List 11

    5. Implementation of Stack Using Dynamic

    Memory Allocation

    15

    6. Implementation Of Queue Using Dynamic

    Memory Allocation

    18

    7. Implementation of circular Queue usng

    Dynamic Memory Allocation

    21

    8. Implementation of List Using Dynamic Memory

    Allocation

    24

    9. Insertion,Deletion And traversal In Binary

    search Tree

    30

    10. Implementation of Binary Search 37

    11. Heapsort 38

    12. Quicksort 41

    13. Insertion Sort 44

    14. Traversing Of Graph Through BFS and DFS 45

    15. Implementation Of Shortest Path Algorithm 50

  • 8/7/2019 DATASTRUCTURE LAB - Copy

    3/53

    3 | P a g e

    VIVEK GUPTA ROLL NO. 0905210064

    ARRAY IMPLEMENTATION OF STACK

    /* Program of stack using array*/

    #include

    #define MAX 5

    int top = -1;

    int stack_arr[MAX];

    main()

    {

    int choice;

    while(1)

    {

    printf("1.Push\n");

    printf("2.Pop\n");

    printf("3.Display\n");

    printf("4.Quit\n");

    printf("Enter your choice : ");

    scanf("%d",&choice);

    switch(choice)

    {case 1 :

    push();

    break;

    case 2:

    pop();

    break;

    case 3:

    display();

    break;case 4:

    exit(1);

    default:

    printf("Wrong choice\n");

    }/*End of switch*/

    }/*End of while*/

    }/*End of main()*/

    push(){

  • 8/7/2019 DATASTRUCTURE LAB - Copy

    4/53

    4 | P a g e

    VIVEK GUPTA ROLL NO. 0905210064

    int pushed_item;

    if(top == (MAX-1))

    printf("Stack Overflow\n");

    else

    {printf("Enter the item to be pushed in stack : ");

    scanf("%d",&pushed_item);

    top=top+1;

    stack_arr[top] = pushed_item;

    }

    }/*End of push()*/

    pop()

    {if(top == -1)

    printf("Stack Underflow\n");

    else

    {

    printf("Popped element is : %d\n",stack_arr[top]);

    top=top-1;

    }

    }/*End of pop()*/

    display()

    {

    int i;

    if(top == -1)

    printf("Stack is empty\n");

    else

    {

    printf("Stack elements :\n");

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

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

    }

    }/*End of display()*/

  • 8/7/2019 DATASTRUCTURE LAB - Copy

    5/53

    5 | P a g e

    VIVEK GUPTA ROLL NO. 0905210064

    ARRAY IMPLEMENTAION OF QUEUE/*Program of queue using array*/

    # include

    # define MAX 5

    int queue_arr[MAX];

    int rear = -1;

    int front = -1;

    main()

    {

    int choice;

    while(1){

    printf("1.Insert\n");

    printf("2.Delete\n");

    printf("3.Display\n");

    printf("4.Quit\n");

    printf("Enter your choice : ");

    scanf("%d",&choice);

    switch(choice){

    case 1 :

    insert();

    break;

    case 2 :

    del();

    break;

    case 3:

    display();

    break;

    case 4:

    exit(1);

    default:

    printf("Wrong choice\n");

    }/*End of switch*/

    }/*End of while*/

    }/*End of main()*/

  • 8/7/2019 DATASTRUCTURE LAB - Copy

    6/53

    6 | P a g e

    VIVEK GUPTA ROLL NO. 0905210064

    insert()

    {

    int added_item;

    if (rear==MAX-1)

    printf("Queue Overflow\n");else

    {

    if (front==-1) /*If queue is initially empty */

    front=0;

    printf("Input the element for adding in queue : ");

    scanf("%d", &added_item);

    rear=rear+1;

    queue_arr[rear] = added_item ;

    }}/*End of insert()*/

    del()

    {

    if (front == -1 || front > rear)

    {

    printf("Queue Underflow\n");

    return ;

    }

    else

    {

    printf("Element deleted from queue is : %d\n", queue_arr[front]);

    front=front+1;

    }

    }/*End of del() */

    display()

    {

    int i;

    if (front == -1)

    printf("Queue is empty\n");

    else

    {

    printf("Queue is :\n");

    for(i=front;i

  • 8/7/2019 DATASTRUCTURE LAB - Copy

    7/53

    7 | P a g e

    VIVEK GUPTA ROLL NO. 0905210064

    }

    }/*End of display() */

  • 8/7/2019 DATASTRUCTURE LAB - Copy

    8/53

    8 | P a g e

    VIVEK GUPTA ROLL NO. 0905210064

    ARRAY IMPLENTATION OF CIRCULAR QUEUES/* Program of circular queue using array*/

    # include

    # define MAX 5

    int cqueue_arr[MAX];

    int front = -1;

    int rear = -1;

    main()

    {

    int choice;

    while(1){

    printf("1.Insert\n");

    printf("2.Delete\n");

    printf("3.Display\n");

    printf("4.Quit\n");

    printf("Enter your choice : ");

    scanf("%d",&choice);

    switch(choice){

    case 1 :

    insert();

    break;

    case 2 :

    del();

    break;

    case 3:

    display();

    break;

    case 4:

    exit(1);

    default:

    printf("Wrong choice\n");

    }/*End of switch*/

    }/*End of while */

    }/*End of main()*/

  • 8/7/2019 DATASTRUCTURE LAB - Copy

    9/53

    9 | P a g e

    VIVEK GUPTA ROLL NO. 0905210064

    insert()

    {

    int added_item;

    if((front == 0 && rear == MAX-1) || (front == rear+1))

    {printf("Queue Overflow \n");

    return;

    }

    if (front == -1) /*If queue is empty */

    {

    front = 0;

    rear = 0;

    }

    elseif(rear == MAX-1)/*rear is at last position of queue */

    rear = 0;

    else

    rear = rear+1;

    printf("Input the element for insertion in queue : ");

    scanf("%d", &added_item);

    cqueue_arr[rear] = added_item ;

    }/*End of insert()*/

    del()

    {

    if (front == -1)

    {

    printf("Queue Underflow\n");

    return ;

    }

    printf("Element deleted from queue is : %d\n",cqueue_arr[front]);

    if(front == rear) /* queue has only one element */

    {

    front = -1;

    rear=-1;

    }

    else

    if(front == MAX-1)

    front = 0;

    else

    front = front+1;

  • 8/7/2019 DATASTRUCTURE LAB - Copy

    10/53

    10 | P a g e

    VIVEK GUPTA ROLL NO. 0905210064

    }/*End of del() */

    display()

    {

    int front_pos = front,rear_pos = rear;if(front == -1)

    {

    printf("Queue is empty\n");

    return;

    }

    printf("Queue elements :\n");

    if( front_pos

  • 8/7/2019 DATASTRUCTURE LAB - Copy

    11/53

    11 | P a g e

    VIVEK GUPTA ROLL NO. 0905210064

    ARRAY IMPLEMENTAION OF LIST/* Program of list using array */

    # include

    # define MAX 20

    int arr[MAX];

    int n; /*Total number of elements in the list */

    main()

    {

    int choice,item,pos;

    while(1)

    {printf("1.Input list\n");

    printf("2.Insert\n");

    printf("3.Search\n");

    printf("4.Delete\n");

    printf("5.Display\n");

    printf("6.Quit\n");

    printf("Enter your choice : ");

    scanf("%d",&choice);

    switch(choice)

    {

    case 1:

    printf("Enter the number of elements to be entered : ");

    scanf("%d",&n);

    input(n);

    break;

    case 2:

    insert();

    break;

    case 3:

    printf("Enter the element to be searched : ");

    scanf("%d", &item);

    pos = search(item);

    if(pos >= 1)

    printf("%d found at position %d\n",item,pos);

    else

    printf("Element not found\n");

  • 8/7/2019 DATASTRUCTURE LAB - Copy

    12/53

    12 | P a g e

    VIVEK GUPTA ROLL NO. 0905210064

    break;

    case 4:

    del();

    break;

    case 5:display();

    break;

    case 6:

    exit();

    break;

    default:

    printf("Wrong choice\n");

    } /*End of switch */

    }/*End of while */}/*End of main() */

    input()

    {

    int i;

    for(i = 0; i< n ; i++)

    {

    printf("Input value for element %d : ", i+1);

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

    }

    }/*End of input()*/

    int search(int item)

    {

    int i;

    for(i=0; i < n; i++)

    {

    if(item == arr[i])

    return(i+1);

    }

    return(0); /* If element not found */

    }/*End of search()*/

    insert()

    {

    int temp,item,position;

    if(n == MAX)

  • 8/7/2019 DATASTRUCTURE LAB - Copy

    13/53

    13 | P a g e

    VIVEK GUPTA ROLL NO. 0905210064

    {

    printf("List overflow\n");

    return;

    }

    printf("Enter position for insertion : ");scanf("%d", &position);

    printf("Enter the value : ");

    scanf("%d",&item);

    if(position > n+1 )

    {

    printf("Enter position less than or equal to %d\n",n+1);

    return;

    }

    if( position == n+1 ) /*Insertion at the end */{

    arr[n] = item;

    n = n+1;

    return;

    }

    /* Insertion in between */

    temp=n-1;

    while( temp >= position-1)

    {

    arr[temp+1] = arr[temp]; /* shifting right */

    temp --;

    }

    arr[position-1] = item;

    n = n +1 ;

    }/*End of insert()*/

    del()

    {

    int temp,position,item;

    if(n == 0)

    {

    printf("List underflow\n");

    return;

    }

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

    scanf("%d",&item);

    if(item==arr[n-1]) /*Deletion at the end*/

  • 8/7/2019 DATASTRUCTURE LAB - Copy

    14/53

    14 | P a g e

    VIVEK GUPTA ROLL NO. 0905210064

    {

    n = n-1;

    return;

    }

    position=search(item);if(position==0)

    {

    printf("Element not present in array\n");

    return;

    }

    /*Deletion in between */

    temp=position-1;

    while(temp

  • 8/7/2019 DATASTRUCTURE LAB - Copy

    15/53

    15 | P a g e

    VIVEK GUPTA ROLL NO. 0905210064

    IMPLEMENTATION OF STACK USING DYNAMIC

    MEMOTY ALLOCATION/* Program of stack using linked list*/

    # include

    # include

    struct node

    {

    int info;

    struct node *link;

    } *top=NULL;

    main()

    {

    int choice;

    while(1)

    { printf("1.Push\n");

    printf("2.Pop\n");

    printf("3.Display\n");

    printf("4.Quit\n");

    printf("Enter your choice : ") ;scanf("%d", &choice);

    switch(choice)

    {

    case 1:

    push();

    break;

    case 2:

    pop();

    break;

    case 3:

    display();

    break;

    case 4:

    exit(1);

    default :

    printf("Wrong choice\n");

    }/*End of switch */

  • 8/7/2019 DATASTRUCTURE LAB - Copy

    16/53

    16 | P a g e

    VIVEK GUPTA ROLL NO. 0905210064

    }/*End of while */

    }/*End of main() */

    push()

    {struct node *tmp;

    int pushed_item;

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

    printf("Input the new value to be pushed on the stack : ");

    scanf("%d",&pushed_item);

    tmp->info=pushed_item;

    tmp->link=top;

    top=tmp;

    }/*End of push()*/

    pop()

    {

    struct node *tmp;

    if(top == NULL)

    printf("Stack is empty\n");

    else

    { tmp=top;

    printf("Popped item is %d\n",tmp->info);

    top=top->link;

    free(tmp);

    }

    }/*End of pop()*/

    display()

    { struct node *ptr;

    ptr=top;

    if(top==NULL)

    printf("Stack is empty\n");

    else

    {

    printf("Stack elements :\n");

    while(ptr!= NULL)

    {

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

    ptr = ptr->link;

  • 8/7/2019 DATASTRUCTURE LAB - Copy

    17/53

    17 | P a g e

    VIVEK GUPTA ROLL NO. 0905210064

    }/*End of while */

    }/*End of else*/

    }/*End of display()*/

  • 8/7/2019 DATASTRUCTURE LAB - Copy

    18/53

    18 | P a g e

    VIVEK GUPTA ROLL NO. 0905210064

    IMPLEMENTATION OF QUEUE USING DYNAMIC

    MEMOTY ALLOCATION/* Program of queue using linked list*/

    # include

    # include

    struct node

    {

    int info;

    struct node *link;

    }*front=NULL,*rear=NULL;

    main()

    {

    int choice;

    while(1)

    { printf("1.Insert\n");

    printf("2.Delete\n");

    printf("3.Display\n");

    printf("4.Quit\n");

    printf("Enter your choice : ");scanf("%d", &choice);

    switch(choice)

    {

    case 1:

    insert();

    break;

    case 2:

    del();

    break;

    case 3:

    display();

    break;

    case 4:

    exit(1);

    default :

    printf("Wrong choice\n");

    }/*End of switch*/

  • 8/7/2019 DATASTRUCTURE LAB - Copy

    19/53

    19 | P a g e

    VIVEK GUPTA ROLL NO. 0905210064

    }/*End of while*/

    }/*End of main()*/

    insert()

    {struct node *tmp;

    int added_item;

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

    printf("Input the element for adding in queue : ");

    scanf("%d",&added_item);

    tmp->info = added_item;

    tmp->link=NULL;

    if(front==NULL) /*If Queue is empty*/

    front=tmp;else

    rear->link=tmp;

    rear=tmp;

    }/*End of insert()*/

    del()

    {

    struct node *tmp;

    if(front == NULL)

    printf("Queue Underflow\n");

    else

    {

    tmp=front;

    printf("Deleted element is %d\n",tmp->info);

    front=front->link;

    free(tmp);

    }

    }/*End of del()*/

    display()

    {

    struct node *ptr;

    ptr = front;

    if(front == NULL)

    printf("Queue is empty\n");

    else

    {

  • 8/7/2019 DATASTRUCTURE LAB - Copy

    20/53

    20 | P a g e

    VIVEK GUPTA ROLL NO. 0905210064

    printf("Queue elements :\n");

    while(ptr != NULL)

    {

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

    ptr = ptr->link;}

    printf("\n");

    }/*End of else*/

    }/*End of display()*/

  • 8/7/2019 DATASTRUCTURE LAB - Copy

    21/53

    21 | P a g e

    VIVEK GUPTA ROLL NO. 0905210064

    IMPLENTAION OF CIRCULAR QUEUE USING

    DYNAMIC MEMORY ALLOCATION/* Program of queue using circular linked list*/

    # include

    # include

    struct node

    {

    int info;

    struct node *link;

    }*rear=NULL;

    main()

    {

    int choice;

    while(1)

    {

    printf("1.Insert \n");

    printf("2.Delete \n");

    printf("3.Display\n");

    printf("4.Quit\n");printf("Enter your choice : ");

    scanf("%d",&choice);

    switch(choice)

    {

    case 1:

    insert();

    break;

    case 2:

    del();

    break;

    case 3:

    display();

    break;

    case 4:

    exit();

    default:

    printf("Wrong choice\n");

  • 8/7/2019 DATASTRUCTURE LAB - Copy

    22/53

  • 8/7/2019 DATASTRUCTURE LAB - Copy

    23/53

    23 | P a g e

    VIVEK GUPTA ROLL NO. 0905210064

    q=rear->link;

    tmp=q;

    rear->link = q->link;

    printf("Deleted element is %d\n",tmp->info);

    free(tmp);}/*End of del()*/

    display()

    {

    struct node *q;

    if(rear == NULL)

    {

    printf("Queue is empty\n");

    return;}

    q = rear->link;

    printf("Queue is :\n");

    while(q != rear)

    {

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

    q = q->link;

    }

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

    }/*End of display()*/

  • 8/7/2019 DATASTRUCTURE LAB - Copy

    24/53

    24 | P a g e

    VIVEK GUPTA ROLL NO. 0905210064

    IMPLENTATION OF LIST USING DYNAMIC

    MEMORY ALLOCATION/* Program of single linked list*/

    # include

    # include

    struct node

    {

    int info;

    struct node *link;

    }*start;

    main()

    {

    int choice,n,m,position,i;

    start=NULL;

    while(1)

    {

    printf("1.Create List\n");

    printf("2.Add at begining\n");

    printf("3.Add after \n");printf("4.Delete\n");

    printf("5.Display\n");

    printf("6.Count\n");

    printf("7.Reverse\n");

    printf("8.Search\n");

    printf("9.Quit\n");

    printf("Enter your choice : ");

    scanf("%d",&choice);

    switch(choice)

    {

    case 1:

    printf("How many nodes you want : ");

    scanf("%d",&n);

    for(i=0;i

  • 8/7/2019 DATASTRUCTURE LAB - Copy

    25/53

  • 8/7/2019 DATASTRUCTURE LAB - Copy

    26/53

    26 | P a g e

    VIVEK GUPTA ROLL NO. 0905210064

    printf("Wrong choice\n");

    }/*End of switch */

    }/*End of while */

    }/*End of main()*/

    create_list(int data)

    {

    struct node *q,*tmp;

    tmp= malloc(sizeof(struct node));

    tmp->info=data;

    tmp->link=NULL;

    if(start==NULL) /*If list is empty */

    start=tmp;else

    { /*Element inserted at the end */

    q=start;

    while(q->link!=NULL)

    q=q->link;

    q->link=tmp;

    }

    }/*End of create_list()*/

    addatbeg(int data)

    {

    struct node *tmp;

    tmp=malloc(sizeof(struct node));

    tmp->info=data;

    tmp->link=start;

    start=tmp;

    }/*End of addatbeg()*/

    addafter(int data,int pos)

    {

    struct node *tmp,*q;

    int i;

    q=start;

    for(i=0;ilink;

    if(q==NULL)

  • 8/7/2019 DATASTRUCTURE LAB - Copy

    27/53

    27 | P a g e

    VIVEK GUPTA ROLL NO. 0905210064

    {

    printf("There are less than %d elements",pos);

    return;

    }

    }/*End of for*/

    tmp=malloc(sizeof(struct node) );

    tmp->link=q->link;

    tmp->info=data;

    q->link=tmp;

    }/*End of addafter()*/

    del(int data)

    {struct node *tmp,*q;

    if(start->info == data)

    {

    tmp=start;

    start=start->link; /*First element deleted*/

    free(tmp);

    return;

    }

    q=start;

    while(q->link->link != NULL)

    {

    if(q->link->info==data) /*Element deleted in between*/

    {

    tmp=q->link;

    q->link=tmp->link;

    free(tmp);

    return;

    }

    q=q->link;

    }/*End of while */

    if(q->link->info==data) /*Last element deleted*/

    {

    tmp=q->link;

    free(tmp);

    q->link=NULL;

    return;

    }

  • 8/7/2019 DATASTRUCTURE LAB - Copy

    28/53

    28 | P a g e

    VIVEK GUPTA ROLL NO. 0905210064

    printf("Element %d not found\n",data);

    }/*End of del()*/

    display()

    {struct node *q;

    if(start == NULL)

    {

    printf("List is empty\n");

    return;

    }

    q=start;

    printf("List is :\n");

    while(q!=NULL){

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

    q=q->link;

    }

    printf("\n");

    }/*End of display() */

    count()

    {

    struct node *q=start;

    int cnt=0;

    while(q!=NULL)

    {

    q=q->link;

    cnt++;

    }

    printf("Number of elements are %d\n",cnt);

    }/*End of count() */

    rev()

    {

    struct node *p1,*p2,*p3;

    if(start->link==NULL) /*only one element*/

    return;

    p1=start;

    p2=p1->link;

    p3=p2->link;

  • 8/7/2019 DATASTRUCTURE LAB - Copy

    29/53

    29 | P a g e

    VIVEK GUPTA ROLL NO. 0905210064

    p1->link=NULL;

    p2->link=p1;

    while(p3!=NULL)

    {

    p1=p2;p2=p3;

    p3=p3->link;

    p2->link=p1;

    }

    start=p2;

    }/*End of rev()*/

    search(int data)

    {struct node *ptr = start;

    int pos = 1;

    while(ptr!=NULL)

    {

    if(ptr->info==data)

    {

    printf("Item %d found at position %d\n",data,pos);

    return;

    }

    ptr = ptr->link;

    pos++;

    }

    if(ptr == NULL)

    printf("Item %d not found in list\n",data);

    }/*End of search()*/

  • 8/7/2019 DATASTRUCTURE LAB - Copy

    30/53

  • 8/7/2019 DATASTRUCTURE LAB - Copy

    31/53

    31 | P a g e

    VIVEK GUPTA ROLL NO. 0905210064

    if(item==ptr->info)

    { *loc=ptr;

    *par=ptrsave;

    return;

    }ptrsave=ptr;

    if(iteminfo)

    ptr=ptr->lchild;

    else

    ptr=ptr->rchild;

    }/*End of while */

    *loc=NULL; /*item not found*/

    *par=ptrsave;

    }/*End of find()*/

    void insert(int item)

    { struct node *tmp,*parent,*location;

    find(item,&parent,&location);

    if(location!=NULL)

    {

    printf("Item already present");

    return;

    }

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

    tmp->info=item;

    tmp->lchild=NULL;

    tmp->rchild=NULL;

    if(parent==NULL)

    root=tmp;

    else

    if(iteminfo)

    parent->lchild=tmp;

    else

    parent->rchild=tmp;

    }/*End of insert()*/

    void del(int item)

  • 8/7/2019 DATASTRUCTURE LAB - Copy

    32/53

    32 | P a g e

    VIVEK GUPTA ROLL NO. 0905210064

    {

    struct node *parent,*location;

    if(root==NULL)

    {

    printf("Tree empty");return;

    }

    find(item,&parent,&location);

    if(location==NULL)

    {

    printf("Item not present in tree");

    return;

    }

    if(location->lchild==NULL && location->rchild==NULL)

    case_a(parent,location);

    if(location->lchild!=NULL && location->rchild==NULL)

    case_b(parent,location);

    if(location->lchild==NULL && location->rchild!=NULL)

    case_b(parent,location);

    if(location->lchild!=NULL && location->rchild!=NULL)

    case_c(parent,location);

    free(location);

    }/*End of del()*/

    void case_a(struct node *par,struct node *loc )

    {

    if(par==NULL) /*item to be deleted is root node*/

    root=NULL;

    else

    if(loc==par->lchild)

    par->lchild=NULL;

    else

    par->rchild=NULL;

    }/*End of case_a()*/

    case_b(struct node *par,struct node *loc)

    {

    struct node *child;

  • 8/7/2019 DATASTRUCTURE LAB - Copy

    33/53

    33 | P a g e

    VIVEK GUPTA ROLL NO. 0905210064

    /*Initialize child*/

    if(loc->lchild!=NULL) /*item to be deleted has lchild */

    child=loc->lchild;

    else /*item to be deleted has rchild */

    child=loc->rchild;

    if(par==NULL ) /*Item to be deleted is root node*/

    root=child;

    else

    if( loc==par->lchild) /*item is lchild of its parent*/

    par->lchild=child;

    else /*item is rchild of its parent*/

    par->rchild=child;

    }/*End of case_b()*/

    case_c(struct node *par,struct node *loc)

    {

    struct node *ptr,*ptrsave,*suc,*parsuc;

    /*Find inorder successor and its parent*/

    ptrsave=loc;

    ptr=loc->rchild;

    while(ptr->lchild!=NULL)

    {

    ptrsave=ptr;

    ptr=ptr->lchild;

    }

    suc=ptr;

    parsuc=ptrsave;

    if(suc->lchild==NULL && suc->rchild==NULL)

    case_a(parsuc,suc);

    else

    case_b(parsuc,suc);

    if(par==NULL) /*if item to be deleted is root node */

    root=suc;

    else

    if(loc==par->lchild)

    par->lchild=suc;

    else

  • 8/7/2019 DATASTRUCTURE LAB - Copy

    34/53

    34 | P a g e

    VIVEK GUPTA ROLL NO. 0905210064

    par->rchild=suc;

    suc->lchild=loc->lchild;

    suc->rchild=loc->rchild;

    }/*End of case_c()*/

    preorder(struct node *ptr)

    {

    if(root==NULL)

    {

    printf("Tree is empty");

    return;

    }

    if(ptr!=NULL){

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

    preorder(ptr->lchild);

    preorder(ptr->rchild);

    }

    }/*End of preorder()*/

    inorder(struct node *ptr)

    {

    if(root==NULL)

    {

    printf("Tree is empty");

    return;

    }

    if(ptr!=NULL)

    {

    inorder(ptr->lchild);

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

    inorder(ptr->rchild);

    }

    }/*End of inorder()*/

    postorder(struct node *ptr)

    {

    if(root==NULL)

    {

    printf("Tree is empty");

  • 8/7/2019 DATASTRUCTURE LAB - Copy

    35/53

    35 | P a g e

    VIVEK GUPTA ROLL NO. 0905210064

    return;

    }

    if(ptr!=NULL)

    {

    postorder(ptr->lchild);postorder(ptr->rchild);

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

    }

    }/*End of postorder()*/

    display(struct node *ptr,int level)

    {

    int i;

    if ( ptr!=NULL ){

    display(ptr->rchild, level+1);

    printf("\n");

    for (i = 0; i < level; i++)

    printf(" ");

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

    display(ptr->lchild, level+1);

    }/*End of if*/

    }/*End of display()*/main()

    {

    int choice,num;

    root=NULL;

    while(1)

    {

    printf("\n");

    printf("1.Insert\n");

    printf("2.Delete\n");

    printf("3.Inorder Traversal\n");

    printf("4.Preorder Traversal\n");

    printf("5.Postorder Traversal\n");

    printf("6.Display\n");

    printf("7.Quit\n");

    printf("Enter your choice : ");

    scanf("%d",&choice);

    switch(choice)

    {

  • 8/7/2019 DATASTRUCTURE LAB - Copy

    36/53

    36 | P a g e

    VIVEK GUPTA ROLL NO. 0905210064

    case 1:

    printf("Enter the number to be inserted : ");

    scanf("%d",&num);

    insert(num);break;

    case 2:

    printf("Enter the number to be deleted : ");

    scanf("%d",&num);

    del(num);

    break;

    case 3:

    inorder(root);

    break;case 4:

    preorder(root);

    break;

    case 5:

    postorder(root);

    break;

    case 6:

    display(root,1);

    break;

    case 7:

    exit();

    default:

    printf("Wrong choice\n");

    }/*End of switch */

    }/*End of while */

    }/*End of main()*/

  • 8/7/2019 DATASTRUCTURE LAB - Copy

    37/53

    37 | P a g e

    VIVEK GUPTA ROLL NO. 0905210064

    IMPLENTATION OF BINARY SEARCHING/*Program for binary search*/

    #include

    main()

    {

    int arr[20],start,end,middle,n,i,item;

    printf("How many elements you want to enter in the array : ");

    scanf("%d",&n);

    for(i=0; i < n; i++)

    {

    printf("Enter element %d : ",i+1);scanf("%d",&arr[i]);

    }

    printf("Enter the element to be searched : ");

    scanf("%d",&item);

    start=0;

    end=n-1;

    middle=(start+end)/2;

    while(item != arr[middle] && start arr[middle])

    start=middle+1;

    else

    end=middle-1;

    middle=(start+end)/2;

    }

    if(item==arr[middle])

    printf("%d found at position %d\n",item,middle+1);

    if(start>end)

    printf("%d not found in array\n",item);

    }/*End of main()*/

  • 8/7/2019 DATASTRUCTURE LAB - Copy

    38/53

    38 | P a g e

    VIVEK GUPTA ROLL NO. 0905210064

    IMPLENTATION OF HEAPSORT* Program of sorting through heapsort*/

    # include

    int arr[20],n;

    main()

    {

    int i;

    printf("Enter number of elements : ");

    scanf("%d",&n);

    for(i=0;i

  • 8/7/2019 DATASTRUCTURE LAB - Copy

    39/53

    39 | P a g e

    VIVEK GUPTA ROLL NO. 0905210064

    insert(arr[i],i);

    }/*End of create_heap()*/

    insert(int num,int loc)

    {int par;

    while(loc>0)

    {

    par=(loc-1)/2;

    if(num0; last--)

    del_root(last);

    }/*End of del_root*/

    del_root(int last)

    {

    int left,right,i,temp;

    i=0; /*Since every time we have to replace root with last*/

    /*Exchange last element with the root */

    temp=arr[i];

    arr[i]=arr[last];

    arr[last]=temp;

    left=2*i+1; /*left child of root*/

    right=2*i+2;/*right child of root*/

    while( right < last)

  • 8/7/2019 DATASTRUCTURE LAB - Copy

    40/53

    40 | P a g e

    VIVEK GUPTA ROLL NO. 0905210064

    {

    if( arr[i]>=arr[left] && arr[i]>=arr[right] )

    return;

    if( arr[right]

  • 8/7/2019 DATASTRUCTURE LAB - Copy

    41/53

    41 | P a g e

    VIVEK GUPTA ROLL NO. 0905210064

    IMPLEMENTATION OF QUICK SORT/*Program of sorting using quick sort through recursion*/

    #include

    #define MAX 30

    enum bool { FALSE,TRUE };

    main()

    {

    int array[MAX],n,i;

    printf("Enter the number of elements : ");

    scanf("%d",&n);

    for(i=0;i=up)

    return;

  • 8/7/2019 DATASTRUCTURE LAB - Copy

    42/53

    42 | P a g e

    VIVEK GUPTA ROLL NO. 0905210064

    printf("Sublist : ");

    display(arr,low,up);

    /*Loop till pivot is placed at proper place in the sublist*/

    while(pivot_placed==FALSE){

    /*Compare from right to left */

    while( arr[piv] arr[right] )

    {

    temp=arr[piv];arr[piv]=arr[right];

    arr[right]=temp;

    piv=right;

    }

    /*Compare from left to right */

    while( arr[piv]>=arr[left] && left!=piv )

    left=left+1;

    if(piv==left)

    pivot_placed=TRUE;

    if( arr[piv] < arr[left] )

    {

    temp=arr[piv];

    arr[piv]=arr[left];

    arr[left]=temp;

    piv=left;

    }

    }/*End of while */

    printf("-> Pivot Placed is %d -> ",arr[piv]);

    display(arr,low,up);

    printf("\n");

    quick(arr,low,piv-1);

    quick(arr,piv+1,up);

    }/*End of quick()*/

    display(int arr[],int low,int up)

    {

  • 8/7/2019 DATASTRUCTURE LAB - Copy

    43/53

    43 | P a g e

    VIVEK GUPTA ROLL NO. 0905210064

    int i;

    for(i=low;i

  • 8/7/2019 DATASTRUCTURE LAB - Copy

    44/53

  • 8/7/2019 DATASTRUCTURE LAB - Copy

    45/53

  • 8/7/2019 DATASTRUCTURE LAB - Copy

    46/53

  • 8/7/2019 DATASTRUCTURE LAB - Copy

    47/53

    47 | P a g e

    VIVEK GUPTA ROLL NO. 0905210064

    {

    printf("Enter edge %d( 0 0 to quit ) : ",i);

    scanf("%d %d",&origin,&destin);

    if((origin==0) && (destin==0))break;

    if( origin > n || destin > n || origin

  • 8/7/2019 DATASTRUCTURE LAB - Copy

    48/53

  • 8/7/2019 DATASTRUCTURE LAB - Copy

    49/53

    49 | P a g e

    VIVEK GUPTA ROLL NO. 0905210064

    while(front

  • 8/7/2019 DATASTRUCTURE LAB - Copy

    50/53

  • 8/7/2019 DATASTRUCTURE LAB - Copy

    51/53

    51 | P a g e

    VIVEK GUPTA ROLL NO. 0905210064

    if(shortdist!=0)

    {

    printf("Shortest distance is : %d\n", shortdist);

    printf("Shortest Path is : ");

    for(i=count;i>1;i--)printf("%d->",path[i]);

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

    printf("\n");

    }

    else

    printf("There is no path from source to destination node\n");

    }/*End of while*/

    }/*End of main()*/

    create_graph()

    {

    int i,max_edges,origin,destin,wt;

    printf("Enter number of vertices : ");

    scanf("%d",&n);

    max_edges=n*(n-1);

    for(i=1;i n || destin > n || origin

  • 8/7/2019 DATASTRUCTURE LAB - Copy

    52/53

    52 | P a g e

    VIVEK GUPTA ROLL NO. 0905210064

    {

    int i,j;

    for(i=1;i

  • 8/7/2019 DATASTRUCTURE LAB - Copy

    53/53

    53 | P a g e

    {

    state[i].predecessor = current;

    state[i].dist = newdist;

    }

    }}/*End of for*/

    /*Search for temporary node with minimum distand make it current node*/

    min=infinity;

    current=0;

    for(i=1;i1;i--)

    {

    u=path[i];

    v=path[i-1];

    *sdist+= adj[u][v];