#include <iostream>
using namespace std;
typedef int nodeType;
struct tlist
{
nodeType data;
tlist * next;
tlist();
~tlist();
};
tlist::tlist()
{
data = -1;
next = NULL;
}
tlist::~tlist()
{
tlist * temp;
while (next)
{
temp = next;
next = next->next;
delete temp;
}
}
class tlistManager
{
public:
tlistManager();
~tlistManager();
bool insert(nodeType num);
bool deleteNode(nodeType num,bool duoge=false);
tlist * findNode(nodeType num);
tlist * findNodeParent(nodeType num);
void print();
int getSize();
public:
tlist * m_root; //头结点
tlist * m_curNode; //最后一个结点
int length; //长度
};
tlistManager::tlistManager()
{
m_root = NULL;
m_curNode = NULL;
length = 0;
}
tlistManager::~tlistManager()
{
delete m_root;
}
bool tlistManager::insert(nodeType num)
{
//链表为空时
if (!m_curNode)
{
tlist *temp = new tlist;
temp->data = num;
m_root = temp;
m_curNode = temp;
}else
{
tlist *temp = new tlist;
temp->data = num;
m_curNode->next = temp;
m_curNode = temp;
}
length++;
return true;
}
//是否删除多个结点
bool tlistManager::deleteNode(nodeType num,bool duoge)
{
tlist * chrid = findNode(num);
tlist * parent = findNodeParent(num);
tlist * tempNode = NULL;
int size = 0;
while (chrid)
{
tempNode = chrid;
if (parent)
{
parent->next = chrid->next;
//如果是删除尾结点
m_curNode = chrid == m_curNode? parent:m_curNode;
}else
{
//如果是删除头结点
m_root = m_root == chrid?chrid->next:m_root;
}
tempNode->next = NULL;
delete tempNode;
tempNode= NULL;
length --;
size++;
//是否需要删除多个
if (duoge)
{
chrid = findNode(num);
parent = findNodeParent(num);
}else
{
return true;
}
}
if (size)
{
return true;
}
return false;
}
tlist * tlistManager::findNode(nodeType num)
{
//返回第一个符合的结点
tlist * temp = m_root;
while (temp)
{
if (temp->data == num)
{
return temp;
}
temp = temp->next;
}
return NULL;
}
tlist * tlistManager::findNodeParent(nodeType num)
{
//如果是头结点
if (m_root->data == num)
{
return NULL;
}
tlist * temp = m_root;
while (temp && temp->next)
{
if (temp->next->data == num)
{
return temp;
}
temp = temp->next;
}
return NULL;
}
void tlistManager::print()
{
tlist * temp = m_root;
while (temp)
{
cout<<temp->data<<" ";
temp = temp->next;
}
cout<<"链表总长度为:"<<length<<endl;
}
int tlistManager::getSize()
{
return length;
}
int main()
{
tlistManager * m_list = new tlistManager;
for (int i= 0;i<10;++i)
{
m_list->insert(i);
m_list->insert(i);
}
m_list->print();
m_list->deleteNode(0,true);
m_list->deleteNode(1);
m_list->deleteNode(2,true);
m_list->deleteNode(7);
m_list->deleteNode(8);
m_list->deleteNode(9,true);
m_list->print();
for (int i= 11;i<20 ;++i)
{
m_list->insert(i);
}
m_list->print();
return 0;
}
using namespace std;
typedef int nodeType;
struct tlist
{
nodeType data;
tlist * next;
tlist();
~tlist();
};
tlist::tlist()
{
data = -1;
next = NULL;
}
tlist::~tlist()
{
tlist * temp;
while (next)
{
temp = next;
next = next->next;
delete temp;
}
}
class tlistManager
{
public:
tlistManager();
~tlistManager();
bool insert(nodeType num);
bool deleteNode(nodeType num,bool duoge=false);
tlist * findNode(nodeType num);
tlist * findNodeParent(nodeType num);
void print();
int getSize();
public:
tlist * m_root; //头结点
tlist * m_curNode; //最后一个结点
int length; //长度
};
tlistManager::tlistManager()
{
m_root = NULL;
m_curNode = NULL;
length = 0;
}
tlistManager::~tlistManager()
{
delete m_root;
}
bool tlistManager::insert(nodeType num)
{
//链表为空时
if (!m_curNode)
{
tlist *temp = new tlist;
temp->data = num;
m_root = temp;
m_curNode = temp;
}else
{
tlist *temp = new tlist;
temp->data = num;
m_curNode->next = temp;
m_curNode = temp;
}
length++;
return true;
}
//是否删除多个结点
bool tlistManager::deleteNode(nodeType num,bool duoge)
{
tlist * chrid = findNode(num);
tlist * parent = findNodeParent(num);
tlist * tempNode = NULL;
int size = 0;
while (chrid)
{
tempNode = chrid;
if (parent)
{
parent->next = chrid->next;
//如果是删除尾结点
m_curNode = chrid == m_curNode? parent:m_curNode;
}else
{
//如果是删除头结点
m_root = m_root == chrid?chrid->next:m_root;
}
tempNode->next = NULL;
delete tempNode;
tempNode= NULL;
length --;
size++;
//是否需要删除多个
if (duoge)
{
chrid = findNode(num);
parent = findNodeParent(num);
}else
{
return true;
}
}
if (size)
{
return true;
}
return false;
}
tlist * tlistManager::findNode(nodeType num)
{
//返回第一个符合的结点
tlist * temp = m_root;
while (temp)
{
if (temp->data == num)
{
return temp;
}
temp = temp->next;
}
return NULL;
}
tlist * tlistManager::findNodeParent(nodeType num)
{
//如果是头结点
if (m_root->data == num)
{
return NULL;
}
tlist * temp = m_root;
while (temp && temp->next)
{
if (temp->next->data == num)
{
return temp;
}
temp = temp->next;
}
return NULL;
}
void tlistManager::print()
{
tlist * temp = m_root;
while (temp)
{
cout<<temp->data<<" ";
temp = temp->next;
}
cout<<"链表总长度为:"<<length<<endl;
}
int tlistManager::getSize()
{
return length;
}
int main()
{
tlistManager * m_list = new tlistManager;
for (int i= 0;i<10;++i)
{
m_list->insert(i);
m_list->insert(i);
}
m_list->print();
m_list->deleteNode(0,true);
m_list->deleteNode(1);
m_list->deleteNode(2,true);
m_list->deleteNode(7);
m_list->deleteNode(8);
m_list->deleteNode(9,true);
m_list->print();
for (int i= 11;i<20 ;++i)
{
m_list->insert(i);
}
m_list->print();
return 0;
}
本文介绍了一个简单的单链表管理类的实现,包括插入、删除节点等基本操作,并通过示例展示了如何使用该类进行链表操作。
1106

被折叠的 条评论
为什么被折叠?



