C++ 链表 增删查改

#if 1
#include<algorithm>
#include<iostream>
#include<vector>
using namespace std;
struct ListNode
{
	int data;
	ListNode* next;//结构体指针
};
void Listprintf(ListNode* phead)
{
	ListNode* cur = phead;
	while (cur != NULL)
	{
		cout << cur->data << "->" << endl;
		cur = cur->next;
	}

	cout << "printf done" << endl;
}

//尾插
void Listpushback(ListNode** pphead, int x)//原本的指针phead是没有任何指向的,这个指针没有指向某一个地址,
//而是一个空指针,在函数传参的时候,如果参数pphead是一级指针,则pphead也是空指针,
//改变pphead,并不会影响到phead,所以最终一通操作下来,phead还是空指针,输出结果也就是空的。
//类似引用 ?????????
{
	ListNode* newnode = new ListNode{ x,NULL };//创建一个类的新指针newnode,作为中间过程以后存放新节点提供过度
											   //为了让pphead永远指向链表中的第一个节点
	if (*pphead == NULL)
	{
		*pphead = newnode;
	}
	else
	{
		ListNode* tail = *pphead; //将pphead的地址赋予一个类的新指针tail,
		                          //此时tail的地址与pphead的地址一样,对tail操作就是对pphead操作
		static int tailCount = 0;
		while (tail->next != NULL)
		{
			tail = tail->next;//链表的地址是每个首节点地址,目的是找到本链表的最后一个节点

			tailCount++;
			std::cout << "tail count:  " << tailCount << "    " << tail->data << std::endl;
		}

		tail->next = newnode; //上一个节点的值指向这个新建立的节点也
	}
}

//头插
void ListPushfront(ListNode** pphead, int x) {
	ListNode* newnode = new ListNode{ x, NULL };
	newnode->next = *pphead;
	*pphead = newnode;
}

//尾删
void listPopBack(ListNode** pphead) {

	if (*pphead == NULL)
	{
		return;
	}
	if ((*pphead)->next == NULL )
	{
		delete(*pphead);
		*pphead = NULL;
	}
	else
	{
		ListNode* tail = *pphead;
		ListNode* prev = NULL;

		while (tail->next != NULL)
		{
			prev = tail;
			tail = tail->next;
		}
		delete (tail);
		tail = NULL;
		prev->next = NULL;

	}

}

//头删
void listPopFront(ListNode** pphead) {
	
	if (*pphead == NULL)
	{
		return;
	}
	else
	{
		ListNode* newnode = (*pphead)->next;
		delete (*pphead);
		*pphead = newnode;
	}


}

//查找元素,返回值是地址
ListNode* listFind(ListNode* phead, int x) {
	ListNode* cur = phead;
	while (cur)
	{
		if (cur->data == x)
		{
			return cur;
		}
		else
		{
			cur = cur->next;
		}
	}

	return NULL;
}

//插入元素,在pos的前一个位置插入
//配合Listfind使用,具体使用见test_insert函数
void listInsertFront(ListNode** pphead, ListNode* pos, int x){
	ListNode* newnode = new ListNode{ x,NULL };
	if (*pphead == NULL )
	{
		*pphead = newnode;
	}
	else
	{
		ListNode* prePos = *pphead;
		while (prePos->next != pos)
		{
			prePos = prePos->next;
		}
		prePos->next = newnode;
		newnode->next = pos;
	}

}


//单链表并不适合在前一个位置插入,因为运算较麻烦,会损失效率
//包括c++中为单链表提供的库函数也只有一个insert_after而没有前一个位置插入
//在后一个位置插入相对简单
void listInsertBack(ListNode** phead, ListNode* pos, int x){
	ListNode* newnode = new ListNode{ x,NULL };
	newnode->next = pos->next;
	pos->next = newnode;

}


//删除指定位置的节点
void listErase(ListNode** pphead, ListNode* pos)
{
	if (*pphead == pos)
	{
		*pphead = pos->next;
		delete(pos);
	}
	else
	{
		ListNode* prev = *pphead;
		while (prev->next != pos)
		{
			prev = prev->next;
		}
		prev->next = pos->next;
		delete(pos);
	}
}
//释放链表
void listDestory(ListNode** pphead)
{
	ListNode* cur = *pphead;
	while (cur)
	{
		ListNode* next = cur->next;
		delete(cur);
		cur = next;
	}
	*pphead = NULL;
}

void testPushback()
{
	ListNode* phead = NULL;
	for (int i = 1; i < 9; i++)
	{
		Listpushback(&phead, i);
	}

	Listprintf(phead);
}

void testPushfront() {
	ListNode* phead = NULL;
	for (int i = 1; i < 6; i++)
	{
		Listpushback(&phead, i);
	}

	for (int i = 0; i < 3; i++)
	{
		ListPushfront(&phead, 8);
	}

	Listprintf(phead);

}

void testPopBack() {
	ListNode* phead = NULL;
	for (int i = 1; i < 6; i++)
	{
		Listpushback(&phead, i);
	}

	for (int i = 0; i < 3; i++)
	{
		ListPushfront(&phead, 8);
	}

	for (int i = 0; i < 3; i++)
	{
		listPopBack(&phead);
	}
	Listprintf(phead);

}

void testPopFront() {
	ListNode* phead = NULL;
	for (int i = 1; i < 6; i++)
	{
		Listpushback(&phead, i);
	}

	for (int i = 0; i < 3; i++)
	{
		ListPushfront(&phead, 8);
	}

	for (int i = 0; i < 3; i++)
	{
		listPopFront(&phead);
	}
	Listprintf(phead);

}

void testListFind() {
	ListNode* phead = NULL;
	for (int i = 1; i < 6; i++)
	{
		Listpushback(&phead, i);
	}

	for (int i = 0; i < 3; i++)
	{
		ListPushfront(&phead, 8);
	}

	ListNode* cur = listFind(phead, 3);
	if (cur != NULL)
	{
		cur->data = 20;
	}
	Listprintf(phead);

}

void testListInsertFront() { 

	ListNode* phead = NULL;
	for (int i = 1; i < 6; i++)
	{
		Listpushback(&phead, i);
	}

	for (int i = 0; i < 3; i++)
	{
		ListPushfront(&phead, 8);
	}
	ListNode* pos = listFind(phead,3);
	listInsertFront(&phead, pos, 88889999);

	Listprintf(phead);
}

void testListInsertBack() {

	ListNode* phead = NULL;
	for (int i = 1; i < 6; i++)
	{
		Listpushback(&phead, i);
	}

	for (int i = 0; i < 3; i++)
	{
		ListPushfront(&phead, 8);
	}
	ListNode* pos = listFind(phead, 3);
	listInsertBack(&phead, pos, 88889999);

	Listprintf(phead);
}

void testListErase() {
	ListNode* phead = NULL;
	for (int i = 1; i < 6; i++)
	{
		Listpushback(&phead, i);
	}

	for (int i = 0; i < 3; i++)
	{
		ListPushfront(&phead, 8);
	}
	ListNode* pos = listFind(phead, 3);
	listErase(&phead, pos);

	Listprintf(phead);

}

int main()
{
	//testPushback();
	//testPushfront();
	//testPopBack();
	//testPopFront();
	//testListFind();
	//testListInsertFront();
	//testListInsertBack();
	testListErase();

	return 0;
}

#endif // 1

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值