Chained storage for linear tables
- Chained storage for linear tables
- Basic concepts
- Design and implementation
- Implementation code
- Advantages and Disadvantages
1. 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.
2. Design and implementation
Lead node, position from 0 single-linked list returns the value of the element at the 3rd position in the list
linklistnode* linklist_get (linklist* list,int POS){inti =0; Tlinklist*tlist= (tlinklist*)List Linklistnode*current= NULL; Linklistnode*ret= NULL;if(List==null | |POS<0||POS>=tList->length) {returnNULL; } current = (Linklistnode*)TList; for(i=0; i<POS; i++) {current = Current->Next; } ret = current->Next;returnRET;}
-Returns a third position
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;
So if you want to return the value of the element of position n, what you need to do
RET = current->next;
The problem is that the pointer to the head node moves n times to the first N-1 element.
3. Implementing the Code
#ifndef _mylinklist_h_#define _MYLINKLIST_H_#include <stdlib.h>#include <stdio.h>#include <memory.h>typedef voidlinklist;//Data type encapsulation/ * Data structure of a linear table (linked list) node--only a pointer to its own type (domain) */typedef struct_tag_linklistnode{struct_tag_linklistnode* Next;} Linklistnode; linklist* linklist_create ();voidLinklist_destroy (linklist*List);voidLinklist_clear (linklist*List);intLinklist_length (linklist*List);intLinklist_insert (linklist*List, linklistnode* node,intPOS); linklistnode* Linklist_get (linklist*List,intPOS); linklistnode* Linklist_delete (linklist*List,intPOS);#endif
#include "TLinkList.h"/ * Header data structure for linear table-type storage * /typedef struct _tag_linklist{Linklistnode head;//Header node (the Pointer field in the list points to the first node of the list)int length;//Number of nodes}tlinklist;abstract data type for the//linked list/ * Linked list creation * /Linklist*Linklist_create () {tlinklist*Tmp= NULL; Tmp=(tlinklist*) malloc (sizeof (tlinklist));//Allocate memory for table header if(NULL ==tmp//Check allocation results{printf ("malloc error!\n");return NULL; }/ * Table Header data member initialization * /memset (TMP,0, sizeof (tlinklist));/ * Return table header address * / returnTMP;}/ * Linked list destroyed * /voidLinklist_destroy (linklist* List){if(List != NULL)//legality detection{Free (List);//Free memory List = NULL;//Avoid wild hands}Else{printf ("list is error!\n"); }return;}/ * Empty the list */voidLinklist_clear (linklist* List) {Tlinklist*Tmp= NULL;if(List == NULL) {printf ("list is error!\n");return; } tmp=(tlinklist*)List;/ * Place length 0 * /Tmp -Length= 0;/ * Position the pointer pointing to the first node 0 * /Tmp -Head.Next= NULL;return;}/ * Get the number of nodes in the list * /int Linklist_length (linklist* List) {Tlinklist*Tmp= NULL;if(List == NULL) {printf ("list is error!\n");return -1; } tmp=(tlinklist*)List;returnTmp -Length;}/ * Insert node * /int Linklist_insert (linklist* List, Linklistnode*node, int pos) {tlinklist*Tmp= NULL;//Point to list headerLinklistnode*Current= NULL;//Auxiliary pointer variableint I= 0;//Cyclic variables / * Legality detection * / if(List == NULL ||Node== NULL ||Pos< 0) {printf ("argv is error!\n");return -2; } tmp=(tlinklist*)List;/ * Fault tolerance correction * / if(POS>=Tmp -Length) {pos=Tmp -Length }/ * Point the auxiliary pointer variable to the header node * /Current= &(TMP -Head);/ * Secondary pointer variable jumps back to the previous node where you want to insert the position * /for (i= 0; I<Pos&&Current -Next!= NULL; I++) {Current=Current -Next }/ * The pointer field of the new node points to the next node * /Node -Next=Current -Next/ * The pointer field of the previous node points to the new node * /Current -Next=Node/ * Modify length * /Tmp -Length++;return 0;}/ * Gets the element at the specified position * /Linklistnode*Linklist_get (linklist* List, int pos) {tlinklist*Tmp= NULL; Linklistnode*Current= NULL; int I= 0;if(List == NULL ||Pos< 0) {printf ("argv is error!\n");return NULL; } tmp=(tlinklist*)List;if(POS>=Tmp -Length) {pos=Tmp -Length } current= &(TMP -Head); for (i= 0; I<Pos&&Current -Next!= NULL; I++) {Current=Current -Next }/ * Returns the address of the business node in the appropriate location * / returnCurrent -Next;}/ * Delete the node at the specified location * /Linklistnode*Linklist_delete (linklist* List, int pos) {tlinklist*Tmp= NULL; Linklistnode*Current= NULL; Linklistnode*Del_res= NULL;//second auxiliary pointer variable to hold the node to be deletedint I= 0;/ * Legality detection * / if(List == NULL ||Pos< 0) {printf ("argv is error!\n");return NULL; } tmp=(tlinklist*)List;/ * Fault tolerance correction * / if(POS>=Tmp -Length) {pos=Tmp -Length }/ * Auxiliary pointer variable points to header node * /Current= &(TMP -Head);/ * Secondary pointer variable jumps back to the previous position in the specified position * /for (i= 0; I<Pos&&Current -Next!= NULL; I++) {Current=Current -Next }/ * Save the node you want to delete so that the upper app reclaims the memory * /Del_res=Current -Next/ * Point the Pointer field in the previous position to the next node in the specified position * /Current -Next=Del_res -Next/ * Modify length * /Tmp -Length--;/ * Returns the address of the deleted node * / returnDel_res;}
#include "TLinkList.h"typedef structteacher{linklistnode node;Charname[ -];intAge;} Teacher;intMain () {Teacher T1, T2, T3;intLength, i =0; Linklist *List= NULL; T1.age = to; T2.age = +; T3.age = -;List= Linklist_create (); Length = Linklist_length (List);//Business node is teacher and algorithm classification .... ThoughtLinklist_insert (List, (Linklistnode *) &t1, Linklist_length (List)); Linklist_insert (List, (Linklistnode *) &t2, Linklist_length (List)); Linklist_insert (List, (Linklistnode *) &t3, Linklist_length (List));//Traverse linked list for(i =0; I<linklist_length (List); i++) {Teacher *tmp = (Teacher *) Linklist_get (List, i);if(tmp! = NULL) {printf("age:%d", tmp->age); } } while(Linklist_length (List) >0) {Teacher *tmp = (Teacher *) Linklist_delete (List,0);if(tmp! = NULL) {printf("age:%d", tmp->age); }} Linklist_destroy (List); System"Pause");return 0;}
4. Pros and cons
- No need to customize the capacity of the linked list at once
- Insert and delete operations do not need to move data elements
- 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
Chained storage for linear tables