Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to simulate linked list with array in C++

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

The content of this article mainly revolves around how to describe C++ with array simulation linked list. The content of the article is clear and clear. It is very suitable for beginners to learn and is worth reading. Interested friends can follow the editor to read together. I hope you can get something through this article!

Preface

A linked list is a storage structure composed of a series of nodes stored in a discontinuous storage space. Each node consists of two parts: one is the data field that stores the element, and the other is the pointer domain that stores the address of the next node. Using an array to simulate a linked list can clearly understand this definition.

Here, we briefly introduce the single-linked list and double-linked list and how to implement them with array simulation.

1. Single linked list

A single linked list is an one-way linked list in the direction of the pointer, that is, the address of node b is stored in the pointer domain of node a, while the address of node an is not stored in the pointer domain of node b When accessing, it can be accessed from a to b, but not from b to a.

As can be clearly seen in the figure, the direction of each node is unidirectional.

Q: so, how do you implement it with an array?

A: the method is as follows

Insert the element x to the right of the k node. First, x is assigned to the data field of the node (e [IDX]), then the pointer domain of the k node is assigned to the pointer domain of the node, and finally the address stored in the pointer field of the k node is changed to the address of the node.

Void add (int k, int x) {e [idx] = x; ne [idx] = ne [k]; ne [k] = idx++;} delete the node pointed to by the k node. The deletion here refers to changing the direction of k to the point of the node. Originally a-> b-> c, instead of a-> c, the b node still exists, but no other node points to it, so it cannot be accessed through the linked list. We think it has been deleted on the linked list. Void remove (int k) {ne [k] = ne [k];}

Read the linked list. Read the linked list only to note that when scanning with a single pointer, instead of moving the pointer to the right, the pointer is moved to the position that the node points to.

For (int I = head; I! =-1; I = ne [I]) cout > op; if (op = ='H') {cin > > x; add_head (x);} else if (op = ='D') {cin > > k; if (! k) head = ne [head]; remove (k-1) } else {cin > > k > > x; add (k-1, x);}} for (int I = head; I! =-1; I = ne [I]) cout > op; if (op = ='H') {cin > > x; add (0, x) } else if (op = ='D') {cin > > k; if (! K) head = ne [head]; remove (k);} else {cin > > k > > x; add (k, x);}} for (int I = ne [0]; I! =-1 I = ne [I]) cout x; add (0, x);} else if (op = = "R") {cin > > x; add (l [1], x) } else if (op = = "D") {cin > > k; remove (k + 1);} else if (op = = "IL") {cin > > k > > x Add (1 [k + 1], x);} else if (op = = "IR") {cin > > k > > x; add (k + 1, x);}} for (int I = r [0]; I! = 1 I = r [I]) cout

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 235

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report