根据图片 需 要 源代码 用C语言!数据结构数据结构C语言C语言数据结构
你这图片有点模糊
可参考
#include <stdio.h>
#include <stdlib.h>
typedef struct Book {
int number;
struct Book* next;
} Book;
Book* createBookList() {
Book* head = NULL;
int bookNumbers[] = { 45, 53, 12, 3, 37, 24, 90, 100, 61, 78 };
for (int i = 0; i < sizeof(bookNumbers) / sizeof(int); i++) {
Book* newBook = (Book*)malloc(sizeof(Book));
newBook->number = bookNumbers[i];
newBook->next = NULL;
if (head == NULL) {
head = newBook;
}
else {
Book* current = head;
while (current->next != NULL) {
current = current->next;
}
current->next = newBook;
}
}
return head;
}
int findBook(Book* head, int bookNumber) {
int index = 0;
Book* current = head;
while (current != NULL) {
if (current->number == bookNumber) {
return index;
}
current = current->next;
index++;
}
return -1;
}
int main() {
Book* bookList = createBookList();
int bookNumber;
printf("请输入书籍编号:");
scanf("%d", &bookNumber);
int index = findBook(bookList, bookNumber);
if (index != -1) {
printf("书籍编号为%d的书籍在索引%d处找到。\n", bookNumber, index);
}
else {
printf("书籍编号为%d的书籍未找到。\n", bookNumber);
}
return 0;
}
既然说了书不多,看样子也没排序,简单的顺序查找即可。
以下程序实现了基于链表的顺序查找算法来查找图书编号:
#include <stdio.h>
#include <stdlib.h>
// 定义图书节点结构
typedef struct BookNode {
int number; // 书的编号
// 其他图书信息
// ...
struct BookNode* next; // 下一个节点指针
} BookNode;
// 创建图书节点
BookNode* createBookNode(int number) {
BookNode* newNode = (BookNode*)malloc(sizeof(BookNode));
if (newNode != NULL) {
newNode->number = number;
newNode->next = NULL;
}
return newNode;
}
// 顺序查找函数
int sequentialSearch(BookNode* head, int target) {
BookNode* currentNode = head;
int index = 0;
while (currentNode != NULL) {
if (currentNode->number == target) {
return index; // 找到目标编号,返回索引
}
currentNode = currentNode->next;
index++;
}
return -1; // 未找到目标编号,返回-1
}
int main() {
// 创建图书链表
BookNode* head = createBookNode(45);
head->next = createBookNode(53);
head->next->next = createBookNode(12);
head->next->next->next = createBookNode(3);
head->next->next->next->next = createBookNode(37);
head->next->next->next->next->next = createBookNode(24);
head->next->next->next->next->next->next = createBookNode(90);
head->next->next->next->next->next->next->next = createBookNode(100);
head->next->next->next->next->next->next->next->next = createBookNode(61);
head->next->next->next->next->next->next->next->next->next = createBookNode(78);
int target = 90;
int index = sequentialSearch(head, target);
if (index != -1) {
printf("图书编号 %d 存在,索引位置为 %d\n", target, index);
} else {
printf("图书编号 %d 不存在\n", target);
}
// 释放链表内存
BookNode* currentNode = head;
while (currentNode != NULL) {
BookNode* nextNode = currentNode->next;
free(currentNode);
currentNode = nextNode;
}
return 0;
}
该程序创建了一个简单的图书链表,并使用顺序查找算法查找目标图书编号。程序输出目标编号是否存在以及其索引位置。这只是一个示例程序,你可以根据实际需求和使用的查找算法进行修改和扩展。
上表中,优先级同为1 的几种运算符如果同时出现,那怎么确定表达式的优先级呢?这是很多初学者迷糊的地方。下表就整理了这些容易出错的情况: