C++面向对象程序设计 实验报告 实验四

实验4 模板和文件

1. 实验目的

(1)掌握模板的定义和使用方法;

(2)根据要求正确定义模板类,实例化模板;

(3)掌握文件流的打开、关闭及读写方法。(暂未实现)

2. 实验内容

(1)单链表模板的定义和实现。

(2)员工表类数据的读写。

3. 实验要求

(1)单链表模板的定义和实现,并设计主程序对模板进行测试。

(2)使用文件流为实验一的员工表类提供数据读写的方法

4. 程序代码

单链表实现:

ListNode.h

#pragma once
template class SingleList;

template class ListNode {
private:
	friend typename SingleList;

	ListNode() :m_pnext(NULL) {}
	ListNode(const Type item, ListNode *next = NULL) :m_data(item), m_pnext(next) {}
	~ListNode() {
		m_pnext = NULL;
	}

public:
	Type GetData();
	friend ostream& operator<< (ostream&, ListNode&);

private:
	Type m_data;
	ListNode *m_pnext;
};

template Type ListNode::GetData() {
	return this->m_data;
}

template ostream& operator<<(ostream& os, ListNode& out) {
	os << out.m_data;
	return os;
}

SingleList.h

#pragma once
#include "ListNode.h"

template class SingleList {
public:
	SingleList() :head(new ListNode()) {}
	~SingleList() {
		MakeEmpty();
		delete head;
	}

public:
	void MakeEmpty();//清空链表
	int Length();//获取长度
	bool Insert(Type item, int n = 0);         	Type Remove(int n = 0); //删除某一元素
	bool RemoveAll(Type item); //删除所有元素
	Type Get(int n); //查询某一位元素
	void Print();//输出链表

private:
	ListNode *head;
};

template void SingleList::MakeEmpty() {
	ListNode *pdel;
	while (head->m_pnext != NULL) {
		pdel = head->m_pnext;
		head->m_pnext = pdel->m_pnext;
		delete pdel;
	}
}

template int SingleList::Length() {
	ListNode *pmove = head->m_pnext;
	int count = 0;
	while (pmove != NULL) {
		pmove = pmove->m_pnext;
		count++;
	}
	return count;
}

template bool SingleList::Insert(Type item, int n) {
	if (n < 0) {
		cout << "The n is illegal" << endl;
		return 0;
	}
	ListNode *pmove = head;
	ListNode *pnode = new ListNode(item);
	if (pnode == NULL) {
		cout << "Application error!" << endl;
		return 0;
	}
	for (int i = 0; i < n&&pmove; i++) {
		pmove = pmove->m_pnext;
	}
	if (pmove == NULL) {
		cout << "the n is illegal" << endl;
		return 0;
	}
	pnode->m_pnext = pmove->m_pnext;
	pmove->m_pnext = pnode;
	return 1;
}

template bool SingleList::RemoveAll(Type item) {
	ListNode *pmove = head;
	ListNode *pdel = head->m_pnext;
	while (pdel != NULL) {
		if (pdel->m_data == item) {
			pmove->m_pnext = pdel->m_pnext;
			delete pdel;
			pdel = pmove->m_pnext;
			continue;
		}
		pmove = pmove->m_pnext;
		pdel = pdel->m_pnext;
	}
	return 1;
}

template Type SingleList::Remove(int n) {
	if (n < 0) {
		cout << "can't find the element" << endl;
		exit(1);
	}
	ListNode *pmove = head, *pdel;
	for (int i = 0; i < n&&pmove->m_pnext; i++) {
		pmove = pmove->m_pnext;
	}
	if (pmove->m_pnext == NULL) {
		cout << "can't find the element" << endl;
		exit(1);
	}
	pdel = pmove->m_pnext;
	pmove->m_pnext = pdel->m_pnext;
	Type temp = pdel->m_data;
	delete pdel;
	return temp;
}

template Type SingleList::Get(int n) {
	if (n < 0) {
		cout << "The n is out of boundary" << endl;
		exit(1);
	}
	ListNode *pmove = head->m_pnext;
	for (int i = 0; i < n; i++) {
		pmove = pmove->m_pnext;
		if (NULL == pmove) {
			cout << "The n is out of boundary" << endl;
			exit(1);
		}
	}
	return pmove->m_data;
}

template void SingleList::Print() {
	ListNode *pmove = head->m_pnext;
	cout << "head";
	while (pmove) {
		cout << "--->" << pmove->m_data;
		pmove = pmove->m_pnext;
	}
	cout << "--->over" << endl << endl << endl;
}

Main.cpp

#include 
using namespace std;
#include "SingleList.h"

int main()
{
	SingleList list;
	for (int i = 0; i < 20; i++) {
		list.Insert(i * 3, i);
	}
	for (int i = 0; i < 5; i++) {
		list.Insert(3, i * 3);
	}
	cout << "链表的长度为: " << list.Length() << endl;
	list.Print();

	list.Remove(5);
	cout << "删除第5号元素后链表的长度为:" << list.Length() << endl;
	list.Print();

	list.RemoveAll(3);
	cout << "删除所有3后链表长度为: " << list.Length() << endl;
	list.Print();

	cout << "第三号元素为:" << list.Get(3) << endl;
	cout << endl;
	list.MakeEmpty();
	cout << "清空链表后长度为:" << list.Length() << endl;
	list.Print();
	system("pause");
	return 0;
}

运行结果:

C++面向对象程序设计 实验报告 实验四_第1张图片

 

 

 

 

你可能感兴趣的:(C++学习)