有序插入

     设顺序表La中的数据元素递增有序。编写一个程序,将元素n插入到顺序表的适当位置上,以保持表的有序。
#include<iostream>
using namespace std;
#define  MAXSIZE  100
struct   SqList
{
	int data[MAXSIZE];
	int length;
};
int main()
{
	int i, n;
	SqList   La = { { 1, 3, 4, 7, 11, 18, 29, 47, 76, 123 }, 10 };
	cout << "线性表的初始情况为: ";
	for (i = 0; i < La.length; i++)
		cout << La.data[i] << " ";
	cout << endl << "请输入待插入的元素n : ";
	cin >> n;
	if (La.length == MAXSIZE)
	{
		cout << "线性表已满,不能插入!" << endl;
		return 0;

	}
	for (i = La.length; i>0 && n < La.data[i - 1]; i--)
		La.data[i] = La.data[i - 1];
	La.data[i] = n; La.length++;
	cout << "插入元素" << n << "后,线性表的情况为: ";
	for (i = 0; i < La.length; i++)
		cout << La.data[i] << " ";
	cout << endl;
	return 0;
}

在之前的代码基础上实现素材有序插入,通常是按照某个特定的属性(如素材质量、热度等)进行排序插入。下面以按照素材质量进行有序插入为例给实现代码。 假设 `AIGCMaterial` 结构体定义如下: ```cpp struct AIGCMaterial { int id; double quality; std::string name; std::string type; int hotDegree; AIGCMaterial* next; }; ``` 以下是实现按素材质量有序插入的函: ```cpp // 按素材质量有序插入新节点 void insertOrderedByQuality(AIGCMaterial* head, AIGCMaterial* newNode) { AIGCMaterial* current = head; while (current->next != nullptr && current->next->quality < newNode->quality) { current = current->next; } newNode->next = current->next; current->next = newNode; } ``` 该函的工作原理是遍历链表,找到合适的插入位置,使得链表始终按素材质量从小到大排序。 使用示例: ```cpp #include <iostream> #include <string> struct AIGCMaterial { int id; double quality; std::string name; std::string type; int hotDegree; AIGCMaterial* next; }; // 创建头结点(空结点) AIGCMaterial* createHeadNode() { AIGCMaterial* head = new AIGCMaterial(); head->id = -1; // 头结点特殊ID head->quality = 0; head->name = "HEAD"; head->type = "HEAD"; head->hotDegree = 0; head->next = nullptr; return head; } // 按素材质量有序插入新节点 void insertOrderedByQuality(AIGCMaterial* head, AIGCMaterial* newNode) { AIGCMaterial* current = head; while (current->next != nullptr && current->next->quality < newNode->quality) { current = current->next; } newNode->next = current->next; current->next = newNode; } // 打印链表 void printList(AIGCMaterial* head) { AIGCMaterial* current = head->next; while (current != nullptr) { std::cout << "ID: " << current->id << ", Quality: " << current->quality << ", Name: " << current->name << std::endl; current = current->next; } } int main() { AIGCMaterial* head = createHeadNode(); // 创建新节点 AIGCMaterial* node1 = new AIGCMaterial(); node1->id = 1; node1->quality = 3.0; node1->name = "Material1"; node1->type = "Type1"; node1->hotDegree = 10; node1->next = nullptr; AIGCMaterial* node2 = new AIGCMaterial(); node2->id = 2; node2->quality = 1.0; node2->name = "Material2"; node2->type = "Type2"; node2->hotDegree = 20; node2->next = nullptr; AIGCMaterial* node3 = new AIGCMaterial(); node3->id = 3; node3->quality = 2.0; node3->name = "Material3"; node3->type = "Type3"; node3->hotDegree = 30; node3->next = nullptr; // 有序插入节点 insertOrderedByQuality(head, node1); insertOrderedByQuality(head, node2); insertOrderedByQuality(head, node3); // 打印链表 printList(head); return 0; } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值