纯手工打造C++单链表,实现增删改查等基本功能

一直以来,对于数据结构都是后怕,但还是要啃下这骨头的:

#pragma once

/*
* Copyright (c) 侯文斌
* All rights reserved.
* 
* 作 者:houwenbin1986@gmail.com
* 摘 要:链表的操作
*/

//带头节点的链表
class LinkedList
{
	struct Node
	{
	public:
		int data;
		Node* prev;
		Node* next;
	public:
		Node(){
			data = -1;
			prev = nullptr;
			next = nullptr;
		}
	};
public:
	LinkedList();
	~LinkedList();

public:
	void Reset();
	bool isEmpty();
	void Insert(int data, int key=-1);
	void Delete(int data);
	void Modify(int key, int data);
	Node* Find(int data);

	void Print();//打印链表

private:
	Node* head;
	Node* tail;
};

#include "LinkedList.h"
#include <iostream>


LinkedList::LinkedList()
{
	head = new Node();

	std::cout << sizeof(head) << " " << sizeof(Node) << std::endl;//x64指针8字节

	tail = head;//增加尾指针,方便追加
}

LinkedList::~LinkedList()
{
	Reset();
	if (head){
		delete head;
		head = nullptr;
	}
	tail = nullptr;
}

void LinkedList::Reset()
{
	Node* pNode = head->next;
	while (pNode)
	{
		Node* pTemp = pNode;
		pNode = pNode->next;
		//注意顺序!!!
		delete pTemp;
		pTemp = nullptr;
	}
}

bool LinkedList::isEmpty()
{
	if (head && head->next == nullptr){
		return true;
	}
	return false;
}

void LinkedList::Insert(int data, int key)
{
	Node* pTemp = tail;
	if (key == -1){//追加
		pTemp = tail;
	}
	else {//指定位置
		pTemp = Find(key);
                pTemp->prev = nullptr;//重置标识
	}

	Node* pNew = new Node();//新增节点
	pNew->data = data;
	pNew->next = nullptr;

	if (key == -1){//追加
		pTemp->next = pNew;
		tail = pNew;//尾指向最新节点
	}
	else{//插入
		pNew->next = pTemp->next;
		pTemp->next = pNew;
	}
}

void LinkedList::Delete(int data)
{
	Node* pNode = Find(data);
	if ( pNode ){
		//前一个位置怎么找,查找时通过pNode->prev返回
		pNode->prev->next = pNode->next;//删除操作
		//回收空间
		delete pNode; pNode = nullptr;
	}
}

void LinkedList::Modify(int key, int data)
{
	Node* pNode = Find(key);
	if (pNode){
		pNode->data = data;
                pTemp->prev = nullptr;//重置标识
	}
}

LinkedList::Node* LinkedList::Find(int data)
{
	Node* pTemp = head->next;
	Node* pPrev = head;
	while ( pTemp )
	{
		if (pTemp->data == data){
			break;
		}
		pPrev = pTemp;
		pTemp = pTemp->next;
	}
	pTemp->prev = pPrev;//记录该节点前一节点
	return pTemp;
}

void LinkedList::Print()
{
	Node* pTemp = head->next;
	while ( pTemp )
	{
		std::cout << pTemp->data << " ";
		pTemp = pTemp->next;
	}
	std::cout << std::endl;
}


int main(void)
	//链表的操作
	std::cout << "-----链表操作演示-----" << std::endl;
	LinkedList directList;
	std::cout << sizeof(directList) << " empty:" << directList.isEmpty() << std::endl;
	directList.Insert(1);
	directList.Insert(3);
	directList.Insert(5);
	directList.Insert(7);

	std::cout << "empty:" << directList.isEmpty() << std::endl;

	directList.Print();
	directList.Insert(10,3);//在节点3后面添加10
	directList.Print();

	directList.Modify(7,9);//修改节点
	directList.Print();

	directList.Delete(5);//删除节点
	directList.Print();

	system("pause");
	return 0;
}

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值