用c语言写出基于线性表的图书管理
时间: 2023-03-29 08:03:28 浏览: 73
系统,实现图书的增删改查操作。
可以的,基于线性表的图书管理系统可以使用C语言来实现。我们可以定义一个结构体来表示每本书的信息,包括书名、作者、出版社、出版日期等等。然后,我们可以使用链表或数组来存储这些结构体,实现图书的增删改查操作。具体实现方式可以根据具体需求来设计。
相关问题
用c语言基于线性表的图书信息管理
图书信息管理是一个常见的应用场景,可以使用C语言基于线性表来实现。首先,我们需要定义一个结构体来表示图书的信息:
```c
typedef struct {
int bookId;
char bookName[50];
char author[50];
int quantity;
} Book;
```
然后,我们可以使用一个动态数组来存储图书信息:
```c
Book *bookList = NULL; // 初始为空
int numOfBooks = 0; // 图书数量
```
接下来,我们可以实现一些操作函数来管理图书信息。比如,添加图书:
```c
void addBook(int bookId, char *bookName, char *author, int quantity) {
Book book;
book.bookId = bookId;
strcpy(book.bookName, bookName);
strcpy(book.author, author);
book.quantity = quantity;
// 将新图书添加到数组末尾
bookList = realloc(bookList, (numOfBooks + 1) * sizeof(Book));
bookList[numOfBooks] = book;
numOfBooks++;
}
```
还可以实现删除图书、更新图书信息、查找图书等操作函数。比如,删除图书:
```c
void deleteBook(int bookId) {
int i, j;
for (i = 0; i < numOfBooks; i++) {
if (bookList[i].bookId == bookId) {
// 将数组中的后续元素向前移动
for (j = i; j < numOfBooks - 1; j++) {
bookList[j] = bookList[j+1];
}
break;
}
}
// 重新分配内存,减少一个空间大小
bookList = realloc(bookList, (numOfBooks - 1) * sizeof(Book));
numOfBooks--;
}
```
通过以上操作函数,我们可以实现对图书信息的增删改查。这样,基于线性表的图书信息管理系统就实现了。当然,这只是一个简单的实现示例,实际情况下可能还需要考虑更多的功能和细节。
用c语言写一个线性表,并用类封装
下面是用 C 语言实现线性表(单链表)的代码,并使用类进行封装:
```c
#include <stdio.h>
#include <stdlib.h>
// 定义节点结构体
typedef struct Node {
int data; // 数据域
struct Node* next; // 指针域
} Node;
// 定义类(结构体)结构体
typedef struct LinkedList {
Node* head; // 头指针
int size; // 元素个数
// 成员函数
void (*add)(struct LinkedList*, int);
int (*get)(struct LinkedList*, int);
int (*indexOf)(struct LinkedList*, int);
void (*remove)(struct LinkedList*, int);
} LinkedList;
// 初始化线性表
void init(LinkedList* list) {
list->head = NULL;
list->size = 0;
}
// 添加元素
void add(LinkedList* list, int data) {
Node* node = (Node*) malloc(sizeof(Node));
node->data = data;
node->next = NULL;
if (list->head == NULL) {
list->head = node;
} else {
Node* p = list->head;
while (p->next != NULL) {
p = p->next;
}
p->next = node;
}
list->size++;
}
// 获取元素
int get(LinkedList* list, int index) {
if (index < 0 || index >= list->size) {
printf("Index out of range.\n");
return -1;
}
Node* p = list->head;
int i = 0;
while (i < index) {
p = p->next;
i++;
}
return p->data;
}
// 查找元素
int indexOf(LinkedList* list, int data) {
Node* p = list->head;
int index = 0;
while (p != NULL) {
if (p->data == data) {
return index;
}
p = p->next;
index++;
}
return -1;
}
// 删除元素
void remove(LinkedList* list, int index) {
if (index < 0 || index >= list->size) {
printf("Index out of range.\n");
return;
}
Node* p = list->head;
if (index == 0) {
list->head = p->next;
free(p);
} else {
int i = 0;
while (i < index - 1) {
p = p->next;
i++;
}
Node* q = p->next;
p->next = q->next;
free(q);
}
list->size--;
}
// 创建并返回一个新的线性表对象
LinkedList newLinkedList() {
LinkedList list;
// 将成员函数与相应的函数指针绑定
list.add = add;
list.get = get;
list.indexOf = indexOf;
list.remove = remove;
init(&list);
return list;
}
int main() {
// 创建一个新的线性表对象
LinkedList list = newLinkedList();
// 添加元素
list.add(&list, 1);
list.add(&list, 2);
list.add(&list, 3);
// 获取元素并输出
printf("%d\n", list.get(&list, 0)); // 1
printf("%d\n", list.get(&list, 1)); // 2
printf("%d\n", list.get(&list, 2)); // 3
// 查找元素并输出索引
printf("%d\n", list.indexOf(&list, 2)); // 1
printf("%d\n", list.indexOf(&list, 4)); // -1
// 删除元素
list.remove(&list, 1);
printf("%d\n", list.get(&list, 1)); // 3
return 0;
}
```
上面的代码使用了函数指针来实现类的封装。我们定义了一个 `LinkedList` 结构体,其中包含了线性表的头指针和元素个数,以及四个成员函数 `add`、`get`、`indexOf`、`remove`,这些函数都接受一个指向 `LinkedList` 结构体的指针作为第一个参数。在 `newLinkedList` 函数中,我们将这些成员函数与相应的函数指针绑定起来,并返回一个新的 `LinkedList` 对象。
使用这个封装好的类,我们可以很方便地创建一个新的线性表对象,并调用相应的成员函数来操作它。