使用C语言写 (1)实现顺序表的创建;(2)实现顺序表的插入;(3)实现顺序表的删除 (4)实现顺序表的查找;(5)实现顺序表的修改;(6)实现顺序表的显示;*具体内容: 定义一个包含图书信息(书号、
时间: 2024-12-25 18:32:53 浏览: 21
在C语言中,我们可以使用结构体来表示图书信息,并通过数组或链表来实现顺序表。这里我会给出基本的伪代码描述如何实现这些操作:
1. **顺序表创建** (利用数组实现)
```c
typedef struct {
int bookID;
char title[50];
char author[50];
} BookInfo;
// 创建一个固定大小的顺序表
BookInfo sequenceList[MAX_SIZE] = {0}; // 初始化为0
int size = 0;
```
2. **插入操作**
```c
void insert(BookInfo *list, int position, BookInfo newBook) {
if (position < 0 || position > size) {
printf("Invalid position.\n");
} else {
memmove(&sequenceList[position+1], &sequenceList[position], sizeof(BookInfo) * (size - position));
list[position] = newBook;
size++;
}
}
```
3. **删除操作**
```c
void delete(BookInfo *list, int position) {
if (position < 0 || position >= size) {
printf("Invalid position.\n");
} else {
memmove(&sequenceList[position], &sequenceList[position+1], sizeof(BookInfo) * (size - position - 1));
size--;
}
}
```
4. **查找操作**
```c
BookInfo* search(BookInfo *list, int bookID) {
for (int i = 0; i < size; i++) {
if (list[i].bookID == bookID) {
return &list[i];
}
}
return NULL; // 如果未找到,返回NULL
}
```
5. **修改操作**
```c
void modify(BookInfo *list, int position, BookInfo updatedBook) {
if (position < 0 || position >= size) {
printf("Invalid position.\n");
} else {
list[position] = updatedBook;
}
}
```
6. **显示顺序表**
```c
void display(BookInfo *list) {
for (int i = 0; i < size; i++) {
printf("ID: %d, Title: %s, Author: %s\n", list[i].bookID, list[i].title, list[i].author);
}
}
```
阅读全文