首页 > 编程语言 >链表C++

链表C++

时间:2024-10-16 19:52:37浏览次数:19  
标签:head ListNode Linkedlist int C++ next 链表 curr

#include<iostream>
#include<stdexcept>
using namespace std;
#define eleType int
struct ListNode
{
	eleType m_data;
	ListNode* next;
	ListNode(eleType data)
	{
		m_data = data;
		next = NULL;
	}
};
class Linkedlist
{
private:
	ListNode* head;
	int size;
public:
	Linkedlist()
	{
		head = NULL;
		size = 0;
	}
	~Linkedlist();
	void insert(int i, eleType a);
	void remove(int i);
	ListNode* find(eleType a);
	ListNode* get(int i);
	void update(int i, eleType a);
	void print();
};
Linkedlist::~Linkedlist()
{
	ListNode* curr = head;
	if (curr != NULL)
	{
		ListNode* temp = curr;
		curr = curr->next;
		delete temp;
	}
}
 void Linkedlist::insert(int i, eleType a)
{
	if (i<0 || i>size)
	{
		throw std::out_of_range("下标不对");
	}
	else
	{
		ListNode* newNode = new ListNode(a);
		if (i == 0)
		{
			newNode->next = head;
			head = newNode;
		}
		else
		{
			ListNode* curr = head;
			for (int j = 0; j < i - 1; j++)
			{
				curr = curr->next;
			}
			newNode->next = curr->next;
			curr->next=newNode;
		}
		size++;
	}
}
 void Linkedlist::remove(int i)
 {
	 if (i<0 || i>=size)
	 {
		 throw std::out_of_range("下标不对");
	 }
	 else
	 {
		 if (i == 0)
		 {
			 ListNode* temp = head;
			 head = head->next;
			 delete temp;
		 }
		 ListNode* curr = head;
		 for (int j = 0; j < i - 1; j++)
		 {
			 curr = curr->next;
		 }
		 ListNode* temp2 = curr->next;
		 curr->next = temp2->next;
		 delete temp2;
		 size--;
	 }
 }
 ListNode* Linkedlist::find(eleType a)
 {
	 ListNode* curr = head;
	 while (curr && curr->m_data != a)
	 {
		 curr = curr->next;
	 }
	 if (curr->m_data != a)
	 {
		 cout << "没有该元素" << endl;
		 return NULL;
	 }
	 if (curr)
	 {
		 return curr;
	 }
 }
 ListNode* Linkedlist::get(int i)
 {
	 if (i < 0 || i >= size)
	 {
		 throw std::out_of_range("下标不对");
	 }
	 ListNode* curr = head;
	 for (int j = 0; j < i; j++)
	 {
		 curr = curr->next;
	 }
	 return curr;
 }
 void Linkedlist::update(int i, eleType a)
 {
	 if (i < 0 || i >= size)
	 {
		 throw std::out_of_range("下标不对");
	 }
	 ListNode* curr = head;
	 for (int j = 0; j < i; j++)
	 {
		 curr = curr->next;
	 }
	 curr->m_data = a;
 }
 void Linkedlist::print()
 {
	 ListNode* curr = head;
	 while (curr)
	 {
		 cout << curr->m_data << " ";
		 curr = curr->next;
	 }
	 cout << endl;
 }
 int main()
 {
	 Linkedlist list;
	 list.insert(0, 10);
	 list.insert(1, 20);
	 list.print();
	 return 0;
 }

标签:head,ListNode,Linkedlist,int,C++,next,链表,curr
From: https://blog.csdn.net/2401_83496535/article/details/142964210

相关文章

  • 链队(c++)
    //队列的顺序实现//线性表先进先出#include<iostream>usingnamespacestd;#defineMaxSize100typedefstructLinkNode{chardata;structLinkNode*next;}LinkNode,*QueuePtr;typedefstruct{  QueuePtrfront,rear;}LinkQueue;//初始化voidInitQueue(L......
  • C++构建工具-构建系统
    构建CI/CDCI这步,首先需要一个版本控制系统,当前最好用的就是git流程:主线分支上设置静态代码检查,用来检测每一笔提交的质量,比如命名规范等。 还会设置自动化单元测试,看守代码功能,并进行代码覆盖率分析代码拉取功能; 构建 安装包依赖项功能,依赖项需......
  • C++ 排序算法(选择、冒泡、插入)
    八、选择排序(从小到大): 选择排序的基本思想是:每一趟从待排序的数据中,通过“打擂台”比较选出最小元素,放在这些数据的最前面。这样,第一趟把n个数中(第1个到第n个)最小的放在第一个位置,第二趟把剩余的n-1个数中(第2个到第n个)最小的放在第二个位置,第三趟把剩余的n......