封装的单链表方法
typedef struct ListNode
{
ListNode *next;
int data;
}node;
class list
{
public:
list();
~list();
//构造节点时使用尾插法
void CreatNode(int *arr, int n)const;
bool IsEmpty()const;
//获取节点的长度
int ListLenth()const;
void DispList()const;
//获取链表中第index节点的值
bool GetElem(int index, int &num)const;
//查找第一个与num相等的节点位置
int LocateElem(const int num)const;
bool Insert(const int index, const int num);
bool Delete(const int index);
//将一个链表拆分为两个,此方法会改变原链表的指向
void Split(ListNode **lhs, ListNode **rhs)const;
//删除链表中最大节点
void DelMaxNode();
//对链表进行排序,通过构造只含一个数据节点的有序表进行排序(直插法)
void Sort() const;
private:
ListNode *m_head;
};
各方法的实现
#include "ListNode.h"
#include <iostream>
list::list()
{
m_head = new ListNode;
m_head->next = nullptr;
}
list::~list()
{
while (m_head)
{
ListNode *tmp = m_head;
m_head = m_head->next;
delete tmp;
}
}
void list::CreatNode(int* arr, int n)const
{
//采用尾插法进行链表的生成
ListNode *endNode = m_head;
for (int i = 0; i < n; ++i)
{
ListNode *newNode = new ListNode;
newNode->data = arr[i];
endNode->next = newNode;
endNode = newNode;
}
endNode->next = nullptr;
}
bool list::IsEmpty() const
{
return m_head->next == nullptr;
}
int list::ListLenth() const
{
int len = 0;
ListNode *tmp = m_head;
while (tmp->next)
{
++len;
tmp = tmp->next;
}
return len;
}
void list::DispList() const
{
ListNode *tmp = m_head->next;
while (tmp)
{
std::cout << tmp->data << " ";
tmp = tmp->next;
}
std::cout << std::endl;
}
bool list::GetElem(int index, int& num)const
{
bool ret = false;
ListNode *tmp = m_head;
for (int i = 0; i < index; ++i)
{
tmp = tmp->next;
}
if (tmp)
{
num = tmp->data;
ret = true;
}
return ret;
}
int list::LocateElem(const int num) const
{
int ret = 0, index = 0;
ListNode *tmp = m_head;
while (tmp && tmp->data != num)
{
tmp = tmp->next;
++index;
}
if (tmp)
ret = index;
return ret;
}
bool list::Insert(const int index, const int num)
{
bool ret = false;
ListNode *tmp = m_head;
for (int i = 0; i < index - 1; ++i)
{
tmp = tmp->next;
}
if(tmp)
{
ret = true;
ListNode *newNode = new ListNode;
newNode->data = num;
newNode->next = tmp->next;
tmp->next = newNode;
}
return ret;
}
bool list::Delete(const int index)
{
bool ret = false;
ListNode *tmp = m_head;
for (int i = 0; i < index - 1; ++i)
{
tmp = tmp->next;
}
if (tmp)
{
ListNode *nextNode = tmp->next;
if(tmp->next)
{
ret = true;
tmp->next = nextNode->next;
delete nextNode;
nextNode = nullptr;
}
}
return ret;
}
void list::Split(ListNode** lhs, ListNode** rhs) const
{
ListNode *tmp = m_head->next, *nextNode;
*lhs = m_head;
ListNode *lhsTmp = *lhs;
*rhs = new ListNode;
ListNode *rhsTmp = *rhs;
while (tmp)
{
lhsTmp->next = tmp;
lhsTmp = tmp;
tmp = tmp->next;
nextNode = tmp->next;
rhsTmp->next = tmp;
rhsTmp = tmp;
tmp = nextNode;
}
lhsTmp->next = nullptr;
rhsTmp->next = nullptr;
}
void list::DelMaxNode()
{
ListNode *tmp = m_head->next;
ListNode *prev = m_head;
ListNode *maxNode = tmp;
ListNode *maxPrev = prev;
while (tmp)
{
if (maxNode->data < tmp->data)
{
maxNode = tmp;
maxPrev = prev;
}
prev = tmp;
tmp = tmp->next;
}
maxPrev->next = maxNode->next;
delete maxNode;
}
void list::Sort() const
{
ListNode *tmp = m_head->next->next;
m_head->next->next = nullptr;
while (tmp)
{
ListNode *nextNode = tmp->next;
ListNode *prev = m_head;
while (prev->next && prev->next->data < tmp->data)
{
prev = prev->next;
}
tmp->next = prev->next;
prev->next = tmp;
tmp = nextNode;
}
}