1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
| #include<stdio.h> #include<stdlib.h> typedef struct DNode { int data; struct DNode *prior, *next; }DNode, *DLinkList;
DNode* InitDLinkList(DLinkList &L); bool InsertNextDNode(DNode *p, DNode *s); bool DListInsert(DNode *head, int i, int e); DNode* GetElem(DNode *head, int i); bool DeleteNextDNode(DNode *p); void DestoryList(DLinkList &L); void ShowList(DNode *head); DNode * DelData(DNode * head, int data); int main(void) { DNode *head = NULL; DLinkList List; head = InitDLinkList(List);
DListInsert(head, 1, 11); DListInsert(head, 2, 22); DListInsert(head, 3, 33); DListInsert(head, 4, 44); DListInsert(head, 5, 55); DListInsert(head, 6, 66); DListInsert(head, 7, 77); DListInsert(head, 8, 88);
int num = -1; num = GetElem(head, 8)->data; printf("最后一个元素是%d\n", num);
ShowList(head);
DelData(head, 66);
ShowList(head); return 0; }
DNode* InitDLinkList(DLinkList &head) { head = (DNode *)malloc(sizeof(DNode)); if (head == NULL) { return NULL; } head->prior = NULL; head->next = NULL; return head; }
bool InsertNextDNode(DNode *p, DNode *s) { if (p==NULL || s==NULL) { return false; } s->next = p->next; if (p->next != NULL) { p->next->prior = s; } s->prior = p; p->next = s; return true; }
bool DListInsert(DNode *head, int i, int e) { DNode * list = head; DNode * body = (DNode*)malloc(sizeof(DNode)); if (i == 1) { head->prior = NULL; head->next = NULL; head->data = e; } else { int j = 2; while ( j < i ) { j++; list = list->next; } body->prior = NULL; body->next = NULL; body->data = e; InsertNextDNode(list,body); } return true; }
DNode* GetElem(DNode *head, int i) { if (i < 0) { return NULL; } DNode *p; int j = 1; p = head; while (p!=NULL && j < i) { p = p->next; j++; } return p; }
bool DeleteNextDNode(DNode *p) { if (p == NULL) { return false; } DNode *q = p->next; if (q == NULL) { return false; } p->next = q->next; if (q->next != NULL) { q->next->prior = p; } free(q); return true; }
void DestoryList(DLinkList &L) { while (L->next != NULL) { DestoryList(L); } free(L); L = NULL; }
void ShowList(DNode *head) { printf("后向遍历:\n"); DNode * temp = head; while (temp != NULL) { if (temp->next == NULL) { printf("%d\n",temp->data); } else { printf("%d->",temp->data); } temp = temp->next; }
}
DNode * DelData(DNode * head, int data) { DNode * temp = head; while (temp != NULL) { if (temp->data == data) { temp->prior->next = temp->next; temp->next->prior = temp->prior; free(temp); return head; } temp=temp->next; } printf("链表中无该数据元素"); return head; }
|