Skip to content

Commit

Permalink
CLRS C10
Browse files Browse the repository at this point in the history
  • Loading branch information
Louis1992 committed Jul 9, 2015
1 parent 70a6404 commit 16333df
Show file tree
Hide file tree
Showing 11 changed files with 735 additions and 0 deletions.
120 changes: 120 additions & 0 deletions C10-Elementary-Data-Structures/10.1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
### Exercises 10.1-1
***
Using Figure 10.1 as a model, illustrate the result of each operation in the sequence PUSH(S, 4), PUSH(S, 1), PUSH(S, 3), POP(S), PUSH(S, 8), and POP(S) on an initially empty stack S stored in array S[1...6].

### `Answer`
1 |2| 3 |4 |5 |6|
:---:|:---:|:---:|:---:|:---:|:---:
4 |

1 |2| 3 |4 |5 |6|
:---:|:---:|:---:|:---:|:---:|:---:
4 | 1 |

1 |2| 3 |4 |5 |6|
:---:|:---:|:---:|:---:|:---:|:---:
4 | 1 |3

1 |2| 3 |4 |5 |6|
:---:|:---:|:---:|:---:|:---:|:---:
4 | 1

1 |2| 3 |4 |5 |6|
:---:|:---:|:---:|:---:|:---:|:---:
4 | 1 | 8

1 |2| 3 |4 |5 |6|
:---:|:---:|:---:|:---:|:---:|:---:
4 | 1


### Exercises 10.1-2
***
Explain how to implement two stacks in one array A[1...n] in such a way that neither stack overflows unless the total number of elements in both stacks together is n. The PUSH and POP operations should run in O(1) time.

### `Answer`
分别从两头开始做就行了

### Exercises 10.1-3
***
Using Figure 10.2 as a model, illustrate the result of each operation in the sequence ENQUEUE(Q, 4), ENQUEUE(Q, 1), ENQUEUE(Q, 3), DEQUEUE(Q), ENQUEUE(Q, 8), and DEQUEUE(Q) on an initially empty queue Q stored in array Q[1...6].

### `Answer`
1 |2| 3 |4 |5 |6|
:---:|:---:|:---:|:---:|:---:|:---:
4 |

1 |2| 3 |4 |5 |6|
:---:|:---:|:---:|:---:|:---:|:---:
4 | 1 |

1 |2| 3 |4 |5 |6|
:---:|:---:|:---:|:---:|:---:|:---:
4 | 1 | 3

1 |2| 3 |4 |5 |6|
:---:|:---:|:---:|:---:|:---:|:---:
| 1 | 3

1 |2| 3 |4 |5 |6|
:---:|:---:|:---:|:---:|:---:|:---:
| 1 | 3 | 8

1 |2| 3 |4 |5 |6|
:---:|:---:|:---:|:---:|:---:|:---:
| | 3 |8

### Exercises 10.1-4
***
Rewrite ENQUEUE and DEQUEUE to detect underflow and overflow of a queue.

### `Answer`
在开头加上

ENQUEUE(Q, x):
if head[Q] == tail[Q] + 1
then error "overflow"
......
DEQUEUE(Q):
if head[Q] == tail[Q]
then error "underflow"
......


### Exercises 10.1-5
***
Whereas a stack allows insertion and deletion of elements at only one end, and a queue allows insertion at one end and deletion at the other end, a deque (double-ended queue) allows insertion and deletion at both ends. Write four O(1)-time procedures to insert elements into and delete elements from both ends of a deque constructed from an array.

### `Answer`
我没有检查overflow和underflow,用N去判断就可以了.

[implementation](./exercise_code/deque.py)

### Exercises 10.1-6
***
Show how to implement a queue using two stacks. Analyze the running time of the queue operations.

