Quiz, April 10, 2007

  1. Please choose the correct order in which the nodes are inserted.

    1// linkedList.cc P. Conrad
    2
    3#include <iostream>
    4using namespace std;
    5
    6struct Node_S
    7{
    8 int data;
    9 Node_S *next;
    10};
    11
    12int main(int argc, char *argv[])
    13{
    14 Node_S *head = NULL;
    15 Node_S *tail = NULL;
    16 Node_S *p = NULL;
    17
    18 head = new Node_S;
    19 head->data = 6;
    20 head->next = NULL;
    21 tail = head;
    22
    23 p = new Node_S;
    24 p->data = 0;
    25 p->next = NULL;
    26 tail -> next = p;
    27 tail = p;
    28
    29 p = new Node_S;
    30 p->data = 9;
    31 p->next = NULL;
    32 tail -> next = p;
    33 tail = p;
    34
    35 for (p=head; p; p=p->next)
    36 cout << p->data << " ";
    37 cout << endl;
    38
    39 return 0;
    40} // end main

    The output of linkedList.cc will be:
    • 0 6 9
    • 6 0 9
    • 6 9 0
    • 9 0 6
    • 9 6 0
  2. Please choose the correct order in which the nodes are inserted.

    1// linkedList.cc P. Conrad
    2
    3#include <iostream>
    4using namespace std;
    5
    6struct Node_S
    7{
    8 int data;
    9 Node_S *next;
    10};
    11
    12int main(int argc, char *argv[])
    13{
    14 Node_S *head = NULL;
    15 Node_S *tail = NULL;
    16 Node_S *p = NULL;
    17
    18 head = new Node_S;
    19 head->data = 7;
    20 head->next = NULL;
    21 tail = head;
    22
    23 p = new Node_S;
    24 p->data = 6;
    25 p->next = head;
    26 head = p;
    27
    28 p = new Node_S;
    29 p->data = 3;
    30 p->next = head;
    31 head = p;
    32
    33 for (p=head; p; p=p->next)
    34 cout << p->data << " ";
    35 cout << endl;
    36
    37 return 0;
    38} // end main

    The output of linkedList.cc will be:
  3. Please choose the correct order in which the nodes are inserted.

    1// linkedList.cc P. Conrad
    2
    3#include <iostream>
    4using namespace std;
    5
    6struct Node_S
    7{
    8 int data;
    9 Node_S *next;
    10};
    11
    12int main(int argc, char *argv[])
    13{
    14 Node_S *head = NULL;
    15 Node_S *tail = NULL;
    16 Node_S *p = NULL;
    17
    18 head = new Node_S;
    19 head->data = 7;
    20 head->next = NULL;
    21 tail = head;
    22
    23 p = new Node_S;
    24 p->data = 0;
    25 p->next = NULL;
    26 tail -> next = p;
    27 tail = p;
    28 
    29 p = new Node_S;
    30 p->data = 3;
    31 p->next = head;
    32 head = p;
    33 
    34 for (p=head; p; p=p->next)
    35 cout << p->data << " ";
    36 cout << endl;
    37
    38 return 0;
      } // end main

    The output of linkedList.cc will be: