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 use C++ to realize the deduplication of L2002 linked list

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the knowledge of "how to use C++ to remove L2002 linked list". In the operation of practical cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Given a linked list L with integer key values, you need to delete the key nodes in which the absolute values are repeated. That is, for each key value K, only the first node whose absolute value is equal to K is retained. At the same time, all deleted nodes must be saved on another linked list. For example, given a L of 21 →-15 →-15 →-7 → 15, you need to output the heavy linked list 21 →-15 →-7 and the deleted linked list-15 → 15.

Input format:

Enter the address of the first node of L given on the first line and a positive integer N (≤ 105, which is the total number of nodes). The address of a node is a non-negative 5-bit integer, and the empty address NULL is represented by − 1.

Followed by N lines, each line describes a node in the following format:

Address key value next node

Where the address is the address of the node, the key value is an integer whose absolute value does not exceed 104, and the next node is the address of the next node.

Output format:

First output the heavy linked list, and then output the deleted linked list. Each node occupies a line and is output in the input format.

Enter a sample:

00100 5

99999-7 87654

23854-15 00000

87654 15-1

00000-15 99999

00100 21 23854

Sample output:

00100 21 23854

23854-15 99999

99999-7-1

00000-15 87654

87654 15-1

Train of thought:

Many methods can be implemented, I choose an array to simulate dynamic memory, first build a linked list and then traverse, the time complexity is O (n), format control or printf is easy to use.

# include#include#include#define NULL-1using namespace std;typedef struct node {int val; unsigned int next;} node;node store [100001]; / / create a simulated memory int flag [10001]; / / Mark node int main () {int num, startM;//p tag current node cin > > startM > > num; for (int I = 0; I

< num; i++) { int now, val, next; cin >

> now > > val > > next; Store [now] .Val = val; Store [now] .Next = next;} / / linked list Construction completed int p1startMstartShop null; int p2 = 1000000 true; bool k = null While (p1! = NULL) {if (flag [abs (store [p1] .val)]! = 0) {store [pre]. Next = store [p1]. Next = p1; store [p1]. Next = NULL; p2 = p1 If (k) {k = false; startS = p2;} p1 = storepre. Next } else {flag [abs (store [p1] .val)] = 1; pre = p1; p1 = store [p1] .next;}} / / list check to complete p1 = startM While (p1! = NULL) {if (store [p1]. Next! = NULL) printf ("d% d\ n", p1, store [p1] .val, store [p1] .next); else printf ("d% d% d\ n", p1, store [p1] .val, store [p1] .next) P1 = store [p1] .next;} p1 = startS; while (p1! = NULL) {if (store [p1]. Next! = NULL) printf ("d% d\ n", p1, store [p1] .val, store [p1] .next) Else printf ("d% d% d\ n", p1, store [p1] .val, store [p1] .next); p1 = store [p1]. Next;} return 0;} "how to use C++ to achieve L2-002 linked list deduplication" is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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: 0

*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