静态链表常见题型总结

静态链表

 首先,静态链表的原理是Hash,即通过建立一个结构体数组,使用下标去访问数组元素;

一般化步骤:
1)定义静态链表

struct Node{
    int add;//节点地址
    typedef data;//数据域
    int next;//指针域
    XXX ; // 节点某性质,具体问题,具体分析
}node[maxn];

2)  静态链表初始化

for(int i=0;i<maxn;i++){
    node[i].XXX=false;//自行初始化为所需
}

3)  常见逻辑处理

1:由于可能存在无效节点,因此需计数统计(eg:对于节点是否在链表上,遍历链表时将XXX置true标记即可)

int p=begin,cnt=0;
while(p!=-1){
    node[p].XXX=true;
    p=node[p].next;
    cnt++;
}

cnt可用于控制节点输出个数。

2:有时为了访问方便,需将链表元素移置左端处理,一种简单的操作直接排序就好。

那怎么排序呢,第一标尺是节点是否有效,第二、~~等标尺按题目要求。

则原先结构体可以这样定义:

struct Node{
	int add,val,next;
	bool flag;
	friend bool operator <(const Node &a,const Node &b){ //两个参数必须声明为友元函数 
		if(a.flag==false || b.flag==false )//第一标尺
		    return a.flag >b.flag ;
		else return a.val <b.val ; //第二标尺val升序
        //~~~~还可以有更多标尺
	}
}node[maxn];

或者写个cmp函数,当作第三个参数传给 sort()函数

bool cmp(Node a,Node b){
    if(a.XXX==-1 || b.XXX=-1 ){
       //含无效节点时
          return a.XXX>b.XXX;
    else{
       //第二标尺
       //第三、四标尺等
    }
}
sort(node,node+maxn,cmp);

排序之后就可以按各种要求(这里真烦)输出链表了。

例题:

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 (<10​5​​) 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 [−10​5​​,10​5​​], 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

题目大意:输出链表节点个数、首地址,和升序排列的静态链表。

AC代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn=100010;
struct Node{
	int add,val,next;
	bool flag;
	friend bool operator <(const Node &a,const Node &b){ //两个参数必须声明为友元函数 
		if(a.flag==false || b.flag==false )
		    return a.flag >b.flag ;
		else return a.val <b.val ;
	}
}node[maxn];

int main(){
	int n,begin,add;
	cin>>n>>begin;
	for(int i=0;i<maxn;i++) 
	    node[i].flag =false;
	for(int i=0;i<n;i++){ //
		cin>>add;
		cin>>node[add].val >>node[add].next ;
		node[add].add =add;
	}
	int p=begin,cnt=0;
	while(p!=-1){
		node[p].flag =true;
		p=node[p].next ;
		cnt++;
	}	
	if(cnt==0){
		cout<<"0 -1\n";
	}else{
        sort(node,node+maxn);// 排序太暴力了,但他简单啊
		printf("%d %05d\n",cnt,node[0].add);
		for(int i=0;i<cnt-1;i++){
			printf("%05d %d %05d\n",node[i].add,node[i].val,node[i+1].add );
		}
		    printf("%05d %d -1\n",node[cnt-1].add,node[cnt-1].val);
	}
	return 0;
}

1074 Reversing Linked List (25point(s))

题目大意:每k个元素反转下输数,末尾不足k个元素无需反转,直接输出。

分析:可以使用for循环控制边界每k个倒序输出,也可以借助栈入栈出栈实现倒序,这里采用后者。

AC code:
 

#include<bits/stdc++.h>
using namespace std;
struct node{
	int add,val,next;
};
int main(){
	int begin,n,k;
	cin>>begin>>n>>k;
	vector<node>v(100010),temp,ans;
	for(int i=0;i<n;i++){
		int add;
		cin>>add;
		cin>>v[add].val>>v[add].next;
		v[add].add=add;
	}
	int p=begin,cnt=0;
	while(p!=-1){
		cnt++;
		temp.push_back({p,v[p].val ,v[p].next});
		p=v[p].next ;
	}
	int r=cnt-cnt%k,t=0,pp=0;
	while(1){
		t=0;
		stack<node>s;
		while(t<k && pp< r){
			t++;
			s.push(temp[pp++]);
		}
		while(!s.empty()){
			ans.push_back(s.top());
			s.pop();
		}
		if(pp==r) break;
	}
	for(int i=r;i<cnt;i++) ans.push_back(temp[i]);
	for(int i=0;i<cnt;i++){
		if(i!=cnt-1) printf("%05d %d %05d\n",ans[i].add,ans[i].val,ans[i+1].add);
		else printf("%05d %d -1\n",ans[i].add,ans[i].val);
	}
	return 0;
}

其他经典考题:

1 :链表去重

2 :两链表的公用节点地址

3:奇偶链表

4:其他各种要求输出链表

END!
 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZCAIHUI_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值