Linear chain-type storage design and implementation-API implementation

Source: Internet
Author: User

Basic concepts
Chained storage definition
To represent the logical relationship between each data element and its immediate successor, each element needs to store information that indicates its direct successor, in addition to storing its own information.


Table Header Node

The first node in a linked list, containing pointers to the first data element and some information about the list itself

Data node

A node in a linked list that represents a data element, containing pointers to the next data element and information about the data element

Tail knot Point

The last data node in the list, whose next element pointer is empty, indicates no successor.


Chain List technology field deduction



Analysis of chain-link storage _API implementation

In C language, you can use structs to define the pointer fields in a linked list.
Table header nodes in a linked list can also be implemented using structs.



Single linked list with lead node, position from 0
Returns the value of an element at the 3rd position in the list

linklistnode* linklist_get (linklist* list, int pos) {if (list = = NULL | | pos < 0 | | pos >= linklist_length (list)) {RE Turn NULL;} Tlinklist *tlist = Null;tlist = (tlinklist *) list; Linklistnode *cur = Null;cur = & (Tlist->header); for (int i = 0; i < POS; ++i) {cur = cur->next;} return cur->next;}

Returns a third position of the

Where does the current pointer point after moving the POS?

Answer: Point to position 2, so need to return ret = current->next;

Note:

Loop traversal, traverse 1th time, point to position 0

Traverse 2nd time, point to position 1

Traverse 3rd time, point to position 2

Traverse nth time, point to position n-1;


Delete Element



Code:

Linklist.h#ifndef _mylinklist_h_#define _mylinklist_h_typedef void linklist;typedef struct _tag_LinkListNode{ struct _tag_linklistnode* Next;} Linklistnode; linklist* linklist_create (); void Linklist_destroy (linklist* list); void Linklist_clear (linklist* list); int Linklist_ Length (linklist* list), int linklist_insert (linklist* list, linklistnode* node, int pos); linklistnode* linklist_get (linklist* list, int pos); linklistnode* linklist_delete (linklist* list, int pos); #endif

Linklist.cpp#include <iostream> #include <cstdio> #include "linklist.h" using namespace Std;typedef void linklist;typedef struct _tag_linklist{linklistnode header;int length;} Tlinklist; linklist* linklist_create () {tlinklist *tmp = Null;tmp = (tlinklist *) malloc (sizeof (tlinklist)), if (tmp = = NULL) {printf (" function linklist_create () err.\n "); return NULL;} memset (tmp, 0, sizeof (tlinklist)); Initialize to empty list return tmp;} void Linklist_destroy (linklist* list) {if (list = = NULL) {return;} Free (list); return;} void Linklist_clear (linklist* list) {if (list = = NULL) {return;} Tlinklist *tlist = Null;tlist = (tlinklist *) List;tlist->header.next = Null;tlist->length = 0;return;} int linklist_length (linklist* list) {if (list = = NULL) {return-1;} Tlinklist *tlist = Null;tlist = (tlinklist *) List;return tlist->length;} int Linklist_insert (linklist* list, linklistnode* node, int pos) {if (list = = NULL | | node = NULL | | POS < 0) {return- 1;} Tlinklist *tlist = Null;tlist = (tlinklist *) list; LinklIstnode *cur = Null;cur = & (Tlist->header);//fault-tolerant handling of POS, if POS is too large, change to last Face if (pos > Linklist_length (List)) {pos = Lin Klist_length (list);} Move to the location where you want to insert for (int i = 0; i < POS; ++i) {cur = cur->next;} Insert Node->next = Cur->next;cur->next = Node;++tlist->length;return 0;} linklistnode* linklist_get (linklist* list, int pos) {if (list = = NULL | | pos < 0 | | pos >= linklist_length (list)) {RE Turn NULL;} Tlinklist *tlist = Null;tlist = (tlinklist *) list; Linklistnode *cur = Null;cur = & (Tlist->header); for (int i = 0; i < POS; ++i) {cur = cur->next;} return cur->next;} linklistnode* linklist_delete (linklist* list, int pos) {if (list = = NULL | | pos < 0 | | pos >= linklist_length (list)) {return NULL;} Tlinklist *tlist = Null;tlist = (tlinklist *) list; Linklistnode *cur = Null;cur = & (Tlist->header); for (int i = 0; i < POS; ++i) {cur = cur->next;} Linklistnode *ret = Null;ret = cur->next;//Delete node Cur->next = Ret->next;--tlist->length;return ret;} 

Main.cpp#include <iostream> #include <cstdio> #include "linklist.h" using namespace std;typedef struct _ Student{linklistnode Node;charname[32];intage;} Student;int Main () {Student S1, S2, S3, S4, S5, s6;s1.age = 21;s2.age = 22;s3.age = 23;s4.age = 24;s5.age = 25;s6.age = 26; Create linked list Student *list = (Student *) linklist_create ();//Insert node Linklist_insert (list, (Linklistnode *) &s1, 0); Linklist_insert (list, (Linklistnode *) &s2, 0); Linklist_insert (list, (Linklistnode *) &AMP;S3, 0); Linklist_insert (list, (Linklistnode *) &s4, 0); Linklist_insert (list, (Linklistnode *) &s5, 0); Linklist_insert (list, (Linklistnode *) &AMP;S6, 3);//Traverse list for (int i = 0; i < linklist_length (list); ++i) {Student *tmp = (Student *) linklist_get (list, i); if (tmp = = NULL) {return 0;} printf ("Age:%d\n", tmp->age);} Delete the linked list node while (Linklist_length (list)) {Student *tmp = (Student *) linklist_delete (list, 0), if (tmp = = NULL) {return 0;} printf ("Age:%d\n", tmp->age);} Linklist_destroy (list); return 0;}

Advantages:
No need to customize the capacity of the linked list at once
Insert and delete operations do not need to move data elements
Disadvantages:
The data element must hold the location information of the successor element
Gets the element that specifies the data before the element operation requires sequential access


Project Details: Github

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Linear chain-type storage design and implementation-API implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.