[C language data structure] single linked list
LinkList.h
# ifndef LINK_LIST_H#define LINK_LIST_H// linked list node typedef struct _ LinkListNode {struct _ LinkListNode * next;} LinkListNode;// single linked list typedef void LinkList;/* * create single linked list * @ return return single linked list pointer * / LinkList* LinkList_Create (); / * destroy single linked list * @ param list single linked list pointer * / void LinkList_Destroy (LinkList* list) / * clear single linked list * @ param list single linked list pointer * / void LinkList_Clear (LinkList * list); / * * insert element * @ param list single linked list pointer * @ param node element pointer * @ param pos inserted index * / int LinkList_Insert (LinkList * list,LinkListNode * node,int pos) / * * get element at index position in single linked list * @ param list single linked list pointer * @ param pos single linked list index value * @ param return element pointer * / LinkListNode* LinkList_Get (LinkList * list,int pos) / * * Delete the value at the index position in the single linked list * @ param list pointer to the single linked list * @ param pos single linked list index * @ param return non-zero indicates that the deletion is successful * / int LinkList_Remove (LinkList * list,int pos) / * * get the number of currently stored elements in a single linked list * @ param list pointer to a single linked list * @ number of stored elements in a return single linked list * / int LinkList_Length (LinkList * list); # endif / / LINKLIST_H
LinkLink.c
# include "Linklist.h" # include / / single linked list typedef struct _ LinkList {LinkListNode header;// chain header node int length;// linked list length} TLinkList;/* * create single linked list * @ return return single linked list pointer * / LinkList* LinkList_Create () {TLinkList * list = (TLinkList *) malloc (sizeof (LinkList)); if (list! = 0) {list- > header.next = 0 / / initialize the following pointer of the header node is null list- > length = 0;} return list;} / * * destroy single linked list * @ param list single linked list pointer * / void LinkList_Destroy (LinkList * list) {free (list) } / * * clear single linked list * @ param list single linked list pointer * / void LinkList_Clear (LinkList* list) {if (list! = 0) {TLinkList* l_list = (TLinkList*) list; lumped list-> header.next = 0; lumped list-> length = 0 }} / * * insert element * @ param list single linked list pointer * @ param node element pointer * @ param pos inserted index * / int LinkList_Insert (LinkList* list,LinkListNode * node,int pos) {/ / Type conversion TLinkList* l_list = (TLinkList*) list at the single linked list location / / determine whether the linked list pointer and node pointer cannot be null, and whether the current inserted position is legal int ret = ((list! = 0) & & (node! = 0) & & (pos > = 0) & & (pos length)); if (ret) {LinkListNode* current = (LinkList *) lumped list; int I / / move to the front for (I = 0; I) where you need to insert
< pos;i++) { current = current->Next;} node- > next = current- > next; / / the successor pointer of the inserted node to the precursor node current- > next = node; / / the successor pointer of the precursor node points to the inserted node lumped list-> length++;} return ret } / * * get the element * @ param list single linked list pointer * @ param pos single linked list index value * @ param return element pointer * / LinkListNode* LinkList_Get (LinkList * list,int pos) {LinkListNode* node = 0; TLinkList * l_list = (TLinkList *) list in the single linked list / / determine that the pointer to the linked list is not empty and the index obtained is legal if ((l_list! = 0) & & (pos > = 0) & (pos)
< l_list->Length) {LinkListNode* current = (LinkList *) lumped list; int i; for (I = 0; I
< pos; i++) { current = current->Next;} node = current- > next;} return node;} / * * Delete the value at the index position in the single linked list * @ param list pointer to the single linked list * @ param pos single linked list index * @ param return non-zero indicates that the deletion was successful * / int LinkList_Remove (LinkList * list,int pos) {TLinkList * l_list = (TLinkList *) list Int ret = (l_list! = 0) & & (pos > = 0) & & (pos
< l_list->Length); if (ret) {LinkListNode* current = (LinkList *) lumped list; int i; for (I = 0; I
< pos; i++) { current = current->Next;} / / the successor pointer to the precursor element of the deleted element, pointing to the successor pointer current- > next = current- > next- > next; lumped list-> length--;} return ret of the deleted element } / * * get the number of currently stored elements in single linked list * @ pointer to param list single linked list * @ number of stored elements in return single linked list * / int LinkList_Length (LinkList * list) {int ret =-1; if (list! = 0) {TLinkList * l_list = (TLinkList *) list; ret = lumped list-> length;} return ret;}
Test code
# include # include "Linklist.h" typedef struct _ node {LinkListNode node; int v;} Node;int main (void) {int i; Node n [5]; Node * node; for (I = 0polii
< 5;i++) { n[i].v = i; } LinkList *list = LinkList_Create(); for(i = 0; i < 5;i++) { LinkList_Insert(list,(LinkListNode *)&(n[i]),0); } LinkList_Remove(list,2); for(i = 0; i < LinkList_Length(list);i++) { node = (Node *)LinkList_Get(list,i); printf("%d\n",node->V);} return 0;}