链表中出现的问题

代码

cpp 复制代码
#include<iostream>
#include<stdexcept>
using namespace std;
#define eleType int
//节点的实现
struct listNode {
	eleType data;
	listNode* next;
	//数据域这里错了,初始化一直是0啊,无法改变
	//listNode(eleType x):data(0),next(NULL){}
	listNode(eleType x) :data(x), next(NULL) {}
};
//链表类:增删改查
class linkedList {
private:
	listNode* head;
	int size;
public:
	linkedList():head(NULL),size(0){}//这里由于实例化对象调用的是默认构造函数,所以第一个小括号里面不应该有参数
	//插入函数声明
	void insert(eleType value,int i);
	//删除函数声明
	void remove(int i);
	//查找函数声明
	listNode* find(eleType value);
	listNode* get(int i);
	//修改函数声明
	void update(eleType value, int i);
	//调试函数声明
	void print();
	~linkedList();
};
//析构函数->问题4
linkedList::~linkedList() {
	listNode* curr = head;
	while (curr) {
		listNode* temp = curr;
		curr = curr->next;
		delete temp;
	}
}
//插入函数
void linkedList::insert(eleType value,int i) {
	if (i<0 || i>size) {
		throw out_of_range("Invalid Position!");//问题1
	}
	listNode* newCode = new listNode(value);//问题2
	if (i == 0) {
//		listNode* curr = head;//这个指针多余了
		newCode->next = head;
		head = newCode;
	}
	else {
		listNode* curr = head;
		for (int j = 0; j < i - 1; j++) {
			curr = curr->next;
		}
		newCode->next = curr->next;
		curr->next = newCode;
	}
	size++;
}
//删除函数
void linkedList::remove(int i) {
	if (i<0 || i>size) {
		throw out_of_range("Invalid Position!");
	}
	if (i == 0) {
		listNode* curr = head;
		head = curr->next;
		delete curr;
	}
	else {
		listNode* curr = head;
		for (int j = 0; j < i - 1; j++) {
			curr = curr->next;
		}
		listNode* temp = curr->next;
		curr->next = temp->next;
		delete temp;
	}
	size--;
}
//查找函数
listNode* linkedList::find(eleType value) {//给值传数
	listNode* curr = head;
	while (curr && curr->data != value) {
		curr = curr->next;
	}
	return curr;
}
listNode* linkedList::get(int i) {//给数传值
	if (i<0 || i>size) {
		throw out_of_range("Invalid Position!");
	}
	listNode* curr = head;
	for (int j = 0; j < i; j++) {
		curr = curr->next;
	}
	return curr;
}
void linkedList::update(eleType value, int i) {
	if (i<0 || i>size) {
		throw out_of_range("Invalid Position!");
	}
	listNode* curr = head;
	for (int j = 0; j < i; j++) {
		curr = curr->next;
	}
	curr->data = value;
}
void linkedList::print() {
	listNode* curr = head;
	while (curr) {
		cout << curr->data << ' ';
		curr = curr->next;//这里要偏移指针遍历链表啊
	}
	cout << endl;
}
int main() {
	linkedList list;//问题3
	list.insert(10, 0);
	list.insert(20, 1);
	list.insert(30, 2);
	list.insert(40, 3);
	list.insert(50, 4);
	list.print();
	list.remove(0);
	list.print();
	list.update(666, 0);
	list.print();
	listNode* const temp = list.find(666);
	cout << temp->data << endl;
	cout << list.get(0)->data << endl;
	return 0;
}

反思

我想讲一下结构体和类里面的初始化列表:

listNode(eleType x) :data(x), next(NULL) {}

这个是结构体里面的初始化列表,由于每个节点都有数据域和指针域两个部分,所以初始化时包含这两个参数的初始化,而输入端输入的是数据域的值,所以类型也跟data保持一致。

linkedList():head(NULL),size(0){}//这里由于实例化对象调用的是默认构造函数,所以第一个小括号里面不应该有参数

这个是类里面的初始化列表,两个参数一个是头节点,一个是链表长度,暂时不需要从外部输入来确定他们的值,所以初始化为NULL和0,而且调用时为默认构造函数,则第一个小括号中就不用写东西啦。

相关推荐
大江东去浪淘尽千古风流人物5 小时前
【VLN】VLN(Vision-and-Language Navigation视觉语言导航)算法本质,范式难点及解决方向(1)
人工智能·python·算法
rainbow68895 小时前
Linux文件描述符与重定向原理
c++
独好紫罗兰5 小时前
对python的再认识-基于数据结构进行-a003-列表-排序
开发语言·数据结构·python
wuhen_n5 小时前
JavaScript内置数据结构
开发语言·前端·javascript·数据结构
努力学算法的蒟蒻5 小时前
day79(2.7)——leetcode面试经典150
算法·leetcode·职场和发展
2401_841495645 小时前
【LeetCode刷题】二叉树的层序遍历
数据结构·python·算法·leetcode·二叉树··队列
AC赳赳老秦5 小时前
2026国产算力新周期:DeepSeek实战适配英伟达H200,引领大模型训练效率跃升
大数据·前端·人工智能·算法·tidb·memcache·deepseek
CodeSheep程序羊6 小时前
拼多多春节加班工资曝光,没几个敢给这个数的。
java·c语言·开发语言·c++·python·程序人生·职场和发展
独好紫罗兰6 小时前
对python的再认识-基于数据结构进行-a002-列表-列表推导式
开发语言·数据结构·python
2401_841495646 小时前
【LeetCode刷题】二叉树的直径
数据结构·python·算法·leetcode·二叉树··递归