jupyter 中导入python文件
时间: 2024-04-30 20:23:56 浏览: 22
可以的,我可以回答这个问题。使用头插法建立链表时,每个新的节点插入到链表的头部,而使用尾插法建立链表时,每个新的节点插入到链表的末尾。针对字符串“hello”,使用头插法建立链表的顺序为"o"→"l"→"l"→"e"→"h",使用尾插法建立链表的顺序为"h"→"e"→"l"→"l"→"o"。代码实现可以参考以下示例:
使用头插法:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct ListNode {
char val;
struct ListNode* next;
};
struct ListNode* createListByHead(char* str) {
struct ListNode* head = NULL;
int len = strlen(str);
for (int i = 0; i < len; i++) {
struct ListNode* node = (struct ListNode*)malloc(sizeof(struct ListNode));
node->val = str[i];
node->next = head;
head = node;
}
return head;
}
void printList(struct ListNode* head) {
while (head) {
printf("%c", head->val);
head = head->next;
}
printf("\n");
}
int main() {
char str[] = "hello";
struct ListNode* head = createListByHead(str);
printList(head);
return 0;
}
```
使用尾插法:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct ListNode {
char val;
struct ListNode* next;
};
struct ListNode* createListByTail(char* str) {
struct ListNode* head = NULL;
struct ListNode* tail = NULL;
int len = strlen(str);
for (int i = 0; i < len; i++) {
struct ListNode* node = (struct ListNode*)malloc(sizeof(struct ListNode));
node->val = str[i];
node->next = NULL;
if (!head) {
head = node;
tail = node;
} else {
tail->next = node;
tail = tail->next;
}
}
return head;
}
void printList(struct ListNode* head) {
while (head) {
printf("%c", head->val);
head = head->next;
}
printf("\n");
}
int main() {
char str[] = "hello";
struct ListNode* head = createListByTail(str);
printList(head);
return 0;
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)