Uploaded by Sơn Tùng Kiều

Exercises-on-LinkedList

advertisement
Some exercises on linked lists
1. You need to declare a list of linked lists like on slide 128 of the class notes on data structures. The lists
are all singly lists, there are 9 nodes, addresses are 1,2,3,4,5,6,7,8,9. Nodes 1,2,3 form the main list, nodes
4 and 5 form the list that originate from node 1, nodes 6 and 7 form the list originating from node 2 and
finally nodes 8 and 9 form the list originating from node 3. You may assume the nodes in the main list
don’t have a data field.
(a) How many types of nodes you need to create this list of linked lists
(b) Give the typedef of each node type
(c) Give the addresses in the pointer fields of each of the 9 nodes
(d) What will it takes to change the main list into a doubly linked list?
2. Describe the operations (steps) require to add a tail pointer to a singly linked list.
3. We have seen that the asymptotic cost of the operations of adding or deleting a node to the end of a singly
linked list is O(n). How can we make this cost to be O(1)?
4. Assume you have 2 linked lists of each 3 nodes, list1 has nodes 1,2,3, list2 has nodes 4,5,6. Both lists are
singly linked lists. Describe the steps to merge list2 with the end of list1. Assuming in general that n is
the length of the linked lists, what is the asymptotic cost of your merge operation? Justify your answer.
5. Assume you have 2 doubly linked lists of each 3 nodes, list1 has nodes 1,2,3, list2 has nodes 4,5,6. Both
lists have a tail pointer. Describe the steps to merge list2 with the end of list1. Assuming in general
that n is the length of the linked lists, what is the asymptotic cost of this merge operation? Justify your
answer.
6. Describe the typedef of nodes in a singly linked list where each node has a pointer back to the head node.
Given a list of 3 nodes with addresses 1,2,3, where 1 is the head, give the address of all the pointers of all
the nodes in this list.
7. Assume you have 2 linked lists of n nodes each, where each node has a pointer to the head like in the
previous exercise. Assume list1 and list2 are singly linked list, what is the asymptotic cost of merging
list2 at the end of list1, such that each node in the merged list has a pointer to the head node. Justify
your answer.
8. Assume you have 2 linked lists of n nodes each that have pointers to the head. Assume list1 and list2 are
doubly linked list, what is the asymptotic cost of merging list2 at the end of list1, such that each node in
the merged list has a pointer to the head node. Justify your answer.
9. Assume you have two singly linked list, list1 and list2, where node in those lists have a pointer to the
head of the list. Give the pseudo-code or a list of steps of a function that take in input the address of two
nodes an return whether both nodes belong to a same list or not.
10. Given a singly linked list ”LIST”, you must split it into two lists list1 and list2. list1 contain the nodes
of the first half of LIST while list2 contain the nodes of the second half of LIST. You must realize this
operation while doing only one traversal of LIST, thus the simple strategy consisting to traverse LIST to
count the number of nodes in LIST is not allowed. You may assume that LIST has an even number of
nodes.
11. You are given a singly linked list. Reverse the order of the list, that is in the reversed list the first node
(head) should be the last node of the original list, etc. You should not do more than one traversal of the
original list.
12. Assume you have a singly linked list of restaurants. Each node has 3 data fields, string: name; int rating;
int distance from home. You want to impress your new boyfriend/girlfriend by dining in the restaurant
with the highest rating.
(a) How would you search the linked list to find the resto with the highest rating?
1
(b) Assuming n is the length of this linked list, what will be the asymptotic cost for your search operation?
(c) Will it improves the asymptotic cost of your search if the list of restaurants was a doubly linked list,
explain your answer.
(d) Will it improves the asymptotic cost of your search if the list of restaurants was already sorted in
increasing order of the ranking (lowest ranking first node, highest ranking last node)?
(e) Will it improves the asymptotic cost of your search if the list of restaurants was already sorted in
decreasing order of the ranking?
(f) What will be the asymptotic cost of adding a new node in a list of restaurants that needs to stay
sorted along the distances or the ratings?
(g) If the list of restaurants was a doubly linked list with a tail pointer. Will it change the asymptotic
cost of adding a new node in this doubly linked list such that the restaurants stay ordered along the
distances or the ratings?
2
Download