C++单链表

// test.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

// ConsoleApplication1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include<vector>
#include<string>
#include<time.h>
#include<algorithm>
#include<ctime>
using namespace std;

typedef struct ListNode
{
	int value;
	ListNode* next;
}ListNode,LN;

void createList(LN* pHead,vector<int>& arr,int n)
{
	LN* p = pHead;
	for (int i = 0; i < n; i++)
	{
		LN* pNextNode = new LN;
		pNextNode->value = arr.at(i);
		pNextNode->next = NULL;
		p->next = pNextNode;
		p= pNextNode;
	}
}
//输出链表的内容
void OutPutList(LN *head)
{
	LN* current = head->next;
	while (current != NULL)
	{
		cout << current->value << endl;
		current = current->next;
	}
	cout << endl;
}
//在位置为i的节点插入值e
void insertNose(LN *head,int i,int e)
{
	LN* temp;
	temp = head;
	int j = 0;
	while (temp && j < i - 1) {
		temp = temp->next;
		j++;
	}
	if (!temp || j > i - 1) {
		cout << "插入位置错误";
	}
	else {
		LN* s;
		s = new LN;
		s->value = e;
		s->next = temp->next;
		temp->next = s;
	}
}
//在节点a前面加入节点b,也可以是
void insertList(LN *head,int aData,int bData)
{
	LN* p, * q = NULL, * s;		//p指向a,q指向a前一个节点,s指向b
	s = (LN*)new(LN);
	s->value = bData;
	p = head->next;
	if (head->next == NULL)
	{
		head->next = s;
		s->next = NULL;
	}
	else if (p->value == aData)
	{
		s->next = p;
		head->next = s;
	}
	else
	{
		while (p->value != aData && p->next != NULL)
		{
			q = p;
			p = p->next;
		}
		if (p->value == aData)
		{
			q->next = s;
			s->next = p;
		}
		else
		{
			p->next = s;
			s->next = NULL;
		}
	}
}
//删除节点i
void Delete(LN* head, int i)
{
	LN* temp;
	temp = head;
	int j = 0;
	while (temp && j < i - 1) {
		temp = temp->next;
		j++;
	}
	if (!temp || j > i - 1) {
		cout << "删除位置错误";
		return;
	}
	else {
		LN* s;
		s = temp->next;
		temp->next = s->next;
		delete s;
	}
}
//删除倒数第i个节点
void Deletek(LN* head, int k)
{
	LN* p = head->next;
	LN* q = head->next;
	int i = 1;
	while (i <= k) {					//注意是“<=”,为了让q指向倒数第k+1个,q->next为倒数第k个
		i++;
		p = p->next;
	}
	while (p->next != NULL) {
		p = p->next;
		q = q->next;			//这时q指向倒数第k+1个,q->next为倒数第k个
	}
	LN* s;
	s = q->next;
	q->next = s->next;
	delete s;
}

void DeleteList(LN *head, int aData)
{
	LN* p, * q = NULL;
	p = head->next;
	if (p == NULL)
	{
		return;
	}
	if (p->value == aData)
	{
		head->next = p->next;
		delete p;
	}
	else
	{
		while (p->value != aData && p->next != NULL)
		{
			q = p;
			p = p->next;
		}
		if (p->value == aData)
		{
			q->next = p->next;
			delete p;
		}
	}
}
//取得i节点的值
void GetData(LN* head, int i)
{
	LN* temp;
	temp = head;
	int j = 0;
	while (temp && j < i - 1) {
		temp = temp->next;
		j++;
	}
	if (!temp || j > i - 1) {
		cout << "寻找位置错误" << endl;
		return;
	}
	else {
		cout << i << "处的数据为:" << temp->next->value << endl;
		cout << temp->value << endl;
	}
}
//取得倒数第k个的元素值
void GetDatak(LN* head, int k)
{
	LN* p = head->next;
	LN* q = head->next;
	int i = 1;
	while (i < k) {
		i++;
		p = p->next;
	}
	while (p->next != NULL) {
		p = p->next;
		q = q->next;
	}
	cout << "倒数第" << k << "个元素为:" << q->value << "\n";
	cout << q->value << endl;
}
//
//修改i处的元素值
void ChangeData(LN *head,int i)
{
	LN* temp;
	temp = head;
	int j = 0;
	int x;
	cout << "输入要修改的值:";
	cin >> x;
	while (temp && j < i - 1) {
		temp = temp->next;
		j++;
	}
	if (!temp || j > i - 1) {
		cout << "寻找位置错误" << endl;
		return ;
	}
	else {
		temp->next->value = x;
		cout << temp->next->value << endl;
	}
}
//修改倒数第k个的元素值
void ChangeDatak(LN* head, int k)
{
	LN* p = head->next;
	LN* q = head->next;
	int i = 1;
	int x;
	cout << "输入要修改的值:";
	cin >> x;
	while (i < k) {
		i++;
		p = p->next;
	}
	while (p->next != NULL) {
		p = p->next;
		q = q->next;
	}
	q->value = x;
	cout << q->value << endl;
}
//寻找链表中有无与obj匹配的元素:第几个元素为obj
void Search(LN* head, int obj)
{
	int j = 1;
	LN* temp;
	temp = head->next;
	while (temp && temp->value != obj) {
		temp = temp->next;
		j++;
	}
	if (temp == NULL) {
		cout << "该链表中无此元素" << endl;
		return ;
	}
	else {
		cout << "在该链表中的第" << j << "个元素等于" << obj << endl;
		cout << j << endl;
	}
}
//获得表的长度
void ListLength(LN* head)
{
	LN* temp;
	temp = head;
	int j = 0;
	while (temp) {
		temp = temp->next;
		j++;
	}
	cout << "该链表的长度为:" << j - 1 << endl;
	cout << j << endl;
}
//链表倒置
LN* Reverse(LN* head)
{
	LN* r;
	LN* p = head->next;
	head->next = NULL;
	while (p != NULL) {
		r = p->next;
		p->next = head->next;
		head->next = p;
		p = r;
	}
	return head;
}
int main()
{
	int n;
	cout << "请输入需要排序的数字个数" << endl;
	cin >> n;
	vector<int> arr;
	srand((int)time(NULL));
	for (int i = 0; i < n; i++)
	{
		arr.push_back(rand() % 100);
	}
	clock_t startTime = clock();
	LN* head = new LN;
	head->value = 0;
	head->next = NULL;
	createList(head, arr, n);
	clock_t endTime = clock();
	cout << "整个程序用时:" << double(endTime - startTime) / CLOCKS_PER_SEC << "s" << endl;
	insertNose(head, 1, 20);

	//cout << head->value << endl;
	//cout << head->next->value << endl;
	//cout << head->next->next->value << endl;
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以帮你讲解一下如何使用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、付费专栏及课程。

余额充值