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
| #include "linklist.h" #include <stddef.h>
void headInit(LinkList * &ls, int msg[], int n); void tailInit(LinkList * &ls, int msg[], int n); void printLinkList(LinkList *ls); void destory(LinkList *&ls); int getLength(LinkList *ls); bool getElementByIndex(LinkList *ls, int pos, ElemType &e); int getElementIndex(LinkList *ls,ElemType e); bool insert(LinkList *&ls,int pos, ElemType e); bool deleteElement(LinkList *&ls, int pos, ElemType &e);
int main() { LinkList * ls;
ElemType arr1[] = {1,2,3,4,5}; headInit(ls,arr1,5); printLinkList(ls);
ElemType arr2[] = {6,7,8,9,10}; tailInit(ls,arr2,5); printLinkList(ls);
printf("\n单链表的长度是:%d\n",getLength(ls));
ElemType e; if(getElementByIndex(ls,5,e)){ printf("第%d个元素的值是:%d\n",5,e); }
int pos = getElementIndex(ls,10); if(pos){ printf("%d的位置是:%d\n",10,pos); }
e = 11; if(insert(ls,6,11)){ printf("%d插入成功\n",e); } printLinkList(ls);
if(deleteElement(ls,5,e)){ printf("\n要删除的第%d个元素是%d,已删除成功\n",5,e); } printLinkList(ls); destory(ls); return 0; }
void headInit(LinkList * &ls, ElemType msg[], int n) { int i; LinkList *node; ls = (LinkList*)malloc(sizeof(LinkList)); ls->next = NULL; for (i = 0; i < n; i++){ node = (LinkList*)malloc(sizeof(LinkList)); node->data = msg[i]; node->next = ls->next; ls->next = node; } }
void tailInit(LinkList * &ls, int msg[], int n) { ls = (LinkList*)malloc(sizeof(LinkList)); ls->next = NULL; int i; LinkList *node,*tail = ls; for (i = 0; i < n; i++){ node = (LinkList*)malloc(sizeof(LinkList)); node->data = msg[i]; tail->next = node; tail = node; } tail->next = NULL; }
void printLinkList(LinkList *ls) { LinkList *p = ls->next; while (p != NULL) { printf("%d\t", p->data); p = p->next; } }
void destory(LinkList *&ls){ LinkList * head = ls, * p = ls->next; while(p!=NULL){ free(head); head = p; p = p->next; } free(head); }
bool isEmpty(LinkList *ls){ return (ls->next == NULL); }
int getLength(LinkList *ls){ LinkList * p = ls; int i = 0; while(p->next != NULL){ i++; p = p->next; } return i; }
bool getElementByIndex(LinkList *ls, int pos, ElemType &e){ int i = 0; LinkList * p = ls; while(i<pos && p!=NULL){ p = p->next; i++; } if(p == NULL){ printf("不存在第%d个元素\n",pos); return false; }else{ e = p->data; return true; } }
int getElementIndex(LinkList *ls,ElemType e){ int pos = 1; LinkList *p = ls->next; while(p != NULL && p->data != e){ p = p->next; pos++; } if(p == NULL){ return 0; }else{ return pos; } }
bool insert(LinkList *&ls,int pos, ElemType e){ int i = 0; LinkList * p = ls; while(i < pos-1 && p != NULL){ i++; p = p->next; } if(p==NULL){ return false; }else{ LinkList * node = (LinkList *) malloc (sizeof(LinkList)); node->data = e;
node->next = p->next; p->next = node; return true; } }
bool deleteElement(LinkList *&ls, int pos, ElemType &e){ int i = 0; LinkList * p = ls , *temp; while(i < pos-1 && p!=NULL){ p = p->next; i++; } if(p==NULL){ return false; }else{ temp = p->next; e = temp->data; p->next = temp->next; free(temp); return true; } }
|