算法导论第十章----10.2-1-10.2-7

10.2-1
Can you implement the dynamic-set operation INSERTon a singly linked list in O(1) time? How about DELETE?

使用尾指针:

insert(LinkList *tail, ElemType x)
{
    Node* p = new Node;
    p->value = x;
    p->next = tail;
    tail = p;
}
delete(LinkList *tail)
{
    Node* p = tail;
    tail = p-> next;
    delete p;
}

10.2-2

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

an->an-1->......->a2->a1->nil

 ↑

top

单链表


10.2-3

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

an->an-1->......->a2->a1->nil

 ↑                                  ↑

head                           tail

在头部Dequeue,在尾部Enqueue。


10.2-4

As written, each loop iteration in the LIST-SEARCH‘ 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.

http://stackoverflow.com/questions/17906081/introduction-to-algorithm-exercise-10-2-4

LIST-SEARCH(L, k)
    LIST-INSERT(L, k)
    x = x.next
    while x.key != k
        x = x.next
    if x == L.head
        ret = NIL
    else
        ret = x
    LIST-DELETE(L, L.head)
    return ret
http://courses.csail.mit.edu/6.046/fall01/handouts/ps4sol.pdf

LIST-SEARCH'(L, k)
    key[nil[L]] <- k
    x <- next[nil[L]]
    while key[x] ≠ k
        do x <- next[x]
    if x = nil[L]
        return null
    return x

10.2-5

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

typedef struct node {
    char* word;
    struct node* next;
} List;

singly list circular list

same as CLRS


10.2-6
The dynamic-set operation UNIONtakes two disjoint sets S1and S2 as input, and it returns a set S = S1∪S2 consisting of all the elements of S1 and S2.The
sets S1and S2 are usually destroyed by the operation. Show how to support UNION in O(1) time using a suitable list data structure.

带尾指针的循环链表

 ↓ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄│

■■->a0->a1->....->an-1->an<-tailA

 ↓ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄│

■■->b0->b1->....->bn-1->bn<-tailB


 ↓ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ |

■■->a0->a1->......->an-1->an ┐     |

                                                      |     |

 ↓ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄     |

b0->b1->......->bn-2->bn-1->bn---┘

                                                   ↑

                                                tailA

type struct node {
    ElemType v;
    struct node* next;
} Node;

struct List {
    Node* tail;
};

Node* p = tailA-next;
tailA->next = tailB->next->next;
tailB->next = p;free(p)

http://cormensolution.blogspot.com/2012/09/chapter-10-elementary-data-structure.html
 struct node{
                      char* word;
                      struct node* next;
            }
            struct Set{
                    struct node* head;
                    struct node* tail;
            }
        For every list beside with the head pointer we'll also keep a tail pointer.
        Supporting union operation in O(1) time: Suppose we've two Sets S1 and S2.
        
       PSEUDO-CODE:
                node* Union(Set S1,Set S2){
                      S1.tail->next = S2.head;
                      S1.tail = S2.tail;
                      Remove S2 from the list of sets;
                      return S1;
                }



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.

http://blog.csdn.net/lihenair/article/details/17378007


10.2-8 ★
Explain how to implement doubly linked lists using only one pointer value x.np per item instead of the usual two (next and prev). Assume that all pointer values can be interpreted as k-bit integers, and define x.np to be x.np = x:next XOR x:prev, the k-bit “exclusive-or” of x.next and x:prev. (The value NIL is represented by 0.)

Be sure to describe what information you need 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.



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值