單向鏈表特點:
存儲的內存空間不連續?。為了彌補順序存儲存劣勢。
優勢
插入,刪除???O(1)
動態存儲?,在程序運行期間決定大小。
劣勢:
不能隨機訪問???O(N)?
節點->?數據域+指針域?
順序表(數組)?只有數據域
鏈表的操作代碼:
#include "linklist.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>LinkList *CreateLinkList()
{ // 1000LinkList *ll = malloc(sizeof(LinkList));if (NULL == ll){perror("CreateLinkList malloc");return NULL;}ll->head = NULL;ll->clen = 0;return ll;
}int InsertHeadLinkList(LinkList *list, DATATYPE *data)
{LinkNode *newnode = malloc(sizeof(LinkNode));if (NULL == newnode){perror("InsertHeadLinkList malloc");return 1;}//新節點的初始化memcpy(&newnode->data, data, sizeof(DATATYPE));newnode->next = NULL;//鏈表非空的情況if (!IsEmptyLinkList(list)){newnode->next = list->head;}list->head = newnode;list->clen++;return 0;
}int IsEmptyLinkList(LinkList *list)
{return 0 == list->clen;
}int ShowLinkList(LinkList *list)
{LinkNode *tmp = list->head;while (tmp){printf("name:%s sex:%c age:%d score:%d\n", tmp->data.name, tmp->data.sex,tmp->data.age, tmp->data.score);// tmp++tmp = tmp->next;}return 0;
}int InsertTailLinkList(LinkList *list, DATATYPE *data)
{if (IsEmptyLinkList(list)){return InsertHeadLinkList(list, data);}else{LinkNode *tmp = list->head;// tmp 需要停在最后一個有效節點上while (tmp->next){tmp = tmp->next;}LinkNode *newnode = malloc(sizeof(LinkNode));if (NULL == newnode){perror("InsertTailLinkList malloc");return 1;}memcpy(&newnode->data, data, sizeof(DATATYPE));newnode->next = NULL;tmp->next = newnode;}list->clen++;return 0;
}int InsertPosLinkList(LinkList *list, DATATYPE *data, int pos)
{int len = GetSizeLinkList(list);if (pos < 0 || pos > len){fprintf(stderr, "InsertPosLinkList pos error\n");return 1;}// insertheadif (0 == pos){return InsertHeadLinkList(list, data);}// inserttailelse if (pos == len){return InsertTailLinkList(list, data);}else //中間插入{LinkNode *tmp = list->head;int off = pos - 1;// tmp 需要停在待插下標節點的前一位置while (off--){tmp = tmp->next;}LinkNode *newnode = malloc(sizeof(LinkNode));if (NULL == newnode){perror("InsertposLinkList malloc");return 1;}memcpy(&newnode->data, data, sizeof(DATATYPE));newnode->next = NULL;newnode->next = tmp->next;tmp->next = newnode;}list->clen++;return 0;
}int GetSizeLinkList(LinkList *list)
{return list->clen;
}LinkNode *FindLinkList(LinkList *list, char *name)
{LinkNode *tmp = list->head;while (tmp){if (0 == strcmp(tmp->data.name, name)){return tmp;}// tmp++;tmp = tmp->next;}return NULL;
}int DeleteLinkList(LinkList *list, char *name)
{if (IsEmptyLinkList(list)){fprintf(stderr, "DeleteLinkList empty list\n");return 1;}LinkNode *tmp = list->head;//刪除的是第一個節點if (0 == strcmp(tmp->data.name, name)){list->head = list->head->next;free(tmp);list->clen--;}//非第一個節點else{while (tmp->next){if (0 == strcmp(tmp->next->data.name, name)){//標記待刪除的節點LinkNode *del = tmp->next;//鏈表的指針跨過待刪節點tmp->next = tmp->next->next;free(del);list->clen--;break;}tmp = tmp->next;}}return 0;
}int ModifyLinkList(LinkList *list, char *name, DATATYPE *data)
{LinkNode* tmp = FindLinkList(list, name);if(NULL == tmp){printf("modify error\n");return 1;}memcpy(&tmp->data,data,sizeof(DATATYPE));return 0;
}int DestroyLinkList(LinkList *list)
{LinkNode* tmp = list->head;//刪除鏈表while(tmp){list->head = list->head->next;free(tmp);tmp = list->head;}// 釋放鏈表表頭free(list);return 0;
}