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
| #include<stdio.h> #include<stdlib.h> #include<stdbool.h> typedef struct LNode { int data; struct LNode *next; }LNode,*LinkList;
LinkList LinkList_init(LinkList L) { L = (LinkList)malloc(sizeof(LNode)); return L; }
LinkList LinkList_HeadInsert(LinkList L) { LinkList s; int x; LinkList_init(L); scanf("%d",&x); while (x != 9999) { s= (LinkList)malloc(sizeof(LNode)); if (s == NULL) { return NULL; } s->next= L->next; s->data=x; L->next=s; scanf("%d",&x); } return L; }
LinkList LinkList_TailInsert(LinkList L) { LinkList_init(L); LinkList s,r=L; int x; scanf("%d", &x); while (x!= 9999) { s= (LinkList)malloc(sizeof(LNode)); if (s == NULL) { return NULL; } r->next=s; s->data=x; s->next=NULL; r = s; scanf("%d",&x); } return L; }
LinkList GetElem(LinkList L, int i) { if (i < 1) { return NULL; } LinkList p = L->next; for (int j = 1; j < i&&p!=NULL; j++) { p = p->next; } return p; }
LinkList LocateElem(LinkList L, int a) { LinkList p = L->next; while (p->data != a && p != NULL) { p= p->next; } return p; } int LocatePos(LinkList L, int a) { LinkList p = L->next; int i = 1; while (p->data != a && p != NULL) { p = p->next; i++; } return i; }
int LinkListLength(LinkList L) { LinkList p = L; int i = 0; while (p != NULL) { i++; p= p->next; } return i; }
bool Linklist_Insert(LinkList L,int num,int a) { if (a < 1) { return false; } if (a == 1) { LinkList s = (LinkList)malloc(sizeof(LNode)); if (s == NULL) { return false; } s->data = num; s->next = L->next; L->next = s; return true; } LinkList p = GetElem(L, a - 1); LinkList s = (LinkList)malloc(sizeof(LNode)); if (s == NULL) { return false; } s->data = num; s->next = p->next; p->next = s; return true; }
bool LinkList_Delete(LinkList L, int a) { if (a < 1) { return false; } LinkList p = GetElem(L, a - 1); if (p == NULL) { return false; } LinkList q = p->next; p->next = q->next; free(q); return true; }
|