Day 42 算法笔记之提高篇(1) 链表处理

91.链表的定义

struct node{
	typename data;
	node* next;
};

2.分配空间

typename* p = new typename;
delete(p);

3.创建链表

#include<stdio.h>
#include<stdlib.h>
using namespace std;

struct node{
	int data;
	node* next;
};

node* create(int array[]){
	node *head;
	head = new node;
	head->next=NULL;
	node *pre;
	pre=head;
	node *p;
	for(int i=0;i<5;i++){
		p = new node;
		p->data=array[i];
		p->next=NULL;
		pre->next=p;
		pre=p;
	}
	return head;
}



int main(){
	
	int array[5] ={5,3,6,1,2};
	node* l = create(array);
	l = l->next;
	while(l!=NULL){
		printf("%d",l->data);
		l=l->next;
	}

	return 0 ;
}

4.查找元素

int search(node* head,int x){
	int count=0;
	node* p = head->next;
	while(p->data!=NULL){
		if(p->data==x){
			count++;
		}
		p = p->next;
	}
	return count;
}

5.插入元素

void insert(node* head,int pos,int x){
	node* p = head;
	for(int i=0;i<pos-1;i++){
		p = p->next;
	}
	node* q = new node;
	q->data=x;
	q->next=p->next;
	p->next=q;
}

6.删除元素

void del(node* head,int x){
	node* p = head->next;
	node* pre = head;
	while(p!=NULL){
		if(p->data==x){
			pre->next=p->next;
			delete(p);
			p=pre->next;
		}else{
			pre=p;
			p=p->next;
		}
	}
}

7.静态链表

struct node{
	typename data;
	int next;
}node[size];

8.Sharing

#include<stdio.h>
#include<stdlib.h>
using namespace std;

const int maxn=100010;

struct nodes{
	char data;
	int next;
	bool flag;
}node[maxn];

int main(){
	
	for(int i=0;i<maxn;i++){
		node[i].flag=false;
	}
	
	int s1,s2,n;
	scanf("%d%d%d",&s1,&s2,&n);
	int address,next;
	char data;
	for(int i=0;i<n;i++){
		scanf("%d %c %d",&address,&data,&next);
		node[address].data=data;
		node[address].next=next;
	}
	
	int p;
	for(p=s1;p!=-1;p=node[p].next){
		node[p].flag=true;
	}
	for(p=s2;p!=-1;p=node[p].next){
		if(node[p].flag==true) break;
	}
	if(p!=-1) printf("%05d\n",p);
	else printf("-1\n");

	return 0 ;
}

9.Linked List Sorting

#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;

const int maxn=100005;
struct nodes{
	int address,data,next;
	bool flag;
}node[maxn];

bool cmp(nodes a,nodes b){
	if(a.flag==false||b.flag==false){
		return a.flag>b.flag;
	}else{
		return a.data<b.data;
	}
}

int main(){
	for(int i=0;i<maxn;i++){
		node[i].flag=false;
	}
	int n,begin;
	scanf("%d%d",&n,&begin);
	int address;
	for(int i=0;i<n;i++){
		scanf("%d",&address);
		node[address].address=address;
		scanf("%d%d",&node[address].data,&node[address].next);
	}
	int count=0,p=begin;
	while(p!=-1){
		node[p].flag=true;
		count++;
		p=node[p].next;
	}
	if(count==0){
		printf("0 -1");
	}else{
		sort(node,node+maxn,cmp);
		printf("%d %05d\n",count,node[0].address);
		for(int i=0;i<count;i++){
			if(i!=count-1){
				printf("%05d %d %05d\n",node[i].address,node[i].data,node[i].next);
			}else{
				printf("%05d %d -1\n",node[i].address,node[i].data);
			}
		}
	}
	return 0;
}

10.Reversing Linked List

#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;

const int maxn=100010;
struct nodes{
	int address,data,next;
	int order;
}node[maxn];

bool cmp(nodes a,nodes b){
	return a.order<b.order;
}

int main(){
	
	for(int i=0;i<maxn;i++){
		node[i].order=maxn;
	}
	
	int begin,n,k;
	scanf("%d %d %d",&begin,&n,&k);
	int address;
	for(int i=0;i<n;i++){
		scanf("%d",&address);
		node[address].address = address;
		scanf("%d%d",&node[address].data,&node[address].next);	
	}
	
	int p=begin,count=0;
	while(p!=-1){
		node[p].order=count++;
		p=node[p].next;
	}
	sort(node,node+maxn,cmp);
	
	for(int i=0;i<count/k;i++){
		for(int j=(i+1)*k-1;j>i*k;j--){
			printf("%05d %d %05d\n",node[j].address,node[j].data,node[j-1].address);
		}
		printf("%05d %d",node[i*k].address,node[i*k].data);
		if(i<n/k-1){
			printf("%05d\n",node[(i+2)*k-1].address);
		}else{
			if(n%k==0){
				printf("-1\n");
			}else{
				printf(" %05d\n",node[(i+1)*k].address);
				for(int i=n/k*k;i<n;i++){
					printf("%05d %d ",node[i].address,node[i].data);
					if(i<n-1) printf("%05d\n",node[i].next);
					else printf("-1\n");
				}
			}
		}
	}
	
	return 0;
}

11.Deduplication on a Linked List

#include<stdio.h>
#include<stdlib.h>
#include<cstring>
#include<algorithm>
using namespace std;

const int maxn=100005;
const int table = 1000010;
struct nodes{
	int address,data,next;
	int order;
}node[maxn];

bool isexist[table] ={false};
bool cmp(nodes a,nodes b){
	return a.order<b.order;
}

int main(){
	
	memset(isexist,false,sizeof(isexist));
	for(int i=0;i<maxn;i++){
		node[i].order=2*maxn;
	}
	
	int begin,n;
	scanf("%d%d",&begin,&n);
	int address;
	for(int i=0;i<n;i++){
		scanf("%d",&address);
		node[address].address=address;
		scanf("%d %d",&node[address].data,&node[address].next);
	}
	
	int countvalid=0,countremoved=0;
	int p=begin;
	while(p!=-1){
		if(!isexist[abs(node[p].data)]){
			isexist[abs(node[p].data)]=true;
			node[p].order=countvalid++;
		}else{
			node[p].order=maxn+countremoved++;
		}
		p=node[p].next;
	}
	
	sort(node,node+maxn,cmp);
	
	int count=countvalid+countremoved;
	for(int i=0;i<count;i++){
		if(i!=countvalid-1&&i!=count-1){
			printf("%05d %d %05d\n",node[i].address,node[i].data,node[i+1].address);
		}else{
			printf("%05d %d -1\n",node[i].address,node[i].data);
		}
	}
	
	
	return 0;
}

おすすめ

転載: blog.csdn.net/aixiaoxiao13/article/details/121386129