无头单向非循环链表 ~ 程序实现二
本篇博客基于 温故而知新 -> 数据结构 -> 线性表 -> 链表 中的理论知识,并利用 C++
中的 类 对数据结构中的 无头单向非循环链表 进行代码实现!
其中涉及了链表的 增(头插、尾插、任插)删(头删、尾删、任删)查(节点寻找)改(没写(~ ̄▽ ̄)~),销毁,打印等操作!并附带了实例以及对应的运行结果!
具体内容如下:
(1)List.h
#pragma once
#include<iostream>
using namespace std;
#include<assert.h>
typedef int LTDataType;
typedef struct LTNode
{
LTDataType _val;
struct LTNode* _next;
};
LTNode* createLTNode(LTDataType val)
{
struct LTNode *node = (LTNode*)malloc(sizeof(LTNode));
node->_val = val;
node->_next = nullptr;
return node;
}
class List
{
public:
List() :_head(nullptr)
{}
// 单链表打印
void LTPrint();
// 单链表尾插
void PushBack(LTDataType val);
// 单链表的尾删
void PopBack();
// 单链表的头插
void PushFront(LTDataType val);
// 单链表头删
void PopFront();
// 单链表任插
void Insert(LTNode *node, LTDataType val);
// 单链表任删
void EraseAfter(LTNode *node);//删除所给节点的下一级节点
// 单链表查找
LTNode* Find(LTDataType val);
// 单节点显示
void print(LTNode *node);
// 销毁链表
void Destory();
//删除某一结点
void Erase(LTNode *node);
private:
LTNode *_head;
};
(2)main.cpp
#include"List.h"
// 单链表打印
void List::LTPrint()
{
assert(_head != NULL);
LTNode *begin = _head;
cout << "链表内容:";
while (begin != NULL)
{
cout << begin->_val << " ";
begin = begin->_next;
}cout << endl;
}
// 单链表尾插
void List::PushBack(LTDataType val)
{
LTNode *newNode = createLTNode(val);
if (_head == NULL)
{
_head = newNode;
}
else
{
LTNode *end = _head;
while (end->_next != NULL)
end = end->_next;
end->_next = newNode;
}
}
// 单链表的尾删
void List::PopBack()
{
assert(_head != NULL);
LTNode *prev = NULL;
LTNode *end = _head;
while (end->_next != NULL)
{
prev = end;
end = end->_next;
}
free(end);
if (prev == NULL) //相当于只有一个节点,也可换为 _head->next == NULL
_head = NULL;
else
prev->_next = NULL;
}
// 单链表的头插
void List::PushFront(LTDataType val)
{
assert(_head != NULL);
if (_head == NULL)
_head->_next = createLTNode(val);
else
{
LTNode *next = _head;
_head = createLTNode(val);
_head->_next = next;
}
}
// 单链表头删
void List::PopFront()
{
assert(_head != NULL);
LTNode *next = _head->_next;
free(_head);
_head = next;
}
// 单链表任插
void List::Insert(LTNode *node, LTDataType val)//为所给节点的下一节点插入新数据
{
assert(node != NULL && _head != NULL);
LTNode *next = node->_next;
node->_next = createLTNode(val);
node->_next->_next = next;
}
// 单链表任删
void List::EraseAfter(LTNode *node)//删除所给节点的下一级节点
{
assert(node != NULL && _head != NULL);
if (node->_next == NULL)
return;
else
{
LTNode *next = node->_next;
node->_next = next->_next;
free(next);
}
}
// 单链表查找
LTNode* List::Find(LTDataType val)
{
assert(_head != NULL);
LTNode *anw = _head;
while (anw != NULL)
{
if (anw->_val == val)
return anw;
anw = anw->_next;
}
return NULL;
}
// 单节点显示
void List::print(LTNode *node)
{
assert(node != NULL && _head != NULL);
cout << node->_val << endl;
}
// 销毁链表
void List::Destory()
{
assert(_head != NULL);
/*LTNode *cur = _head;
LTNode *next = cur->_next;
while (cur->_next)
{
free(cur);
cur = next;
next = next->_next;
}
free(cur);
cur = NULL;
_head = NULL;*/
LTNode *cur = _head;
while (cur)
{
LTNode *next = cur->_next;
free(cur);
cur = next;
}
_head = NULL;
cout << "销毁结束!" << endl;
}
//删除某一结点
void List::Erase(LTNode *node)
{
assert(node != NULL && _head != NULL);
LTNode *prev = _head;
while (prev != NULL)
{
if (prev != node) // 所查找节点不是第一个节点
{
if (prev->_next == node)
{
prev->_next = node->_next;
free(node);
break;
}
else
prev = prev->_next;
}
else //所查找节点是第一个节点
{
LTNode *next = _head->_next;
free(_head);
_head = next;
break;
}
}
}
void test()
{
List l1;
/* 实验尾插 */
l1.PushBack(1);
l1.PushBack(2);
l1.PushBack(2);
l1.PushBack(3);
l1.LTPrint(); // 结果为 1 2 2 3
/* 实验尾删 */
l1.PopBack();
l1.LTPrint(); // 1 2 2
l1.PopBack();
l1.LTPrint(); // 1 2
l1.PopBack();
l1.LTPrint(); // 1
/* 实验头插 */
l1.PushFront(2);
l1.PushFront(3);
l1.PushFront(4);
l1.LTPrint(); // 4 3 2 1
/* 实验头删 */
l1.PopFront();
l1.LTPrint(); // 3 2 1
//l1.PopFront();
//l1.LTPrint(); // 2 1
//l1.PopFront();
//l1.LTPrint(); // 1
/* 实验查找 */
LTNode *cur = l1.Find(2);
l1.print(cur);
l1.Insert(cur, 666);//3 2 666 1
l1.Insert(cur, 777);//3 2 777 666 1
l1.LTPrint();
l1.EraseAfter(cur);
l1.LTPrint(); // 3 2 666 1
/* 实验删除某一结点 */
l1.Erase(cur);
l1.LTPrint(); // 3 666 1
LTNode *head = l1.Find(3);
l1.Erase(head);
l1.LTPrint(); // 666 1
/* 实验销毁 */
l1.Destory();
}
int main()
{
test();
system("pause");
return 0;
}
(3)运行结果