Exercise 10.1.2
Also https://code.google.com/p/clrs/source/browse/ch10/10.1-4.py
Exercise 10.2.1
https://github.com/gzc/CLRS/blob/master/C10-Elementary-Data-Structures/10.1.md
Explain how to implement two stacks in one arrayA[1..n] in such a way that neither stack overflows unless the total number of elements in both stacks together isn . ThePUSHandPOPoperations should run inO(1) time.
The first stack starts at 1 and grows up towards n , while the second starts form n and grows down towards 1 . Stack overflow happens when an element is pushed when the two stack pointers are adjacent.
Exercise 10.1.4Also https://code.google.com/p/clrs/source/browse/ch10/10.1-4.py
RewriteENQUEUEand `DEQUEUE to detect underflow and overflow of a queue.
I shall go with the pseudo-code version, since I'm too lazy to bother figuring out how to test it in C.
We need to do a slight modification, since the current version provides no way to tell whether a queue is empty or full. We should have
https://github.com/Maeby/CLRS/blob/master/10.1-4Q.head == NIL when the queue is empty and Q.head == Q.tail when the queue is full. An empty queue is initializes with NIL in its head andQ.tail =. We need to update Q.head when a DEQUEUE operation causes the queue to become empty.
| ||
| uint tail; | ||
| uint count; | ||
| } queue_t; | ||
| #define QUEUE_SIZE 10; | ||
| static queue_t queue; | ||
| static int array[QUEUE_SIZE] | ||
| void | ||
| queue_initialize (queue_t *Q) | ||
| { | ||
| uint i; | ||
| memset(array, 0, sizeof(int) * QUEUE_SIZE); | ||
| memset(queue, 0, sizeof(queue_t)); | ||
| } | ||
| void | ||
| ENQUEUE (queue_t *queue, int *array, int x) | ||
| { | ||
| /* | ||
| * Check for overflow | ||
| */ | ||
| if (queue->count == QUEUE_SIZE) { | ||
| printf("\nQueue Overflow"); | ||
| return; | ||
| } | ||
| if (queue->tail == QUEUE_SIZE - 1) { | ||
| queue->tail = 0; | ||
| } else { | ||
| queue_tail++; | ||
| } | ||
| queue->count++; | ||
| array[queue->tail] = x; | ||
| } | ||
| int | ||
| DEQUEUE (queue_t *queue, int *array) | ||
| { | ||
| int x; | ||
| /* | ||
| * Check for underflow | ||
| */ | ||
| if (queue->count == 0) { | ||
| printf("\nQueue Underflow"); | ||
| return (0); | ||
| } | ||
| x = array[queue->head]; | ||
| if (queue->head == QUEUE_SIZE - 1) { | ||
| queue->head = 0; | ||
| } else { | ||
| queue->head++; | ||
| } | ||
| queue->count--; | ||
| return (x); | ||
| } |
Exercise 10.2.1
Can you implement the dynamic-set operationINSERTon a singly linked list inO(1) time? How aboutDELETE?
You can implement
INSERT in constant time by prepending it to the list.
You cannot implement
Exercise 10.2.2DELETE in constant time, unless you pass to it as an argument the predecessor of the element you are deleting.Implement a stack using a singly linked listL. The operationsPUSHandPOPshould still takeO(1) time.
This is too simple to be worth implementing.
The
http://clrs.skanev.com/10/02/03.htmlPUSH operation adds an element in the beginning of the list and the POP operation removes the first element from the list.Implement a queue by using a singly linked listL. The operationsENQUEUEandDEQUEUEshould still takeO(1) time.
\The
ENQUEUE operation inserts an element in the beginning of the list and the DEQUEUE operation removes an element from the end of the list. In this case we need to keep track of the last element of the list. We can do that with a sentinel.https://github.com/gzc/CLRS/blob/master/C10-Elementary-Data-Structures/10.1.md