C++:使用本章中的链表类模板,声明两个int类型的链表a,b,分别插入5个元素,然后把b中元素加入a的尾部

这是c++语言程序设计第五版,郑莉,第九章模板与群体数据中习题9-5的一个题目。

题目:使用本章中的链表类模板,声明两个int类型的链表a,b,分别插入5个元素,然后把b中元素加入a的尾部

当时网上找也没找到满意的于是自己写了一个

这个是节点Node

#ifndef NODE_H
#define NODE_H
template<class T>
class Node {
private:
	Node<T>* next;
public:
	T data;
	Node(const T& data, Node<T>* next = 0);
	void insertAfter(Node<T>* p);
	Node<T>* deleteAfter();
	Node<T>* nextNode();
	const Node<T>* nextNode()const;
};
template<class T>
Node<T>::Node(const T&data,Node<T>*next/*=0 */):data(data),next(next){}
template<class T>
Node<T>* Node<T>::nextNode() {
	return next;
}
template<class T>
const Node<T>* Node<T>::nextNode()const {
	return next;
}
template<class T>
void Node<T>::insertAfter(Node<T>* p) {
	p->next = next;
	next = p;
}
template<class T>
Node<T>* Node<T>::deleteAfter() {
	Node<T>* tempPtr = next;
	if (next == 0)
		return 0;
	next = tempPtr->next;
	return tempPtr;
}
#endif

这个是链表LinkedList

#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include"Node.h"
#include<iostream>
using namespace std;

template<class T>
class LinkedList {
private:
	Node<T>* front, * rear;
	Node<T>* prevPtr, * currPtr;
	int size;
	int position;
	Node<T>* newNode(const T& item, Node<T>* ptrNext = NULL);
	void freeNode(Node<T>* p);
	void copy(const LinkedList<T>& L);
public:
	LinkedList();
	LinkedList(const LinkedList<T>&L);
	~LinkedList();
	LinkedList<T>&operator=(const LinkedList<T>&L);
	int getSize()const;
	bool isEmpty()const;
	void reset(int pos = 0);
	void next();
	bool endOfList()const;
	int currentPosition()const;
	void insertFront(const T& item);
	void insertRear(const T& item);
	void insertAt(const T& item);
	void insertAfter(const T& item);
	T deleteFront();
	void deleteCurrent();
	T& data();
	const T& data()const;
	void clear();
};
template<class T>
Node<T>* LinkedList<T>::newNode(const T& item, Node<T>* ptrNext)
{
	Node<T>* p;
	p = new Node<T>(item, ptrNext);
	if (p == NULL) {
		cout << "Memory allocation failure!\n";
		exit(1);
	}
	return p;
}
template<class T>
void LinkedList<T>::freeNode(Node<T>* p) { delete p; }
template<class T>
void LinkedList<T>::copy(const LinkedList<T>& L) {
	Node<T>* p = L.front;
	int pos;
	while (p!= NULL) {
		insertRear(p->data);
		p = p->nextNode();
	}
	if (position == -1)
		return;
	prevPtr = NULL;
	currPtr = front;
	for (pos = 0; pos != position; pos++) {
		prevPtr = currPtr; 
		currPtr = currPtr->nextNode();
	}
}
template<class T>
LinkedList<T>::LinkedList():front(NULL),rear(NULL),prevPtr(NULL),currPtr(NULL),size(0),position(-1){}
template<class T>
LinkedList<T>::LinkedList(const LinkedList<T>& L) {
	front = rear = NULL;
	prevPtr = currPtr = NULL;
	size = 0;
	position = -1;
	copy(L);
}
template<class T>
LinkedList<T>::~LinkedList() { clear(); }
template<class T>
LinkedList<T>& LinkedList<T>::operator=(const LinkedList<T>& L) {
	if (this == &L)
		return *this;
	clear();
	copy(L);
	return *this;
}
template<class T>
int LinkedList<T>::getSize() const
{
	return size;
}
template<class T>
bool LinkedList<T>::isEmpty() const
{
	return size==0;
}
template<class T>
void LinkedList<T>::reset(int pos) {
	int startPos;
	if (front == NULL)
		return;
	if (pos<0 || pos>size - 1) {
		cerr << "Reset:Invalid list position:" << pos << endl;
		return;
	}
	if (pos == 0) {
		prevPtr = NULL;
		currPtr = front;
		position = 0;
	}
	else {
		currPtr = front->nextNode();
		prevPtr = front;
		startPos = 1;
		for (position = startPos; position != pos; position++) {
			prevPtr = currPtr;
			currPtr = currPtr->nextNode();
		}
	}
}
template<class T>
void LinkedList<T>::next() {
	if (currPtr != NULL) {
		prevPtr = currPtr;
		currPtr = currPtr->nextNode();
		position++;
	}
}
template<class T>
bool LinkedList<T>::endOfList()const { return currPtr == NULL; }
template<class T>
int LinkedList<T>::currentPosition()const { return position; }
template<class T>
void LinkedList<T>::insertFront(const T& item) {
	if (front != NULL)
		reset();
	insertAt(item);
}
template<class T>
void LinkedList<T>::insertRear(const T&item){ 
	Node<T>* nNode;
	prevPtr = rear;
	nNode = newNode(item);
	if (rear == NULL)
		front = rear = nNode;
	else {
		rear->insertAfter(nNode);
		rear = nNode;
	}
	currPtr = rear;
	position = size;
	size++;
}
template<class T>
void LinkedList<T>::insertAt(const T& item) {
	Node<T>* nNode;
	if (prevPtr == NULL) {
		nNode = newNode(item,front);
		front = nNode;
	}
	else {
		nNode = newNode(item);
		prevPtr->insertAfter(nNode);
	}
	if (prevPtr == rear) {
		rear = nNode;
		position = size;
	}
	currPtr = nNode;
	size++;
}
template<class T>
void LinkedList<T>::insertAfter(const T& item) {
	Node<T>* p;
	p = newNode(item);
	if (front == NULL) {
		front = currPtr = rear = p;
		position = 0;
	}
	else {
		if (currPtr == NULL)
			currPtr = prevPtr;
		currPtr->insertAfter(p);
		if (currPtr == rear) {
			rear = p;
			position = size;
		}
		else
			position++;
		prevPtr = currPtr;
		currPtr = p;
	}
	size++;
}
template<class T>
T LinkedList<T>::deleteFront() {
	T item;
	reset();
	if (front == NULL) {
		cerr << "Invalid deletion!" << endl;
		exit(1);
	}
	item = currPtr->data;
	deleteCurrent();
	return item;
}
template<class T>
void LinkedList<T>::deleteCurrent() {
	Node<T>* p;
	if (currPtr == NULL) {
		cerr << "Invalid deletion!" << endl;
		exit(1);
	}
	if (prevPtr == NULL) {
		p = front;
		front = front->nextNode();
	}
	else
		p = prevPtr->deleteAfter();
	if (p == rear) {
		rear = prevPtr;
		position--;
	}
	currPtr = p->nextNode();
	freeNode(p);
	size--;
}
template<class T>
T& LinkedList<T>::data() {
	 if (size == 0 || currPtr == NULL) {
		 cerr << "Data:invalid reference!" << endl;
		 exit(1);
	 }
	 return currPtr->data;
 }
