Chapter 10 basic data structure-linked list

Source: Internet
Author: User

Linked List

The difference between a linked list and an array is that the sequence of elements in the linked list is determined by pointers in each object, and the adjacent elements are not necessarily adjacent to the physical memory. The linked list can be used to flexibly represent dynamic sets. Linked lists include single-chain tables, double-chain tables, and circular linked lists. The book focuses on the concepts and operations of double-stranded tables. Each element of double-stranded table L is an object, and each object contains a keyword and two pointers: Next and Prev. The operations of a linked list include inserting a node, deleting a node, and searching for a node. The key point is to insert and delete nodes in a two-way linked list, as shown in the following figure:

The linked list is the most basic data structure. Anyone who wants to learn computer skills is often asked during interviews. Baidu will know about the implementation of the linked list. You can discuss the exercise questions related to the linked list.

(1) Insert an element into a single-chain table. The time complexity is O (1 ).

A: Normally, an element inserted in the linked list is inserted at the end. In this case, you need to traverse the linked list from the beginning and find the end at O (n ). To insert a new node at O (1) time, you can consider inserting it after the header node each time, that is, each inserted node becomes the first node of the linked list.

(2) deleting a given node P on a single-chain table requires that the time complexity be O (1 ).

Answer: Generally, when deleting a node, we need to find the front node Q of the node P. We need to traverse the linked list and run the O (n-1) time ). We can consider replacing the value of S of the successor node of Q with the value of Q, and then deleting S. For example, the process of deleting node Q:

(3) reverse placement of a single-chain table. No additional storage space can be allocated. recursion is not allowed. Temporary variables can be used and the execution time is O (n ).

Answer: This question is often encountered in the interview test, and the pointer is reversed in basic thinking. As shown in:

(4) traverse the single-chain table once to find the intermediate node of the linked list.

A: define two pointers, p and q, which are initially directed to the head node of the linked list. Then start traversing backward. P moves two steps and Q moves one step at a time. When P reaches the end, P exactly reaches the center position.

(5) use a single-chain table L to implement a stack. The operation time for push and pop is O (1 ).

A: based on the advanced and backward features of the elements in the stack, you can insert and delete elements in the head of the linked list.

(6) use a single-chain table L to implement a queue. The operation time of enqueue and dequeue is O (1 ).

A: The elements in the queue are FIFO. Add a tail pointer to the single-link table structure, insert data from the tail, and delete the data from the header.

3. Tree Representation

The data structure of the linked list is used to represent the tree. In the book, the chain table representation of the binary tree is first reduced, and then the number of branches is expanded to an unlimited number. Let's take a look at the linked list representation of a binary tree. We use the P, left, and right fields to store pointers to the father, left, and right children in the binary tree T. As shown in:

For a root tree with no limit on the number of branches, use the representation of the left child and right brother. In this way, each node of the tree contains a parent pointer P, and the other two pointers:

(1) left_child points to the leftmost child of the node.

(2) right_sibling points to the brother on the right of the node.

Chapter 10 basic data structure-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.