PTA 1052 Linked List Sorting

这篇具有很好参考价值的文章主要介绍了PTA 1052 Linked List Sorting。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

个人学习记录,代码难免不尽人意。

A linked list consists of a series of structures, which are not necessarily adjacent in memory. We assume that each structure contains an integer key and a Next pointer to the next structure. Now given a linked list, you are supposed to sort the structures according to their key values in increasing order.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive N (<10 5) and an address of the head node, where N is the total number of nodes in memory and the address of a node is a 5-digit positive integer. NULL is represented by −1.

Then N lines follow, each describes a node in the format:

Address Key Next

where Address is the address of the node in memory, Key is an integer in [−10 5,10 5], and Next is the address of the next node. It is guaranteed that all the keys are distinct and there is no cycle in the linked list starting from the head node.

Output Specification:
For each test case, the output format is the same as that of the input, where N is the total number of nodes in the list and all the nodes must be sorted order.
Sample Input:
5 00001
11111 100 -1
00001 0 22222
33333 100000 11111
12345 -1 33333
22222 1000 12345
Sample Output:
5 12345
12345 -1 00001
00001 0 11111
11111 100 22222
22222 1000 33333
33333 100000 -1

#include <cstdio>
#include<algorithm>
using namespace std;
struct Node{
	int address;
	int next;
	int data;
	bool flag;
}node[100010];
bool cmp(Node a,Node b){
	if(a.flag==false||b.flag==false){
		return a.flag>b.flag;
	}else{
		return a.data<b.data;
	}
	
}
int main(){
   for(int i=0;i<100010;i++){
   	node[i].flag=false;
   }
   int n,begin;
   scanf("%d %d",&n,&begin);
   for(int i=0;i<n;i++){
   	int address,next;
   	int data;
   	scanf("%d %d %d",&address,&data,&next);
   	node[address].address=address;
   	node[address].data=data;
   	node[address].next=next;
   	//node[address].flag=true;这个地方不能直接赋true,因为题目中给的数据可能不在链表上! 
   } 
   int count=0;
   int p=begin;
   while(p!=-1){
   	node[p].flag=true;
   	count++;
   	p=node[p].next;
   } 
   if(count==0)
   printf("0 -1");
   else{
   	sort(node,node+100010,cmp);
   	printf("%d %05d\n",count,node[0].address);
   	for(int i=0;i<count;i++){
   		if(i!=count-1){
   			printf("%05d %d %05d\n",node[i].address,node[i].data,node[i+1].address);
		   }
		else{
			printf("%05d %d -1\n",node[i].address,node[i].data);
		}
	   }
   }
}

本题采用了静态链表的方法,需要注意的是在链表节点内部存储了其地址,因为链表排序只改变节点的next值,而其本身的address不发生改变。所以最后输出的时候,next输出的是物理存储上的下一个节点的address值而不是next值,需要习惯这种思维方式。文章来源地址https://www.toymoban.com/news/detail-607001.html

到了这里,关于PTA 1052 Linked List Sorting的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 双向链表(Double Linked List)

            虽然单向链表能够100%解决逻辑关系为“一对一”数据的存储问题,但在解决那些需要大量查找前趋节点的问题是,单向链表无疑是不能用了,因为单向链表适合“从前往后”查找,并不适合“从后往前”查找。         如果要提高链表的查找效率,那双向链表(双

    2023年04月13日
    浏览(36)
  • LeetCode 92. Reverse Linked List II【链表,头插法】中等

    本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,

    2024年02月09日
    浏览(42)
  • 1028 List Sorting (PAT甲级)

    Excel can sort records according to any column. Now you are supposed to imitate this function. Input Specification: Each input file contains one test case. For each case, the first line contains two integers N (≤105) and C, where N is the number of records and C is the column that you are supposed to sort the records with. Then N lines follow, eac

    2024年02月15日
    浏览(41)
  • Linked List

    补充知识 typedef 给类型换名字,比如 或者来一个结构体指针定义。 离散存储 离散的含义,任何一个点到其他点之间是有间距的。 n个节点离散分配,彼此通过指针相连接,每个节点只有一个前驱节点,每个节点只有一个后继节点,首节点没有前驱节点,尾节点没有后继节点

    2024年02月15日
    浏览(41)
  • Linked List Mock

    203. Remove Linked List Elements Solved Easy Topics Companies Given the  head  of a linked list and an integer  val , remove all the nodes of the linked list that has  Node.val == val , and return  the new head . 206. Reverse Linked List Solved Easy Topics Companies Given the  head  of a singly linked list, reverse the list, and return  the reversed

    2024年04月12日
    浏览(38)
  • 1074 Reversing Linked List (PAT甲级)

    Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elements on L. For example, given L being 1→2→3→4→5→6, if K=3, then you must output 3→2→1→6→5→4; if K=4, you must output 4→3→2→1→5→6. Input Specification: Each input file contains one test case. For each case, the first line

    2024年02月09日
    浏览(42)
  • LeetCode //C - 141. Linked List Cycle

    Given head , the head of a linked list, determine if the linked list has a cycle in it. There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Internally, pos is used to denote the index of the node that tail’s next pointer is connected to. Note that pos is not passed as a p

    2024年02月11日
    浏览(48)
  • LeetCode //C - 206. Reverse Linked List

    Given the head of a singly linked list, reverse the list, and return the reversed list.   Example 1: Input head = [1,2,3,4,5] Output [5,4,3,2,1] Example 2: Input head = [1,2] Output [2,1] Example 3: Input head = [] Output [] Constraints: The number of nodes in the list is the range [0, 5000]. -5000 = Node.val = 5000 From: LeetCode Link: 206. Reverse Linked

    2024年02月01日
    浏览(50)
  • LeetCode142. Linked List Cycle II

    Given the head of a linked list, return the node where the cycle begins. If there is no cycle, return null. There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Internally, pos is used to denote the index of the node that tail’s next pointer is connected to (0-indexed). It

    2024年02月04日
    浏览(41)
  • 算法竞赛基础:C++双向链表的结构和实现(普通链表、List、静态链表)

    本文将会介绍在算法竞赛中双向链表的几种使用方式,适合有一定基础的人阅读。 一般来说,普通的链表结构是这样的: next指针指向下一个链表,这样的结构只能够支持单向查询。 双向链表,顾名思义,就是可以支持双向的访问和查询。 也就是这样的: 这种链表为访问前

    2024年01月24日
    浏览(42)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包