用尾插法建立单链表,当输入元素为0时停止并输出链表
时间: 2024-05-08 21:17:27 浏览: 22
以下是Python的代码实现:
```python
class ListNode:
def __init__(self, val):
self.val = val
self.next = None
# 尾插法建立链表
head = ListNode(None)
tail = head
while True:
val = int(input())
if val == 0:
break
node = ListNode(val)
tail.next = node
tail = node
# 输出链表
node = head.next
while node:
print(node.val)
node = node.next
```
首先定义了一个`ListNode`类表示链表的节点,包含一个`val`属性表示节点的值,和一个`next`属性指向下一个节点。然后用`head`表示链表的头节点,初始时指向一个值为`None`的节点。用`tail`表示链表的尾节点,初始时指向`head`。每次输入一个元素,如果为0则停止循环,否则创建一个新的节点,并将其连接到链表的尾部,然后将`tail`指向这个新节点,继续循环输入。最后从链表的头节点开始,依次输出每个节点的值。
相关问题
C语言用尾插法建立单链表,当输入元素为0时停止并输出链表
的所有元素。
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node *next;
} Node;
int main() {
Node *head = NULL;
Node *tail = NULL;
int num;
do {
scanf("%d", &num);
if (num == 0) {
break;
}
Node *node = (Node *)malloc(sizeof(Node));
node->data = num;
node->next = NULL;
if (tail == NULL) {
head = node;
tail = node;
} else {
tail->next = node;
tail = node;
}
} while (1);
Node *p = head;
while (p != NULL) {
printf("%d ", p->data);
p = p->next;
}
printf("\n");
return 0;
}
```
输入:
```
1 2 3 4 5 6 0
```
输出:
```
1 2 3 4 5 6
```
用C语言利用尾插法建立单链表,利用头插法完成单链表的逆置并输出逆置后的单链表
下面是用 C 语言利用尾插法建立单链表,利用头插法完成单链表的逆置并输出逆置后的单链表的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
// 定义单链表节点结构体
typedef struct node {
int data;
struct node *next;
} Node;
// 尾插法建立单链表
Node *createLinkedList() {
int val;
Node *head = NULL, *tail = NULL;
while (1) {
printf("请输入节点的值(-1 结束输入):");
scanf("%d", &val);
if (val == -1) {
break;
}
Node *newNode = (Node *)malloc(sizeof(Node));
newNode->data = val;
newNode->next = NULL;
if (head == NULL) {
head = newNode;
tail = newNode;
} else {
tail->next = newNode;
tail = newNode;
}
}
return head;
}
// 头插法逆置单链表
Node *reverseLinkedList(Node *head) {
Node *newHead = NULL;
while (head != NULL) {
Node *p = head;
head = head->next;
p->next = newHead;
newHead = p;
}
return newHead;
}
// 输出单链表
void printLinkedList(Node *head) {
Node *p = head;
while (p != NULL) {
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
int main() {
// 建立单链表
Node *head = createLinkedList();
printf("建立的单链表为:");
printLinkedList(head);
// 逆置单链表
Node *newHead = reverseLinkedList(head);
printf("逆置后的单链表为:");
printLinkedList(newHead);
return 0;
}
```
该程序首先通过尾插法建立单链表,调用 reverseLinkedList 函数逆置单链表,再调用 printLinkedList 函数输出逆置后的单链表。逆置单链表的过程是从头到尾遍历原链表,每次取下头节点插入到新链表的头部,最终得到逆置后的单链表。
相关推荐
![-](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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)