某学校的图书管理系统中,书本种类信息包括:书号、书名、价格三项属性,且书的种类数不超过20。编写代码,分别用顺序存储方式和链式存储方式实现该图书管理系统数据结构的构建,并至少能够进行初始化、清空、增加
时间: 2024-10-16 19:22:41 浏览: 32
在图书管理系统的数据结构中,我们可以分别使用数组(顺序存储)和链表(链式存储)来实现书籍信息的存储。这里我会简要地展示如何在C#中实现这两个数据结构的操作。
**顺序存储(数组)实现:**
```csharp
using System;
public class Book
{
public int bookID { get; set; }
public string title { get; set; }
public decimal price { get; set; }
}
class LibraryArray
{
private Book[] books;
private int count = 0;
public LibraryArray(int capacity)
{
books = new Book[capacity];
}
// 初始化
public void Initialize(Book book)
{
if (count < books.Length)
books[count++] = book;
}
// 清空
public void Clear()
{
Array.Clear(books, 0, count);
count = 0;
}
// 增加书籍
public void AddBook(Book book)
{
if (count < books.Length)
books[count++] = book;
}
}
```
**链式存储(链表)实现:**
```csharp
public class BookNode
{
public int bookID { get; set; }
public string title { get; set; }
public decimal price { get; set; }
public BookNode next = null;
}
class LibraryLinkedList
{
private BookNode head;
public LibraryLinkedList()
{
head = null;
}
// 初始化
public void Initialize(BookNode node)
{
if (head == null)
head = node;
}
// 清空
public void Clear()
{
BookNode temp = head;
while (temp != null)
{
BookNode nextTemp = temp.next;
temp = null;
}
head = null;
}
// 增加书籍
public void AddBook(BookNode book)
{
book.next = head;
head = book;
}
}
```
阅读全文