[c++]单链表(class)

//list.h
#include<iostream>
typedef char Type;
using namespace std;
class List
{
public:
List *head;
~List();
List(Type body, List *add = NULL);
List();
void Creat(List *&head);
void Insert();
void Delete(List *&head);
void Print();
void Release();
private:
bool search(Type item, List *&p);
Type body;
List *next;
Type item;
};
</pre><pre name="code" class="cpp">//list.cpp
#include<iostream>
#include"list.h"
using namespace std;
typedef char Type;
//********************************************************************
void List::Creat(List *&head)//生成函数
{
int n;
cout << "please enter the length of the list: " << endl;
cin >> n;
Type *temp = new Type[n];
cout << "please enter the content of the list" << endl;
for (int i = 0; i < n;i++)  cin >> temp[i];
List *p = NULL;
this->head = new List(temp[0]);
p = this->head;
for (int i = 1; i < n; i++)
{
p->next = new List(temp[i]);
p = p->next;
}
p->next = NULL;
cout << "Save succeed!" << endl;
}
//**********************************************************************
bool List::search(Type item, List *&p)//搜索函数
{p = new List('0');
p->next = this->head;
List *temp = this->head;
if (temp->body == item)
return true;
else
{
p = this->head;
temp = p->next;
}
while (p&&temp->body != item)
{
p = p->next;
List *temp = p->next;
}
if (p) return true;
else return false;
}
//**********************************************************************
void List::Insert()//插入函数
{
Type n;
cout << "please enter the item you want to add on: " << endl;
cin >> n;
List *p = NULL;
if (search(n,p))
{
p = p->next;
Type m;
cout << "please enter the item you want to add: " << endl;
cin >> m;
List *q = new List(m,p->next);
p->next = q;
cout << "Insert success!" << endl;
}
else cout << "Don't have this item." << endl;
}
//***********************************************************************
void List::Delete(List *&head)//删除函数
{
Type n;
cout << "please enter the item you want to delete:" << endl;
cin >> n;
List *p = NULL;
if (this->search(n,p)&&head->body==n)
{
List *q = p->next;
head = q->next;
p->next = head;
delete q;
cout << "Delete success!" << endl;
}
else if (this->search(n, p))
{
List *q = p->next;
p->next = q->next;
delete q;
cout << "Delete success!" << endl;
}
else cout << "Don't have this item." << endl;
}
//************************************************************************
void List::Print()//输出函数
{
List *p = this->head;
while (p)
{
cout << p->body<<' ';
p = p->next;
}
cout << endl << "Print Succeed!" << endl;
}
//************************************************************************
void List::Release()//释放函数
{
List *p = this->head;
while (p) {
head = head->next;
delete p;
}
cout << "Release succeed!" << endl;
}
//************************************************************************
List::List(Type body, List *add)//构造函数
{
this->body = body;
this->next = add;
}


//**************************************************************************
List::~List()//析构函数
{
next = NULL;
}
//****************************************************************
List::List()
{
}
//main.cpp
#include<iostream>
#include"list.h"
using namespace std;
typedef char Type;
void main()
{
	cout << "**************************************MENU*************************************" << endl;
	cout << "1.Creat a list." << endl;
	cout << "2.Insert a item." << endl;
	cout << "3.Delete a item." << endl;
	cout << "4.Print the list." << endl;
	cout << "-1.END" << endl;
	cout << "FBI WARNING: please press '0'to release before end the programm!" << endl;
	List list1;
	int n=1;
	while (n!=-1)
	{
		cout << "please choose the number above." << endl;
		cin >> n;
		switch (n)
		{
		case 1:
			list1.Creat(list1.head);
			break;
		case 2:
			list1.Insert();
			break;
		case 3:
			list1.Delete(list1.head);
			break;
		case 4:
			list1.Print();
			break;
		case 0:
			list1.Release();
		default:
			cout << "Wrong Input" << endl;
			break;
		}
	}
}


面向对象程序设计课程作业 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
好的,我可以帮你讲解一下如何使用C++单链表来存储火车信息。 首先需要定义一个火车信息的结构体,例如: ```c++ struct Train { string name; // 火车名 string from; // 起点站 string to; // 终点站 string time; // 发车时间 }; ``` 接下来,定义一个单链表节点的结构体,其中包含一个火车信息的结构体和一个指向下一个节点的指针,例如: ```c++ struct Node { Train train; // 火车信息 Node* next; // 指向下一个节点的指针 }; ``` 定义一个单链表的类,其中包含头节点指针和尾节点指针,以及一些操作方法,例如: ```c++ class TrainList { public: TrainList() { head = new Node(); tail = head; } ~TrainList() { Node* current = head; while (current != nullptr) { Node* tmp = current; current = current->next; delete tmp; } } void insert(const Train& train) { Node* node = new Node(); node->train = train; tail->next = node; tail = node; } void remove(const Train& train) { Node* current = head->next; Node* prev = head; while (current != nullptr) { if (current->train.name == train.name && current->train.from == train.from && current->train.to == train.to && current->train.time == train.time) { prev->next = current->next; if (current == tail) { tail = prev; } delete current; break; } prev = current; current = current->next; } } void print() const { Node* current = head->next; while (current != nullptr) { cout << "火车名:" << current->train.name << endl; cout << "起点站:" << current->train.from << endl; cout << "终点站:" << current->train.to << endl; cout << "发车时间:" << current->train.time << endl; cout << endl; current = current->next; } } private: Node* head; Node* tail; }; ``` 然后在主函数中,可以创建一个TrainList对象,调用insert方法添加火车信息,调用remove方法删除火车信息,调用print方法打印所有火车信息,例如: ```c++ int main() { TrainList trainList; Train train1 = {"G1", "北京", "上海", "08:00"}; Train train2 = {"G2", "上海", "北京", "14:00"}; Train train3 = {"D1", "北京", "天津", "09:00"}; trainList.insert(train1); trainList.insert(train2); trainList.insert(train3); trainList.print(); trainList.remove(train1); trainList.print(); return 0; } ``` 以上就是使用C++单链表存储火车信息的示例,希望对你有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值