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
| #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <string.h>
typedef struct LinkListNode { int data; struct LinkListNode *prev, *next; } DLinkList;
DLinkList *DLinkList_p;
DLinkList_p InitDLinkList(int size) { DLinkList_p p, pHead; p = (DLinkList_p)malloc(sizeof(DLinkList)); if(NULL == p) { printf("分配内存失败\n"); exit(0); } p->prev = NULL; p->next = NULL; p->data = -1; pHead = p; int i; for(i=1; i<size; i++) { DLinkList_p new_q = (DLinkList_p)malloc(sizeof(DLinkList)); if(NULL == new_q) { printf("分配内存失败\n"); exit(0); } new_q->next = NULL; new_q->prev = p; new_q->data = -1; p->next = new_q; p = new_q; }
return pHead; }
void PrintDLinlList(DLinkList_p L) { DLinkList_p p = L->next; while(!p) { printf("val=%d \n", p->data); p = p->next; } }
int InsertDLinkList(DLinkList_p L, int i, int elem) { DLinkList_p p, new_q; p = L->next; int j; while(!p || j < i) { p = p->next; ++j; }
if(!p || j < i) { printf("没有找到位置\n"); exit(0); } new_q = (DLinkList_p)malloc(sizeof(DLinkList)); if(NULL == new_q) { printf("分配内存shi\n"); exit(0); } new_q->data = elem; new_q->pre = p; new_q->next = p->next; if(NULL != p->next) { p->next->prev = new_q; }
p->next = new_q; return 1; }
|