1052. Linked List Sorting (25)

102 篇文章 0 订阅
37 篇文章 0 订阅

1052. Linked List Sorting (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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
首先给出N个地址,再给出第一个地址
接着每个地址  地址存放值  下一个地址
……
目的:通过第一个地址获得这条所带有的全部存储单元,在把这些单元通过存储的值从小到大排序。输出地址注意格式
用到
#include<vector>动态数组
#include<algorithm>排序
#include<iomanip>设置格式

评测结果

时间结果得分题目语言用时(ms)内存(kB)用户
8月12日 15:04答案正确251052C++ (g++ 4.7.2)2793752datrilla

测试点

测试点结果用时(ms)内存(kB)得分/满分
0答案正确130815/15
1答案正确13083/3
2答案正确13083/3
3答案正确27937523/3
4答案正确13081/1

#include<iostream> 
#include<vector>
#include<algorithm>
#include<iomanip>
#define MAX 100000
using namespace std; 
struct Links
{
  int key;
  int next;
  Links(){}
  Links(int k,int n):key(k),next(n){}
};
void readln(Links*H, int N)
{
  Links temp;
  int addr;
  while (N--)
  {
    cin >> addr >> temp.key >> temp.next;
    H[addr] = temp;
  }
}
void Together(Links*H, int Headaddr, vector< Links>*sortH)
{ 
  while (Headaddr != -1)
  {
    sortH->push_back(Links(H[Headaddr].key, Headaddr));
    Headaddr = H[Headaddr].next; 
  }
}
bool Hcmp(const Links &A, const Links &B)
{
  return A.key < B.key;
}
void format(int a, int b, int c)
{
  cout << setw(5) << setfill('0') << a <<" "<< b << " "<<setw(5) << setfill('0') << c << endl;
}
int main()
{   
  Links H[MAX];
  int N, Headaddr;  
  vector< Links>sortH;
  cin >> N >> Headaddr;
  readln(H, N);
  Together(H, Headaddr, &sortH);
  sort(sortH.begin(), sortH.end(), Hcmp);
  Headaddr=sortH.size();
  if (Headaddr == 0)
  {
    cout << "0 -1" << endl;
  }
  else
  {
    cout << Headaddr-- << " " << setw(5) << setfill('0') << sortH[0].next << endl;
    for (N = 0; N < Headaddr; N++)
      format(sortH[N].next, sortH[N].key,  sortH[N + 1].next); 
    cout << setw(5) << setfill('0')<< sortH[N].next << " " << sortH[N].key << " -1" << endl;
  }
  system("pause");
  return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
请参考我给出的代码框架,实现对EMPLOYEE结构体为数据的双向链表的排序算法,要求按照按employeeId升序排列 typedef struct linkNode { void* data; //使用空指针使得NODE适配多种数据结构 struct linkNode* preNode; struct linkNode* nextNode; }LINKED_NODE; /*Define the struct of double linked list.*/ typedef struct { LINKED_NODE* head; LINKED_NODE* tail; size_t size; }DOUBLE_LINK_LIST; typedef struct { int employeeId; char name[20]; char ipAddress[30]; char seatNumber[20]; char group[10]; } EMPLOYEE; DOUBLE_LINK_LIST* createDoubleLinkedList() { DOUBLE_LINK_LIST* newList = (DOUBLE_LINK_LIST*)malloc(sizeof(DOUBLE_LINK_LIST)); newList->head = NULL; newList->tail = NULL; newList->size = 0; return newList; } void destroyDoubleLinkedList(DOUBLE_LINK_LIST* list) {} /*Add a new node before the head.*/ void insertHead(DOUBLE_LINK_LIST* list, void* data) // void执政适配其他data类型? {} /*Add a new node after tail.*/ void insertTail(DOUBLE_LINK_LIST* list, void* data) // 如何适配其他data类型? {} /*Insert a new node.*/ void insertNode(DOUBLE_LINK_LIST* list, void* data,int index) // 如何适配其他data类型? {} void deleteHead(DOUBLE_LINK_LIST* list) {} void deleteTail(DOUBLE_LINK_LIST* list) {} void deleteNode(DOUBLE_LINK_LIST* list, int index) {} LINKED_NODE* getNode(DOUBLE_LINK_LIST* list, int index) {} /* 遍历链表,对每个节点执行指定操作*/ void traverseList(DOUBLE_LINK_LIST* list, void (*callback)(void*)) { LINKED_NODE* currentNode = list->head; while (currentNode != NULL) { callback(currentNode->data); currentNode = currentNode->nextNode; } } void printEmployee(void* data) {}
07-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值