"How to run up the computer" || • Chapter VI seven points and data structures become good friends

Here Insert Picture Description
Author: Hisao Yazawa

——————————————————————————————————————

• Chapter VI seven points and data structures become good friends

——————————————————————————————————————

[01] we understand the relationship between memory and variables

Variable is the smallest unit of data storage savings, each variable corresponding to a physical space on the memory.

In high-level languages, we are through the use of variable data stored in memory. When run the program, for us to be divided by the operating system from memory unused space in a part assigned to the variable.

Shown below Here Insert Picture Description
-

[02] as an array of data to understand the structural basis of

Unlike a single variable, in the memory space is a separate existence.

Memory array reflects the physical structure itself, which is stored in the data memory space is continuously distributed.

By using an array, we can either define multiple variables simultaneously, but also can improve the efficiency of the preparation process.
Here Insert Picture Description
-

[03] array of applications understand - as a typical data structure algorithm

An array is a basic data structure, as long as we can through the use of an array of programs to achieve a variety of algorithms to process large amounts of data.

For example, the linear search algorithm to find prime data.

for (i=0;i<1000;i++){
	if (x[i] == 777){
		printf("找到777了!")
	}
}

As another example, bubble sort algorithm arrangement of data.

for (i=999;i>0;i--){
	for (j=0;j>0;j++){
		if (x[i]>x[j]){
			tmp = x[i];
			x[i] = x[j];
			x[j] = tmp;
		}
	}
}

In the two examples above, such variables as the variable i is used to record the number of cycles calledLoop counter (Loop Counter)

The reason why an array of convenient, it is also possible because the index value and the array of loop counter into correspondence with use.
Here Insert Picture Description
-

[04] understand and grasp the concept of typical data types and structures

The array is the most basic data structure of the physical structure of a direct memory utilization. Only need to use a for loop, we can handle continuous data stored in the array in order to achieve a variety of algorithms.

But in real life there are some data agency is light array itself can not be achieved alone.
Here Insert Picture Description
Stack: Use and stacking sequence in reverse order, the first data stored usage data. This access method is called LIFO (Last In First Out).

Queue: using sequence data is consistent with the accumulation of sequence data previously stored prior to use. This access method is called FIF0 (First In First Out)

List: Data hand in hand in a row, further loosening the hand chain, the order data is changed.

Binary Tree: a data connection with the rear two-way data. Binary equivalent special form of the list.

——

[05] understand the stack and queue implementation

Implementing stacks

Makeup:

  • Stack pointer (always point to the empty data)
  • Stack Array
  • Drawing Functions
  • Stack function

Implementation code

char Stack[100];     # 栈数组
char StackPointer = 0;   # 栈顶指针
void Push(char Data){      # 入栈函数
	Stack[StackPointer] = Data;
	StackPointer++;
}
void Pop(){                # 出栈函数
	StackPointer--;
	Data = Stack[StackPointer];
	return Data
}

Icon
Here Insert Picture Description

Queue

Makeup:

  • Queue array
  • Head-pointer
  • The tail pointer
  • The team function
  • A team function

Note that, if the data stored in the end of the array, then the next point to the location of the head of the array, i.e. the actual formation of a ring.

Implementation code

char Queue[100];       # 队列数组
char SetIndex = 0;     # 队头指针
char GetIndex = 0;     # 队尾指针
void Set(char Data){   # 入队函数
	Queue[SetIndex] = Data;
	SetIndex++;
	if (SetIndex>=100){
		SetIndex = 0;
	}
}
void Get(){           # 出队函数
	Data = Queue[GetIndex];
	GetIndex++;
	if (GetIndex>=100){
		GetIndex = 0;
	}
	return Data;
}

Icon
Here Insert Picture Description
-

[06] understand the structure of the composition of body

Before you implement linked lists and binary trees, we need to know what is the structure.

After a so-called integral structure, the number of data items is aggregated to give it a name and formed

struct TestResult{
	char Chinese;
	char Math;
	char English;
}

Once finished defining a structure, the structure can be used as a data type, use it to define variables.

struct TestResult xiaoming;
xiaoming.Chinese = 80;
xiaoming.Math = 90;
xiaoming.English = 100;

Using an array of structures
Here Insert Picture Description
-

[07] see the implementation of linked lists and binary trees

Linked list implementation

Code

strcuct TestResult{
	char Chinese;
	char Math;
	char English;
	struct TestResult* Ptr;  # 指向其他元素的指针,Ptr存储了数组中另一个元素的地址
}

The list of advantages

  1. Sorting process large amounts of data, you only need to change the value Ptr, many will be short processing time
  2. Applicable to delete and insert elements

Icon
Here Insert Picture Description

Achieve binary tree

Code

strcuct TestResult{
	char Chinese;
	char Math;
	char English;
	struct TestResult* Ptr1;
	struct TestResult* Ptr2;
}

The advantages of binary tree

  • Binary search, quickly find data

Icon
Here Insert Picture Description

——
Here Insert Picture Description

Published 35 original articles · won praise 35 · views 2753

Guess you like

Origin blog.csdn.net/nilvya/article/details/103881365