编写程序以顺序表和链表两种方式实现: (1) 对图书表的创建(初始创建5本书); (2) 分别在第5和第11的位置上插入两本书(011,python程序设计,23)和(012,数据结构,55) (3) 删除第2本书 (4) 获取第3本书的信息并打印 (5) 打印出表中最贵的书和最便宜的书。 (6) 打印经过上述操作后,所剩的图书信息。
时间: 2024-01-21 12:03:46 浏览: 71
好的,我可以为您编写这个程序。首先,我们需要定义一个图书结构体,包含书名、书号和价格等信息。然后,我们可以使用顺序表或链表来存储这些图书信息。下面分别给出两种实现方式的代码。
## 顺序表方式
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_SIZE 20
typedef struct Book {
char name[30];
char id[10];
float price;
} Book;
typedef struct BookList {
Book books[MAX_SIZE];
int length;
} BookList;
// 初始化图书表
void initBookList(BookList *list) {
Book books[] = {
{"C语言程序设计", "001", 32.5},
{"数据结构与算法", "002", 45.0},
{"计算机网络", "003", 55.0},
{"操作系统", "004", 60.5},
{"计算机组成原理", "005", 49.0}
};
memcpy(list->books, books, sizeof(books));
list->length = sizeof(books) / sizeof(Book);
}
// 在指定位置插入书籍
void insertBook(BookList *list, int index, Book book) {
if (index < 1 || index > list->length + 1) {
printf("插入位置不合法!\n");
return;
}
if (list->length >= MAX_SIZE) {
printf("图书表已满,无法插入!\n");
return;
}
for (int i = list->length; i >= index; i--) {
list->books[i] = list->books[i - 1];
}
list->books[index - 1] = book;
list->length++;
}
// 删除指定位置的书籍
void deleteBook(BookList *list, int index) {
if (index < 1 || index > list->length) {
printf("删除位置不合法!\n");
return;
}
for (int i = index - 1; i < list->length - 1; i++) {
list->books[i] = list->books[i + 1];
}
list->length--;
}
// 获取指定位置的书籍信息
Book getBook(BookList *list, int index) {
if (index < 1 || index > list->length) {
printf("获取位置不合法!\n");
exit(1);
}
return list->books[index - 1];
}
// 打印表中最贵的书和最便宜的书
void printMinMax(BookList *list) {
float maxPrice = 0, minPrice = list->books[0].price;
int maxIndex = -1, minIndex = 0;
for (int i = 0; i < list->length; i++) {
if (list->books[i].price > maxPrice) {
maxPrice = list->books[i].price;
maxIndex = i;
}
if (list->books[i].price < minPrice) {
minPrice = list->books[i].price;
minIndex = i;
}
}
printf("最贵的书为:%s,价格为%.2f元\n", list->books[maxIndex].name, maxPrice);
printf("最便宜的书为:%s,价格为%.2f元\n", list->books[minIndex].name, minPrice);
}
// 打印图书表
void printBookList(BookList *list) {
printf("图书表如下:\n");
printf("编号\t书名\t价格\n");
for (int i = 0; i < list->length; i++) {
printf("%s\t%s\t%.2f元\n", list->books[i].id, list->books[i].name, list->books[i].price);
}
}
int main() {
BookList list;
initBookList(&list);
printf("初始化后的图书表:\n");
printBookList(&list);
Book book1 = {"011", "python程序设计", 23};
Book book2 = {"012", "数据结构", 55};
insertBook(&list, 5, book1);
insertBook(&list, 11, book2);
printf("插入两本书籍后的图书表:\n");
printBookList(&list);
deleteBook(&list, 2);
printf("删除第2本书籍后的图书表:\n");
printBookList(&list);
Book book3 = getBook(&list, 3);
printf("第3本书籍的信息为:%s\t%s\t%.2f元\n", book3.id, book3.name, book3.price);
printMinMax(&list);
return 0;
}
```
## 链表方式
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Book {
char name[30];
char id[10];
float price;
struct Book *next;
} Book;
// 创建图书链表
Book *createBookList() {
Book *head = NULL;
Book *tail = NULL;
Book books[] = {
{"C语言程序设计", "001", 32.5, NULL},
{"数据结构与算法", "002", 45.0, NULL},
{"计算机网络", "003", 55.0, NULL},
{"操作系统", "004", 60.5, NULL},
{"计算机组成原理", "005", 49.0, NULL}
};
for (int i = 0; i < sizeof(books) / sizeof(Book); i++) {
Book *p = (Book *)malloc(sizeof(Book));
memcpy(p, &books[i], sizeof(Book));
if (head == NULL) {
head = p;
tail = p;
} else {
tail->next = p;
tail = p;
}
}
return head;
}
// 在指定位置插入书籍
void insertBook(Book **head, int index, Book *book) {
if (index < 1) {
printf("插入位置不合法!\n");
return;
}
Book *p = *head;
int i = 1;
while (p != NULL && i < index - 1) {
p = p->next;
i++;
}
if (p == NULL) {
printf("插入位置不合法!\n");
return;
}
book->next = p->next;
p->next = book;
}
// 删除指定位置的书籍
void deleteBook(Book **head, int index) {
if (index < 1) {
printf("删除位置不合法!\n");
return;
}
Book *p = *head;
if (index == 1) {
*head = p->next;
free(p);
return;
}
int i = 1;
while (p != NULL && i < index - 1) {
p = p->next;
i++;
}
if (p == NULL || p->next == NULL) {
printf("删除位置不合法!\n");
return;
}
Book *q = p->next;
p->next = q->next;
free(q);
}
// 获取指定位置的书籍信息
Book *getBook(Book *head, int index) {
if (index < 1) {
printf("获取位置不合法!\n");
exit(1);
}
Book *p = head;
int i = 1;
while (p != NULL && i < index) {
p = p->next;
i++;
}
if (p == NULL) {
printf("获取位置不合法!\n");
exit(1);
}
return p;
}
// 打印表中最贵的书和最便宜的书
void printMinMax(Book *head) {
float maxPrice = 0, minPrice = head->price;
Book *p = head;
while (p != NULL) {
if (p->price > maxPrice) {
maxPrice = p->price;
}
if (p->price < minPrice) {
minPrice = p->price;
}
p = p->next;
}
p = head;
while (p != NULL) {
if (p->price == maxPrice) {
printf("最贵的书为:%s,价格为%.2f元\n", p->name, maxPrice);
break;
}
p = p->next;
}
p = head;
while (p != NULL) {
if (p->price == minPrice) {
printf("最便宜的书为:%s,价格为%.2f元\n", p->name, minPrice);
break;
}
p = p->next;
}
}
// 打印图书表
void printBookList(Book *head) {
printf("图书表如下:\n");
printf("编号\t书名\t价格\n");
Book *p = head;
while (p != NULL) {
printf("%s\t%s\t%.2f元\n", p->id, p->name, p->price);
p = p->next;
}
}
int main() {
Book *head = createBookList();
printf("初始化后的图书表:\n");
printBookList(head);
Book book1 = {"011", "python程序设计", 23, NULL};
Book book2 = {"012", "数据结构", 55, NULL};
insertBook(&head, 5, &book1);
insertBook(&head, 11, &book2);
printf("插入两本书籍后的图书表:\n");
printBookList(head);
deleteBook(&head, 2);
printf("删除第2本书籍后的图书表:\n");
printBookList(head);
Book *book3 = getBook(head, 3);
printf("第3本书籍的信息为:%s\t%s\t%.2f元\n", book3->id, book3->name, book3->price);
printMinMax(head);
return 0;
}
```
以上两个程序可以实现相同的功能,您可以根据需要选择其中一个来使用。
阅读全文