一个简单的C++的链表实现(使用类模板)

在Ubuntu下编译通过,未在Windows下测试。

代码:

/*
 * LinkedList.h
 *
 *  Created on: 2013-6-2
 *      Author: btyh17mxy@gmail.com
 */


#ifndef LINKEDLIST_H_
#define LINKEDLIST_H_

#include <stdio.h>

#ifndef NULL
#define NULL 0
#endif

//表示链表节点的结构体
template<class N>
struct Node {
	//节点值
	N value;
	//指向下一个节点的指针
	Node *next;
};

template<class N> class LinkedList {
private:
	//链表长度
	int Length;
	//头节点
	Node<N> *head;

	//获取指定位置的节点
	Node<N>* __GetNode(int);
public:
	LinkedList();
	~LinkedList();
	//在链表尾部添加一个节点
	bool Add(N);
	//移除指定位置的节点
	bool Remove(int);
	//获取指定位置的值
	N* GetValue(int);
	//获取某节点的索引值(位置)
	int GetIndex(N);
	//获取链表长度
	int GetLength();
	//获取头节点
	Node<N>* GetHead();
	//在指定的位置添加节点
	bool Add(int, N);
	//释放整个链表
	void FreeLinkList();
};

template<class N>
LinkedList<N>::LinkedList() {
	this->head = (new Node<N>());
	this->Length = -1;
	this->head->next = NULL;
}

template<class N>
LinkedList<N>::~LinkedList() {
}
/*
 * 在链表的末尾添加一个值
 * @param value 要添加进链表的值
 * @return 成功返回真,否则返回假
 */
template<typename N>
bool LinkedList<N>::Add(N value) {
	try {
		//如果当前链表为空
		if (this->Length == -1) {
			this->head->value = value;
			this->head->next = NULL;
			this->Length++;
			return true;
		}

		Node<N> *temp = this->head;
		while (temp->next != NULL) {
			temp = temp->next;
		}
		Node<N> *node = new Node<N>();
		node->value = value;
		node->next = NULL;
		temp->next = node;
		this->Length++;
		return true;
	} catch (...) {
	}
	return false;
}

/*
 * 移除链表中指定位置的节点
 * @param index 要移除的节点的位置
 * @return 如果成功返回真,否则返回假
 */
template<typename N>
bool LinkedList<N>::Remove(int index) {
	if (index > this->Length || index < 0) {
		return false;
	}

	Node<N> *aTemp = this->head;
	Node<N> *pTemp = NULL;
	int count = 0;
	while (count <= this->Length) {
		if (count == index && index == 0) {
			this->head = aTemp->next;
			free(aTemp);
			aTemp = NULL;
			this->Length--;
			break;
		}
		if (count == index) {
			pTemp->next = aTemp->next;
			free(aTemp);
			this->Length--;
			aTemp = NULL;
			break;
		}
		pTemp = aTemp;
		aTemp = aTemp->next;
		count++;
	}
	return true;
}

/*
 * 在指定的位置添加一个值。如果index小于0则添加在首位,如果index大于链表长度则添加在尾部
 * @param index 要添加的位置
 * @param value 要添加的值
 * @return 成功返回真否则返回假
 */
template<typename N>
bool LinkedList<N>::Add(int index, N value) {
	try {
		if (index <= 0) {
			//插到第一个
			Node<N> *node = new Node<N>();
			node->value = value;
			node->next = this->head;
			this->head = node;
			this->Length++;
			return true;

		} else if (index == this->Length || index > this->Length) {
			//插到末尾
			return this->Add(value);

		} else {
			//插到中间

			Node<N> *node = this->__GetNode(index - 1);

			Node<N> *temp = new Node<N>();
			temp->value = value;
			temp->next = this->__GetNode(index);
			node->next = temp;
			this->Length++;
			return true;
		}

	} catch (...) {
	}
	return false;
}

/*
 * 获取链表的长度
 * @return 返回链表的长度
 */
template<typename N>
int LinkedList<N>::GetLength() {
	return this->Length + 1;
}

/*
 * 获取头指针
 * @return
 */
template<typename N>
Node<N>* LinkedList<N>::GetHead() {
	return this->head;
}

/*
 * 获取指定位置节点
 * @param index 链表位置
 * @return
 */
template<typename N>
Node<N>* LinkedList<N>::__GetNode(int index) {
	if (index > this->Length) {
		return NULL;
	} else {
		int count = 0;
		for (Node<N> *temp = this->head;; temp = temp->next) {
			if (count == index) {
				return temp;
			}
			count++;
		}
	}
	return NULL;
}
/*
 * 获取指向指定位置节点的值的指针
 * @param index 要获取的节点位置
 * @return 节点值
 */
template<typename N>
N * LinkedList<N>::GetValue(int index) {
	if (index < 0 || index > this->Length) {
		return NULL;
	}
	Node<N> * temp = this->__GetNode(index);
	if (temp != NULL) {
		return &temp->value;
	}
	return NULL;
}

/*
 * 获取指定值在链表中的位置
 * @param value 要找的值
 * @return 如果找到返回位置,没找到返回-1
 */
template<typename N>
int LinkedList<N>::GetIndex(N value) {
	if (this->Length == -1) {
		return -1;
	}
	Node<N> *temp = this->head;
	int count = 0;
	while (count <= this->Length) {
		N v = temp->value;
		if (v == value) {
			return count;
		}
		temp = temp->next;
		count++;
	}
	return -1;
}
#endif /* LINKEDLIST_H_ */

测试代码:

/*
 * LinkedList_test.cpp
 *
 *  Created on: 2013-6-2
 *      Author: btyh17mxy@gmail.com
 */

#include "LinkedList.h"
#include <stdio.h>

int testLinkedList(){
	LinkedList<int> l;
	l.Add(0);
	l.Add(1);
	l.Add(2);
	l.Add(3);
	l.Add(4);

	for(int i = 0; i < l.GetLength();i++){
		printf("index:%d\tvalue:%d\n",i,*l.GetValue(i));
	}
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
面向对象程序设计课程作业 1. 请创建一个数据类型为T的链表类模板List实现以下成员函数: 1) 默认构造函数List(),将该链表初始化为一个链表(10分) 2) 拷贝构造函数List(const List& list),根据一个给定的链表构造当前链表(10分) 3) 析构函数~List(),释放链表中的所有节点(10分) 4) Push_back(T e)函数,往链表最末尾插入一个元素为e的节点(10分) 5) operator<<()友元函数,将链表的所有元素按顺序输出(10分) 6) operator=()函数,实现两个链表的赋值操作(10分) 7) operator+()函数,实现两个链表的连接,A=B+C(10分) 2. 请编写main函数,测试该类模板的正确性: 1) 用List模板定义一个List类型的模板类对象int_listB,从键盘读入m个整数,调用Push_back函数将这m个整数依次插入到该链表中;(4分) 2) 用List模板定义一个List类型的模板类对象int_listC,从键盘读入n个整数,调用Push_back函数将这n个整数依次插入到该链表中;(4分) 3) 用List模板定义一个List类型的模板类对象int_listA,调用List的成员函数实现A = B + C;(4分) 4) 用cout直接输出int_listA的所有元素(3分) 5) 用List模板定义List类型的模板类对象double_listA, double_listB, double_listC,重复上述操作。(15分) 3. 输入输出样例: 1) 输入样例 4 12 23 34 45 3 56 67 78 3 1.2 2.3 3.4 4 4.5 5.6 6.7 7.8 2) 输出样例 12 23 34 45 56 67 78 1.2 2.3 3.4 4.5 5.6 6.7 7.8
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值