C/C++用链表简单实现

参考链接:https://blog.csdn.net/swag_wg/article/details/89673850

链表快速入门

很久没学了,先写一个快速入门给以后的自己查阅。
语法上简述:往结构体内部插入结构体指针就是链表

写法一
struct node1
{
	int date;
	struct node1* next1;
}Node1, * Link1,node[10];
//实际上述代码可拆解为
//1.struct node Node;
//2.struct node* Link;
//3.struct node node[10];

写法二
typedef struct node2
{
	int data;
	struct node2* next2;
}Node2, * Link2;
//实际上述代码可拆解为
//1.typedef struct node Node;
//2.typedef struct node* Link;

如上只需要注意一点:既定义结构体同时又创建一个结构体变量不能用typedef
为了自己方便阅读代码,一般不 typedef 结构体指针

  • 如何访问:指针用 ->,结构体数组用 . (小数点)

函数:初始化,插入,删除,查找,遍历,释放

其实一般不去记代码具体如何实现,每个人的方法不同
也不用全实现下面的代码,但代码还是自己敲自己去实现才是真的能巩固。
在这里插入图片描述

实现单链表代码

stack.cpp

#include <iostream>
#include "Stack.h"
using namespace std;

Link::Link()
{
	this->head = nullptr;
}
Link::~Link()
{}
Node::Node()
{
	this->next = nullptr;
	this->num = 0;
}
Node::~Node()
{}
void Link::initLink(Node* newnode)
{
	head = newnode;
	head->next = nullptr;
}
Node* Link::getNode(int index)
{
	Node* ptr = this->head;
	for (int i = 0; i < index; i++)
	{
		ptr = ptr->next;
	}
	return ptr;
}
void Link::insertHeadNode(int num)
{
	Node* ptr = new Node;
	ptr->num = num;
	ptr->next = head;
	head = ptr;
}
void Link::insertMidNode(int index, int num)
{
	Node* ptr = getNode(index);
	ptr->num = num;
}
void Link::insertTailNode(int num)
{
	int len = getLen();
	Node* ptr = getNode(len);
	ptr->num = num;
}

bool Link::isEmpty()
{
	if (head->next == nullptr)
		return true;
	return false;
}
void Link::deleteNode(int num)
{
	if (isEmpty())
	{
		cout << "链表为空,删除失败" << endl;
		return;
	}
	Node* ptr = head;
	int count = 0;
	while (ptr->num == num)
	{
		ptr = ptr->next;
		count++;
	}
	if (ptr == head->next)
	{
		head->next = ptr->next;
		delete ptr;
	}
	else if (ptr->next == nullptr)
	{
		int len = getLen();
		Node* a = getNode(len);
		a->next = nullptr;
		delete ptr;
	}
	else
	{
		Node* b = getNode(count);
		b->next = ptr->next;
		delete ptr;
	}
}

void Link::display()
{
	Node* ptr = this->head;
	int len = getLen();
	for(int i=0;i<len;i++)
	{
		cout << ptr->num << endl;
		ptr = ptr->next;
	}
}

void Link::reverse()
{
	Node* ptr = new Node;
	int i = getLen();
	while(1)
	{
		ptr = getNode(i-1);
		i--;
		if (i < 0)
			break;
		cout << ptr->num << endl;
	}
}

int Link::getLen()
{
	int len = 0;
	Node* ptr = this->head;
	while (ptr->next!=nullptr)
	{
		ptr = ptr->next;
		len++;
	}
	return len+1;
}

int main(void)
{
	Link link;
	Node* head = new Node;
	head->next = nullptr;
	head->num = 0;
	link.initLink(head);//不显示
	link.insertHeadNode(1);
	link.insertHeadNode(2);
	link.insertHeadNode(3);
	link.display();
	cout << endl;
	link.reverse();
	cout << endl;
	link.deleteNode(3);//删除第三个结点
	link.display();
	return 0;
}

Stack.h

#pragma once

class Node
{
public:
    int num;
    Node* next;
    Node();
    ~Node();
};

class Link
{
public:
    Link();
    void initLink(Node* newnode);
    void insertHeadNode(int num);
    void insertMidNode(int index, int num);
    void insertTailNode(int num);
    Node* getNode(int index);
    bool isEmpty();
    void deleteNode(int num);
    void display();
    void reverse();
    int getLen();
    ~Link();
private:
    Node* head;
};

猜你喜欢

转载自blog.csdn.net/GameStrategist/article/details/107530422