首页 > 其他分享 >链表——增删查改

链表——增删查改

时间:2024-03-24 12:33:26浏览次数:17  
标签:node current head ListNode next 链表 num 查改 增删

题目

 题目解析

  一个很基础的关于链表的增删查改题目,学过数据结构的应该都蛮熟的吧(^^),为了更好的完成题目,我们可以将其变为一个菜单,直接使用。

代码

bool Push(ListNode** head, int num)
{
	ListNode* new_node = new ListNode{ num, NULL }; 
	if (!new_node)  
	{
		return false;
	}
	if (*head == NULL)
	{
		*head = new_node;
	}
	else
	{
		ListNode* current = *head;
		while (current->next_node)
		{
			current = current->next_node;
		}
		current->next_node = new_node;
	}
	return true;
}

bool Pop(ListNode** head, int num)
{
	bool found = false;
	ListNode dummy{ num,NULL }; // 创建一个dummy节点,其值设置为0  
	dummy.next_node = *head;
	ListNode* current = &dummy;

	while (current->next_node != NULL)
	{
		if (current->next_node->num == num)
		{
			ListNode* temp = current->next_node;
			current->next_node = current->next_node->next_node;
			delete temp;
			found = true;
			break; // 找到后退出循环  
		}
		current = current->next_node;
	}

	*head = dummy.next_node; // 更新头节点  
	return found;
}

ListNode* Find(ListNode *head, int num)
{
	ListNode* current = head;
	while (current)
	{
		if (current->num == num) 
		{
			return current;
		}
		current = current->next_node;
	}
	return NULL;
}

bool Updata(ListNode** head, int num1, int num2)
{
	ListNode* current = *head;
	while (current)
	{
		if (current->num == num1) 
		{
			current->num = num2;
			return true;
		}
		current = current->next_node;
	}
	return false;
}

总代码

/*2024-3-15 rou*/
#include<iostream>
#include<string>
using namespace std;

struct ListNode
{
	int num;
	struct ListNode *next_node;
};

//增
bool Push(ListNode** head, int num)
{
	ListNode* new_node = new ListNode{ num, NULL };
	if (!new_node)
	{
		return false;
	}
	if (*head == NULL)
	{
		*head = new_node;
	}
	else
	{
		ListNode* current = *head;
		while (current->next_node)
		{
			current = current->next_node;
		}
		current->next_node = new_node;
	}
	return true;
}

//删
bool Pop(ListNode** head, int num)
{
	bool found = false;
	ListNode dummy{ num,NULL }; // 创建一个dummy节点,其值设置为0  
	dummy.next_node = *head;
	ListNode* current = &dummy;

	while (current->next_node != NULL)
	{
		if (current->next_node->num == num)
		{
			ListNode* temp = current->next_node;
			current->next_node = current->next_node->next_node;
			delete temp;
			found = true;
			break; // 找到后退出循环  
		}
		current = current->next_node;
	}

	*head = dummy.next_node; // 更新头节点  
	return found;
}

//查
ListNode* Find(ListNode *head, int num)
{
	ListNode* current = head;
	while (current)
	{
		if (current->num == num) 
		{
			return current;
		}
		current = current->next_node;
	}
	return NULL;
}

//改
bool Updata(ListNode** head, int num1, int num2)
{
	ListNode* current = *head;
	while (current)
	{
		if (current->num == num1) 
		{
			current->num = num2;
			return true;
		}
		current = current->next_node;
	}
	return false;
}

//打印链表
bool PrintList(ListNode** head)
{
	cout << "是否需要打印链表?(1.是 ,2.不是)" << endl;

	int yes_or_no;
	cin >> yes_or_no;

	if (yes_or_no == 1)
	{
		ListNode* current = *head;
		while (current)
		{
			cout << current->num << " ";
			current=current->next_node;
		}
		cout << endl;
		return true;
	}
	else if (yes_or_no == 2)
	{
		return false;
	}
	else
	{
		cout << "输入错误,取消打印" << endl;
		return false;
	}
}

bool Clear(ListNode** head)
{
	ListNode* temp;
	while (*head)
	{
		temp = *head;
		*head = (*head)->next_node;
		delete temp;
	}
	return true;
}

