Overview of data Structures <2> basic concepts of linked lists

Source: Internet
Author: User

Definition of a linked list

Before you discuss a linked list, say the linear table first.

A linear table is one of the most common and simplest data structures. A linear table is a finite set of n data elements. For a non-empty linear table, there are a few characteristics: (1) There is only one data element known as "the First", (2) There is only one data element known as "The Last", and (3) each data element in the linear table has only one precursor except the first; (4) except the last one, There is only one successor for each data element in the collection.

The most common linear table is the array. For arrays, each element's storage space is contiguous, its logical relationship is adjacent to the two elements, and is adjacent to the physical location, and can be accessed through the index. The disadvantage of this storage structure is that a large number of elements need to be moved when inserting or deleting operations. And when the number of elements in the collection is large, a whole block of storage space is required.

So there's the list.

A linked list is characterized by the use of a set of arbitrary storage units to store each data element in a linear table. So how do you relate logically to the two adjacent elements? The solution to a list is to store the data element and store the address of the next element for each storage unit. Each of these storage spaces is called a node. A node consists of two parts: The data field stores the value of the element, and the pointer field stores the address of the next element. Of course, for a doubly linked list, the pointer field stores not only the address of the next element, but also the address of the previous element. It is easy to see the characteristics of the storage structure of arrays and lists: The array is the storage of contiguous addresses, the linked list is a distributed storage address, and the neighboring elements are linked by pointers.


Array



Linked list



Basic operation of two-linked list


1. Junction points

Before discussing the operation of the linked list, let's look at how to represent a linked list. We know that the constituent unit of a list is a node, so representing a node basically shows the list. In the C language, you can use structs to represent nodes:

typedef struct Node *link;struct node {    item item;    link next;};


The item is a data type, in C, it may be a basic type such as int,char,double, or it may be a struct object or even a linked list, in short, it represents the data field of the node. Link represents the pointer field, which points to the next node.

In the actual application process, often implementation does not know how many such nodes are needed, so the dynamic application is definitely required. When a node is needed, a new node is created. The C language is generally done through the malloc function.

Link x = malloc (sizeof *x);


Corresponding, release a node with free

Free (x);

2. Insert operation

A node t is inserted after the node x, without regard to the list being empty, which can be represented.

T->next = X->next;x->next = t;

3. Delete operation

Similarly, if the list is empty, the following node of a node x is deleted, which can be said.

t = x->next; X->next =t->next;free (t);

4. Traverse

One of the most common operations performed on a linked list is the traversal operation, which iterates through the elements in the linked list sequentially, performing some action on each element. If x is a pointer to the first node of the list, the pointer in the tail node is empty and the visit is a function of the element parameter, then the traversal operation can use the following statement:

for (t = x;t! = null;t = T->next)    visit (T->item);

Three-way circular linked list with Sentinel

, is a two-way circular linked list with Sentinels. Sentinel is a dumb element, that is, its data part is meaningless, just to occupy a position, so that the list is never empty, thus simplifying the judgment of the edge condition. The black part of the figure is the Sentinel, located between the head and tail of the chain. Unlike the single linked list described above, the pointer portion of a doubly linked list includes addresses that point to successors and precursors. Similarly, we briefly describe the basic operations of a two-way circular linked list, as described in the order of the single-linked lists above.


1. Junction points

typedef struct Node *link;struct node {    item item;    link next;    Link prev;};

2. Insert operation

Assuming nil is a pointer to the Sentinel element, X is the node to be inserted, and the inserted position is the table header, then the procedure is as follows.

X->next = Nil->next;nil->next->prev = X;nil->next = X;x->prev = nil;

3. Delete operation

Assuming that the node address you want to delete is X, the following steps are removed:

X->prev->next = X->next;x->next->prev = x->prev;

4. Traverse

for (t = nil->next;t! = nil;t = T->next)     visit (T->item);

The circular doubly linked list with Sentinel covers both the doubly linked list and the circular link list, mastering this list, and basically mastering all the basic knowledge of the linked list. The next blog will give a list of several applications.








Overview of data Structures <2> basic concepts of linked lists

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.