从0实现链表(动态/静态)

从0实现各种链表

//——动态单链表——
//1.创建节点

#include<iostream>
using namespace std;
struct Node
{
	int date;
	Node* next;
};

//2.动态申请节点并初始化

Node* creatNode(int x)
{
	Node* newNode = new Node;
	newNode->date = x;
	newNode->next = nullptr;
	return newNode;
}

//3.打印链表

void printfNode(Node* cur)//cur为头结点指针
{
	while (cur != nullptr)
	{
		cout << cur->date << "-->";
		cur = cur->next;
	}
	cout << "nullptr" << endl;
	cout << endl;
}

//4.头插法

void push_front(Node* &phead, int x)
{
	Node* newNode = creatNode(x);
	newNode->next = phead;//把头结点放到新节点的next
	phead = newNode;

}

//5.尾插法

void push_back(Node* &phead, int x)
{
	Node* newNode = creatNode(x);
	if (phead == nullptr)//链表为空
	{
		phead = newNode;
	}
	else
	{
		Node* taxi = phead;
		while (taxi->next != nullptr)
		{
			taxi->next = taxi->next->next;
		}
		taxi->next = newNode;
	}

}

//6.头删法

void pop_front(Node*& phead)
{
	if (phead == nullptr)
	{
		cout << "链表为空" << endl;
		return;
	}
	else
	{
		Node* tmp = phead;
		phead = phead->next;
		delete tmp;
		tmp = nullptr;
	}
}

//7.尾删法

void pop_back(Node*& phead)
{
	if (phead == nullptr)
	{
		cout << "链表为空" << endl;
		return;
	}
	else if (phead->next == nullptr)
	{
		delete phead;
		phead == nullptr;
	}
	else
	{
		Node* taxi = phead;
		while (taxi->next->next != nullptr)
		{
			taxi->next = taxi->next->next;
		}
		delete taxi->next;
		taxi->next = nullptr;
	}
}

//8.删除所有节点

void clean(Node* phead)
{
	while (phead)
	{
		pop_front(phead);
	}
}

//test

//int main()
//{
//	Node* phead = nullptr;
//	push_front(phead, 1);
//	push_front(phead, 3);
//	push_front(phead, 5);
//	printfNode(phead);
//	pop_front(phead);
//	printfNode(phead);
//	pop_back(phead);
//	printfNode(phead);
//}

//——静态单链表——
//1.创建-初始

const int N = 1e5 + 10;
int h;
int id;
int n[N],ne[N];
int mp[N];//(哈希表)

//2.头插法

void push_front(int x)
{
	id++;
	n[id] = x;
	ne[id] = ne[h];
	ne[h] = id;
}

//3.遍历链表

void printList()
{
	for (int i = ne[h]; i; i = ne[i])
	{
		cout << n[i] << " ";
	}
	cout << endl;
}

//4.按值查找

//(1)遍历
int find(int x)
{
	for (int i = ne[h]; i; i = ne[i])
	{
		if (n[i] == x)
		{
			return i;
		}
	}
	return 0;
	cout << endl;
}
//(2)哈希表优化
int find2(int x)
{
	return mp[x];
	//push_front 和 insert 的时候,打上标记
	//	mp[x] = id; // x 这个元素存放的位置是 id
	//erase 的时候,消除标记
	//	mp[x] = 0
}

//5.在任意位置插入

void insert(int p,int x)
{
	id++;
	n[id] = x;
	ne[id] = ne[p];
	ne[p] = id;
}

//6.删除元素

void erase(int p) // 注意 p 表⽰元素的位置
{
	if (ne[p])
	{
		mp[n[ne[p]]] = 0; // 将 p 后⾯的元素从 mp 中删除
		ne[p] = ne[ne[p]]; // 指向下⼀个元素的下⼀个元素
	}
}

//test

int main()
{
	for (int i = 1; i <= 5; i++)
	{
		push_front(i);
		printList();
	}
	/*cout << find(1) << endl;
	cout << find(5) << endl;
	cout << find(6) << endl;*/
	 insert(1, 10);
	 printList();
	 insert(2, 100);
	 printList();
	/*erase(2);
	printList();
	erase(4);
	printList();*/
	return 0;
}

下一篇从0实现栈

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值