c++实现单链表的操作

20 篇文章 0 订阅
15 篇文章 0 订阅
#include <iostream>
using namespace std;
#define NULL 0
class Node  //节点类
{
public:
Node(){};//构造函数
Node(int n){ num = n; next = NULL; }//构造函数
Node(int n, Node *p) { num = n; next = p; }
void setNum(int n){ num = n; }//数据和指针两个属性分开操作
void setNext(Node *p){ next = p; }
int getNum(){ return num; }
Node *getNext(){ return next; }
//~Node();


private:
int num;
Node *next;
};


class Linklist//链表类
{
public :
Linklist(){ head = NULL; }
Linklist(int n){ head = new Node(n); }


void addAtEnd(int n); //数据和指针分开操作
void addAtEnd(Node *p);
void visitAllNode();
void insert(Node *p, int num);
void deleteNum(int num);


private:
Node *head;
};


void Linklist::addAtEnd(int n)//若head=null就是采用尾插法创建单链表,也可以采用头插法。
{
Node *p = head;//p指向链表
if (head == NULL)//空链表
{
head = new Node(n);
}
else
{
while (p->getNext()!=NULL)
{
           p = p->getNext();
}
p->setNext(new Node(n));//表尾插入元素值。
}
}


void Linklist::addAtEnd(Node *p) //表尾设置指针。比c看起来复杂繁琐些,
//这就是面相对像编程的思想精髓。但复杂问题这种思想的好处就体现出来了
{
Node *pn = head;
if (head == NULL){ head = p; }
else
{
while (pn->getNext() != NULL)
{
pn = pn->getNext();
}
pn->setNext(p);
}
}


void Linklist::visitAllNode()//遍历链表
{
Node *p;
p = head;
if (p == NULL)
{
cout << "空链表!" << endl;
}
else
{
cout << "表:" << endl;
while (p->getNext() != NULL)
{
cout << p->getNum() << "->";
p = p->getNext();
}
cout << p->getNum() << endl;
}
}


void Linklist::insert(Node *p, int num)
{
Node *pn = head, *follow = pn;
if (pn == NULL)
{
cout << "空链表,插入节点" << endl;
head = p;
return;
}
while (pn->getNum() != num&&pn->getNext() != NULL)
{
follow = pn;//follow始终指向pn节点
pn = pn->getNext();
}
if (pn->getNum() == num&&pn == head)//如果是头结点
{
p->setNext(pn); //新节点的指针域指向pn
head = p;
}
else if (pn->getNum()==num)
{
p->setNext(pn);
follow->setNext(p);
}
else
{
cout << "没有找到,插入链表尾部。" << endl;
pn->setNext(p);
}
}


void Linklist::deleteNum(int num)
{
Node *p = head, *follow = p;
if (p->getNum() == num)     //删除的节点为第一个节点
{
head = p->getNext();  //下一个节点成为头指针
delete p;
}
else
{
while (p->getNext()!=NULL&&p->getNum()!=num)
{
follow = p;//follow一直指向p,不管p指向的节点如何变化
p = p->getNext();
}
if (p->getNum() == num)
{
follow->setNext(p->getNext());
delete p;
}
}
}


void main()
{
Linklist *pl = NULL;
int num, n;
//Node *p;
char option;
while (1)
{
cout << "链表操作练习:\n" << endl;
cout << "1:创建链表" << endl;
cout << "2:在链表尾部插入节点" << endl;
cout << "3:在节点num的节点前插入新节点" << endl;
cout << "4:删除节点" << endl;
cout << "5:遍历节点" << endl;
cout << "0:退出" << endl;
cout << "请输入你的选择:\n" << endl;
cin >> option;


switch (option)
{
case '0':
break;
case '1':
cout << "请输入第一个节点的值," << endl;
cin >> num;
pl = new Linklist(num);
pl->visitAllNode();
break;
case '2':
cout << "请输入要尾插的节点值:" << endl;
cin >> num;
if (pl == NULL){ pl = new Linklist(num); }
else
{
pl->addAtEnd(num);
}
pl->visitAllNode();
break;
case '3':
cout << "请输入插入的节点值:";
cin >> num;
cout << "请输入要插入到哪个节点前:";
cin >> n;
pl->insert(new Node(num), n);
pl->visitAllNode();
break;
case '4':
cout << "请输入要删除的节点:";
cin >> num;
pl->deleteNum(num);
pl->visitAllNode();
break;
case '5':
pl->visitAllNode();
break;
default:
cout << "你输入错误,请重新输入!" << endl;
}
if (option == '0')
break;
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值