链表-PTA-链表的游标实现

Merging Linked Lists
Given two singly linked lists L​1​​=a​1​​→a​2​​→⋯→a​n−1​​→a​n​​ and L​2​​=b​1​​→b​2​​→⋯→b​m−1​​→b​m​​. If n≥2m, you are supposed to reverse and merge the shorter one into the longer one to obtain a list like a​1​​→a​2​​→b​m​​→a​3​​→a​4​​→b​m−1​​⋯. For example, given one list being 6→7 and the other one 1→2→3→4→5, you must output 1→2→7→3→4→6→5.

Input Specification:
Each input file contains one test case. For each case, the first line contains the two addresses of the first nodes of L​1​​ and L​2​​, plus a positive N (≤10​^5​​) which is the total number of nodes given. The address of a node is a 5-digit nonnegative integer, and NULL is represented by -1.

Then N lines follow, each describes a node in the format:

Address Data Next

where Address is the position of the node, Data is a positive integer no more than 10​5​​, and Next is the position of the next node. It is guaranteed that no list is empty, and the longer list is at least twice as long as the shorter one.

Output Specification:
For each case, output in order the resulting linked list. Each node occupies a line, and is printed in the same format as in the input.

Sample Input:

00100 01000 7
02233 2 34891
00100 6 00001
34891 3 10086
01000 1 02233
00033 5 -1
10086 4 00033
00001 7 -1

Sample Output:

01000 1 02233
02233 2 00001
00001 7 34891
34891 3 10086
10086 4 00100
00100 6 00033
00033 5 -1

Analysis:

  • 链表的游标实现:
    数组的索引是“地址”,数组元素为结构变量,它的结构类型是:
typedef struct node {
	int last;
	int val;
	int next;
} NODE;

last, next为int类型的原因是,他们是所指单元的“地址”(数组索引)。

  • 常用的类比替换:
NODE* p;				-->>		int p;
p->next					-->>		list[p].next
p->next->last->val		-->>		list[list[list[p].next].last].val
  • 缺点:
    ① 对于每一个链表,必须事先知道其数据规模,开好数组。该数组其实就相当于该链表所能触及的“整个内存”,因为数组索引就是“地址”。
    ② 一个这样的大数组只能在其上建立一个链表,对于多链表需要多个大数组。
    ③ 巨大的空间浪费。
  • 优点
    ① 适合解决这种给定“地址”的鬼畜题目。
  • 此题用正常的建立链表,反序,归并当然也可以做。约200行代码,但笔者不知为什么PTA最后一个测试点总是过不去,故用此下策,顺便学习链表的游标表示法。

My answer:

#include<stdio.h>
#include<stdlib.h>
typedef struct node {
	int last;
	int val;
	int next;
} NODE;

int CountList(int head_add);
int Reverse(int head_add);
int Merge(int head1_add, int head2_rev_add);
void Print(int merge);

NODE list[100001];

int main (void)
{
	int head1_add, head2_add, N, head2_rev_add, merge;
	int i, add, val, next, t;
	scanf("%d %d %d", &head1_add, &head2_add, &N);

	for (i = 0; i <= 100000; i++) {
		list[i].last = list[i].next = -1;
	}

	for (i = 0; i <= N - 1; i++) {
		scanf("%d %d %d", &add, &val, &next);
		list[add].val = val; list[add].next = next;
		if (next != -1)list[next].last = add;
	}
	
	if (CountList(head1_add) < CountList(head2_add)) {
		t = head1_add; head1_add = head2_add; head2_add = t;
	}//then the first list is longer

	head2_rev_add = Reverse(head2_add);
	merge = Merge(head1_add, head2_rev_add);
	Print(merge);

	return 0;
}

int Merge(int head1_add, int head2_rev_add)
{
	int i = 2, t;
	int p1, p2;
	p1 = head1_add; p2 = head2_rev_add;

	while (1) {
		if ((i++) % 3 == 0) {
			t = list[p2].next;
			list[list[p1].next].last = p2; list[p2].next = list[p1].next;
			list[p1].next = p2; list[p2].last = p1;

			p1 = list[p1].next;
			p2 = t;
			if (p2 == -1)break;
		}
		else p1 = list[p1].next;
	}

	return head1_add;
}

int Reverse(int head_add)
{
	int t;
	int p = head_add;
	while (1) {
		t = list[p].next; list[p].next = list[p].last; list[p].last = t;
		if (list[p].last == -1)break;
		else p = list[p].last;			//original "next"
	}
	return p;
}

int CountList(int head_add) 
{
	if (head_add == -1) return 0;

	int cnt = 1;
	while ((head_add = list[head_add].next) != -1) cnt++;
	return cnt;
}

void Print(int merge)
{
	while (merge != -1) {
		if(list[merge].next != -1)
			printf("%05d %d %05d\n", 
				merge, list[merge].val, list[merge].next);
		else printf("%05d %d %d\n",
				merge, list[merge].val, -1);
		
		merge = list[merge].next;
	}
	return;
}

Appendix: 传统的链表实现

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<stdbool.h>
typedef struct input {
	int add;
	int val;
	int next;
} NODE_IN;
typedef struct node {
	struct node* last;
	int val;
	struct node* next;
} NODE;

void BuildLinkedList(NODE** head1_add, NODE** head2_add, NODE_IN* input);
NODE* Reverse(NODE* list);
NODE* MergeList(NODE* list1, NODE* list2_rev);
int CountListNode(NODE* list);
void Print(NODE* list, NODE_IN* input);
int GetIndex(int val, NODE_IN* input);

