国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

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);
		}
	   }
   }
}

本題采用了靜態(tài)鏈表的方法,需要注意的是在鏈表節(jié)點內部存儲了其地址,因為鏈表排序只改變節(jié)點的next值,而其本身的address不發(fā)生改變。所以最后輸出的時候,next輸出的是物理存儲上的下一個節(jié)點的address值而不是next值,需要習慣這種思維方式。文章來源地址http://www.zghlxwxcb.cn/news/detail-607001.html

到了這里,關于PTA 1052 Linked List Sorting的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!

本文來自互聯(lián)網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。如若轉載,請注明出處: 如若內容造成侵權/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經查實,立即刪除!

領支付寶紅包贊助服務器費用

相關文章

  • 雙向鏈表(Double Linked List)

    雙向鏈表(Double Linked List)

    ? ? ? ? 雖然單向鏈表能夠100%解決邏輯關系為“一對一”數據的存儲問題,但在解決那些需要大量查找前趨節(jié)點的問題是,單向鏈表無疑是不能用了,因為單向鏈表適合“從前往后”查找,并不適合“從后往前”查找。 ? ? ? ? 如果要提高鏈表的查找效率,那雙向鏈表(雙

    2023年04月13日
    瀏覽(20)
  • LeetCode 92. Reverse Linked List II【鏈表,頭插法】中等

    LeetCode 92. Reverse Linked List II【鏈表,頭插法】中等

    本文屬于「征服LeetCode」系列文章之一,這一系列正式開始于2021/08/12。由于LeetCode上部分題目有鎖,本系列將至少持續(xù)到刷完所有無鎖題之日為止;由于LeetCode還在不斷地創(chuàng)建新題,本系列的終止日期可能是永遠。在這一系列刷題文章中,我不僅會講解多種解題思路及其優(yōu)化,

    2024年02月09日
    瀏覽(19)
  • 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日
    瀏覽(16)
  • Linked List

    Linked List

    補充知識 typedef 給類型換名字,比如 或者來一個結構體指針定義。 離散存儲 離散的含義,任何一個點到其他點之間是有間距的。 n個節(jié)點離散分配,彼此通過指針相連接,每個節(jié)點只有一個前驅節(jié)點,每個節(jié)點只有一個后繼節(jié)點,首節(jié)點沒有前驅節(jié)點,尾節(jié)點沒有后繼節(jié)點

    2024年02月15日
    瀏覽(32)
  • 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日
    瀏覽(20)
  • 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日
    瀏覽(15)
  • 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日
    瀏覽(23)
  • LeetCode //C - 206. Reverse Linked List

    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日
    瀏覽(22)
  • LeetCode //C - 141. Linked List Cycle

    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日
    瀏覽(19)
  • 算法競賽基礎:C++雙向鏈表的結構和實現(xiàn)(普通鏈表、List、靜態(tài)鏈表)

    本文將會介紹在算法競賽中雙向鏈表的幾種使用方式,適合有一定基礎的人閱讀。 一般來說,普通的鏈表結構是這樣的: next指針指向下一個鏈表,這樣的結構只能夠支持單向查詢。 雙向鏈表,顧名思義,就是可以支持雙向的訪問和查詢。 也就是這樣的: 這種鏈表為訪問前

    2024年01月24日
    瀏覽(12)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領取紅包,優(yōu)惠每天領

二維碼1

領取紅包

二維碼2

領紅包