1052. Linked List Sorting

题目链接:http://pat.zju.edu.cn/contests/pat-a-practise/1052

这个题目告诉我们:读题、理解题目、分析题目很重要。

题目说只有1个链表了吗?

题目说链表的结尾一定是NULL了吗?

链表空的时候应该输出什么?

另外,都注意到NULL定义为-1了。


这个题目用hash的方法,很适合的。因此也可以试着用map。

数组hash的方法见:http://blog.csdn.net/sunbaigui/article/details/8656800


给出2个代码,总体思路上是一样的,仅仅部分细节处理方式不同。

代码1


// 1. 题目没有说仅有1条链表
// 2. 所给的链表空时输出0 -1
// 3. 题目没有说结尾是null


#include 
   
   
    
    
#include 
    
    
     
     
#include 
     
     
      
      
#include 
      
      
#include 
       
       
         #define MAXADDR 1000000 using namespace std; struct Node { int add; int value; int next; bool operator < (const Node& A) const { return value < A.value; } }; int N, head; vector 
        
          buf; vector 
         
           list; map 
          
            m; void Output() { int i; for(i=0; i 
           
             ::iterator it=m.find(head); while(it != m.end()) { int pos = (*it).second; list.push_back(buf[pos]); if(buf[pos].next == -1) { break; }// 如果不加这个,会有一个测试点错误 it = m.find(buf[pos].next); // 如果下址为-1,按道理是找不到的,同样会退出,上面判断不就多余了吗 // 谁说结束为-1了呢? } } int main() { #ifdef ONLINE_JUDGE #else freopen("E:\\in.txt", "r", stdin); #endif Input(); creatList(); if(list.size() == 0) { printf("0 -1\n"); } else { sortByValue(); head = list[0].add; printf("%d %05d\n", list.size(), head); int i; for(i=0; i 
            
              #include 
             
               #include 
              
                #include 
                #include 
                
                  #define MAXADDR 1000000 using namespace std; struct Node { int add; int value; int next; bool operator < (const Node& A) const { return value < A.value; } }; int N, head; vector 
                 
                   buf; vector 
                  
                    list; map 
                   
                     m; void Output() { int i; for(i=0; i 
                    
                      ::iterator it=m.find(pos); if(it != m.end()) { int index=(*it).second; list.push_back(buf[index]); pos = buf[index].next; } else { break; } } } int main() { #ifdef ONLINE_JUDGE #else freopen("E:\\in.txt", "r", stdin); #endif Input(); creatList(); if(list.size() == 0) { printf("0 -1\n"); } else { sortByValue(); head = list[0].add; printf("%d %05d\n", list.size(), head); int i; for(i=0; i 
                      
                     
                    
                   
                  
                 
               
              
             
            
           
          
         
       
     
     
    
    
   
   

代码2

#include <stdio.h>
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>

#define MAXADDR 1000000

using namespace std;

struct Node
{
	int add;
	int value;
	int next;
	bool operator < (const Node& A) const
	{
		return value < A.value;
	}
};

int N, head;
vector<Node> buf;
vector<Node> list;
map<int, int> m;

void Output()
{
	int i;
	for(i=0; i<buf.size() ; i++)
	{
		printf("%d %d %d\n", buf[i].add, buf[i].value, buf[i].next);
	}
}

void Input()
{
	scanf("%d %d", &N, &head);
	Node temp;
	for(int i=0; i<N; i++)
	{
		scanf("%d %d %d", &temp.add, &temp.value, &temp.next);
		buf.push_back(temp);
		m.insert(make_pair(temp.add, i));
	}
}

void sortByValue()
{
	sort(list.begin(), list.end());
}

void creatList()
{
	// 两种退出情况
	// 1. next为null
	// 2. 没有找到下一个地址的单元
	int pos = head;
	while(pos != -1)//如果没有找到null
	{
		map<int, int>::iterator it=m.find(pos);
		if(it != m.end())
		{
			int index=(*it).second;
			list.push_back(buf[index]);
			pos = buf[index].next;
		}
		else
		{
			break;
		}
	}
}


int main()
{
	#ifdef ONLINE_JUDGE
	#else
		freopen("E:\\in.txt", "r", stdin);
	#endif

	Input();
	creatList();
	if(list.size() == 0)
	{
		printf("0 -1\n");
	}
	else
	{
		sortByValue();
		head = list[0].add;
		printf("%d %05d\n", list.size(), head);
		int i;
		for(i=0; i<list.size()-1; i++)
		{
			list[i].next = list[i+1].add;
			printf("%05d %d %05d\n", list[i].add, list[i].value, list[i].next);
		}
		printf("%05d %d -1\n", list[i].add, list[i].value);
	}
	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、付费专栏及课程。

余额充值