【C++】数据结构之单链表

1.头插法创建单链表

LK creat_TList(){
	LK head,p;
	head=new LinkList;
	head->next=NULL;
	int x;
	cin>>x;
	while(x!=-1){
		p=new LinkList;
		p->data=x;
		p->next=head->next;
		head->next=p;
		cin>>x;
	}
	return head;
}

2. 尾插法创建单链表

LK creat_WList(){
	LK head,p,r;
	head=new LinkList;
	head->next=NULL;
	r=head;
	int x;
	cin>>x;
	while(x!=-1){
		p=new LinkList;
		p->data=x;
		p->next=r->next;
		r->next=p;
		r=p;
		cin>>x;
	}
	r->next=NULL;
	return head;
}

3.单链表长度

void listlength(LK head){
	LK L;
	L=head->next;
	while(L){
		length++;
		L=L->next;
	}
}

4.单链表删除操作

LK delet(LK head){
	LK L,p;
	L=head;
	int flag;
	cin>>flag;
	//flag=1 按位置删除
	if(flag==1){
		int k;
		cin>>k;
		if(k>length) return head;
		for(int i=0;i<k-1;i++)
		{
			L=L->next;
		}
		p=new LinkList;
		p=L->next;
		L->next=p->next;
		free(p);
		length--;
	    }
	//flag=2 按元素删除
	else if(flag==2){
		int x;
		cin>>x;
		while(L->next&&L->next->data!=x){
			    L=L->next;
		}
		//如果删除链表中指定元素
		/*
		while(L->next){
		     if(L->next->data==x){
			p=new LinkList;
		    p=L->next;
		    L->next=p->next;
		    free(p);
		     }
		}
		*/
		p=new LinkList;
		p=L->next;
		L->next=p->next;
		free(p);
		length--;
	  }
	return head;
}

5.单链表插入操作

LK insertx(LK head){
	LK L,p;
	L=head;
	int k,x;
	cin>>k>>x;
	if(k>length+1) return head;
	for(int i=0;i<k-1;i++){
		L=L->next;
	}
	p=new LinkList;
	p->data=x;
	p->next=L->next;
	L->next=p;
	length++;
	return head;
}

6.单链表逆置

LK inversion_List(LK head){
	   LK p,L,r;
	   r=head->next;
	   L=new LinkList;
	   L->next=NULL;
	   while(r){
			p= new LinkList;
		  p->data=r->data;
		  p->next=L->next;
		  L->next=p;
		  r=r->next;
	  }
	  free(head);
	  return L;
}

7.单链表遍历

void Printf_List(LK head){
	LK L;
	L=head->next;
	while(L){
		cout<<L->data<<" ";
		L=L->next;
	}
}

8.主函数部分

int main(){
	LK head1,head2;
	head1=new LinkList;
	head1=creat_TList();
	Printf_List(head1);
	cout<<endl;
	head2=new LinkList;
	head2=creat_WList();
	Printf_List(head2);
	cout<<endl;
    listlength(head2);
    cout<<length<<endl;
	head2=delet(head2);
	Printf_List(head2);
	cout<<endl;
	head2=insertx(head2);
	Printf_List(head2);
	cout<<endl;
	head2=inversion_List(head2);
	Printf_List(head2);
	cout<<endl;
	return 0;
}

9.完整代码

#include<bits/stdc++.h>
using namespace std;
typedef struct LinkList{
	int data;
	struct LinkList *next;
}LinkList,*LK;
int length;
//头插法创建单链表
LK creat_TList(){
	LK head,p;
	head=new LinkList;
	head->next=NULL;
	int x;
	cin>>x;
	while(x!=-1){
		p=new LinkList;
		p->data=x;
		p->next=head->next;
		head->next=p;
		cin>>x;
	}
	return head;
}
//尾插法创建单链表
LK creat_WList(){
	LK head,p,r;
	head=new LinkList;
	head->next=NULL;
	r=head;
	int x;
	cin>>x;
	while(x!=-1){
		p=new LinkList;
		p->data=x;
		p->next=r->next;
		r->next=p;
		r=p;
		cin>>x;
	}
	r->next=NULL;
	return head;
}
//单链表长度
void listlength(LK head){
	LK L;
	L=head->next;
	while(L){
		length++;
		L=L->next;
	}
}
//单链表删除
LK delet(LK head){
	LK L,p;
	L=head;
	int flag;
	cin>>flag;
	//flag=1 按位置删除
	if(flag==1){
		int k;
		cin>>k;
		if(k>length) return head;
		for(int i=0;i<k-1;i++)
		{
			L=L->next;
		}
		p=new LinkList;
		p=L->next;
		L->next=p->next;
		free(p);
		length--;
	    }
	//flag=2 按元素删除
	else if(flag==2){
		int x;
		cin>>x;
		while(L->next&&L->next->data!=x){
			    L=L->next;
		}
		//如果删除链表中指定元素
		/*
		while(L->next){
		     if(L->next->data==x){
			p=new LinkList;
		    p=L->next;
		    L->next=p->next;
		    free(p);
		     }
		}
		*/
		p=new LinkList;
		p=L->next;
		L->next=p->next;
		free(p);
		length--;
	  }
	return head;
}
//插入新元素
LK insertx(LK head){
	LK L,p;
	L=head;
		int k,x;
		cin>>k>>x;
		if(k>length+1) return head;
		for(int i=0;i<k-1;i++){
			L=L->next;
		}
		p=new LinkList;
		p->data=x;
		p->next=L->next;
		L->next=p;
		length++;
		return head;
}
//单链表逆序输出
LK inversion_List(LK head){
	   LK p,L,r;
	   r=head->next;
	   L=new LinkList;
	   L->next=NULL;
	     while(r){
			p= new LinkList;
		     p->data=r->data;
		     p->next=L->next;
		     L->next=p;
		     r=r->next;
	   }
	   free(head);
	   return L;
}
//遍历单链表
void Printf_List(LK head){
	LK L;
	L=head->next;
	while(L){
		cout<<L->data<<" ";
		L=L->next;
	}
}
int main(){
	LK head1,head2;
	head1=new LinkList;
	head1=creat_TList();
	Printf_List(head1);
	cout<<endl;
	head2=new LinkList;
	head2=creat_WList();
	Printf_List(head2);
	cout<<endl;
    listlength(head2);
    cout<<length<<endl;
	head2=delet(head2);
	Printf_List(head2);
	cout<<endl;
	head2=insertx(head2);
	Printf_List(head2);
	cout<<endl;
	head2=inversion_List(head2);
	Printf_List(head2);
	cout<<endl;
	return 0;
}

如有错误,希望各位大佬及时纠正一下!
希望能帮到更多的人!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值