int head1_add, head2_add, N;

int main (void)
{
	int i;
	scanf("%d %d %d", &head1_add, &head2_add, &N);

	NODE_IN* input = (NODE_IN*)malloc(N * sizeof(NODE_IN));
	for (i = 0; i <= N - 1; i++) {
		scanf("%d %d %d", &input[i].add, &input[i].val, &input[i].next);
	}

	NODE* list1, *list2, *list1_rev, *list2_rev, *merge;
	
	BuildLinkedList(&list1, &list2, input);
	if (CountListNode(list1) < CountListNode(list2)) {
		list1_rev = Reverse(list1);
		merge = MergeList(list2, list1_rev);
	}
	else {
		list2_rev = Reverse(list2);
		merge = MergeList(list1, list2_rev);
	}
	
	Print(merge, input);
	return 0;
}

NODE* MergeList(NODE* list1, NODE* list2_rev)
{
	int cnt = 1;
	NODE*p1, *p2;
	NODE* head3, *resp, *resq;
	if (CountListNode(list1) > CountListNode(list2_rev)){
		p1 = list1->next; p2 = list2_rev->next;
	}
	else {
		p1 = list2_rev->next; p2 = list1->next;
	}
	//then the "p head1" list has more nodes

	resp = head3 = (NODE*)malloc(sizeof(NODE));
	head3->last = head3->next = NULL;
	while (1) {
		if ((cnt++) % 3 != 0) {
			resq = (NODE*)malloc(sizeof(NODE));
			resq->val = p1->val;
			resp->next = resq; resq->last = resp; resq->next = NULL; resp = resq;
			p1 = p1->next;
			if (p1 == NULL)break;
		}
		else {
			resq = (NODE*)malloc(sizeof(NODE));
			resq->val = p2->val;
			resp->next = resq; resq->last = resp; resq->next = NULL; resp = resq;
			p2 = p2->next;
			if (p2 == NULL)break;
		}
	}
	if (p2 == NULL && p1 != NULL) {
		while (p1 != NULL) {
			resq = (NODE*)malloc(sizeof(NODE));
			resq->val = p1->val;
			resp->next = resq; resq->last = resp; resq->next = NULL; resp = resq;
			p1 = p1->next;
		}
	}
	if (p1 == NULL && p2 != NULL) {
		while (p2 != NULL) {
			resq = (NODE*)malloc(sizeof(NODE));
			resq->val = p2->val;
			resp->next = resq; resq->last = resp; resq->next = NULL; resp = resq;
			p2 = p2->next;
		}
	}

	return head3;
}

NODE* Reverse(NODE* list) //list with dummy header
{
	NODE* rev = (NODE*)malloc(sizeof(NODE));
	NODE* p = rev, *q;
	p->next = p->last = NULL;

	while ((list = list->next) != NULL) {
		q = (NODE*)malloc(sizeof(NODE));
		q->val = list->val; 
		q->next = p->next; 
		if(p->next != NULL) p->next->last = q;
		p->next = q; q->last = p;
	}

	return rev;
}

void BuildLinkedList(NODE** list1_add, NODE** list2_add, NODE_IN* input)
{
	int i, cur_index;
	NODE* p, *q;

	//build the first list
	(*list1_add) = (NODE*)malloc(sizeof(NODE));
	(*list1_add)->last = (*list1_add)->next = NULL;
	p = (*list1_add);

	for (i = 0; i <= N - 1; i++) {
		if (input[i].add == head1_add)break;
	}
	cur_index = i;
	q = (NODE*)malloc(sizeof(NODE));
	q->val = input[cur_index].val;
	p->next = q; q->last = p; q->next = NULL; p = q;

	while (input[cur_index].next != -1) {
		for (i = 0; i <= N - 1; i++) {
			if (input[i].add == input[cur_index].next)break;
		}
		cur_index = i;
		q = (NODE*)malloc(sizeof(NODE));
		q->val = input[cur_index].val;
		p->next = q; q->last = p; q->next = NULL; p = q;
	}

	//build the second list
	(*list2_add) = (NODE*)malloc(sizeof(NODE));
	(*list2_add)->last = (*list2_add)->next = NULL;
	p = (*list2_add);

	for (i = 0; i <= N - 1; i++) {
		if (input[i].add == head2_add)break;
	}
	cur_index = i;
	q = (NODE*)malloc(sizeof(NODE));
	q->val = input[cur_index].val;
	p->next = q; q->last = p; q->next = NULL; p = q;

	while (input[cur_index].next != -1) {
		for (i = 0; i <= N - 1; i++) {
			if (input[i].add == input[cur_index].next)break;
		}
		cur_index = i;
		q = (NODE*)malloc(sizeof(NODE));
		q->val = input[cur_index].val;
		p->next = q; q->last = p; q->next = NULL; p = q;
	}

	return;
}

int CountListNode(NODE* list)
{
	int cnt = 0;
	while ((list = list->next) != NULL)cnt++;
	return cnt;
}

void Print(NODE* list, NODE_IN* input)
{
	int i;
	NODE* p = list->next;

	while (p != NULL) {
		i = GetIndex(p->val, input);
		if(p->next!=NULL)
			printf("%05d %d %05d\n", input[i].add, input[i].val, input[GetIndex(p->next->val, input)].add);
		else 
			printf("%05d %d %d\n", input[i].add, input[i].val, -1);

		p = p->next;
	}

	return;
}

int GetIndex(int val, NODE_IN* input)
{
	int i;
	for (i = 0; i <= N - 1; i++) {
		if (input[i].val == val)break;
	}
	return i;
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值