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

参照算法笔记P265。静态链表模板。


有些结点可能不在链表上。flag==1表示在链表上。


注意:1.comp的写法。

    2.有可能数据均无效,输出 0 -1


方法一错误,data!=inf不能说明结点就在链表上

#include<stdio.h>
#include<algorithm>
#define inf 99999999
using namespace std;
struct node{
	int address;
	int data;
	int next;
}stu[100005];
bool comp(node a,node b){
	return a.data<b.data;
}
int main(){
	int n,start,address,data,next,i;
	scanf("%d %d",&n,&start);
	for(i=0;i<100005;i++){
		stu[i].data=inf;
	}
	for(i=0;i<n;i++){
		scanf("%d %d %d",&address,&data,&next);
		stu[address].address=address;
		stu[address].data=data;
		stu[address].next=next;
	}
	int p=start;
	int cou=0;
	while(p!=-1){
		cou++;
		p=stu[p].next;
	}
	if(cou==0){
		printf("0 -1");//测试点4 
		return 0;
	}
	sort(stu,stu+100005,comp);//有可能不在链表上? data!=inf不能表示在链表上 
	printf("%d %05d\n",cou,stu[0].address);
	for(i=0;i<cou-1;i++){
		printf("%05d %d %05d\n",stu[i].address,stu[i].data,stu[i+1].address);
	}
	printf("%05d %d -1",stu[i].address,stu[i].data);
} 


方法二参照算法笔记


#include<stdio.h>
#include<algorithm>
#define inf 99999999
using namespace std;
struct node{
	int address;
	int data;
	int next;
	int flag; 
}stu[100005];
bool comp(node a,node b){
	if(a.flag==0||b.flag==0){
		return a.flag>b.flag;
	}
	else{
		return a.data<b.data;
	}	
}
int main(){
	int n,start,address,data,next,i;
	scanf("%d %d",&n,&start);
	for(i=0;i<n;i++){
		scanf("%d %d %d",&address,&data,&next);
		stu[address].address=address;
		stu[address].data=data;
		stu[address].next=next;
	}
	int p=start;
	int cou=0;
	while(p!=-1){
		cou++;
		stu[p].flag=1;
		p=stu[p].next;
	}
	if(cou==0){
		printf("0 -1");//测试点4 
		return 0;
	}
	sort(stu,stu+100005,comp);//测试点1 
	printf("%d %05d\n",cou,stu[0].address);
	for(i=0;i<cou-1;i++){
		printf("%05d %d %05d\n",stu[i].address,stu[i].data,stu[i+1].address);
	}
	printf("%05d %d -1",stu[i].address,stu[i].data);
} 



  • 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
