单链表的增删查改(C++实现)

Slist.h:

#pragma once 



#include<iostream>
using namespace std;


typedef int DataType;


struct Node
{
Node(const DataType& d)
:_date(d)
, _next(NULL)
{}


DataType _date;
struct Node* _next;
};


class Slist
{
public:
friend ostream& operator<<(ostream& os, Slist& s);//重载<< 输出


Slist()//构造
:_head(NULL)//头
, _tail(NULL)//尾
{}


~Slist()//析构
{
if (_head == NULL)
return;
Node* cur = _head;
while (cur->_next != NULL)
{
Node* del = cur;
cur = cur->_next;
delete del;
}
delete cur;
_head = NULL;
_tail = NULL;
}
public:
void PushBack(const DataType& d);//尾插
void PopBack();//尾删
void PushFront(const DataType& d);//头插
void PopFront();//头删
Node* Find(DataType x);//查找d的位置
void Insert(Node* pos, DataType x);//在pos插入一个节点
int  GetLinkNode();//获取节点数
void  Remove(DataType x);//删除链表中的第一个x
void  RemoveAll(DataType x);//删除链表中所有的x


private:
Node* _head;//头指针
Node* _tail;//尾指针

};


Teat.cpp:


#define _CRT_SECURE_NO_WARNINGS 1
#include "Slist.h"
#include<stdlib.h>


int main()
{
Slist slist1;
slist1.PushBack(1);
slist1.PushBack(2);
slist1.PushBack(3);
slist1.PushBack(3);
slist1.PushBack(4);
cout << slist1 << endl;
//slist1.PopBack();
//cout << slist1 << endl;
/*slist1.PushFront(1);
cout << slist1 << endl;
slist1.PopFront();
cout << slist1 << endl;*/
//slist1.Find(3);
//slist1.Find(7);
//slist1.Insert(slist1.Find(2), 2);
//cout << slist1 << endl;
//slist1.Insert(slist1.Find(7), 2);
//cout << slist1 << endl;
/*int count = slist1.GetLinkNode();
cout <<"该单链表一共有" <<count<<"个数"<< endl;*/
//slist1.Remove(3);
//cout << slist1 << endl;
slist1.RemoveAll(3);
cout << slist1 << endl;
system("pause");
return 0;
}


各功能函数实现:


<<的重载:

ostream& operator<<(ostream& os, Slist& s)
{
if (s._head == NULL)
return os;
Node* cur = s._head;
while (cur != NULL)
{
os << cur->_date << "->";
cur = cur->_next;
}
cout << "over" << endl;
return os;
}


尾插PushBack()的实现:

void Slist::PushBack(const DataType& d)

{
Node* newNode = new Node(d);
if (_head == NULL)//无节点
{
_head = newNode;
_tail = _head;
}
else
{
_tail->_next = newNode;
_tail = newNode;
}

}


尾删PopBack()的实现:

void Slist::PopBack()
{
if (_head == NULL)
return;
else if (_head->_next == NULL)
{
delete _head;
_head = NULL;
}
else
{
Node* del = _head;
Node *cur = NULL;
while (del->_next != NULL)
{
cur = del;
del = del->_next;
}
cur->_next = NULL;
delete del;
_tail = cur;
}
}



头插PushFront()的实现:

void Slist::PushFront(const DataType& d)
{
if (_head == NULL)//无节点
{
_head = new Node(d);
_tail = _head;
}
else
{
Node* newNode = new Node(d);
newNode->_next = _head;
_head = newNode;
}
}



头删PopFront()的实现:

void Slist::PopFront()
{
if (_head == NULL)
return;
else if (_head->_next == NULL)
{
delete _head;
_head = NULL;
}
else
{
Node* del = _head;
_head = _head->_next;
delete del;
del == NULL;
}
}



查找函数Find()的实现:

Node* Slist::Find(DataType x)
{
Node*cur = _head;
if (_head == NULL)
{
printf("该单链表中没有元素\n");
}
else
{
while (cur)
{
if (cur->_date == x)
{
printf("找到了%d\n", x);
return cur;
}
cur = cur->_next;
if (cur == _tail&&_tail->_date != x)
{
printf("没找到%d\n", x);
}
}
}
return NULL;
}



在pos后插入一个数Insert()的实现:

void Slist::Insert(Node* pos, DataType x)
{
Node* newNode = new Node(x);
if (pos == NULL)
{
printf("插入位置错误\n");
return;
}
//从后往前插入
newNode->_next = pos->_next;
pos->_next = newNode;
}



获取节点数GetLinkNode()的实现:

int  Slist::GetLinkNode()
{
int count=0;
Node *cur = _head;
while (cur != NULL)
{
count++;
cur = cur->_next;
}
return count;
}



删除链表中的第一个x Remove():

void  Slist::Remove(DataType x)
{
Node* pos = Find(x);
if (pos == NULL)
return;
else
{
if (pos == _tail)
{
PopBack();
return;
}
Node* cur = _head;
while (cur->_next != pos)
{
cur = cur->_next;
}
cur->_next = pos->_next;
delete pos;
}
}

删除的是尾结点:


删除的是非尾结点:



删除链表中所有的x RemoveAll():

void  Slist::RemoveAll(DataType x)//
{
Node*cur = _head;
Node*del = NULL;
if (cur->_date == x)
{
_head = _head->_next;
delete cur;
}
while (Find(x)!=NULL)
{
Remove(x);
}
}


  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值