Dancing Links_初解

网上的Dancing Links

用L[R[x]] ← L[x], R[L[x]] ← R[x]来表示节点的删除

用L[R[x]] ← x, R[L[x]] ← x来表示节点的复原

搞的我一头雾水,其实就是修改双向链表的左右地址,自己写一个马上有一个深刻的理解了,

不过表现的形式有点不一样。

 1 #include <cstdio>
 2 #include <windows.h>
 3 const int MAX=10;
 4 typedef struct node
 5 {
 6    int num;
 7    struct node *left,*right;   
 8 }point;
 9 point *head=(point*)malloc(sizeof(point)),*p,*q;
10 point *L[MAX],*R[MAX];
11 
12 int n;
13 void Creat(void)
14 {
15    p=head;
16    p->left=NULL;
17    p->right=(point*)malloc(sizeof(point));
18    for(int i=1;i<=10;i++)
19       {
20          L[i-1]=p;
21          p->right->left=p;
22          p=p->right;
23          p->num=i;
24          p->right=(point*)malloc(sizeof(point));
25          R[i-1]=p->right;
26       }
27    p->right=NULL;
28 }
29 
30 void Show(void)
31 {
32    printf("Show The Links:\n");
33    for(p=head->right;p!=NULL;p=p->right)
34       printf("%d ",p->num);
35    printf("\n");
36 }
37 
38 void Remove(void)
39 {
40    printf("\nInput n To Remove Node.n:");
41    scanf("%d",&n);
42    q=L[n]->right;
43    R[n]->left=L[n];
44    L[n]->right=R[n];
45 }
46 
47 void Resume(void)
48 {
49    printf("\nResume Node.n:");
50    R[n]->left=q;
51    L[n]->right=q;
52 }
53 
54 int main()
55 {
56    Creat();
57    Show();
58    Remove();
59    Show();
60    Resume();
61    Show();
62    
63    system("pause");
64    return 0;
65 }

 

posted @ 2013-06-08 23:55  瓶哥  Views(149)  Comments(0Edit  收藏  举报