Explain sequential and chain structures with stories

study hard, improve every day

This article has been included in my Github repository DayDayUP : github.com/RobodLee/DayDayUP , welcome Star

⭐⭐⭐⭐⭐转载请注明出处:blog.csdn.net/weixin_4346…

foreword

We have a class this semester, which is about teaching information technology, that is, each student assigns a chapter on data structures, and then comes to the stage to talk about it. I was assigned the content of sequential structure and chain structure. In order to make it interesting, I made up a short story and made some animations. It feels pretty good. The animation has been done for a long time. In order not to waste the stories and animations I worked so hard to make up, I will share this article in the water article.

computer memory

Whether it is a sequential structure or a chain structure, it is a description of how data is stored in memory. Therefore, in order to understand these two data structures, it is necessary to first introduce the memory of the computer.

In the above picture, each row in the picture is one 存储单元, composed of several storage units 存储体. Each storage unit is connected with one 字选线, corresponding 内存地址.

For example, the data of the memory unit whose memory address is 0 is now read: first, the CPU sends the address information to the MAR through the address bus , and the MAR then sends the address to the decoder . The decoder can select the corresponding word selection line through the address, and then give One signal for the corresponding storage unit. Finally , the binary data in the storage unit can be sent to the MDR through the data line (green line) . In this way, the data read operation is completed . If it is a write operation, it is only necessary to control whether to read or write through the read/write control line . If it is a write, the data in the MDR is sent to the corresponding storage unit through the data line.

These are some knowledge of the principles of computer composition. Those who have not learned it may not understand it well. It doesn’t matter. Just know one concept here, that is, the computer memory is composed of several storage units, and each storage unit has A unique number (memory address) that the computer can read/write to the storage unit at the specified memory address .

This is like, the computer memory can be regarded as a hotel , each room is a storage unit, the room number corresponds to the memory address, and the tenant in the room is the data stored in the memory.

sequential structure

Before introducing the sequential structure, let me tell you a little story:

One day, my grandfather came to the city for a tour with seven gourd babies.

Since we're here for tourism, we can't sleep on the street, so grandpa and the gourds are going to find a hotel to stay first. They found a hotel with more vacant rooms. Grandpa opened seven connected rooms at one time, and then let the seven gourd babies live in one by one.

It can be seen from the picture that the seven rooms are next to each other, so the seven gourd babies also live in their own rooms one by one. Something like this is a sequential structure.

Sequential structure, which means that each data element is stored in sequence in a group of storage units with consecutive addresses in the computer . With this storage method, two elements that are logically adjacent to each other are also adjacent in the computer memory .

The sequence structure corresponds to the array, so how to represent the above example in code:

In the code, an array of size 7 is opened through the new keyword, which is equivalent to grandpa opening seven rooms, and then storing seven data in the array, which is equivalent to seven gourd babies living in their respective rooms in turn. .

chain structure

After introducing the sequence structure, let's introduce the chain structure. Or continue with the above story.

After staying in this hotel for a period of time, the gourd children felt a little uncomfortable and wanted to change. But grandpa thought: Why should I change if I live here so well? But I can't beat these little ones, so I can only rely on them. After all, two fists can't beat fourteen hands, although my grandfather is relatively strong,,,

然后他们就收拾东西来到了一家新的宾馆。

由于这家宾馆生意比较好,住的人有点多,没有连在一起的七个房间了,爷爷索性就让他们随便住了,挑自己喜欢的房间住。

等住完了之后爷爷心里想:这几个小娃娃怎么住的这么分散,要是弄丢了可怎么搞,不得心疼死我呀。

于是乎,爷爷就找到了前台小姐姐,找她要了七张小纸条。

然后爷爷将七张小纸条分给了葫芦娃们,然后他们每个人都在小纸条上记着下一个葫芦娃的房间号。这样虽然几个葫芦娃都不住在一起,但是通过纸条上的房间号就可以依次找到下一个葫芦娃的房间。这样从大娃开始,就可以找到所有的葫芦娃了。

链式结构的特点是存储各个数据元素的计算机存储单元的地址不一定是连续的,但是在逻辑上相邻的。每个结点都包含数据域指针域两部分。

故事中所表示的就是链式结构。我们再来思考一个问题,现在通过纸条可以找到下一个葫芦娃的房间,但是怎么找到上一个呢?好像不可以,稍微做一下修改,比如现在每个葫芦娃手上有两张纸条,另一张纸条上记录的是上一个葫芦娃的房间号。

其实这个就是双链表,每个结点都有两个指针,分别指向上一个结点与下一个结点。

所以说,数据结构研究的就是数据如何在内存中存储。尽管数据结构有多种,但是数据都是在内存中存放的,只不过约束条件不同,概念就有所区别。不管是栈、队列等简单一点的数据结构,还是树、图等复杂一点的数据结构,他们的实现方式无非就三种:顺序存储、链式存储、顺序存储+链式存储。

拓展

最后,我们再来拓展一个知识点,就是如何用顺序结构和链式结构来实现栈。

栈是一种只能从栈顶存放数据和取出数据的数据结构。不能从栈底对元素进行操作。

如果采用顺序结构实现,可以准备一个数组,规定数组起始位置为栈底,靠近末尾一侧为栈顶。那么添加和取出元素就只能从末尾一侧进行操作了。

typedef struct {
    ElemType data[MaxSize];     //用于存放数据
    int top;                    //指向栈顶
} SqStack;
复制代码

If replaced by a chain structure. Then specify that the header is the bottom of the stack and the tail is the top of the stack. Then you can only add or remove elements from the end of the table. If it is specified in reverse that the header is the top of the stack and the tail of the table is the bottom of the stack, anyway, it can only be operated from one end, which conforms to the definition of the stack.

typedef struct StackNode {
    ElemType data;
    struct StackNode *next;
} *LinkStack;
复制代码

Summarize

To study data structure is to study how data is stored in memory. When learning a new data structure, it is easy to understand its definition first, and then think about its implementation and how to store it in memory.

Writing is not easy, remember to like it after reading it~~~~

⭐⭐⭐⭐⭐转载请注明出处:blog.csdn.net/weixin_4346…

This article has been included in my Github repository DayDayUP : github.com/RobodLee/DayDayUP , welcome Star

If you think the article is not bad, please give me one 点赞, 收藏,关注

To learn more programming knowledge, pay attention to the official account 『 R o b o d 』:

Guess you like

Origin juejin.im/post/7082758443886444580