Data Structures-linear chain-type storage structure

Source: Internet
Author: User

Chain-Store

Chained storage: Stores data elements in a linear table with a set of arbitrary storage units. Linear lists, which are stored in this way, are referred to as linear list.

存储链表中结点的一组任意的存储单元可以是连续的,也可以是不连续的,甚至是零散分布在内存中的任意位置上的。链表中结点的逻辑顺序和物理顺序不一定相同。(即不要求逻辑上相邻的元素在物理位置上也相邻)

In order to correctly represent the logical relationship between nodes, each node value must be stored at the same time as the address (or position) indicating its immediate successor, called the pointer (pointer) or chain, which makes up the storage image of the data element AI
A linked list links the n nodes of a linear table to their logical order by the pointer field of each node.
Each node contains only a linked list of pointer fields, called a single-linked list.

For ease of operation, always precede the first node of the list with a head node (head pointer) head pointing to the first node (that is, the pointer field of the head node holds the storage location of the first node). The data field of the head node may not store any information (or information such as the length of the linked list).

Because the last data element has no direct successor, the pointer to the last node in the linear list is empty (null).

For ease of operation, always precede the first node of the list with a head node (head pointer) head pointing to the first node (that is, the pointer field of the head node holds the storage location of the first node). The data field of the head node may not store any information (or information such as the length of the linked list).

Because the last data element has no direct successor, the pointer to the last node in the linear list is empty (null).

The description and realization of node
  C语言中用带指针的结构体类型来描述
typedef  struct  Lnode{   ElemType  data;     /*数据域,保存结点的值 */struct   Lnode  *next;      /*指针域,类型是struct   Lnode */}LNode, *LinkList;        /*结点的类型名 *///LinkList L;L为单链表名,同时也可作为表的头指针名,指向表中第一个结点,即L的指针域存储第一个结点的地址。//若L为空,则表示线性表为空表,其长度为0.

The realization of the knot point
Nodes are implemented by dynamic allocation and deallocation, which are allocated when needed, and are freed when not needed. Implementation is the standard function provided by the C language, respectively: malloc (), realloc (), sizeof (), free ().

Dynamically allocated p= (lnode*) malloc (sizeof (Lnode));
The function malloc allocates a space for a node variable of type Lnode and puts its first address in the pointer variable p.

Dynamic release free (p);
The system recycles the memory area pointed to by the pointer variable p. P must be the return value when the malloc function was last called.

Algorithm Description Lnode*Create_linklist (void)/ * Header Insertion method to create a single linked list, the head node of the linked list as the return value * /{intData; Lnode*Head*P;head=(Lnode*) malloc (sizeof (Lnode)); head -Next=NULL;/ * CREATE TABLE header node of the linked list head * /  while(1)//Always execute{scanf ("%D ",&Data) ;if(Data==32767) Break;p=(Lnode*) malloc (sizeof (Lnode));p -Data=Data;/ * Data field Assignment * /P -Next=Head -Next; Head -Next=P;/ * Hook chain, the newly created node is always the first node. * /}return(head);}The //return value is the header node head, which is actually the list created. This function is called when a variable of type Lnode * is built in the main function. AsLnode*Head or linklist head; Head=Create_linklist ();
Tail Insert method to build a table

The head insertion method establishes a linked list although the algorithm is simple, but the order of the nodes in the resulting list is the opposite of the input sequence. If the order of the two is consistent, the method of tail interpolation can be used to construct the table.

The method is to insert a new node into the footer of the current linked list, making it the tail node of the current list.

Algorithm Description Lnode*Create_linklist (void)/ * Tail insert to create a single linked list, the head node of the linked list as the return value * /{intData; Lnode*Head*P*Q;head=P=(Lnode*) malloc (sizeof (Lnode)); P -Next=NULL;/ * Create a single-linked table header node head * / while(1) {scanf ("%D ",& Data);if(Data==32767) break; q=(Lnode*) malloc (sizeof (Lnode)); Q -Data=Data;/ * Data field Assignment * /Q -Next=P -Next P -Next=Q P=Q;/ * Hook chain, the newly created node is always the last node.}return(head); }
     无论是哪种插入方法,如果要插入建立单链表的结点是n个,算法的时间复杂度均为O(n)。对于单链表,无论是哪种操作,只要涉及到钩链(或重新钩链),如果没有明确给出直接后继,钩链(或重新钩链)的次序必须是“先右后左”,否则就会丢掉链表中的一些结点。
Single-linked list lookup

(1) Find the first element in the single linked list by ordinal.
For single-linked lists, it is not possible to access the nodes directly by ordinal I, as in the sequential table, but only from the head node of the linked list, and down the Link field next node to search until the first node is searched. Therefore, the linked list is not a random access structure.
To set the length of the single-linked list to n, to find the first node in the table, the value of I is valid only when 1≦i≦n.

The algorithm idea is as follows:

 从头结点开始顺链扫描,用指针p指向当前扫描到的结点,用j作统计已扫描结点数的计数器,当p扫描下一个结点时,j自动加1。 P的初值指向头结点,j的初值为1。当j=i时,指针p所指的结点就是第i个结点。
Algorithm description Elemtype Get_elem (lnode*L, int i) {int J; Lnode*P;p=L -Next J=1;/ * to point P to the first node * / while(p!=NULL &&J<i) {p=P –>Next J++; }/ * Move pointer p, j count * /if(!P||J>Ireturn(ERROR);/ * p is null to denote i>n; J>i represents i=0 * / Else      return(p -Data);} Frequency of moving the pointer p: I=0When0Times I∈[1N]: i-1 times; i>n:n times. ∴ time Complexity: O (n). 

Find by value
Lookup by value is the node in the linked list that finds whether the node value equals the given value of key? If so, returns the location where the node where the value was first found is the key, otherwise null is returned. From the start node, the value of the node and the given value key are compared on a list-by-point basis.

Algorithm Description Lnode*Locate_node (Lnode*L,int key)/ * Look for the first node of key in a single-linked list with L as the head node * /{Lnode*P=L -Next while(p!=NULL&&P -Data!=Key) P=P –>Nextif(p -Data==KeyreturnPElse{printf ("The node you are looking for does not exist.!! \n "); RETUTN (NULL); }} The execution of the algorithm is related to the parameter key, and the average time complexity is O (n).

Data Structures-linear chain-type storage structure

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.