Can the dynamic-set operation INSERT be implemented on a singly linked list in O(1) time? How about DELETE?
INSERT - yes, words can be inserted directly at the beginning of the list, DELETE - no, because it requires traversing the whole list.
Implement a stack using a singly linked list L. The operations PUSH and POP should still take O(1) time.
PUSH - make element list's head,POP - remove list's had,both operations take O(1).
Implement a queue by a singly linked list L. The operations ENQUEUE and DEQUEUE should still take O(1) time.
Keep two pointers: one will point to the beginning of the list, other to the end.
- ENQUEUE - insert into end of the list,
- DEQUEUE - remove list's head
As written, each loop iteration in the LIST-SEARC′ procedure requires two tests: one for x ≠ nil[L] and one for key[x] ≠ k. Show how to eliminate the test for x ≠ nil[L] in each iteration.
LIST-SEARC′(L, k):
key[nil[L]] = k
x ← next[nil[L]]
while(key[x] != k):
x ← next[x]
if x == nil[L]:
return NULL
return x
Implement the dictionary operations INSERT, DELETE, and SEARCH using singly linked, circular lists. What are the running times of your procedures?
The dynamic-set operation UNION takes two disjoint sets S1 and S2 as input, and it returns a set S = S1 U S2 consisting of all the elements of S1 and S2. The sets S1 and S2 are usually destroyed by the operation. Show how to support UNION in O(1) time using a suitable list data structure.
如果用链表实现,可以将第二个list连接到第一个list上.
Give a Θ(n)-time nonrecursive procedure that reverses a singly linked list of n elements. The procedure should use no more than constant storage beyond that needed for the list itself.
Explain how to implement doubly linked lists using only one pointer value np[x] per item instead of the usual two (next and prev). Assume that all pointer values can be interpreted as k-bit integers, and define np[x] to be np[x] = next[x] XOR prev[x], the k-bit "exclusive-or" of next[x] and prev[x]. (The value NIL is represented by 0.) Be sure to describe what information is needed to access the head of the list. Show how to implement the SEARCH, INSERT, and DELETE operations on such a list. Also show how to reverse such a list in O(1) time.
为了访问下一个元素,只需要XOR(np,prev).为了访问前一个元素,只需要XOR(np,next).
Follow @louis1992 on github to help finish this task.