typedef int ElemType;
typedef struct LNode
{
ElemType data;
LNode* next;
}LNode;
class LinkList
{
private:
LNode* head;
public:
LinkList();
~LinkList();
int deletelist(int x);
void CreatList_h(int n);
void print_evennode();
void divide_LinkList(LNode*& heada, LNode*& headb);
void print();
LNode* Gethead()
{
return head;
}
};
LinkList::LinkList()//创建空表
{
head = new LNode;
head->next = NULL;
}
void LinkList::CreatList_h(int n)//头插法创建n个元素的线性表
{
LNode* s;
int i = 0;
for (i = 0; i < n; i++)
{
s = new LNode;
cin >> s->data;
s->next = head->next;
head->next = s;
}
}
int LinkList::deletelist(int x)//一个带头结点的单链表存放一组整数,设计一个算法删除值等于x的所有结点(成功return 1,失败return 0)
{
LNode* pr = head, * p = head->next;
int tag = 0;
while (p)
{
if (p->data != x)
{
pr = p;
p = p->next;
}
else
{
pr->next = p->next;
delete p;
p = pr->next;
tag = 1;
}
}
if (tag == 1)
return 1;
else
return 0;
}
void LinkList::print_evennode()//计算线性表中值为偶数的结点的个数
{
LNode* p = head->next;
int count = 0;
while (p)
{
if (p->data % 2 == 0)
count++;
p = p->next;
}
cout << "该链表中偶数结点的个数为:" << count << endl;
}
void LinkList::divide_LinkList(LNode*& heada, LNode*& headb)//一个带头结点的单链表heada存放一组整数表,设计分裂heada的算法,偶数在heada中,奇数存放在headb中
{
LNode* pr = heada, * p = heada->next;
LNode* pb = headb;
while (p)
{
if (p->data % 2 == 0)
{
pr = p;
p = p->next;
}
else
{
pb->next = p;
pb = pb->next;
pr->next = p->next;
p = p->next;
}
}
pb->next = NULL;
}
void LinkList::print()//打印链表
{
LNode* p;
p = head->next;
while (p)
{
cout << p->data << " ";
p = p->next;
}
cout << endl;
}
#define _CRT_SECURE_NO_WARNINGS 1
//单链表的基本操作
#include
using namespace std;
typedef int ElemType;
typedef struct LNode
{
ElemType data;
LNode* next;
}LNode;
class LinkList
{
private:
LNode* head;
public:
LinkList();
~LinkList();
int deletelist(int x);
void CreatList_h(int n);
void print_evennode();
void divide_LinkList(LNode*& heada, LNode*& headb);
void print();
LNode* Gethead()
{
return head;
}
};
LinkList::LinkList()//创建空表
{
head = new LNode;
head->next = NULL;
}
LinkList::~LinkList()
{
LNode* p = head;
while (head)
{
p = head;
head = head->next;
delete p;
}
}
void LinkList::CreatList_h(int n)//头插法创建n个元素的线性表
{
LNode* s;
int i = 0;
for (i = 0; i < n; i++)
{
s = new LNode;
cin >> s->data;
s->next = head->next;
head->next = s;
}
}
int LinkList::deletelist(int x)//一个带头结点的单链表存放一组整数,设计一个算法删除值等于x的所有结点(成功return 1,失败return 0)
{
LNode* pr = head, * p = head->next;
int tag = 0;
while (p)
{
if (p->data != x)
{
pr = p;
p = p->next;
}
else
{
pr->next = p->next;
delete p;
p = pr->next;
tag = 1;
}
}
if (tag == 1)
return 1;
else
return 0;
}
void LinkList::print_evennode()//计算线性表中值为偶数的结点的个数
{
LNode* p = head->next;
int count = 0;
while (p)
{
if (p->data % 2 == 0)
count++;
p = p->next;
}
cout << "该链表中偶数结点的个数为:" << count << endl;
}
void LinkList::divide_LinkList(LNode*& heada, LNode*& headb)//一个带头结点的单链表heada存放一组整数表,设计分裂heada的算法,偶数在heada中,奇数存放在headb中
{
LNode* pr = heada, * p = heada->next;
LNode* pb = headb;
while (p)
{
if (p->data % 2 == 0)
{
pr = p;
p = p->next;
}
else
{
pb->next = p;
pb = pb->next;
pr->next = p->next;
p = p->next;
}
}
pb->next = NULL;
}
void LinkList::print()//打印链表
{
LNode* p;
p = head->next;
while (p)
{
cout << p->data << " ";
p = p->next;
}
cout << endl;
}
int main()
{
LinkList L,B;
LNode* HeadA = L.Gethead();
LNode* HeadB = B.Gethead();
L.CreatList_h(10);
cout << "该线性表为:";
L.print();
L.print_evennode();
L.deletelist(4);
cout << "删除值为4的结点后线性表为:";
L.print();
cout << "未分裂前的链表:";
L.print();
L.divide_LinkList(HeadA, HeadB);
cout << "分裂后的链表:" << endl;
L.print();
B.print();
return 0;
}
好啦,关于单链表的小练习到这里就结束啦,后期会继续更新数据结构与算法的相关知识,欢迎大家持续关注、点赞和评论!❤️❤️❤️