1052 Linked List Sorting (25 points) [static list]

By Jalan Article catalog **By Jalan** Knowledge tool requirements mathematics Data structure and algorithm language...
mathematics
Data structure and algorithm
language
Input conditions
output condition
for the first time
By Jalan

Article catalog

Knowledge tool requirements

mathematics

Data structure and algorithm

language

stem

A linked list contains a series of structures, which are not adjacent in memory. Let's assume that each structure has an integer value key and a pointer to the next structure next. Now I will give you a linked list, which is arranged in ascending order of key values

Input conditions

In the first line, there is a positive integer N[0,10^ 5] (the soul of my big array burns when I see 10 ^ 5). It is the node always, the address is 5 bits (the big array can't stop) and the NULL is - 1. It ensures that the key doesn't repeat and has no ring
The format of the next N lines is
Address Key Next

Example 1

5 00001
11111 100 -1
00001 0 22222
33333 100000 11111
12345 -1 33333
22222 1000 12345

output condition

Output in key ascending order by modifying next (same format as input)

Example 1

5 12345
12345 -1 00001
00001 0 11111
11111 100 22222
22222 1000 33333
33333 100000 -1

Problem solving

for the first time

thinking

There is a very disgusting place in this question
The list he gave may have redundant nodes or a NULL at the beginning
Test cases are very misleading
Before the list operation, you need to use a linklist (static list) to save all nodes, and then traverse the header node he gave, find the node behind the node and insert it into the list
Storing all nodes in a structure array list
Arrange the array in ascending order according to the key value
In this case, add example 1 and it will be as follows:
12345 -1 33333
00001 0 22222
11111 100 -1
22222 1000 12345
33333 100000 11111
Then we just need to change the next of the ith to the address of i+1. At the end, next is given to - 1

Expected time complexity

nlogn

Writing time

30 minutes (wrong once, I don't know the meaning of trying to use the stem and the small details in the test case)

code

CPP
#include <algorithm> #include <stdio.h> #include <vector> using namespace std; typedef struct node { int ad; int key; int next; } node; bool cmp(node a, node b) { return a.key < b.key ? true : false; } int main(int argc, char const *argv[]) { //input int N; int temp1, temp2, temp3; int nowNodeId; scanf("%d%d", &N, &nowNodeId); node *linkedList=new node[100001]; for (int i = 0; i < N; i++) { scanf("%d%d%d", &temp1, &temp2, &temp3); linkedList[temp1].ad=temp1; linkedList[temp1].key=temp2; linkedList[temp1].next=temp3; } //process vector<node> list; int nodeCounter=0; if (nowNodeId==-1) { printf("0 -1"); return 0; }else{ ++nodeCounter; list.push_back({nowNodeId,linkedList[nowNodeId].key,linkedList[nowNodeId].next}); while (linkedList[nowNodeId].next!=-1) { ++nodeCounter; nowNodeId = linkedList[nowNodeId].next; list.push_back({nowNodeId, linkedList[nowNodeId].key, linkedList[nowNodeId].next}); } sort(list.begin(), list.begin() + nodeCounter, cmp); nowNodeId = list[0].ad; for (int i = 0; i < nodeCounter - 1; i++) { list[i].next = list[i + 1].ad; } list[nodeCounter - 1].next = -1; //Because I typed - 1 directly below, and I could not write this step //output printf("%d %05d\n", nodeCounter, nowNodeId); for (int i = 0; i < nodeCounter - 1; i++) { printf("%05d %d %05d\n", list[i].ad, list[i].key, list[i].next); } printf("%05d %d -1", list[nodeCounter - 1].ad, list[nodeCounter - 1].key); } return 0; }
Run time

ending

I've written so many comments that I can give me a compliment. Please make me angry@
Please also pay attention to my CSDN account. In the next two months, I should update all PTA class a topics in this format

9 June 2020, 01:46 | Views: 8074

Add new comment

For adding a comment, please log in
or create account

0 comments