A Linked list is a common basic data structure. It is a linear table but does not store data in a linear order, instead, it takes O (n) Time to store the Pointer to the next node in each node to access a node with a specific number. the time complexity of the sequence table is O (logn) and O (1.

The linked list structure can be used to overcome the disadvantages that the array linked list needs to know the data size in advance. The linked list structure can make full use of the computer memory space for flexible dynamic memory management.

However, the linked list loses the advantage of random array reading. At the same time, the linked list has a large space overhead due to the addition of pointer fields of nodes. In computer science, linked lists can be used as a basic data structure to generate other types of data structures. A linked list is usually composed of a series of nodes. Each node contains any instance data (data fields) and one or two links ("links") that point to the location of the previous/or next node ").

The most obvious advantage of a linked list is that the regular array arrangement of associated projects may be different from the order of these data items in the memory or disk. Data Access is usually converted in different order. A linked list is a self-indicated data type because it contains a pointer (Link) pointing to another data of the same type ).

The above are provided by Du Niang! The following are the key points !!!

The linked list allows you to insert and remove nodes at any position on the table, but does not allow random access. There are many different types of linked lists: one-way linked list, two-way linked list, and cyclic linked list.

Today, let's write a single-chain table definition and an operation!

First, let's take a look at the header file content. It should include all the basic operations on the linked list provided in data structure materials. Advanced operations will be implemented in the next article!

# Ifndef _ LINK_H _ # define _ LINK_H _ # include
# Include
# Include
/* Single-chain table data field type definition */typedef int ElemType;/* single-chain table Node structure definition */typedef struct _ Node {ElemType data; struct _ NODE * next;} Node, * PNODE;/* single-chain header NODE type definition */typedef node head, * PHEAD;/* single-chain header NODE initialization */int Init (PHEAD * pHead ); /* single-chain table printing */void Show (PHEAD pHead);/* single-chain table inserting nodes behind the header node */int Insert_Head (PHEAD pHead, ElemType e ); /* Insert node at the end of a single-chain table */int Insert_Tail (PHEAD pHead, ElemType e);/* Insert a node at a node in a single-chain table */int Insert_AnyPos (PNODE node, elemType e);/* Insert a node before a single-chain table node. If e does not exist, insert it to the end */int Insert_Previous (PHEAD pHead, ElemType e ); /* obtain the precursor of a node from a single-chain table */int Get_Prior (PHEAD pHead, PNODE, PNODE * pre);/* Delete a specified node */int Delete (PHEAD pHead, elemType e);/* single-chain table destruction */int Destory (PHEAD * pHead);/* Reverse */void Reverse (PHEAD pHead ); /* Sort a single-chain table */int Sort (PHEAD Sort); # endif

The following is the implementation of the basic operations of a single-chain table, which is basically not difficult. I have carefully typed it one word at a time and verified it through debugging!

Hope to help the children's shoes in need!

/* Function: single-chain table definition and basic operations! Name: Ben time: 22: 46 2014/1/21 */# include "link. h" int Init (PHEAD * pHead) {int rv = 0; if (NULL! = * PHead) {return rv;} * pHead = (PHEAD) malloc (sizeof (HEAD); if (NULL = * pHead) {rv =-1; return rv;} memset (* pHead, 0, sizeof (HEAD); return rv;} void Show (PHEAD pHead) {if (NULL = pHead | NULL = pHead-> next) {printf ("Empty Link! \ N "); return;} pHead = pHead-> next; while (pHead) {printf (" % d-> ", pHead-> data ); pHead = pHead-> next;} printf ("NULL \ n"); return;} int Insert_Head (PHEAD pHead, ElemType e) {int rv = 0; if (NULL = pHead) {rv =-2; return rv;} PNODE node = NULL; node = (PNODE) malloc (sizeof (NODE )); if (node = NULL) {rv =-1; return rv;} node-> data = e; node-> next = pHead-> next; pHead-> next = node; return rv;} int Insert_Tail (PHEAD pHead, ElemTy Pe e) {int rv = 0; if (NULL = pHead) {rv =-2; return rv;} PNODE node = NULL; node = (PNODE) malloc (sizeof (NODE); if (node = NULL) {rv =-1; return rv;} while (pHead-> next! = NULL) {pHead = pHead-> next;} node-> data = e; node-> next = pHead-> next; pHead-> next = node; return rv ;} int Insert_AnyPos (PNODE node, ElemType e) {int rv = 0; if (NULL = node) {rv =-2; return rv;} PNODE pNode = NULL; pNode = (PNODE) malloc (sizeof (NODE); if (NULL = pNode) {rv =-1; return rv;} pNode-> data = e; pNode-> next = node-> next; node-> next = pNode; return rv;} int Insert_Previous (PHEAD pHead, ElemType e) {int rv = 0; if (NULL = pHead) {rv =-2; return rv;} while (pHead-> data! = E & pHead-> next! = NULL) {pHead = pHead-> next;} PNODE node = NULL; node = (PNODE) malloc (sizeof (NODE); if (node = NULL) {rv =-1; return rv;} node-> data = e; node-> next = pHead-> next; pHead-> next = node; return rv ;} PNODE Get_Prior (PHEAD pHead, PNODE node) {if (pHead = NULL | node = NULL) {return NULL;} PNODE p = pHead; while (p-> next! = Node & p-> next! = NULL) {p = p-> next;} if (p-> next = NULL) {return NULL;} return p;} int Delete (PHEAD pHead, ElemType e) {int rv = 0; if (NULL = pHead) {rv =-2; return rv;} while (pHead-> next-> data! = E & pHead-> next! = NULL) {pHead = pHead-> next;} if (NULL = pHead-> next) {return rv;} else {PNODE tmp = pHead-> next; pHead-> next = pHead-> next; free (tmp); tmp = NULL;} return rv;} int Destory (PHEAD * pHead) {int rv = 0; if (NULL = * pHead) {rv =-2; return rv;} while (NULL! = (* PHead)-> next) {PNODE tmp = (* pHead)-> next; (* pHead)-> next = (* pHead)-> next; free (tmp);} free (* pHead); * pHead = NULL; return rv;} void Reverse (PHEAD pHead) {if (! PHead |! PHead-> next |! PHead-> next) {return;} PNODE p1 = pHead-> next; PNODE p2 = p1-> next; PNODE p3; p1-> next = NULL; while (p2) {p3 = p2-> next; p2-> next = p1; p1 = p2; p2 = p3;} pHead-> next = p1 ;} void Sort (PHEAD pHead) {if (! PHead |! PHead-> next |! PHead-> next) {return;} PNODE p1 = pHead-> next; PNODE p2; while (p1) {p2 = Get_Prior (pHead, p1 ); PNODE tmp = p1-> next; while (p2! = PHead & p2-> data> p1-> data) {if (p2-> next = p1) {p2-> next = tmp;} p2 = Get_Prior (pHead, p2);} if (p2-> next! = P1) {p1-> next = p2-> next; p2-> next = p1;} p1 = tmp ;}}