自定义函数中创建链表
时间: 2023-12-12 17:32:19 浏览: 43
在自定义函数中创建链表,需要定义一个链表节点的结构体,然后在函数中动态分配内存空间来创建节点,并将节点按照链表的方式连接起来。下面是一个示例代码:
```C++
struct ListNode {
int val;
ListNode* next;
ListNode(int x) : val(x), next(NULL) {}
};
ListNode* createList(vector<int>& nums) {
ListNode* head = new ListNode(0);
ListNode* p = head;
for (int i = 0; i < nums.size(); i++) {
ListNode* node = new ListNode(nums[i]);
p->next = node;
p = p->next;
}
return head->next;
}
```
上述代码中,我们定义了一个链表节点的结构体`ListNode`,其中包含一个整型变量`val`和一个指向下一个节点的指针`next`。然后我们定义了一个函数`createList`,该函数接受一个整型数组`nums`作为参数,返回一个链表的头节点指针。在函数中,我们首先创建一个头节点`head`,然后遍历整型数组`nums`,依次创建节点,并将节点按照链表的方式连接起来。最后返回头节点的下一个节点即可。
相关问题
结构体类型定义如下: struct stu{ char name[20]; float price; struct stu *next; }; 编写自定义函数创建链表,输入书名和价格,输入Y继续创建,输入其他字符结束创建,编写自定义函数输出最贵的书籍信息,编写自定义函数释放链表内存 输入格式为:请输入书名 价格: \n(循环) 输入格式:"%s %f" 是否继续创建的提示:“是否继续输入,按Y继续输入,其他键结束。\n” 输出提示:“result:\n” 输出格式:“%s %.2f\n”
下面是代码实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct stu {
char name[20];
float price;
struct stu *next;
};
// 创建链表
struct stu *createList() {
struct stu *head = NULL;
struct stu *tail = NULL;
char input[10];
do {
struct stu *node = (struct stu*)malloc(sizeof(struct stu));
printf("请输入书名和价格:\n");
scanf("%s %f", node->name, &(node->price));
node->next = NULL;
if (head == NULL) {
head = tail = node;
} else {
tail->next = node;
tail = node;
}
printf("是否继续输入,按Y继续输入,其他键结束。\n");
scanf("%s", input);
} while (strcmp(input, "Y") == 0 || strcmp(input, "y") == 0);
return head;
}
// 输出最贵的书籍信息
void printMax(struct stu *head) {
struct stu *p = head;
struct stu *maxNode = p;
while (p != NULL) {
if (p->price > maxNode->price) {
maxNode = p;
}
p = p->next;
}
printf("result:\n");
printf("%s %.2f\n", maxNode->name, maxNode->price);
}
// 释放链表内存
void freeList(struct stu *head) {
struct stu *p = head;
while (p != NULL) {
struct stu *temp = p;
p = p->next;
free(temp);
}
}
int main() {
struct stu *head = createList();
printMax(head);
freeList(head);
return 0;
}
```
1.(40分)输出最贵的书籍信息:结构体类型定义如下: struct stu{ char name[20]; float price; struct stu *next; }; 要求: (1)编写自定义函数创建链表,输入书名和价格。输入Y继续创建,输入其他字符结束创建。 (2)编写自定义函数输出最贵的书籍信息。(3)编写自定义函数释放链表内存。 **输入提示:"请输入书名 价格:\n”(循环)**输入格式:“%s%f” **是否继续创建的提示:"是否继续输入,按Y键继续输入,其他键 就结束.\n" CH输出提示“result“输出格式“%s%.2f””
以下是代码实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct stu {
char name[20];
float price;
struct stu* next;
};
// 创建链表
struct stu* create_list() {
struct stu* head = NULL;
struct stu* tail = NULL;
char choice;
do {
printf("请输入书名 价格:\n");
struct stu* node = (struct stu*)malloc(sizeof(struct stu));
scanf("%s%f", node->name, &node->price);
node->next = NULL;
if (head == NULL) {
head = node;
tail = node;
} else {
tail->next = node;
tail = node;
}
printf("是否继续输入,按Y键继续输入,其他键就结束.\n");
getchar();
choice = getchar();
} while (choice == 'Y' || choice == 'y');
return head;
}
// 输出最贵的书籍信息
void print_max(struct stu* head) {
struct stu* p = head;
struct stu* max_book = head;
while (p != NULL) {
if (p->price > max_book->price) {
max_book = p;
}
p = p->next;
}
printf("result:%s %.2f\n", max_book->name, max_book->price);
}
// 释放链表内存
void free_list(struct stu* head) {
struct stu* p = head;
while (p != NULL) {
struct stu* temp = p;
p = p->next;
free(temp);
}
}
int main() {
struct stu* head = create_list();
print_max(head);
free_list(head);
return 0;
}
```
运行效果如下:
```
请输入书名 价格:
C语言程序设计 60.5
是否继续输入,按Y键继续输入,其他键就结束.
y
请输入书名 价格:
Java编程思想 88.9
是否继续输入,按Y键继续输入,其他键就结束.
y
请输入书名 价格:
Python基础教程 75.2
是否继续输入,按Y键继续输入,其他键就结束.
n
result:Java编程思想 88.90
```