单链表的基本操作

211 阅读2分钟

带有头结点的单链表,基本实现了查找、排序、输入、输出、逆置、合并的操作,对于一些特殊条件判断可能不够细致,只通过了牛客网的全部测试样例。


using namespace std;

class LinkNode{
public:
	LinkNode *next;
	int data;
	LinkNode(LinkNode * next1=NULL){
		next=next1;
	}
	
};
class List{
protected:
	LinkNode *first;
	int size; //链表长度 
public:
	List(){
		first=new LinkNode();
	} 
	~List(){
		LinkNode*q;
		while(first->next!=NULL){
			q=first->next;
			first->next=q->next;
			delete q;
		}
		delete first;
	}
	bool search(int x);
	void input(int n);
	void output();
	void inst(int x, int n);
	void del(int n);
	void reset();
	void together(List& a,List &b,List& c);
	void fun();
	LinkNode* find();//寻找最大值,返回最大值节点 
};
LinkNode* List::find(){
	LinkNode *p=first->next;
	LinkNode *q=first->next;
	for(int i=0;i<size-1;i++){
		q=q->next;
		if(p->data<q->data){
			p=q;
		}
	}
	return p;
}
// 合并函数(合并两个递增) 
void List::together(List& a,List& b,List& c){
	LinkNode *pa=a.first->next,*pb=b.first->next;
	
	LinkNode *pc1=c.first;
	while(pa != NULL && pb != NULL){
		if(pa->data<pb->data){
			LinkNode *pc=new LinkNode();
			pc->data=pa->data;  
			pc1->next=pc;
			pc1=pc; 		                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
			pa=pa->next;
			
		}
		else{
			LinkNode *pc=new LinkNode();
			pc->data=pb->data;  
			pc1->next=pc;
			pc1=pc;    
		                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
			pb=pb->next;
		}
	}
		if(pa == NULL){
			while(pb != NULL){
			LinkNode *pc=new LinkNode();
				pc->data=pb->data;
				pc1->next=pc;
				pc1=pc;
			
				pb=pb->next;
			
			}
		}
		else{
			while(pa != NULL){
				LinkNode *pc=new LinkNode();
				pc->data=pa->data;
				pc1->next=pc;
				pc1=pc;
			 
				pa=pa->next;
			}
		}
		pc1->next=NULL;

}
//巧妙的逆置函数 
void List::fun() {
	if( first== NULL )
		return;
	LinkNode*p = first->next->next,*pr=NULL;
	while(p != NULL ){
		first->next->next = pr;
		pr =first->next;
		first->next=p;
		p= p->next;	
	}
	first->next->next=pr;
}
// 自己想的逆置函数 
void List::reset(){
	LinkNode *p,*q,*d;

	p=first->next;
	q=p->next;
	p->next=NULL;
	while(q){
		d=q;
		q=q->next;
		d->next=first->next;		 
		first->next=d;
		
	}

}
//删除函数 
void List::del(int n){
	LinkNode *p,*q;
	q=new LinkNode();
	int k=1;
	p=first->next;
	q=p->next;
	if(n==1){
		first->next=p->next;
		p=NULL;
	}
	while(p){		
		if(k==n-1){		 
		p->next=q->next;
		break;
		}
		p=p->next;q=q->next;
		k++;
	}

}
//插入元素 
void List::inst(int x,int n){
	LinkNode *p,*q;
	q=new LinkNode();
	q->data=x;
	int k=1;
	p=first->next;
	if(n==1){
		q->next=first->next;
		first->next=q;
	}
	while(p){
		
		if(k==n-1){		 
		q->next=p->next;
		p->next=q;
		break;
		}
		p=p->next;
		k++;
	}
}
//查找对应元素 
bool List::search(int x){
	LinkNode *p,*last;
	p=first->next;
	int n=1;
	while(p){
		if(p->data==x){
			cout<<n<<endl;
			return true;
		}
		else{
			p=p->next;
			n++;
		}
	}
	return false;
}
//输入函数 
void List::input(int n){
	LinkNode *p,*last;
	size=n;
	last=first;
	for(int i=0;i<n;i++){
		p=new LinkNode();
		cin>>p->data;
		last->next=p;
		last=p;
	}
	last->next=NULL;
}
//输出函数 
void List::output(){
	LinkNode *p=first->next;
	
	while(p){
		cout<<p->data<<" ";
		p=p->next;
	}
	cout<<endl;
	p=NULL;
}
int main(){
	List a;
	List b;
	List c;
	int n;
	cin>>n;
	a.input(n);
	a.output();
	LinkNode*p=new LinkNode();
	p=a.find();
	cout<<p->data<<endl;
	int x;int i;
	cin>>x;
	cin>>i;
	a.inst(x,i);
	a.output();
	cin>>i;
	a.del(i);
	a.output();
	cin>>x;
	if(a.search(x)){
		
	}
	else{
		cout<<"Not found"<<endl;
	}

	a.reset();
	a.output();
	int m;
	cin>>m;
	b.input(m);
	c.together(a,b,c);
	c.output();
	
	return 0;
}