Concept and establishment of linked list

Source: Internet
Author: User

We know that an array computer automatically assigns a contiguous storage unit based on a pre-defined array type and length, the position and distance of the same array are fixed, that is, the address of any one of the array elements can be computed by a simple formula, so this structure can be effectively random access to the array elements.

However, if the array element is inserted and deleted, it will cause a lot of data movement, which makes the simple data processing become very complex and inefficient.

In order to solve these problems effectively, a kind of data structure called "linked list" has been widely used.

What is a linked list:

A linked list is a dynamic data structure that is characterized by a set of arbitrary storage units (which can be contiguous or discontinuous) to hold the elements.

Each element in the list becomes a "node", and each node is composed of a data field and a pointer field , and the pointer field in each node points to the next node.

Head is the "first pointer", which indicates the beginning of the list, which points to the first node, and the pointer field of the last pointer is NULL (open address), which indicates the end of the linked list.

It can be seen that the list structure must be implemented using pointers, that is, a node must contain a pointer variable to hold the address of the next node. In fact, each node in a linked list can have several data and several pointers.

A linked list with only one pointer in a node is called a single-linked list, which is the simplest list structure.

It is simpler to implement a single-linked list structure in C + +. For example, the simplest form of a single-linked list structure can be defined as follows

struct struct linknode{    int  Data defined;     *Next;};

The structure type is used here. Where *next is the pointer field, which points to the next node of the node; data is an shaping variable that holds information in the node. Of course, data can be any data type, including struct type or class type, structure can also contain multiple data fields, and can contain two pointer fields, containing two pointer fields called doubly linked list.

There are a number of different types of linked lists: one-way lists, two-way lists, and two-way circular lists. Linked lists can be implemented in a variety of programming languages. The built-in data types of languages like Lisp and scheme include the access and operation of the linked list. Programming languages or object-oriented languages such as c,c++ and Java rely on variable tools to generate linked lists.

The creation and output steps of the linked list. The creation of a single-linked list takes the following steps:

1) Define the data structure of the linked list, and the advantage of the structure is that it can store data of different data types;

2) Create an empty table;

3) Use the malloc () function to apply a node to the system;

4) Assign a null pointer member to the new node. If the table is empty, connect the new node to the table header, or the new node to the end of the table if it is not a null table;

5) Determine if there are subsequent nodes to access the linked list, if there is a switch to 3, or end;

See Example:

//struct struct linknode{definedintData; struct Linknode*Next;};    typedef linknode Node; //defines the type of node and node as struct type//Create a single linked list Node*createlinktable (void) {Node*head, *a, *C; Head= NULL; //Define a head node, assuming it is empty, that it is an empty list//Add node A to the empty list below= B = (node*) malloc (sizeof (Node)); //Use the malloc () function to allocate a node memory space to the system memory request. printf ("Please enter an integer:"); scanf_s ("%d", &a->data, sizeof (A->data)); //input data to Node A's data fieldsif(Head = =NULL) {Head= A; //if it is an empty list, let the head node point to the new Node A, which is the header node. The pointer field of a points to the location of the next node} A= (node*) malloc (sizeof (Node)); //A continue to open up new node space for printf ("Please enter an integer:"); scanf_s ("%d", &a->data, sizeof (A->data)); //input data to the newly opened Node a data field B-Next=a; //The next node pointer of B points to the new node A B= A; //old node B again points to the new node A a= (node*) malloc (sizeof (Node)); //A continue to open up new node space for printf ("Please enter an integer:"); scanf_s ("%d", &a->data, sizeof (A->data)); //input data to the newly opened Node a data field B-Next= A; //The next node pointer of B points to the new node A B= A; //the new becomes old node B again points to the new node A a= (node*) malloc (sizeof (Node)); //A continue to open up new node space for printf ("Please enter an integer:"); scanf_s ("%d", &a->data, sizeof (A->data)); //input data to the newly opened Node a data field B-Next= NULL; //The node points to null, indicating that no subsequent nodes are available, the head node no longer points to the new node, and the data entered is invalidated.returnHead; //head node return}

The output process for a single-linked list has the following steps:

1) Find the table header;

2) If a non-empty table, the value of the output node member, is an empty table is exited;

3) Tracking the growth of the linked list, that is, to find the next node address;

4) go to 2).

Print (Node *head) {     // parameter is the head node     returned by the list *temp = head;      sets a node pointer to the head node,     while(Temp! = NULL) {   // If the head node is not empty, start outputting a node's information         printf ( " temp=%d,  temp->data=%d,  temp->next=%d \ n", Temp, Temp->data, temp->Next);         = temp->Next;  pointer along next pointer field point to next node and output     }}

The following call output is made in the source file:

int _tmain (int argc, _tchar* argv[]) {    print(createlinktable ());    System ("Pause");     return 0 ;}

Working principle flow such as:

Concept and establishment of linked list

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.