int main()
{
	ListNode* head = NULL;
	bool keepRunning = true;

	cout << "请输入要插入的数据数量(数量在100以内):" << endl;

	int count;
	cin >> count;

	if (count > 100 || count < 0)
	{
		cout << "输入的数据数量不在有效范围内,请重新输入:" << endl;
		return 1;
	}

	for (int i = 0; i < count; ++i) 
	{
		int num;
		cout << "请输入第 " << i + 1 << " 个数据:" << endl;
		cin >> num;
		if (!Push(&head, num)) {
			cout << "内存分配失败,无法插入数据。" << endl;
			Clear(&head);
			return 1;
		}
	}
	
	PrintList(&head);

	while (keepRunning)
	{
		
		int choice;
	
		do {
			cout << "请选择要执行的操作:" << endl;
			cout << "1.增加数据" << endl;
			cout << "2.删除数据" << endl;
			cout << "3.查询数据" << endl;
			cout << "4.修改数据" << endl;
			cout << "5.退出程序" << endl;

			cin >> choice;

			switch (choice)
			{
				case 1:
				{
					cout << "请输入要增加的数据数量:" << endl;
					int addCount;
					cin >> addCount;

					for (int i = 0; i < addCount; ++i)
					{
						int num;
						cout << "请输入要添加的数据:" << endl;
						cin >> num;
						if (!Push(&head, num))
						{
							cout << "内存分配失败,无法插入数据" << endl;
							break;
						}
					}
					break;
				}

				case 2:
				{
					cout << "请输入要删除的数据:" << endl;
					int num;
					cin >> num;

					if (Pop(&head, num))
					{
						cout << "删除数据" << num << "成功" << endl;
					}
					else
					{
						cout << "删除数据" << num << "失败" << endl;
					}
					break;
				}

				case 3:
				{
					cout << "请输入要查询的数据:" << endl;
					int num;
					cin >> num;
					ListNode* found = Find(head, num);
					if (found)
					{
						cout << "找到数据 " << num << endl;
					}
					else
					{
						cout << "未找到数据 " << num << endl;
					}
					break;
				}

				case 4:
				{
					cout << "请输入要将数据num1修改为数据num2的数值:" << endl;
					int num1, num2;
					cout << "num1:";
					cin >> num1;
					cout << endl << "num2:";
					cin >> num2;
					if (Updata(&head, num1, num2))
					{
						cout << "数据 " << num1 << " 修改为 " << num2 << " 成功" << endl;
					}
					else
					{
						cout << "数据 " << num1 << " 未找到,修改失败" << endl;
					}
					break;
				}

				case 5:
					cout << "退出程序。" << endl;
					keepRunning = false;
					break;

				default:
					cout << "无效的选择,请重新选择" << endl;
					break;
			}

			// 打印链表  
			PrintList(&head);

		} while (choice != 5);
	}

	// 清除链表  
	Clear(&head);

	return 0;
}

 

标签:node,current,head,ListNode,next,链表,num,查改,增删
From: https://www.cnblogs.com/hcrzhi/p/18092260

相关文章

  • 算法 链表 206.反转链表
    文章目录一.题目二.代码三.总结一.题目题意:反转一个单链表。示例:输入:1->2->3->4->5->NULL输出:5->4->3->2->1->NULL二.代码publicclassReverseList{publicstaticvoidmain(String[]args){ListNode2head=newListNode2(1,newList......
  • 算法 链表 160.链表相交
    文章目录一.题目二.代码三.总结一.题目力扣160:给你两个单链表的头节点headA和headB,请你找出并返回两个单链表相交的起始节点。如果两个链表没有交点,返回null。二.代码publicclassLeetCode160{staticclassListNode{intval;L......
  • 增删改查
    增增加单条数据insertintoDepartment(DepartmentName,DepartmentDesc)values('研发部','这是研发部')--插入单条数据多条/*insertinto[Rank](RankName,RankDesc)select'A','A级'unionselect'B','B级'unionselect'......
  • 代码随想录算法训练营第4天 | 链表 |
    24两两交换列表元素ListNode*swapPairs(ListNode*head){ListNode*shead=newListNode();//初始化shead->next=head;ListNode*cur=shead;ListNode*tmp;//确定先后顺序很重要while(cur->next!=nullptr&&cur->next->next!=nul......
  • 线性表的单链表
    目录1>.单链表的定义和表示2>.单链表基本操作1.初始化2.取值3.查找4.插入5.删除1>.单链表的定义和表示1.基本概念特点:用一组任意的存储单元存储线性表的数据元素(存储单元可以连续,也可以不连续)。对数据元素ai,存储本身的信息和一个指示直接后继的存储位置,这两部分信......
  • 03天【代码随想录算法训练营34期】第二章 链表part01(203.移除链表元素 、707.设计链表
    203.移除链表元素竟然可以做个假head,学到了classListNode(object):def__init__(self,val=0,next=None):self.val=valself.next=nextclassSolution(object):defremoveElements(self,head,val):dummy_head=ListNode(-1)......
  • 代码随想录算法训练营第3天 | 链表 |虚拟头哨兵
    链表理论基础链表节点的定义structListNode{intval;//节点上存储的元素ListNode*next;//指向下一个节点的指针ListNode(intx):val(x),next(NULL){}//节点的构造函数};==如果不自己定义构造函数,就无法通过ListNodep(5);来初始化203删除......
  • Rest-优雅的请求风格(图书增删改查的案例)
    前的浏览器只支持post/get请求,因此为了得到put/delete的请求方式需要使用Spring提供的HiddenHttpMethodFilter过滤器进行转换(只能转换post).前端代码<%--CreatedbyIntelliJIDEA.User:YRXDate:2024/3/13Time:13:29TochangethistemplateuseFile......
  • 数据结构——单向链表(C语言版)
    在数据结构和算法中,链表是一种常见的数据结构,它由一系列节点组成,每个节点包含数据和指向下一个节点的指针。在C语言中,我们可以使用指针来实现单向链表。下面将详细介绍如何用C语言实现单向链表。目录1.定义节点结构体2.初始化链表3.插入节点4.删除节点5.遍历链......
  • 数据结构链表交换
    把一个长度为n的数组转换成链表并把链表前两个节点交换位置和把链表最后两个节点交换位置。输入描述:第一行输入一个正整数n表示数组的长度第二行输入n个正整数,表示数组中各个元素的值输出描述:把数组转换成链表后输出交换位置后的链表输入:42345输出:3254#......