链表基本操作详解

注释:这段时间在参加校招,问了很多链表的问题,虽然都可以完全不错的写出来,但却花了不少时间,一怒之下我就把链表的基本所有操作都重新编写一遍

备注:适合于才学链表和有一些链表操作的经验的同学看,代码全部都可以通过,可以放心使用

头文件:list.h

#include <iostream>
#include <assert.h>
using namespace std;
#include <vector>
struct Node
{
	int value;
	struct Node *next;
	Node(int v=0):value(v),next(NULL)
	{}
};

class List 
{
public:
	void insert(int value=0)
	{
		Node *node=BuyNode(value);
		if(head==NULL)
		{
			head=node;
			return;
		}
		//头插
		node->next=head->next;
		node->next=head;
		head=node;
	}
	void show(Node *Head)
	{
		Node *cur=Head;
		while(cur)
		{
			cout<<cur->value<<" ";
			cur=cur->next;
		}
		cout<<endl;
	}
	void print()
	{
		print(head);
	}
	//链表逆制
	void resver()
	{
		resver(head);
	}
	//从尾打印链表
	void print_tail()
	{
		print_tail(head);
	}
	//在O(1)时间内删除一个链表
	void remove(int value)
	{
		remove(head,value);
	}
	//链表的倒数第k个节点
	Node* get_k(int k)
	{
		return get_k(head,k);
	}
	//寻找两个链表的公共节点
	Node* find_comm_node(List *list1,List *list2)
	{
		if(list1 ==NULL || list2 ==NULL)
			return NULL;
		return find_comm_node(list1->head,list2->head);
	}
	//合并两个有序链表
	Node* megre(List *head,List *phead)
	{
		if(head==NULL || phead==NULL)
			return NULL;
		return megre(head->head,phead->head);
	}
	//链表合并第二种写法
	Node* Megre(List *head,List *phead)
	{
		if(head==NULL || phead==NUL
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值