你可以使用以下代码来实现对EMPLOYEE结构体为数据的双向链表的排序算法,按照按employeeId升序排列: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> 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) { LINKED_NODE* currentNode = list->head; while (currentNode != NULL) { LINKED_NODE* nextNode = currentNode->nextNode; free(currentNode); currentNode = nextNode; } free(list); } /*Add a new node before the head.*/ void insertHead(DOUBLE_LINK_LIST* list, void* data) { LINKED_NODE* newNode = (LINKED_NODE*)malloc(sizeof(LINKED_NODE)); newNode->data = data; newNode->preNode = NULL; newNode->nextNode = list->head; if (list->head != NULL) list->head->preNode = newNode; list->head = newNode; if (list->tail == NULL) list->tail = newNode; list->size++; } /*Add a new node after tail.*/ void insertTail(DOUBLE_LINK_LIST* list, void* data) { LINKED_NODE* newNode = (LINKED_NODE*)malloc(sizeof(LINKED_NODE)); newNode->data = data; newNode->preNode = list->tail; newNode->nextNode = NULL; if (list->tail != NULL) list->tail->nextNode = newNode; list->tail = newNode; if (list->head == NULL) list->head = newNode; list->size++; } /*Insert a new node.*/ void insertNode(DOUBLE_LINK_LIST* list, void* data, int index) { if (index < 0 || index > list->size) { printf("Invalid index\n"); return; } if (index == 0) { insertHead(list, data); return; } if (index == list->size) { insertTail(list, data); return; } LINKED_NODE* currentNode = getNode(list, index); LINKED_NODE* newNode = (LINKED_NODE*)malloc(sizeof(LINKED_NODE)); newNode->data = data; newNode->preNode = currentNode->preNode; newNode->nextNode = currentNode; currentNode->preNode->nextNode = newNode; currentNode->preNode = newNode; list->size++; } void deleteHead(DOUBLE_LINK_LIST* list) { if (list->head == NULL) return; LINKED_NODE* oldHead = list->head; list->head = oldHead->nextNode; if (list->head != NULL) list->head->preNode = NULL; free(oldHead); list->size--; if (list->size == 0) list->tail = NULL; } void deleteTail(DOUBLE_LINK_LIST* list) { if (list->tail == NULL) return; LINKED_NODE* oldTail = list->tail; list->tail = oldTail->preNode; if (list->tail != NULL) list->tail->nextNode = NULL; free(oldTail); list->size--; if (list->size == 0) list->head = NULL; } void deleteNode(DOUBLE_LINK_LIST* list, int index) { if (index < 0 || index >= list->size) { printf("Invalid index\n"); return; } if (index == 0) { deleteHead(list); return; } if (index == list->size - 1) { deleteTail(list); return; } LINKED_NODE* currentNode = getNode(list, index); currentNode->preNode->nextNode = currentNode->nextNode; currentNode->nextNode->preNode = currentNode->preNode; free(currentNode); list->size--; } LINKED_NODE* getNode(DOUBLE_LINK_LIST* list, int index) { if (index < 0 || index >= list->size) { printf("Invalid index\n"); return NULL; } LINKED_NODE* currentNode = list->head; int i = 0; while (i < index) { currentNode = currentNode->nextNode; i++; } return currentNode; } 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) { EMPLOYEE* employee = (EMPLOYEE*)data; printf("Employee ID: %d, Name: %s\n", employee->employeeId, employee->name); } int compareEmployee(const void* a, const void* b) { EMPLOYEE* employeeA = (EMPLOYEE*)a; EMPLOYEE* employeeB = (EMPLOYEE*)b; return employeeA->employeeId - employeeB->employeeId; } void sortList(DOUBLE_LINK_LIST* list) { size_t dataSize = sizeof(EMPLOYEE); EMPLOYEE** employeeArray = (EMPLOYEE**)malloc(list->size * sizeof(EMPLOYEE*)); LINKED_NODE* currentNode = list->head; size_t i = 0; while (currentNode != NULL) { employeeArray[i] = (EMPLOYEE*)currentNode->data; currentNode = currentNode->nextNode; i++; } qsort(employeeArray, list->size, dataSize, compareEmployee); currentNode = list->head; i = 0; while (currentNode != NULL) { currentNode->data = employeeArray[i]; currentNode = currentNode->nextNode; i++; } free(employeeArray); } int main() { DOUBLE_LINK_LIST* list = createDoubleLinkedList(); EMPLOYEE* employee1 = (EMPLOYEE*)malloc(sizeof(EMPLOYEE)); employee1->employeeId = 2; strcpy(employee1->name, "John"); insertHead(list, employee1); EMPLOYEE* employee2 = (EMPLOYEE*)malloc(sizeof(EMPLOYEE)); employee2->employeeId = 1; strcpy(employee2->name, "Alice"); insertHead(list, employee2); EMPLOYEE* employee3 = (EMPLOYEE*)malloc(sizeof(EMPLOYEE)); employee3->employeeId = 3; strcpy(employee3->name, "Bob"); insertHead(list, employee3); printf("Before sorting:\n"); traverseList(list, printEmployee); sortList(list); printf("\nAfter sorting:\n"); traverseList(list, printEmployee); destroyDoubleLinkedList(list); return 0; } ``` 这段代码首先定义了双向链表的结构体和EMPLOYEE结构体,然后实现了双向链表的创建、销毁、插入、删除、遍历等操作。其中,`sortList`函数使用了快速排序算法对双向链表中的EMPLOYEE结构体按照employeeId升序进行排序。在`main`函数中,创建了一个双向链表并插入了三个EMPLOYEE结构体,然后调用`sortList`函数对链表进行排序并输出结果。 请注意,在代码中使用了动态内存分配(`malloc`)来分配内存,并在适当的时候使用了`free`来释放内存,以防止内存泄漏。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值