Algorithm and implementation of single-link and double-link tables

Source: Internet
Author: User

A linked list is a collection of independent data structures (also known as nodes) that contain some data. Each node is linked together by a pointer. Generally, nodes are dynamically allocated. In fact, nodes in the linked list can exist in various places in the memory ., Therefore, it does not affect the physical connection.

The linked list plays a major role in the data structure. It can allocate memory based on usage to avoid Memory waste. Its insertion, deletion, and search operations are based on Pointer access. Deleting or adding a node does not change the location and content of other data. Compared with arrays, the linked list is flexible and operable, but it is a little complicated and abstract when used and implemented.

 

Single-chain table

In a single-chain table, each node contains a pointer to the next node. the pointer to the last node is null to mark the last node. The single-chain table is also called because each node only has one node pointer, so it can only access the next node in sequence. As a saying goes, this store is no longer available in this village. To remember the first position of a single-chain table, you can define a head pointer.

 

 

Create a single-linked table node as follows:

Typedef struct linear_chain_node </P> <p >{</P> <p> int data; </P> <p> struct linear_chain_node * link; </P> <p >}node; </P> <p>

After the node is created, it is the data type of the node. It is an encapsulated data that contains an integer and a node pointer.

 

 

Function Algorithm Implementation for creating a single-chain table

Node * creat_single_chain (int n) </P> <p >{</P> <p> node * head, * Current, * Previous; </P> <p> int I; </P> <p> previous = (node *) malloc (sizeof (node )); </P> <p> previous-> DATA = 0; </P> <p> previous-> link = NULL; </P> <p> head = previous; </P> <p> for (I = 0; I <n; I ++) </P> <p >{</P> <p> current = (node *) malloc (sizeof (node )); </P> <p> scanf ("% d", current-> data); </P> <p> current-> link = NULL; </P> <p> previous-> link = current; </P> <p> previous = current; </P> <p >}</P> <p> return head; </P> <p >}</P> <p>

Create a single-chain table. A function is a pointer function and a pointer type is a node. Its final return value is a header node. The function creates three node pointers: The first node pointer, the current node pointer, and the previous node pointer. First, create a header pointer. The data is 0 and the pointer is null. The number of nodes is determined by the parameter n passed to the function. The current node has been created in N cycles. The data keyboard input of the current node is empty. The pointer of the previous node points to the current node, and finally replaces the current pointer with the previous pointer for the next loop.

 

 

Insert a node algorithm into a single-link table (before node I)

Int insert (node * head, int I, int X) </P> <p >{</P> <p> node * Current, * New; </P> <p> Int J; </P> <p> if (I <= 0) </P> <p> retuen 0; </P> <p> current = head; </P> <p> J = 0; </P> <p> while (j <i-1) & current! = NULL) </P> <p >{</P> <p> current = Current-> link; </P> <p> J ++; </P> <p >}</P> <p> If (current = NULL) </P> <p> return 0; </P> <p> New = (node *) malloc (sizeof (node); </P> <p> New-> DATA = x; </P> <p> New-> link = Current-> link; </P> <p> current-> link = new; </P> <p> return 1; </P> <p >}</P> <p>

In the function, traverse the node until you find the location where the node to be added is located. If the node is traversed to the last node, the function returns. If the traversal loop exists, it indicates the Insertion Location, apply for a new node. The data is X, and the Pointer Points to the next node. The current node (actually an old node) points to the new node.

 

Deletion algorithm in a single-chain table

Int Delete (node * head, int I) </P> <p >{</P> <p> node * Previous, * current; </P> <p> Int J = 1; </P> <p> if (I <1) </P> <p> return 0; </P> <p> previous = head; </P> <p> while (j <I) & previous-> link! = NULL) </P> <p >{</P> <p> previous = previous-> link; </P> <p> J ++; </P> <p >}</P> <p> If (previous-> link = NULL) </P> <p> return 0; </P> <p> current = previous-> link; </P> <p> previous-> link = Current-> link; </P> <p> free (current); </P> <p> return 1 </P> <p >}</P> <p>

In the function, traverse the node first until you find the location of the node to be deleted, point the pointer to the previous node of the node to be deleted to the next node, and release the memory.

 

 

 

Two-way linked list

As the name suggests, a two-way linked list has a node pointer more than a single-chain table to indicate the data of the previous node. The advantages of this are self-evident, avoiding the inconvenience of searching for the previous node. In the data structure, the subsequent node is called the successor, and the previous node is called the precursor.

 

The following uses C to describe the establishment of a two-way linked list.

 

In fact, a two-way linked list only adds a forward pointer. The forward Pointer Points to the previous node, and the backward pointer of the previous Pointer Points to the next node, this method enables frontend and backend access. After a single-chain table is understood, the double-chain table is easy to understand and the principle is similar.

 

Double-stranded table Insertion Algorithm

Int insert (node * head, int X, int y) <br/>{< br/> node * Current, * New; <br/> current = head-> flink; <br/> while (current! = Head) & (current-> data! = Y) <br/> current = Current-> flink; <br/> If (current = head) <br/> return 0; </P> <p> New = (node *) malloc (sizeof (node); <br/> New-> DATA = X; <br/> New-> blink = current; <br/> New-> flink = Current-> flink; <br/> (current-> flink) -> blink = new; <br/> current-> flink-> New; <br/> return 1; <br/>}< br/>

 

Double-stranded table deletion algorithm

Int Delete (node * head, int X) <br/>{< br/> node * Current; <br/> current = head-> flink; <br/> while (current! = Head) & (current-> data! = X) <br/> current = Current-> flink; <br/> If (current = head) <br/> return 0; </P> <p> (current-> blink)-> flink = Current-> flink; <br/> (current-> flink) -> vlink = Current-> blink; </P> <p> free (current); <br/> return 1; <br/>}< br/>

Typedef struct double_chain_node <br/>{< br/> int data; <br/> struct double_chain_node * blink, * flink; <br/>} node; </P> <p> node * ceate_double_chain (int n) <br/>{< br/> int I; <br/> node * head, * Previous, * current; <br/> previous = (node *) malloc (sizeof (node); <br/> previous-> DATA = 0; <br/> previous-> blink = previous-> flink = NULL; <br/> head = previous; </P> <p> for (I = 0; I <n; I ++) <br/>{< br/> current = (node *) malloc (sizeof (node )); <br/> scanf ("% d", doesn t-> data); <br/> current-> flink = NULL; <br/> current-> blink = previous; <br/> previous-> flink = current; <br/> previous = current; <br/>}< br/> return head; <br/>}< br/>

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.