Skip to content

Latest commit

 

History

History
76 lines (53 loc) · 2.69 KB

File metadata and controls

76 lines (53 loc) · 2.69 KB

Exercises 10.2-1


Can the dynamic-set operation INSERT be implemented on a singly linked list in O(1) time? How about DELETE?

Answer

插入的话可以直接插入在开头,删除的话需要遍历.

Exercises 10.2-2


Implement a stack using a singly linked list L. The operations PUSH and POP should still take O(1) time.

Answer

PUSH插入到链表头,POP从链表头操作,都是O(1)的.

Exercises 10.2-3


Implement a queue by a singly linked list L. The operations ENQUEUE and DEQUEUE should still take O(1) time.

Answer

  • ENQUEUE插入到链表尾
  • DEQUEUE从链表头取出

Exercises 10.2-4


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.

Answer

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

Exercises 10.2-5


Implement the dictionary operations INSERT, DELETE, and SEARCH using singly linked, circular lists. What are the running times of your procedures?

Answer

implementation

Exercises 10.2-6


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.

Answer

如果用链表实现,可以将第二个list连接到第一个list上.

Exercises 10.2-7


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.

Answer

solution

Exercises 10.2-8


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.

Answer

为了访问下一个元素,只需要XOR(np,prev).为了访问前一个元素,只需要XOR(np,next).


Follow @louis1992 on github to help finish this task.