1. #include <stdio.h>
    2. #include <malloc.h>
    3. #define OPT_SUCCESS 1
    4. #define OPT_FAILURE 0
    5. typedef int ElemType;
    6. struct LinkedList
    7. {
    8. int data;
    9. struct LinkedList *next;
    10. struct LinkedList *prev;
    11. };
    12. typedef struct LinkedList Node;
    13. Node* InitList(){
    14. Node *p;
    15. p = (Node *) malloc (sizeof(Node));
    16. if(p==NULL) return NULL;
    17. p->data = 0;
    18. p->next = NULL;
    19. p->prev = NULL;
    20. return p;
    21. }
    22. Node* NewNode(ElemType k){
    23. Node* p;
    24. p = (Node *)malloc(sizeof(Node));
    25. if( p == NULL) return NULL;
    26. p->data = k;
    27. p->next = p->prev = NULL;
    28. return p;
    29. }
    30. int IsEmptyList(Node* plist){
    31. if(plist->next == plist->prev) return OPT_SUCCESS;
    32. return OPT_FAILURE;
    33. }
    34. int IsCircleList(Node* plist){
    35. Node *pSentry = plist->next;
    36. Node *opt = pSentry;
    37. if (pSentry->prev != NULL) return OPT_SUCCESS;
    38. while(opt != NULL){
    39. opt = opt->next;
    40. if (opt == pSentry) return OPT_SUCCESS;
    41. }
    42. return OPT_FAILURE;
    43. }
    44. void DestoryList(Node* plist){
    45. Node *pSentry = plist->next;
    46. Node *opt = plist->next;
    47. while(opt != NULL){
    48. pSentry = opt;
    49. opt = opt->next;
    50. free(pSentry);
    51. }
    52. free(plist);
    53. }
    54. int AppendElem(Node* phead,ElemType e){
    55. Node *plist;
    56. plist = phead;
    57. if(plist->next == NULL){
    58. plist->next = NewNode(e);
    59. plist->prev = NULL;
    60. return OPT_SUCCESS;
    61. }
    62. Node* pSentry = NULL;
    63. Node* opt;
    64. opt = plist->next;
    65. while(opt!=NULL){
    66. pSentry = opt;
    67. if(opt->next!=NULL) opt = opt->next;
    68. else break;
    69. }
    70. opt->next = NewNode(e);
    71. opt->next->prev = pSentry;
    72. opt->next->next = NULL;
    73. return OPT_SUCCESS;
    74. }
    75. int HeadinterElem(Node* phead, ElemType e){
    76. Node * plist;
    77. plist = phead;
    78. if (plist == NULL) return OPT_FAILURE;
    79. if (plist->next == NULL){
    80. plist->next = NewNode(e);
    81. plist->prev = NULL;
    82. return OPT_SUCCESS;
    83. }
    84. Node *p;
    85. p = NewNode(e);
    86. p->next = plist->next;
    87. p->prev = NULL;
    88. plist->next = p;
    89. if(p->next!= NULL)
    90. p->next->prev = p;
    91. return OPT_SUCCESS;
    92. }
    93. int DeleteElem(Node* plist,ElemType e){
    94. if (plist == NULL) return OPT_FAILURE;
    95. Node *pSentry = NULL;
    96. Node* opt = plist;
    97. while(opt != NULL){
    98. pSentry = opt;
    99. opt = opt->next;
    100. if(opt->data == e){
    101. pSentry->next = opt->next;
    102. opt->next->prev = pSentry;
    103. free(opt);
    104. return OPT_SUCCESS;
    105. }
    106. }
    107. return OPT_FAILURE;
    108. }
    109. int UpdateElem(Node* plist,ElemType e, ElemType update){
    110. if (plist == NULL) return OPT_FAILURE;
    111. Node *pSentry = NULL;
    112. Node* opt = plist->next;
    113. while(opt != NULL){
    114. pSentry = opt;
    115. opt = opt->next;
    116. if(opt->data == e){
    117. opt->data = update;
    118. return OPT_SUCCESS;
    119. }
    120. }
    121. return OPT_FAILURE;
    122. }
    123. Node* SearchElem(Node *plist, ElemType e){
    124. if (plist == NULL) return OPT_FAILURE;
    125. Node *pSentry = NULL;
    126. Node* opt = plist;
    127. while(opt != NULL){
    128. pSentry = opt;
    129. opt = opt->next;
    130. if(opt->data == e){
    131. return opt;
    132. }
    133. }
    134. return NULL;
    135. }
    136. int main(){
    137. Node* head;
    138. head = InitList();
    139. Node *opt =NULL;
    140. printf("%d\n",IsEmptyList(head));
    141. ElemType e;
    142. for (int i = 0; i < 10; i++)
    143. {
    144. scanf("%d",&e);
    145. if(HeadinterElem(head,e) == OPT_SUCCESS){
    146. printf("Insert Success\n");
    147. }else{
    148. printf("Insert Failure\n");
    149. }
    150. }
    151. opt = head->next;
    152. int i = 0;
    153. while(opt!=NULL){
    154. printf("%d\n",opt->data);
    155. opt = opt->next;
    156. i++;
    157. if(i > 100) break;
    158. }
    159. return 0;
    160. }