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
| #include "dlinklist.h" #include <stddef.h> void headInit(DLinkList * &dll,ElemType arr[], int n); void printDoubleLinkList(DLinkList * dll); void tailInit(DLinkList * &dll,ElemType arr[], int n); bool insertElement(DLinkList *&dll, int pos, ElemType e); bool deleteElement(DLinkList *&dll, int pos, ElemType &e); int main() { DLinkList * dll; ElemType arr[] = {1,2,3,4,5}; headInit(dll,arr,5); printf("头插法:\t"); printDoubleLinkList(dll);
tailInit(dll,arr,5); printf("尾插法:\t"); printDoubleLinkList(dll);
insertElement(dll,3,6); printf("在第3个位置上插入6之后:\t"); printDoubleLinkList(dll);
ElemType e; deleteElement(dll,3,e); printf("删除第3个元素之后:\t"); printDoubleLinkList(dll); }
void headInit(DLinkList * &dll,ElemType arr[], int n){ int i; dll = (DLinkList *) malloc(sizeof(DLinkList)); dll->next = dll->prior = NULL; DLinkList * node; for(i = 0; i<n; i++){ node = (DLinkList *) malloc(sizeof(DLinkList)); node->data = arr[i];
node->next = dll->next; if(dll->next != NULL){ dll->next->prior = node; } node->prior = dll; dll->next = node; } }
void tailInit(DLinkList * &dll,ElemType arr[], int n){ int i; dll = (DLinkList *) malloc(sizeof(DLinkList)); dll->next = dll->prior = NULL; DLinkList * node, *r; r = dll; for (i = 0; i < n; ++i) { node = (DLinkList *) malloc(sizeof(DLinkList)); node->data = arr[i]; node->prior = r; r->next = node; r=node; } r=NULL; }
bool insertElement(DLinkList *&dll, int pos, ElemType e){ int i = 0; DLinkList *p = dll, *node; while(i<pos-1 && p!=NULL){ p = p->next; i++; } if(p==NULL){ printf("插入的位置不合法"); return false; }else{ node = (DLinkList *)malloc(sizeof(DLinkList)); node->data = e; node->next = p->next; node->prior = p; if(p->next != NULL){ p->next->prior = node; } p->next = node; return true; } }
bool deleteElement(DLinkList *&dll, int pos, ElemType &e){ int i = 0; DLinkList *p = dll, *node; while(i<pos-1 && p!=NULL){ p = p->next; i++; } if(p==NULL){ return false; }else{ node = p->next; if(node == NULL){ return false; } e = node->data; p->next = node->next; if(p->next != NULL){ node->next->prior = p; } free(node); return true; }
} void printDoubleLinkList(DLinkList * dll){ DLinkList *p = dll->next; while(p != NULL){ printf("%d\t",p->data); p = p->next; } printf("\n"); }
|