Chapter One Data Structure and Algorithm Overview

Chapter One Data Structure and Algorithm Overview

1.1 The relationship between data structure and algorithm

(1) Data structure is a discipline that studies the way of organizing data. With a programming language, there is also a data structure. Learning the data structure well can write more beautiful and efficient code.

(2) To learn a good data structure, it is necessary to think more about how to solve the problems encountered in life with programs.

(3) Program = data structure + algorithm

(4) The data structure is the basis of the algorithm. In other words, if you want to learn the algorithm well, you need to learn the data structure in place.

1.2 Look at a few problems encountered in actual programming

1.2.1 String replacement problem

Insert picture description here
Summary: Need to use the singly linked list data structure

1.2.2 A Gobang program

Insert picture description here
How to judge the win or loss of the game, and complete the function of saving, exiting and continuing the game
(1) Two-dimensional chessboard array => (sparse array) -> write file [archive function]
(2) read file-"sparse array- 》Two-dimensional array-》Chess board【Continue to the previous game】

1.2.3 Josephu's problem (the problem of losing the handkerchief)

(1) Josephu’s question is: suppose n people numbered 1, 2, ... n sit in a circle, and agree that the numbered person k (1<=k<=n) starts counting from 1 and counts to the one with m When a person leaves the queue, its next person starts counting from 1, and the person who counts to m leaves the queue again, and so on, until everyone leaves the queue, thus generating a sequence of dequeue numbers.

(2) Tip: Use a circular linked list without a leading node to deal with Josephu's problem: first form a single circular linked list with n nodes (one-way circular linked list), and then count from 1 from the k nodes, and count When reaching m, the corresponding node is deleted from the linked list, and then counts from 1 to the next node of the deleted node, until the last node is deleted from the linked list and the algorithm ends.

(3) Summary: To complete the Joseph problem, you need to use the data structure of a one-way circular linked list

1.2.4 Other common algorithm problems

Insert picture description here
(1) Road construction problem => minimum spanning tree (weighted value) [data structure] + Prim's algorithm

(2) Shortest path problem => graph + Freud algorithm

(3) Tower of Hanoi => branch algorithm

(4) The Eight Queens Problem => Backtracking

1.3 Linear structure and non-linear structure

The data structure includes: linear structure and non-linear structure.

1.3.1 Linear structure

  1. As the most commonly used data structure, linear structure is characterized by a one-to-one linear relationship between data elements
  2. Linear structure has two different storage structures, namely sequential storage structure (array) and chain storage structure (linked list). The linear table stored sequentially is called the sequential table, and the storage elements in the sequential table are continuous
  3. The linear list of chain storage is called a linked list. The storage elements in the linked list are not necessarily continuous. The data elements and the address information of adjacent elements are stored in the element nodes.
  4. Common linear structures are: arrays, queues, linked lists, and stacks, which we will explain in detail later.

1.3.2 Non-linear structure

Non-linear structures include: two-dimensional arrays, multi-dimensional arrays, generalized tables, tree structures, graph structures

Guess you like

Origin blog.csdn.net/Lu1048728731/article/details/114057759