编写程序,对单链表做生成运算。 1)头插法 2)尾插法
时间: 2023-06-14 22:03:18 浏览: 45
以下是Python语言实现单链表的生成运算,包括头插法和尾插法:
```python
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def createList_head(nums):
head = ListNode()
for num in nums:
node = ListNode(num)
node.next = head.next
head.next = node
return head.next
def createList_tail(nums):
head = ListNode()
tail = head
for num in nums:
node = ListNode(num)
tail.next = node
tail = node
return head.next
```
其中,`createList_head`为头插法的实现,`createList_tail`为尾插法的实现。两种方法都接收一个列表作为参数,返回一个单链表的头结点。在头插法中,我们从列表的头部开始遍历,每次新建一个节点并将其插入到链表的头部;在尾插法中,我们从列表的头部开始遍历,每次新建一个节点并将其插入到链表的尾部。
相关问题
用c++语言 编写程序,对单链表做生成运算。 1)头插法 2)尾插法
以下是用C语言实现单链表的生成,包括头插法和尾插法:
```c
#include <stdio.h>
#include <stdlib.h>
//定义链表结构体
typedef struct Node {
int data;
struct Node *next;
} Node;
//头插法创建链表
Node *createListHead(int n) {
Node *head = NULL;
Node *p = NULL;
int i;
for (i = 0; i < n; i++) {
p = (Node *)malloc(sizeof(Node));
printf("请输入第%d个节点的值: ", i + 1);
scanf("%d", &(p->data));
p->next = head;
head = p;
}
return head;
}
//尾插法创建链表
Node *createListTail(int n) {
Node *head = NULL;
Node *tail = NULL;
Node *p = NULL;
int i;
for (i = 0; i < n; i++) {
p = (Node *)malloc(sizeof(Node));
printf("请输入第%d个节点的值: ", i + 1);
scanf("%d", &(p->data));
p->next = NULL;
if (head == NULL) {
head = p;
tail = p;
} else {
tail->next = p;
tail = p;
}
}
return head;
}
//打印链表
void printList(Node *head) {
Node *p = head;
while (p != NULL) {
printf("%d ", p->data);
p = p->next;
}
}
int main() {
int n;
printf("请输入链表节点个数: ");
scanf("%d", &n);
printf("使用头插法创建链表: \n");
Node *head1 = createListHead(n);
printf("链表的内容为: ");
printList(head1);
printf("\n使用尾插法创建链表: \n");
Node *head2 = createListTail(n);
printf("链表的内容为: ");
printList(head2);
return 0;
}
```
使用方法:在终端输入 `gcc -o main main.c` 编译程序,然后输入 `./main` 运行程序,按照提示输入节点个数和每个节点的值即可。
编写程序,对单链表做生成运算。 1)头插法 2)尾插法 3)改写为类模板,将上述两个封装为Link类中的方法。
以下是使用C++语言编写的单链表生成运算的程序,包括头插法和尾插法,以及使用类模板封装的Link类中的方法:
```C++
#include <iostream>
using namespace std;
// 定义单链表结构体
template <typename T>
struct ListNode {
T val;
ListNode<T>* next;
ListNode(T x) : val(x), next(NULL) {}
};
// 头插法
template <typename T>
ListNode<T>* createListHead(int n) {
ListNode<T>* head = new ListNode<T>(0);
for (int i = 0; i < n; i++) {
T x;
cin >> x;
ListNode<T>* p = new ListNode<T>(x);
p->next = head->next;
head->next = p;
}
return head->next;
}
// 尾插法
template <typename T>
ListNode<T>* createListTail(int n) {
ListNode<T>* head = new ListNode<T>(0);
ListNode<T>* tail = head;
for (int i = 0; i < n; i++) {
T x;
cin >> x;
ListNode<T>* p = new ListNode<T>(x);
tail->next = p;
tail = p;
}
return head->next;
}
// Link类
template <typename T>
class Link {
public:
// 头插法
static ListNode<T>* createListHead(int n) {
ListNode<T>* head = new ListNode<T>(0);
for (int i = 0; i < n; i++) {
T x;
cin >> x;
ListNode<T>* p = new ListNode<T>(x);
p->next = head->next;
head->next = p;
}
return head->next;
}
// 尾插法
static ListNode<T>* createListTail(int n) {
ListNode<T>* head = new ListNode<T>(0);
ListNode<T>* tail = head;
for (int i = 0; i < n; i++) {
T x;
cin >> x;
ListNode<T>* p = new ListNode<T>(x);
tail->next = p;
tail = p;
}
return head->next;
}
};
int main() {
// 头插法生成单链表
cout << "Please input the length of the list:";
int n;
cin >> n;
cout << "Please input the list:";
ListNode<int>* head1 = createListHead<int>(n);
cout << "The list created by head insertion is:";
while (head1) {
cout << head1->val << " ";
head1 = head1->next;
}
cout << endl;
// 尾插法生成单链表
cout << "Please input the length of the list:";
cin >> n;
cout << "Please input the list:";
ListNode<int>* head2 = createListTail<int>(n);
cout << "The list created by tail insertion is:";
while (head2) {
cout << head2->val << " ";
head2 = head2->next;
}
cout << endl;
// 使用Link类生成单链表
cout << "Please input the length of the list:";
cin >> n;
cout << "Please input the list:";
ListNode<int>* head3 = Link<int>::createListHead(n);
cout << "The list created by head insertion is:";
while (head3) {
cout << head3->val << " ";
head3 = head3->next;
}
cout << endl;
cout << "Please input the length of the list:";
cin >> n;
cout << "Please input the list:";
ListNode<int>* head4 = Link<int>::createListTail(n);
cout << "The list created by tail insertion is:";
while (head4) {
cout << head4->val << " ";
head4 = head4->next;
}
cout << endl;
return 0;
}
```
使用时,可以根据需要选择相应的方法。例如,以下是使用头插法生成单链表的示例:
```
Please input the length of the list:5
Please input the list:1 2 3 4 5
The list created by head insertion is:5 4 3 2 1
```
以上程序中,我们使用了模板类,可以根据需要修改元素类型。