template<class T>
const T& LinkedList<T>::data() const
 {
	return currPtr->data;
 }
 template<class T>
 void LinkedList<T>::clear() {
	 Node<T>* currPosition, * nextPosition;
	 currPosition = front;
	 while (currPosition != NULL) {
		 nextPosition = currPosition->nextNode();
		 freeNode(currPosition);
		 currPosition = nextPosition;
	 }
	 front = rear = NULL;
	 prevPtr = currPtr = NULL;
	 size = 0;
	 position = -1;
 }
#endif // !LINKEDLIST_H

下面是main函数

#include<iostream>
#include"LinkedList.h"
using namespace std;
int main() {
	LinkedList<int> a,b;
	cout << "输入链表a的元素:";
	for (int i = 0; i <5; i++)
	{
		int item;
		cin >> item;
		a.insertFront(item);
	}
	cout << "输入链表b的元素:";
	for (int i = 0; i < 5; i++)
	{
		int item;
		cin >> item;
		b.insertFront(item);
	}
    b.reset();
	if (!b.endOfList()) {
		for (int i = 0; i < b.getSize(); i++) {
			a.insertRear(b.data());
			b.next();
		}
	}

	cout << "输出链表b插入a队尾后的的元素:";
	a.reset();
	while (!a.endOfList()) {
		cout << a.data()<<" ";
		a.next();
	}
	return 0;
}

我用的是头插法,不喜欢倒序的可以用尾插法

把a.insertFront换成a.insertRear()即可。不加注释是上面两个模板课本有,很详细。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值