Linked Lists: Short Answer
Linked Lists: Short Answer
20CLC3 – 20CLC6
LINKED LISTS
Short Answer
In this material, without any other specification, we assume that we are using a singly struct Node {
linked list, in which a node has two members, key – a positive integer representing data int key;
Node* next;
and next – a pointer to the next element in the linked list. };
1. * What are the advantages and disadvantages of linked lists over arrays?
2. * What is a singly linked list? How is the end of a singly linked list usually signified?
3. * Describe basic operations of a singly linked list.
Which of the following operations has its running time proportional to the length of the list?
Briefly explain for each case.
• Insert an element before the first element
• Insert an element after the last element
• Delete the first element
• Delete the last element
7. ** Let n and m be two variables of type Node. What is the result of each of the following operations
on a singly linked list where n (but not m) belongs to?
Nguyễn Ngọc Thảo – Bùi Huy Thông
1 HCMUS
• n.next = n.next.next
• m.next = n.next; n.next = m
• n.next = m; m.next = n.next
8. ** Point out the syntax errors and/or the semantic error in the following code fragments.
• This function traverses a singly linked list.
void traverse(Node *head){
while (head->next != nullptr){
cout << head->key;
head = head->next;
}
}
• This code segment performs a linked list operation.
Node *nodePtr, *nextNode;
nodePtr = head;
while (nodePtr != nullptr){
nextNode = nodePtr->next;
nodePtr->next = nullptr;
nodePtr = nextNode;
}
• This function deletes the last node of a singly linked list, in which the pointer head points
to the first node of the linked list.
void delete_end_node(Node* head){
Node *temp1, *temp2;
if (head == nullptr)
cout << "The list is empty!" << endl;
else {
temp1 = head;
while (temp1->next != nullptr){
temp2 = temp1;
temp1 = temp1->next;
}
delete temp1;
temp2->next = nullptr;
}
}
9. ** What will be printed out for each of the following code segments. Assume that head points to
the first node of the singly linked list.
• 34, 5, 2, 1, 9, 8, 0, 3, 6
while (list->key > 1)
list = list->next->next;
cout << list->key + list->next->key;
Nguyễn Ngọc Thảo – Bùi Huy Thông
2 HCMUS
• 1, 2, 3, 4, 5, 6
sum = 0;
while (list->key < 5) {
sum += list->key;
list = list->next;
}
cout << sum;
• 4, 5, 21, 28, 29, 18, 60, 73, 86
list->next->next = list;
cout << list->next->next->next->next->key;
cout << list->next->next->next->next->next->next->next->key;
• 4, 5, 21, 28, 29, 18, 60, 73, 86
sum = 0;
while (list->key < list->next->key){
list = list->next;
sum += list->key;
}
cout << sum;
10. ** What task does the following function perform?
Node* func(Node* &head){
Node *p, *q;
if ((head == nullptr) || (head->next == nullptr))
return head;
q = nullptr; p = head;
while (p->next != nullptr){
q = p;
p = p->next;
}
q->next = nullptr;
p->next = head;
head = p;
return head;
}
11. ** Consider the following operations for a singly linked list.
(a) Create an empty list.
(b) Destroy a list.
(c) Determine whether a list is empty.
(d) Determine the number of items in a list.
(e) Insert an item at a given position in the list.
(f) Delete the item at a given position in the list.
(g) Look at (retrieve) the item at a given position in the list.
Nguyễn Ngọc Thảo – Bùi Huy Thông
3 HCMUS
Using only the operations above, describe how to replace an item at a given position with a new
item by picking appropriate operations and specifying necessary parameters.
For example, given the list of integers 0, 3, 8, 9, 4, 10, if we replace the fourth item with 7, the
new list will be 0, 3, 8, 7, 4, 10.
12. ** Given the following sorted singly linked list,
14. *** Explain clearly how to fulfill the following operations in constant time on a singly linked list.
• Remove the middle node, given only the pointer to that node (i.e., no head pointer).
• Given the pointer current to a node in the list, implement the InsertBefore operation.
15. *** There are many ways to link dynamically allocated data structures together. Two linked list
variations are doubly linked list and circular linked list.
What are their advantages and disadvantages over the singly linked list? List some practical
applications of these linked list variations.
2. ** The following function is supposed to reverse a singly linked list. There is one line missing at the
end of the function. Fill in the blank with an appropriate line of code.
void reverse(Node* &head){
Node* prev = nullptr;
Node* current = head;
Node* next;
while (current != nullptr){
next = current->next;
current->next = prev;
prev = current;
current = next;
}
________________________________________ /*ADD A STATEMENT HERE*/
}
3. ** The following function takes a singly linked list of integers, {1, 2, 3, 4, 5, 6, 7} in given
order, as a parameter and rearranges the elements of the list.
void rearrange(Node* &node){
if ((!node) || !node->next)
return;
Node *p = node;
Node *q = node->next;
while (q){
int temp = p->key;
p->key = q->key;
q->key = temp;
p = q->next;
q = p ? p->next : 0;
}
}
After the function completes execution, the content of the list is ___________________________________.
Draw a diagram of the list after the following code segment is executed.
Node* prev = head->next;
Node* nodeToInsert = new Node();
nodeToInsert->key = 4;
nodeToInsert->next = prev->next;
prev->next = nodeToInsert;
8. ** The intent of the method below is to delete the last node of the list.
void removeLast(Node first) {
Node p = first;
Node q = p.next;
while (q.next != null) {
p = q;
q = q.next;
}
p.next = null;
}
Which of the following linked lists for which this method works correctly? Justify your selection.
No linked lists
All nonempty linked lists
All linked lists with more than one node
The empty list and all linked lists with more than one node
All linked lists
Choose T (True) or F (False) for each of the following statements and then briefly explain in one or two
sentences.
1. T F The linked list supports both random and sequential access to its elements.
2. T F The programmer must know in advance how many nodes in a linked list.
3. T F Arrays have to fix their sizes in advance, while linked lists can change their sizes any time.
4. T F To store a fixed number of integers, an array always uses more memory than a singly
linked list does
5. T F When the head pointer points to nullptr, it signifies an empty linked list.
6. T F Insertion/deletion in a linked list incurs less data moves than those in an array.
7. T F In a singly linked list, every node (except the last) contains the address of the next node.
8. T F You can use the pointer to the head of a linked list to traverse the list.
9. T F In a singly linked list, it is easier to insert a new node after a specific node rather than
before the specific node.
10. T F To insert a new node into a singly linked list, the addresses of every other nodes in the list
must change.
1. * Let S be a singly linked list that has at least three elements. Write code to complete each of the
following requirements.
• Return the last-but-two node of S
• Invert S in place (i.e., no use of additional data structures)
• Remove a given node n from S
2. * Define a singly linked list that holds double values. Write code to store the following values, 12.7,
9.65, 8.72, and 4.69, to the list and then reverse the order of the items.
3. * Assume that a linked list has been created with the head pointer head. Write code to traverse the
linked list and display the content of each node. Then write code to destroy this linked list.
4. ** In this question, the tables are shown with left columns indicating the initial configurations and
right columns indicating the corresponding final configurations, which will be achieved by
solving the problems raised in the questions.
a. Use a single assignment statement to make p refer to the node whose info is “2”.
p->next=a->next
b. Redo a. but, the assignment statement must refer to both variables p and q.
p->next=q->next
c. Use a single assignment statement to make q refer to the node whose info is “1”.
q->next=a
d. Use a single assignment statement to make r refer to the node whose info is “2”.
r->next=b
a b
e. Use a single assignment statement to set the info of the node referred to by p equal to that
referred to by r. You must access this info through r; do not refer to “3” directly.
p->next->data=r->next->data
p->next->data=3;
g. Write a single assignment statement to transform the linked list into a circular linked list.
The assignment statement must refer to both p and r.
r->next->next=p->next
j. Write a single assignment statement to remove the node whose info is “B”.
p->next->next=a->next
k. Write a single assignment statement to remove the node whose info is “B”. The statement
must refer to both p and q.
p->next->next=q->next
l. Create a new node whose info is “A” and insert it at the beginning of the list.
r->next=p->next
p->next=r
r
m. Create a new node whose info is “D” and insert it at the end of the list.
p. Write a while loop to make q refer successively to each node in the list and q must end up
referring to the last node in the list.
q. Write a while loop to make q refer successively to each node in the list until q refers to
the first node with info (lowercase) “c”.
r. Use four assignment statements, each referring to p, to create a linked list headed by p
and containing 4 nodes with info 'A', 'B', 'C', and 'D', in this order.
s. Merge the two lists headed by p and q into a single list headed by p such that the nodes
are sorted in alphabetical order.
t. Use only the three variables, p, q, and r, to reverse the order of the nodes in the list.
8. ** Assume you have the following singly linked list and you have direct access to the head of the list.
Write a code to split the given linked list. The split point should be decided when the user inputs
an integer S. Every value less than S should be in one list and every value greater than S should
be in the second list. For example, if the user inputs 60, then the values, 10, 51, 36 and 25, should
be in one list and the others should be in another list.
9. *** The polynomial 𝑎𝑛 𝑥 𝑛 + 𝑎𝑛−1 𝑥 𝑛−1 + ⋯ 𝑎0 can be represented by a singly linked list, in which each
node contains an integer for the power of 𝑥 and another integer for the corresponding coefficient.
The user inputs from console a polynomial as follows, anx^n + an-1x^(n-1) + ... + a0.
A term aix^i can be omitted if ai is 0, e.g., the polynomial 3x4 + 7x2 + 5 can be inputted as either
3x^4 + 7x^2 + 5 or 3x^4 + 0x^3 + 7x^2 + 0x^1 + 5. If a coefficient is negative, a minus sign is
used in place of a plus sign, e.g., 3x^5 - 7x^3 + 2x^1 – 8 or -7x^4 + 5x^2 + 9.
Nguyễn Ngọc Thảo – Bùi Huy Thông
13 HCMUS
Define necessary structures for the polynomial and its terms.
The user inputs a polynomial and a value of 𝑥. The program evaluates the polynomial with the
given value.
The user inputs two polynomials. The program performs additions, subtraction, and
multiplication on the two given polynomials.
10. *** In an ancient land, the beautiful princess Eve had many suitors. She decided on the following
procedure to determine which suitor she would marry. First, all the suitors would be lined up
one after the other and assigned numbers. The first suitor would be number 1, the second
number 2, and so on up to the last suitor, number n. Starting at the first suitor, she would then
count three suitors down the line (because of the three letters in her name) and the third suitor
would be eliminated from winning her hand and removed from the line. Eve would then continue,
counting three more suitors, and eliminating every third suitor. When she reached the end of the
line, she would continue counting from the beginning. For example, if there were six suitors, then
the elimination process would proceed as follows:
123456 Initial list of suitors, start counting from 1
12456 Suitor 3 eliminated, continue counting from 4
1245 Suitor 6 eliminated, continue counting from 1
125 Suitor 4 eliminated, continue counting from 5
15 Suitor 2 eliminated, continue counting from 5
1 Suitor 5 eliminated, 1 is the lucky winner
Write code to create a singly linked list and determine which position you should stand in to
marry the princess if there are n suitors. Your program should simulate the elimination process
by deleting the node that corresponds to the suitor to be eliminated in each step of the process.
Be careful that you do not leave any memory leaks.
Although singly linked list works for this problem, it is not the best choice. Think of another
linked list that better suits this problem.