Programming and data structure miscellaneous


Programming and data structure miscellaneous

Programming & Data Structure

  1. Consider the C program below.
    #include
    int *A, stkTop;
    int stkFunc(int opcode, int val)
    {
    static int size = 0, stkTop = 0;
    switch (opcode)
    {
      case –1: size = val; break;
      case 0: if(stkTop < size) A[stkTop++]=
       val; break;
    default: if (stkTop) return A[– –stkTop];
    }
    return – 1;
    }
    int main()
    {
    int B[20]; A = B; stkTop = –1;
    stkFunc(–1, 10);
    stkFunc(0, 5);
    stkFunc(0, 10);
    printf("%d\n", stkFunc(1,0)+stkFun(1,0);
    }
    The value printed by the above program is ______.









  1. View Hint View Answer Discuss in Forum

    The code is pushing 5 and 10 on stack and then popping the top two elements and printing their sum.

    Correct Option: B

    The code is pushing 5 and 10 on stack and then popping the top two elements and printing their sum.


  1. A Young tableau is a 2D array of integers increasing from left to right and from top to bottom. Any unfilled entries are marked with ∞, and hence there cannot be any entry to the right of, or below a ∞. The following Young tableau consists of unique entries.

    When an element is removed from a Young tableau, other elements should be moved into its place so that the resulting table is still a Young tableau (unfilled entries may be filled in with a ∞). The minimum number of entries (other than 1) to be shifted, to remove 1 from the given Young tableau is ________.









  1. View Hint View Answer Discuss in Forum

    Correct Option: A



  1. A circularly linked list is used to represent a queue. A single variable p is used to access the queue. To which node should p point such that both the operations en-queue and dequeue can be performed in constant time?










  1. View Hint View Answer Discuss in Forum

    Answer is not “(b) front node”, as we cannot get rear from front in O(1), but if P is rear we can implement both enqueue and de-Queue in O(1) because from rear we can get front in O(1). Below are sample functions. Shows The pointer points to the rear node : -
    Enqueue :- Insert new node after rear, and make rear point to the newly inserted node :
    Struct Node * New Node;
    New Node → Next = Rear → Next,
    Rear → Next = New Node;
    Rear = New Node;
    Dequeue :- Delete the front node, and make the second node the front node.
    Rear → Next points to the front node.
    Front → Next points to the second node.
    Struct Node * Front;
    Front = Rear → Next;
    Rear → Next = Front → Next;
    Free (front);

    Correct Option: A

    Answer is not “(b) front node”, as we cannot get rear from front in O(1), but if P is rear we can implement both enqueue and de-Queue in O(1) because from rear we can get front in O(1). Below are sample functions. Shows The pointer points to the rear node : -
    Enqueue :- Insert new node after rear, and make rear point to the newly inserted node :
    Struct Node * New Node;
    New Node → Next = Rear → Next,
    Rear → Next = New Node;
    Rear = New Node;
    Dequeue :- Delete the front node, and make the second node the front node.
    Rear → Next points to the front node.
    Front → Next points to the second node.
    Struct Node * Front;
    Front = Rear → Next;
    Rear → Next = Front → Next;
    Free (front);


  1. The following C function takes a single-linked list of integers as a parameter and rearranges the elements of the list. The function is called with the list containing the integers 1, 2, 3, 4, 5, 6, 7 in the given order. What will be the contents of the list after the function completes execution?
    struct node {
        int value;
       struct node *next;
    };
    void rearrange (struct node *list) {
    struct node *p, *q; int temp; if (!list | | !list -> next) return; p = list, q = list –> next; while (q){ temp = p - > value; p -> value = q - > value; q - > value = temp; p = q - > next; q = p?p -> next: 0; } }









  1. View Hint View Answer Discuss in Forum

    The function rearrange() exchanges data of every node with its next node. It starts exchanging data from the first node itself.

    Correct Option: B

    The function rearrange() exchanges data of every node with its next node. It starts exchanging data from the first node itself.



  1. The following C function takes a simply-linked list as input argument. It modifies the list as input argument. It modifies the list by moving the last element to the front of the list and returns the modified list. Some part of the code is left blank.
    type def struct node {
        int value;
       struct node *next;
    }    Node*;
    Node *move_to_front (Node *head) {
       Node *p, *q;
    if (head = = NULL | | (head -> next = = NULL)) return head;
       q = NULL; p = head;
       while (p - > next! = NULL)   {
       q = p;
       p = p - > next;
    }
    return head;
    }
    Choose the correct alternative to replace the blank line.









  1. View Hint View Answer Discuss in Forum

    When the while loop ends, q contains address of second last node and p contains address of last node. So we need to do following things after while loop.
    (i) Set next of q as NULL (q->next = NULL).
    (ii) Set next of p as head (p->next = head).
    (iii) Make head as p (head = p)
    Step (ii) must be performed before step (iii). If we change head first, then we lose track of head node in the original linked list.

    Correct Option: D

    When the while loop ends, q contains address of second last node and p contains address of last node. So we need to do following things after while loop.
    (i) Set next of q as NULL (q->next = NULL).
    (ii) Set next of p as head (p->next = head).
    (iii) Make head as p (head = p)
    Step (ii) must be performed before step (iii). If we change head first, then we lose track of head node in the original linked list.