python利用头插法将列表a=[0,1,2,3,4,5,6]插入到单向循环链表
时间: 2023-10-03 21:10:25 浏览: 42
的头部
```python
class Node:
def __init__(self, data=None, next=None):
self.data = data
self.next = next
class CircularLinkedList:
def __init__(self):
self.head = None
def insert_at_beginning(self, data):
node = Node(data, None)
if not self.head:
node.next = node
self.head = node
else:
curr = self.head
while curr.next != self.head:
curr = curr.next
node.next = self.head
curr.next = node
self.head = node
a = [0,1,2,3,4,5,6]
circular_list = CircularLinkedList()
for i in a:
circular_list.insert_at_beginning(i)
```
相关问题
python利用头插法单向循环链表
以下是Python代码示例,实现了头插法单向循环链表:
```python
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def add_to_front(self, data):
new_node = Node(data)
if self.head is None:
self.head = new_node
new_node.next = self.head
else:
curr_node = self.head
while curr_node.next != self.head:
curr_node = curr_node.next
curr_node.next = new_node
new_node.next = self.head
self.head = new_node
def print_list(self):
if self.head is None:
print("List is empty")
else:
curr_node = self.head
while True:
print(curr_node.data, end=" ")
curr_node = curr_node.next
if curr_node == self.head:
break
print()
```
使用示例:
```python
ll = LinkedList()
ll.add_to_front(3)
ll.add_to_front(2)
ll.add_to_front(1)
ll.print_list() # 1 2 3
```
头插法创建链表 输入: 4 1 2 3 4 5 6 7 8 输出: 7 8 5 6 3 4 1 2
好的,这是一个关于头插法创建链表的问题。我来解答一下。
首先,头插法是指在链表头部插入新节点,新节点成为链表的新头部。这种方法可以方便地实现链表的逆序,并且时间复杂度为 O(n)。
下面是使用头插法创建链表并逆序的代码:
```
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* next;
} Node;
Node* createList() {
Node* head = NULL;
Node* p = NULL;
int n, i, x;
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%d", &x);
p = (Node*)malloc(sizeof(Node));
p->data = x;
p->next = head;
head = p;
}
return head;
}
void printList(Node* head) {
Node* p = head;
while (p != NULL) {
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
void reverseList(Node** head) {
Node* prev = NULL;
Node* curr = *head;
Node* next = NULL;
while (curr != NULL) {
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
*head = prev;
}
int main() {
Node* head = createList();
printList(head);
reverseList(&head);
printList(head);
return 0;
}
```
在 createList 函数中,先读入链表的长度 n,然后依次读入每个节点的值 x,创建新节点并将其插入到链表头部,最后返回链表的头指针。
在 reverseList 函数中,使用三个指针 prev、curr 和 next 分别表示当前节点的前一个节点、当前节点和当前节点的后一个节点,依次将当前节点的 next 指针指向前一个节点,然后将 prev、curr 和 next 分别向后移动一位,直到 curr 指向链表的最后一个节点为止。最后将 head 指针指向 prev,完成链表的逆序。
最后在主函数中,先打印原始链表,然后调用 reverseList 函数逆序链表,最后再打印逆序后的链表。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)