c++实现单链表(通俗易懂)

链表,可以将之理解为一种结构体指针数据类型。c++的实现方式和c相比没有什么太大的区别。
首先定义一个结构体,

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

这里data用于储存数据,node *next用于指向下一节点的地址。
由于我们平常使用的都是结构体的指针,这里就自定义一个数据类型List,用于表示链表。

创建一个链表

//创建一个单链表 
void creat_List(List a,int length)
{
	List b,c;
	b=c=a;
	int i=0,j;
    while(i<length)
    {
    	b=new node;
    	cin>>j;
		b->data=j;
    	c->next=b;
    	c=b;
    	++i;
	}
	c->next=nullptr;
	
	return ;
}

这里的nullptr和c语言的NULL其实差不多,但是由于一些原先,使用NULL可能会造成一些异常,这里建议使用nullptr,具体原因可以自行百度。
和c语言的申请空间差不多,这里使用了new来代替。注意,第一个创建的节点的下一节点应该指向的是nullptr。

插入节点

//从后插入节点 
void push_back(List a,int num)
{
	while(a->next)
	a=a->next;
	

	List b;
	b=new node;
	b->data=num;
	b->next=nullptr;
	a->next=b;
	
	return ;
}

从表头依次遍历到表尾,再申请一块空间开储存数据
先将表尾指向新建的b,再让b指向nullptr。这样就成功插入节点。
在这里插入图片描述

删除节点

首先判断链表是否为空,然后依次遍历到对应的节点,先新建一个节点b,
让它指向待删除节点a的下一节点,再让节点a前一节点指向节点b后一节点(防止链表断开),最后再将节点b清空内存,删除成功。删除整个链表也是一样的操作。

//删除对应节点 
void de_List(List a,int n)
{
	int i=1;
	
	if(Is_empty(a))
	return ;

	while(i!=n)
	{
		a=a->next;
		++i;
	}
	List b;
	b=a->next;
	a->next=b->next;
	delete b;
	return ;
}

源码:

#include<bits/stdc++.h>
using namespace std;
struct node
{
   int data;
   node *next;	
};
typedef node* List;
//创建一个单链表 
void creat_List(List a,int length)
{
	List b,c;
	b=c=a;
	int i=0,j;
    while(i<length)
    {
    	b=new node;
    	cin>>j;
		b->data=j;
    	c->next=b;
    	c=b;
    	++i;
	}
	c->next=nullptr;
	
	return ;
}
bool Is_empty(List a)
{
	if(a->next)
	return 0;
	else
	return 1;
}
//删除对应节点 
void de_List(List a,int n)
{
	int i=1;
	
	if(Is_empty(a))
	return ;
	
	
	while(i!=n)
	{
		a=a->next;
		++i;
	}
	a->next=a->next->next;
	return ;
}
//删除整个链表 
void de_all(List a)
{
	List b;
	while(a->next)
	{
		b=a->next;
		a->next=b->next;
		delete b;
	}
	cout<<"deleted!"<<endl;
	return ;
}
//依次输出整个链表 
void get_List(List a)
{
	if(Is_empty(a))
	{
		cout<<"list is empty";
		return ;
	}
	
	a=a->next;
	while(a->next)
	{
		cout<<a->data<<" ";
		a=a->next;
	}
	cout<<a->data;
	cout<<endl;
	return ;
}
//输出链表长度 
int get_length(List a)
{
   int i=0;
   while(a->next)
   {
   	 a=a->next;
   	 ++i;
   }	
   
   return i;
}
//查询对应节点的数据 
int  get_num(List a,int n)
{
	if(Is_empty(a))
	{
		cout<<"list is empty";
		return 0;
	}
	
	int i=0;
	while(i!=n)
	{
		a=a->next;
		++i;
	}
	return a->data;
}
//从头部插入节点 
void push_front(List a,int num)
{

	List b=new node;

	b->next=a->next;
	b->data=num;
	a->next=b;

	return ;
}
//从后插入节点 
void push_back(List a,int num)
{
	while(a->next)
	a=a->next;

	List b;
	b=new node;
	b->data=num;
	b->next=nullptr;
	a->next=b;
	
	return ;
}
//从指定位置前插入 
void push_assign(List a,int loc,int num)
{
	int i=1;
	
	if(loc>get_length(a)+1)
	{
		cout<<"over the lengthen!"<<endl;
		return ;
	}
	
	while(i<loc)
	{
		a=a->next;
		++i;
	}
	
	List b=new node;
	b->data=num;
	b->next=a->next;
	a->next=b;
	
	return ;
}
//连接两个链表 
void add_List(List a,List b)
{
	while(a->next)
	a=a->next;
	a->next=b->next;
	
	return ;
}
int main()
{
	List l=new node;
	l->data=0;
	l->next=nullptr;
	
	List k=new node;
	k->data=0;
	k->next=nullptr;
	
	//分布创建两个单链表,连接他们; 
	creat_List(l,2);
	get_List(l);
    push_assign(l,3,100);
    get_List(l);
	cout<<get_length(l);
	creat_List(k,3);
	add_List(l,k);
	get_List(l);


	return 0; 
}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是用C++实现单链表操作的示例代码: ```cpp #include <iostream> using namespace std; // 定义链表节点结构体 struct ListNode { int val; // 存储节点的值 ListNode *next; // 指向下一个节点的指针 ListNode(int x) : val(x), next(NULL) {} // 构造函数 }; // 定义链表类 class LinkedList { public: LinkedList() { // 构造函数 head = NULL; } ~LinkedList() { // 析构函数 ListNode *cur = head; while (cur != NULL) { ListNode *temp = cur; cur = cur->next; delete temp; } } void add(int val) { // 添加节点 if (head == NULL) { head = new ListNode(val); } else { ListNode *cur = head; while (cur->next != NULL) { cur = cur->next; } cur->next = new ListNode(val); } } void remove(int val) { // 移除节点 if (head == NULL) { return; } if (head->val == val) { ListNode *temp = head; head = head->next; delete temp; } else { ListNode *cur = head; while (cur->next != NULL && cur->next->val != val) { cur = cur->next; } if (cur->next != NULL) { ListNode *temp = cur->next; cur->next = cur->next->next; delete temp; } } } void print() { // 打印链表 ListNode *cur = head; while (cur != NULL) { cout << cur->val << " "; cur = cur->next; } cout << endl; } private: ListNode *head; // 链表头指针 }; int main() { LinkedList list; list.add(1); list.add(2); list.add(3); list.print(); // 输出:1 2 3 list.remove(2); list.print(); // 输出:1 3 return 0; } ``` 该代码实现单链表的添加、移除和打印操作。需要注意的是,析构函数需要手动释放链表中所有节点的内存,否则可能会出现内存泄漏的问题。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值