单链表

大神请绕过
/****************\
* 单链表C++实现
* Created by Satan
* Time:2015年1月28号
\****************/
#include<iostream>
using namespace std;


//储存节点的结构体
struct listElmt{
	int data;//存放数据
	listElmt * next;//指向下一个元素
	listElmt(int tmp){
		this->data = tmp;
		this->next = NULL;
	}
};

//链表类
class List{
private:
	listElmt * head;//头节点指针
	listElmt * tail;//尾节点指针
	int length;//链表长度
public:
	/**********************\
	*构造函数,初始化链表
	\**********************/
	List(){
		this->head = NULL;
		this->tail = NULL;
		this->length = 0;
	}

	/***********************\
	*参数:数据地址
	*返回值:插入元素成功则返回true,否则返回false
	*描述:在链表尾部插入一个新的节点,节点的数据为参数传入的数据。如果此时链表为空,
	*则将头指针和尾指针同时指向该节点,不为空则将尾指针指向该节点
	\***********************/
	bool insert(int data){
		int priorLength = this->length;//记录操作之前的链表长度
		listElmt * newElmt = new listElmt(data);
		if(this->length == 0){//如果链表为空,则将头、尾指针指向该节点
			this->head = newElmt;
			this->tail = newElmt;
			this->length++;
		}else{
			this->tail->next = newElmt;
			this->tail = this->tail->next;
			this->length++;
		}
		return bool(this->length - priorLength);
	}

	/**********************\
	*参数:数据地址
	*返回值:删除掉的元素的个数
	*描述:从链表中删除所有与指定数据相同的元素。如果被删除的是头节点,则头指针指向头节点之后的那个节点;
	*如果删除的是中间节点,则该节点的前一个节点应该指向该节点的后一个节点;如果删除的是尾节点,
	*则尾指针应该指向尾节点的前一个节点
	\**********************/
	int remove(int data){
		listElmt * currentElmt = this->head;//遍历链表时当前指向的节点
		int count = 0;//删除的节点个数
		if(currentElmt){
			return count;
		}
		if(currentElmt->data == data){//如果删除头节点,头指针向后移
			this->head = currentElmt->next;
			count++;
			delete(currentElmt);
		}

		while(currentElmt->next != NULL){//遍历头节点之后的节点
			if(currentElmt->next->data == data){
				listElmt * tmpElmt = currentElmt->next;//临时存放将要被删除的节点

				if(tmpElmt->next == NULL){//如果删除尾节点,尾指针前移
					currentElmt->next = NULL;
					this->tail = currentElmt;
				}else{//如果删除中间节点
					currentElmt->next = tmpElmt->next;
				}
				count++;
				delete(tmpElmt);
			}
		}
		return count;
	}

	/*************************\
	*参数:数据地址
	*返回值:若找到元素则返回true,否则返回false
	*描述:遍历链表,查找数据与参数数据相同的节点
	\*************************/
	bool contains(int data){
		listElmt * currentElmt = this->head;
		if(currentElmt == NULL){//链表为空
			return false;
		}
		do{//从头节点开始遍历链表
			if(currentElmt->data == data){
				return true;
			}
		}while(currentElmt = currentElmt->next);//当前节点指向尾节点之后则跳出循环
		
		return false;
	}

	/**********************\
	*参数:void
	*返回值:void
	*描述:遍历链表,打印出所有节点中储存的数据
	\**********************/
	void print(){
		listElmt * currentElmt = this->head;
		if(currentElmt == NULL){
			return;
		}
		do{
			cout << currentElmt->data << '\n';
		}while(currentElmt = currentElmt->next);
	}

	/*************************\
	*参数:void
	*返回值:链表长度
	*描述:返回链表长度
	\*************************/
	int getLength(){
		return this->length;
	}
	/************************\
	*析构函数,销毁链表
	\************************/
	~List(){
		listElmt * currentElmt = this->head;
		listElmt * nextElmt;
		do{
			nextElmt = currentElmt->next;
			delete(currentElmt);
		}while(currentElmt);
	}
};

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值