Differences between linked list and ordered table

Source: Internet
Author: User

Sequence tables and linked lists are very basic data structures. They can be collectively referred to as linear tables.
A linear table (linear list) is composed of n (n ≥ 0) data elements (nodes) A [0], a [1], a [2]…, A [n-1] finite sequence.
Ordered tables and linked lists are different storage structures of linear tables. They have different characteristics and applicability. There are also many improvement measures for their respective shortcomings.

I. Sequence Table

An ordered table is generally represented as an array. data elements are stored in sequence using a set of sequential storage units, as shown in 1. It has the following features:

  • Fixed length. The length of the array must be determined before memory allocation.
  • Sequential storage space, that is, Random Access to elements is allowed.
  • The storage density is high, and all stored in the memory is data elements.
  • To access a specific element, you can use the index for access. The time complexity is.
  • To insert or delete an element in an ordered table, the movement of all elements is involved, so the time complexity is.

    Figure 1 sequence table
    The main problem with sequence tables is that the length is fixed. You can use the multiplication-replication method to support dynamic resizing and change the sequence table to a variable length.
    The specific method is to use an array with an initial capacity (which can be specified) in the initial situation. When the number of elements exceeds the length of the array, apply for an array with a length of twice the original length, and copy the old data, so that you can have a new space to store elements. In this way, the list looks like a variable length.
    A simple implementation is as follows. The initial capacity is 4.
#include <string.h> struct sqlist {    int *items, size, capacity;    sqlist():size(0), capacity(4) {        // initial capacity = 4        items = new int[capacity];    }    void doubleCapacity() {        capacity *= 2;        int* newItems = new int[capacity];        memcpy(newItems, items, sizeof(int)*size);        delete[] items;        items = newItems;    }    void add(int value) {        if (size >= capacity) {            doubleCapacity();        }        items[size++] = value;    }};

This method will inevitably waste some memory, because the array capacity will always multiply. In addition, you need to copy all the old data during each expansion, which will definitely affect the efficiency. But in fact, it is highly efficient to directly use the linked list. The specific cause will be analyzed in the next section.

Ii. Linked List

A linked list, similar to its name. each node in the table stores a pointer to the next node, and all nodes are chained. There are also single-chain tables, double-chain tables, and cyclic linked lists, as shown in figure 2.

Figure 2 linked list
A single-chain table contains only pointers to the next node and can only be traversed in one way.
A double-stranded table contains a pointer to the next node and a pointer to the previous node. Therefore, the table can be traversed in two directions.
A cyclic single-chain table links the end node with the first node to form a ring structure, which is useful in some cases.
There is also a Circular Double-chain table, which is similar to a circular single-chain table and will not be repeated here.
Because linked lists use pointers to connect nodes, they do not need to use continuous space. They have the following features:

  • The length is not fixed and can be added or deleted at will.
  • The storage space is not continuous, and data elements are linked by pointers. Each Data Element can only access one element (varies depending on a single-chain table or double-chain table ).
  • Low storage density, because each data element requires an additional pointer to the next element (two pointers are required for a double-stranded table ).
  • To access a specific element, you can only traverse the element from the linked list header. the time complexity is.
  • Insert or delete an element after a specific data element, which does not involve moving other elements. Therefore, the time complexity is. A double-linked table also allows you to insert or delete elements before a specific data element.
    As mentioned in the previous section, the multiplication-replication method can also make the sequence table length variable and the efficiency is better than that of the linked list. Below we will simply implement a single-chain table to verify this, do not care about the sequence of element insertion.
#include <stdio.h>#include <time.h>  struct node {    int value;    node *next;};struct llist {    node *head;    void add(int value) {        node *newNode = new node();        newNode->value = value;        newNode->next = head;        head = newNode;    }}; int main() {    int size = 100000;    sqlist list1;    llist list2;    long start = clock();    for (int i = 0;i < size;i++) {        list1.add(i);    }    long end = clock();    printf("sequence list: %d\n", end - start);    start = clock();    for (int i = 0;i < size;i++) {        list2.add(i);    }    end = clock();    printf("linked list: %d\n", end - start);    return 0;}

On my computer, the time consumed by the linked list is about 4 ~ 8 times. This is because the array requires only a few large memory allocations, while the linked list requires many small memory allocations. The memory allocation operation is relatively slow, as a result, the linked list speed is greatly slowed down. This is why the memory pool exists.

Therefore, the linked list is not as good as the theoretical analysis. It is subject to many constraints in practical application. In general, it is better to use sequence tables with peace of mind.

Differences between linked list and ordered table

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.