### `Answer`
这个题目有出现在[leetcode](https://leetcode.com/problems/implement-stack-using-queues/),这是我的[solution](https://github.com/gzc/leetcode/blob/master/cpp/221-230/Implement%20Stack%20using%20Queues.cpp)

* push操作是O(1)的
* empty操作是O(1)的
* top操作是O(1)的
* 但是pop操作最坏需要O(n)

### Exercises 10.1-7
***
Show how to implement a stack using two queues. Analyze the running time of the stack operations.

### `Answer`
这个题目也出现在[leetcode](https://leetcode.com/problems/implement-queue-using-stacks/)

[my solution](https://github.com/gzc/leetcode/blob/master/cpp/231-240/Implement%20Queue%20using%20Stacks.cpp)

最坏情况pop需要O(n),但是pop的平均情况只需要O(1)


***
Follow [@louis1992](https://github.com/gzc) on github to help finish this task.

Expand Down
72 changes: 72 additions & 0 deletions C10-Elementary-Data-Structures/10.2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
### 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 stilltake 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 DEQUEUEshould 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](./exercise_code/dict.cpp)

### 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](https://github.com/gzc/leetcode/blob/master/cpp/201-210/Reverse%20Linked%20List.cpp)

### 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](https://github.com/gzc) on github to help finish this task.

Expand Down
80 changes: 80 additions & 0 deletions C10-Elementary-Data-Structures/10.3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
### Exercises 10.3-1
***
Draw a picture of the sequence[13, 4, 8, 19, 5, 11]stored as a doubly linked list using the multiple-array representation. Do the same for the single-array representation.

### `Answer`
<pre><code>+---+
| L |-------------------------------------------+
+---+ V
1 2 3 4 5 6 7 8 9 10 11 12
+----+----+----+----+----+----+----+----+----+----+----+----+
next | | | 5 | 7 | 10 | | / | | 3 | 4 | | |
+----+----+----+----+----+----+----+----+----+----+----+----+
key | | | 4 | 5 | 8 | | 11 | | 13 | 19 | | |
+----+----+----+----+----+----+----+----+----+----+----+----+
prev | | | 8 | 10 | 2 | | 4 | | / | 5 | | |
+----+----+----+----+----+----+----+----+----+----+----+----+
</code></pre>

<pre><code>
+----+----+----+
| key|next|prev|
+----+----+----+

13 is head.

1 2 3 4 5 6 7 8 9 10 11 12
+----+----+----++----+----+----++----+----+----++----+----+----++--
| 4 | 7 | 13 || 5 | 10 | 16 || 8 | 16 | 1 || 11 | / | 4 ||
+----+----+----++----+----+----++----+----+----++----+----+----++--

13 14 15 16 17 18
--++----+----+----++----+----+----+
|| 13 | 1 | / || 19 | 4 | 7 |
--++----+----+----++----+----+----+
</code></pre>



### Exercises 10.3-2
***
Write the procedures ALLOCATE-OBJECT and FREE-OBJECT for a homogeneous collection of objects implemented by the single-array representation.

### `Answer`
[implementation](./exercise_code/af-obj.c)

### Exercises 10.3-3
***
Why don't we need to set or reset the prev fields of objects in the implementation of the ALLOCATE-OBJECT and FREE-OBJECT procedures?

### `Answer`

因为这类似于一个栈,没有prev.

### Exercises 10.3-4
***
It is often desirable to keep all elements of a doubly linked list compact in storage, using, for example, the first m index locations in the multiple-array representation. (This is the case in a paged, virtual-memory computing environment.) Explain how the procedures ALLOCATE>- OBJECT and FREE-OBJECT can be implemented so that the representation is compact. Assume that there are no pointers to elements of the linked list outside the list itself. (Hint: Use the array implementation of a stack.)

### `Answer`

* ALLOCATE:选free_list的head.
* FREE-OBJECT:不是直接变成head,而是按sorted的方式插入.

这样可以保证每次分配的时候取排在最前面的空位置



### Exercises 10.3-5
***
Let L be a doubly linked list of length m stored in arrays key, prev, and next of length n. Suppose that these arrays are managed by ALLOCATE-OBJECT and FREE-OBJECT procedures that keep a doubly linked free list F. Suppose further that of the n items, exactly m are on list L and n-m are on the free list. Write a procedure COMPACTIFY-LIST(L, F) that, given the list L and the free list F, moves the items in L so that they occupy array positions 1, 2,..., m and adjusts the free list F so that it remains correct, occupying array positions m + 1, m + 2,..., n. The running time of your procedure should be Θ(n), and it should use only a constant amount of extra space. Give a careful argument for the correctness of your procedure.

### `Answer`

* 先遍历free_list,给每个item的prev标记一下,用来区别L中的item.
* 两根指针,一根p1在array的头,另一个p2在尾巴.p1向右移动知道遇到free item,p2向左移动直到遇到used item,然后交换p1和p2的内容.遍历一直到p1,p2遇到为止.
* 重新整理free_list中的元素.


***
Follow [@louis1992](https://github.com/gzc) on github to help finish this task.

Expand Down
Loading

0 comments on commit 16333df

Please sign in to comment.