Two basic data structures:

Source: Internet
Author: User
Tags stack pop

Two basic data structures:
The basic operations of the stack include push and pop. The stack has a top pointer, pointing to the latest elements such as the stack, both the inbound and outbound operations are performed from the top of the stack.

The basic operations of the queue include enqueue and dequeue. The queue includes head and tail pointers. Elements always exit from the head of the team and enter from the end of the team. When queues are implemented using arrays, the queue space can be used cyclically to make proper use of space.

The basic operations on stacks and queues are shown in:

The stack and queue are implemented using arrays. When the queue is implemented, arrays with n length can contain up to n-1 elements, which can be used cyclically to determine whether the queue is empty or full. The program is as follows:

View CodeView Code

The test results are as follows:

Problem:

(1) describes how to use two stacks to implement a queue and analyze the running time of queue operations.

Answer: The elements in the stack are first-in-first-out, while those in the queue are first-in-first-out. The existing stacks s1 and s2 are used to store the results in the queue, and s2 is used to convert s1 to the queue. Inbound queue operation: When an element is in the queue, first judge whether s1 is null. If it is null, the new element is directly in s1, if it is not empty, all elements in s1 are stored in s2, and then the elements are imported into s1. Finally, all elements in s2 are merged into S1. In this case, s1 stores the queuing order. Team-out operation: If s1 is empty, it indicates that the queue is empty. If it is not empty, S1. The process of joining the team requires exchange between s1 and s2. The running time is O (n), and the running time of the exit operation is O (1 ). The conversion process is illustrated as follows:

I use the C ++ language to implement the whole program as follows:

View Code

(2) describes how to use two queues to implement one stack and analyze the running time of stack operations.

A: similar to the above questions, the queue is FIFO, while the stack is FIFO. The existing queues q1 and q2, q1 store the stack results, and q2 assists q1 in switching to the stack. Inbound stack operation: When an element such as a stack is used, first judge whether q1 is empty. If so, q1 is entered into the queue between the elements, if it is not empty, all elements in q1 will be merged into q2, then this element will be included in q1, and then all elements in q2 will be merged into q1. In q1, the stack sequence is stored. Out-of-stack operation: If q1 is empty, the stack is empty; otherwise, q1 is performed directly. The inbound stack operation requires direct exchange in the queue q1 and q2. The running time is O (n). The outbound stack operation is an outbound operation in the queue q1 and the running time is O (1 ). I use the C ++ language to implement the complete program as follows:

View Code

2. 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.


In the data structure, the two most basic and commonly used storage structures are: () and ()

Linked List, stack
 
What are the basic algorithms for data structures?

The so-called basic algorithm should refer:
I. Sorting Algorithm
1. Simple sorting (including Bubble sorting, insertion sorting, and selection sorting)
2. Fast sorting, very common
3. Heap sorting,
4. Merge Sorting, which is the most stable, that is, there is no bad situation.
Ii. Search Algorithms
The most basic is the binary search algorithm, the most common search algorithm, provided that the sequence has been ordered
Deep priority and limited breadth search are also available. pruning, A *, hash table, and other methods are used to optimize the search.
Iii. Of course, for basic data structures, stacks, queues, and trees. There are some basic operations
For example, stack pop, push, queue headers, such as queues; and the specific implementation of these data structures, using continuous storage space (arrays), or using linked lists, the specific implementation of the operation methods under the two storage methods is different.
There are also Tree operations, such as first-order traversal, middle-order traversal, and subsequent traversal.
Of course, these are just some basic algorithms for data structures.
The basic algorithms should have the following ideas:
1. Backtracking
2. Recursion
3. Greedy
4. Dynamic Planning
5. Sub-Governance
Some data structure textbooks do not involve basic algorithms. lz can also look for some basic algorithm books. If you are interested, you can do the exercises on oj. Algorithms are hard to learn.

Related Article

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.