链表的模板实现

博客提及了BaseList.h、LinkList.h和main.cpp文件,这些文件可能与程序开发相关,推测用于构建一个完整的程序,涉及代码的组织与实现。

BaseList.h

#pragma once

template <class T>
class BaseList {
public:
	typedef size_t pos;

	virtual void clear() = 0;						// empty the list
	virtual bool isEmpty() = 0;						// Is empty list?
	virtual void append(const T&) = 0;				// add element in the end
	virtual void insert(const pos, const T&) = 0;	// insert element in the middle
	virtual void del(const pos) = 0;				// delete element from list
	virtual T getValue(const pos) = 0;				// return the value in the position
	virtual void setValue(const pos, const T&) = 0;	// modify the value at position
	virtual pos getPosition(const T&) = 0;			// get the position of value T
};

LinkList.h

#pragma once

#include <iostream>

#include "BaseList.h"

template <class T>
class Node {
public:
	T val;
	Node* next;

	Node(const T& val, Node *p = nullptr)
		: val(val), next(p) {}

	Node(Node *p = nullptr)
		:next(p) {}
};

template <class T>
class LinkList : public BaseList<T> {
private:
	Node<T>* head;

	Node<T>* getNodePosition(BaseList<T>::pos p) {
		if (p == -1) {
			return head;
		}
		Node<T>* tmpNode = head->next;
		for (int i = 0; i < p; i++) {
			tmpNode = tmpNode->next;
		}
		return tmpNode;
	}

	Node<T>* getTail() {
		if (!head->next) {
			return head;
		}
		Node<T>* itr = head->next;
		while (itr->next) {
			itr = itr->next;
		}
		return itr;
	}

	void checkLinkList() {
		if (!head->next) {
			throw std::exception("Empty list");
		}
		return;
	}

public:
	LinkList()
		: head(new Node<T>()) {};
	~LinkList() {
		Node<T>* itr = head->next;
		while (itr) {
			Node<T> *temp = itr;
			itr = itr->next;
			delete temp;
		}
	}

	virtual void clear() override {
		checkLinkList();
		Node<T>* itr = head->next;
		while (itr != nullptr) {
			Node<T>* temp = itr;
			delete temp;
			itr = itr->next;
		}
		head = nullptr;
	}

	virtual bool isEmpty() override {
		return head->next == nullptr;
	}

	virtual void append(const T& val) override {
		Node<T>* newNode = new Node<T>(val);
		if (!head->next) {
			head->next = newNode;
			return;
		}
		Node<T>* tail = getTail();
		tail->next = newNode;
		return;
	}

	virtual void insert(const BaseList<T>::pos p, const T& val) override {
		if (!head->next) {
			if (p == 0) {
				append(val);
			}
			else {
				throw std::exception("Empty list");
			}
		}
		Node<T>* insertPos = getNodePosition(p);
		Node<T>* newNode = new Node<T>(val);
		newNode->next = insertPos->next;
		insertPos->next = newNode;
		return;
	}

	virtual void del(const BaseList<T>::pos p) override {
		checkLinkList();
		Node<T>* beforeDelPos = getNodePosition(p - 1);
		if (beforeDelPos) {
			if (beforeDelPos->next) {
				Node<T>* deleteNode = beforeDelPos->next;
				beforeDelPos->next = deleteNode->next;
				delete deleteNode;
				return;
			}
			throw std::exception("Delete node exceed list tail");
		}
		throw std::exception("Invalid position to delete");
	}

	virtual T getValue(const BaseList<T>::pos p) override {
		checkLinkList();
		Node<T>* readPos = getNodePosition(p);
		return readPos->val;
	}

	virtual void setValue(const BaseList<T>::pos p, const T& val) override {
		checkLinkList();
		Node<T>* writePos = getNodePosition(p);
		writePos->val = val;
		return;
	}

	virtual BaseList<T>::pos getPosition(const T& val) override {
		checkLinkList();
		int count = 0;
		Node<T>* itr = head->next;
		while (itr->val != val) {
			itr = itr->next;
			++count;
		}
		return count;
	}

	void print() {
		Node<T>* itr = head->next;
		int count = 0;
		while (itr) {
			std::cout << "Node Index: " << count++ << '\t';
			std::cout << "Node value: " << itr->val << std::endl;
			itr = itr->next;
		}
		return;
	}
};

main.cpp

// LinkList.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>

#include "LinkList.h"

using namespace std;

int main()
{
    LinkList<int> linklist;

    cout << "Enter the count of nodes you want to append" << endl;
    int count;
    cin >> count;
    while (count--) {
        cout << "Enter the value of node" << endl;
        int val;
        cin >> val;
        linklist.append(val);
    }
    linklist.print();

    cout << "Enter the node index you want to delete" << endl;
    int delIndex = 0;
    cin >> delIndex;
    linklist.del(delIndex);
    linklist.print();

    cout << "Enter the node index you want to insert" << endl;
    int insertIndex;
    cin >> insertIndex;
    cout << "Enter the node value you want to insert" << endl;
    int nodeVal;
    cin >> nodeVal;
    linklist.insert(insertIndex, nodeVal);
    linklist.print();

    return 0;
}


面向对象程序设计课程作业 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
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值