【PTA】基于顺序存储结构的图书信息表的创建和输出 (18 分)

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

我发现我最后输出老是忘记r=r->next ;orz…
(这代码比我前两次写的简洁多了~

#include<iostream>
using namespace std;

typedef struct LNode
{	
	string ISBN;
	string name;
	double price;
	struct LNode *next;
}LNode,*LinkList;

int createList(LinkList &L)
{
	int num=0;
	L=new LNode;
	L->next=NULL;
	LinkList r=L;//r是扫描指针 
	string name;
	string ISBN;
	double price;
	while(1)
	{
		cin>>ISBN>>name>>price;
		if(ISBN=="0"||name=="0"||price==0) break;
		LinkList p=new LNode;
		p->ISBN=ISBN;
		p->name=name;
		p->price=price;
		p->next=NULL;
		r->next=p;
		r=p;
		num++;
	}
	
	return num;
}

int main()
{
	LinkList s1;
	int num=createList(s1);
	LinkList r=s1->next;
	
	cout<<num<<endl;
	while(r)
	{		
		cout.precision(2);
		cout<<r->ISBN<<" "<<r->name<<" "<<fixed<<r->price;
		if(r->next) cout<<endl;
		r=r->next;
	}
	return 0;
}
  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
稀疏矩阵是其中大多数元素为0的矩阵,我们可以使用十字链示稀疏矩阵,从而节省存储空间。下面是基于十字链存储的稀疏矩阵创建输出操作的实现。 首先,我们定义一个节点类,用于示稀疏矩阵的非零元素节点。该节点类包含四个成员变量:row、col、value和right。其中row和col分别示该节点所在的行和列的索引值,value示该节点的数值,right是指向下一个非零元素的指针。 然后,我们创建一个稀疏矩阵类,该类包含一个头节点head、一个保存行头节点的一维数组rows、一个保存列头节点的一维数组cols以及两个整型变量row_num和col_num。头节点head用于示稀疏矩阵的整体信息,rows和cols数组分别用于保存每一行和每一列的头节点,row_num和col_num分别保存稀疏矩阵的行数和列数。 在创建操作中,我们首先通过读取输入,获取稀疏矩阵的行数row_num和列数col_num。然后,根据row_num和col_num创建rows和cols数组并初始化头节点head。接下来,我们通过遍历输入,获取每个非零元素的行列索引和数值,并创建相应的节点,将其插入到十字链中。最后,我们根据稀疏矩阵的规模和十字链结构输出稀疏矩阵的元素。 具体的实现过程可以参考以下伪代码: class Node: def __init__(self, row, col, value): self.row = row self.col = col self.value = value self.right = None class SparseMatrix: def __init__(self): self.head = Node(-1, -1, -1) self.rows = [] self.cols = [] self.row_num = 0 self.col_num = 0 def create_sparse_matrix(self): self.row_num, self.col_num = input().split() self.row_num = int(self.row_num) self.col_num = int(self.col_num) self.rows = [Node(-1, -1, -1) for _ in range(self.row_num)] self.cols = [Node(-1, -1, -1) for _ in range(self.col_num)] self.head.row = self.row_num self.head.col = self.col_num for _ in range(self.row_num): row_data = input().split() row = int(row_data[0]) col = int(row_data[1]) value = int(row_data[2]) node = Node(row, col, value) # 插入链中 self.insert_node(node) def insert_node(self, node): # 插入到对应行的链中 if self.rows[node.row].right is None: self.rows[node.row].right = node else: cur = self.rows[node.row].right while cur.right is not None and cur.right.col < node.col: cur = cur.right node.right = cur.right cur.right = node # 插入到对应列的链中 if self.cols[node.col].right is None: self.cols[node.col].right = node else: cur = self.cols[node.col].right while cur.right is not None and cur.right.row < node.row: cur = cur.right node.right = cur.right cur.right = node def output_sparse_matrix(self): print(self.row_num, self.col_num) for i in range(self.row_num): cur = self.rows[i].right while cur is not None: print(cur.row, cur.col, cur.value) cur = cur.right 通过以上代码,我们实现了基于十字链存储的稀疏矩阵的创建输出操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

karshey

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值