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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
| #include<stdio.h> #include<stdlib.h> #include<assert.h> #include"clist.h"
struct CNode* Buynode() { CNode* newnode = (CNode*)malloc(sizeof(CNode)); if (NULL == newnode) { printf("memory error"); exit(1); } return newnode; }
void Init_clist(struct CNode* head) { assert(head != NULL); head->next = head; }
bool Insert_Next(CNode* ptr, int val) { assert(ptr); CNode* pnewnode = Buynode(); pnewnode->next = ptr->next; ptr -> next = pnewnode; pnewnode->data = val; return true; }
bool Push_Front(PCNode head, int val) { assert(head); return Insert_Next(head, val); }
bool Push_Back(PCNode head, int val) { assert(head); CNode* p = head; while (p->next != head) { p = p->next; } return Insert_Next(p, val); }
bool Insert_Pos(PCNode head, int pos, int val) { assert(head); if (pos < 0)return false; CNode* pnode = head; while (pnode->next != head && pos--) { pnode = pnode->next; } if (pos != 0) { return false; } return Insert_Next(pnode, val); }
bool Del_Next(CNode* ptr) { assert(ptr); CNode* p = ptr->next; ptr->next = p->next; free(p); return true; }
bool Pop_Front(PCNode head) { assert(head); if (head == head->next) { return false; } return Del_Next(head); }
bool Pop_Back(PCNode head) { assert(head); CNode* p = head; if (head == p->next) { return false; } while (p->next->next != head) { p = p->next; } return Del_Next(p); }
bool Del_Pos(PCNode head, int pos) { assert(head); if (pos < 0)return false; CNode* p = head; if (NULL == p->next)return false; int count = pos - 1; while (p->next != head && count--) { p = p->next; } if (count != -1) { return false; } return Del_Next(p); }
bool Del_Val(PCNode head, int val) { assert(head); CNode* p = head; if (NULL == p->next)return false; while (p->next != head && val != p->next->data) { p = p->next; } if (p->next == head) { return false; } return Del_Next(p); }
struct CNode* Search(PCNode plist, int val);
struct CNode* Get_Prev(PCNode plist, int val);
struct CNode* Get_Next(PCNode plist, int val);
int Get_Length(PCNode head) { assert(head); CNode* p = head; int count = 0; while (p->next != head) { count++; } return count; }
bool IsEmpty(PCNode head) { assert(head); if (head->next == head)return true; return false; }
void Clear(PCNode head) { assert(head); while (!IsEmpty(head)) { Pop_Front(head); } }
void Destroy1(PCNode head) { assert(head); Clear(head); }
void Destroy2(PCNode plist);
void Show(PCNode head) { assert(head); CNode* p = head->next; while (p != head) { printf("%3d ", p->data); p = p->next; } printf("\n"); } int main() { CNode cn; Init_clist(&cn); for (int i = 0;i < 10;i++) { Insert_Pos(&cn, i,i+1);
} Show(&cn); Push_Front(&cn, 100); Push_Back(&cn, 200); Show(&cn);
Pop_Front(&cn); Pop_Back(&cn); Show(&cn);
Del_Val(&cn, 6); Show(&cn); Del_Pos(&cn, 6); Show(&cn); Destroy1(&cn); Show(&cn); }
|