PAT_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 (< 105) 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 [-105, 105], 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
备注:input中给的node可能不能同一条链表上,所以必须先构造链表。同时注意判断链表为空的情况。
#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;

typedef struct node
{
	int address;
	int key;
	int next;
}NODE;

NODE node_list[100005];
vector<NODE> n_list;

bool compare(NODE a,NODE b)
{
	return a.key<b.key;
}

int main()
{
	int N,head;
	scanf("%d %d",&N,&head);
	for(int i=0;i<N;i++)
	{
		node tnode;
		scanf("%d %d %d",&tnode.address,&tnode.key,&tnode.next);
		node_list[tnode.address] = tnode;
	}
	
	//construct linked list since nodes are probably on different lists
	while(head!=-1)
	{
		n_list.push_back(node_list[head]);
		head = node_list[head].next;
	}
	sort(n_list.begin(),n_list.end(),compare);
	if(n_list.size()==0)
	{
			printf("%d %d\n",0,-1);
	}
	else
	{
		printf("%d %05d\n",n_list.size(),n_list[0].address);

		for(int i=0;i<n_list.size();i++)
		{
			if(i+1<n_list.size())
				printf("%05d %d %05d\n",n_list[i].address,n_list[i].key,n_list[i+1].address);
			else
				printf("%05d %d %d\n",n_list[i].address,n_list[i].key,-1);
		}
	}

	return 0;
}


对下面代码每一步含义进行注释 def convert_to_doubly_linked_list(self): if not self.root: return None def convert(root): if not root.left and not root.right: return ListNode(root.val) if not root.left: right_head = convert(root.right) right_tail = right_head while right_tail.next: right_tail = right_tail.next cur_node = ListNode(root.val, None, right_head) right_head.prev = cur_node return cur_node if not root.right: left_tail = convert(root.left) left_head = left_tail while left_head.prev: left_head = left_head.prev cur_node = ListNode(root.val, left_tail, None) left_tail.next = cur_node return cur_node left_tail = convert(root.left) right_head = convert(root.right) left_head = left_tail while left_head.prev: left_head = left_head.prev right_tail = right_head while right_tail.next: right_tail = right_tail.next cur_node = ListNode(root.val, left_tail, right_head) left_tail.next = cur_node right_head.prev = cur_node return left_head return convert(self.root) def inorder_traversal(self, root): if not root: return self.inorder_traversal(root.left) print(root.val, end=' ') self.inorder_traversal(root.right) def print_bst(self): self.inorder_traversal(self.root) print() def traverse_doubly_linked_list(self, head): cur_node = head while cur_node: print(cur_node.val, end=' ') cur_node = cur_node.next print() def reverse_traverse_doubly_linked_list(self, head): cur_node = head while cur_node.next: cur_node = cur_node.next while cur_node: print(cur_node.val, end=' ') cur_node = cur_node.prev print()
最新发布
06-12
这段代码是一个二叉搜索树(BST)转化为双向链表的函数,同时提供了打印BST和遍历双向链表的函数。 - `def convert_to_doubly_linked_list(self):`:定义一个将BST转化为双向链表的函数。 - `if not self.root:`:如果BST为空,则返回None。 - `def convert(root):`:定义一个递归函数,将BST转化为双向链表。 - `if not root.left and not root.right:`:如果该节点没有左右子树,返回一个只包含该节点值的ListNode。 - `if not root.left:`:如果该节点没有左子树,将右子树转化为双向链表,并将节点值作为新的头结点,返回该节点。 - `if not root.right:`:如果该节点没有右子树,将左子树转化为双向链表,并将节点值作为新的尾结点,返回该节点。 - `left_tail = convert(root.left)`:将左子树转化为双向链表,并返回左子树的尾结点。 - `right_head = convert(root.right)`:将右子树转化为双向链表,并返回右子树的头结点。 - `left_head = left_tail`:将左子树的头结点设置为左子树的尾结点。 - `while left_head.prev:`:找到左子树双向链表的头结点。 - `right_tail = right_head`:将右子树的尾结点设置为右子树的头结点。 - `while right_tail.next:`:找到右子树双向链表的尾结点。 - `cur_node = ListNode(root.val, left_tail, right_head)`:创建一个新的节点,值为当前节点值,左指针指向左子树双向链表的尾结点,右指针指向右子树双向链表的头结点。 - `left_tail.next = cur_node`:将左子树双向链表的尾结点的右指针指向新节点。 - `right_head.prev = cur_node`:将右子树双向链表的头结点的左指针指向新节点。 - `return left_head`:返回双向链表的头结点。 - `return convert(self.root)`:调用递归函数convert并返回结果。 - `def inorder_traversal(self, root):`:定义一个中序遍历BST的函数。 - `if not root:`:如果该节点为空,则返回。 - `self.inorder_traversal(root.left)`:递归遍历左子树。 - `print(root.val, end=' ')`:输出当前节点的值。 - `self.inorder_traversal(root.right)`:递归遍历右子树。 - `def print_bst(self):`:定义一个打印BST的函数。 - `self.inorder_traversal(self.root)`:调用中序遍历函数遍历BST。 - `print()`:输出一个空行。 - `def traverse_doubly_linked_list(self, head):`:定义一个遍历双向链表的函数。 - `cur_node = head`:将当前节点指向链表的头结点。 - `while cur_node:`:遍历整个链表,直到当前节点为空。 - `print(cur_node.val, end=' ')`:输出当前节点的值。 - `cur_node = cur_node.next`:将当前节点指向下一个节点。 - `print()`:输出一个空行。 - `def reverse_traverse_doubly_linked_list(self, head):`:定义一个逆序遍历双向链表的函数。 - `cur_node = head`:将当前节点指向链表的头结点。 - `while cur_node.next:`:找到链表的尾结点。 - `cur_node = cur_node.next`:将当前节点指向下一个节点。 - `while cur_node:`:逆序遍历整个链表,直到当前节点为空。 - `print(cur_node.val, end=' ')`:输出当前节点的值。 - `cur_node = cur_node.prev`:将当前节点指向上一个节点。 - `print()`:输出一个空行。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值