arrays, lists, stacks, and queues

Source: Internet
Author: User
Tags arrays data structures


Data structure : Refers to the collection of one or more specific relationships between each other. It sounds abstract and simple to understand: data structures are the disciplines that describe the logical relationships between objects. For example: The queue is an advanced first-out logical structure, the stack is an advanced logical structure, the family tree is a logical structure of trees. (Learning data structure is very difficult to understand why there is a "stack" this thing; the queue is easy to understand---regardless of the need to queue for shopping, the stack can be thought of as a plank road---only allow a person through the path, and can only enter from one side, and then return from this end, for example, you pushed a box into it, The second person also pushes a box to go in, at this time can only wait for the last person to pull the box out, you can quit. )

data storage Structure : It is a computer concept, simply speaking, is to describe the data in the computer storage method of the subject; The common data storage methods are two kinds: sequential storage, non-sequential storage. Sequential storage is the storage of data on a contiguous storage medium (such as a hard disk or memory)----For example: Take the 100th byte from memory to a contiguous position between 1000 bytes, store the data, and the array is the typical sequential store. The non-sequential storage is that each data does not necessarily exist in a continuous position, as long as each data knows its front data and the subsequent data, it will be able to keep all the data up, the list is a typical non-sequential storage.
arrays, lists, stacks, and queues are the most basic data structures, and any program involves one or more of them.

1 Arrays

Arrays are the most basic data structures, and many languages have built-in support arrays. Arrays are saved using a contiguous memory space, and the number of saved data is determined when allocating memory:


Figure 1.1 An array containing n data

The time spent accessing the nth data in the array is O (1) but to find a specified data in the array is O (n). When inserting or deleting data into an array, it is best to operate at the end of the array, with a time complexity of O (1), but the worst case is to insert or delete the first data, and the time complexity is O (N). When inserting or deleting data at any point in the array, the data in the back is all moved, and the data moved is related to the number of data, so the overall time complexity is still O (N).


Figure 1.2 Inserting data into an array

2 Linked list

A linked list holds data in a non-contiguous memory cell and links the various memory cells together by pointers, and the pointer to one node points to NULL. A linked list does not need to allocate a fixed-size storage space in advance, allocating a piece of memory when it needs to be stored and inserting the memory into the linked list.

The time complexity of finding nth data in a linked list and finding the specified data is O (n), but the time complexity of inserting and deleting data is O (1), because you only need to adjust the pointer to:


Figure 2.1 Linked List


Figure 2.2 Inserting a data into the linked list


Figure 2.3 Deleting a data from a linked list

It is difficult to program the list structure to be inserted and deleted, because it is necessary to remember the previous node of the current node in order to complete the insertions and deletions. For simplicity, a linked list with a head node is usually used:


Figure 2.4 Single-linked list with head node

The list above is a single-linked list, in addition to the double-linked list, the node contains a pointer to the next node and a pointer to the previous node:


Figure 2.5 Doubly linked list

A doubly linked list without a head node does not have a single-linked list when inserting and deleting data. There is also a linked list is a circular link list, it is the two-way list of the tail-to-toe:


Figure 2.6 Two-way loop linked list

Inserting or removing data from a circular doubly linked list and a circular linked list is just a few pointers to move.

3 Stacks

The stack implements a last-in-first-out semantics (LIFO). You can use an array or a linked list to implement it:


Figure 3.1 Stack

All operations on the data in the stack are done at the top of the stack, only the data at the top of the stack can be viewed, only the data can be pushed to the top of the stack, and the data can only be ejected from the top of the stack.

4 Queues

The queue implements the first-in, first-out semantics (FIFO). Queues can also be implemented using arrays and linked lists:


Figure 4.1 Queue

Queues only allow data to be added at the end of a queue and delete data in the team header. However, you can view the data at the head and tail of the team. There is also a double-ended queue that can be inserted and deleted at both ends:


Figure 4.2 Dual-ended queue

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.