编程实现:输入一个正整数 repeat (0<repeat<10),做 repeat 次下列运算: 输入一个正整数 n(0<n<=9)和一组(n个)整数,建立一个单向链表,再输入一个整数 x,将链表中最后一个与x相等的整数删除。 输入输出示例:括号内为说明
题目要求输入一个正整数repeat,然后进行repeat次操作。每次操作需要输入一个正整数n,和一组n个整数,构建一个单向链表。然后输入一个整数x,删除链表中最后一个值等于x的节点。最后输出操作结果,即链表的所有节点值。
示例: 输入样例: 2 3 1 2 3 2 4 4 2 3 4 3 输出样例: 1 2 3 4 4 3
编程实现:输入一个正整数 repeat (0<repeat<10),做 repeat 次下列运算:\n输入一个正整数 n(0<n<=9)和一组(n个)整数,建立一个单向链表,再输入一个整数 x,将链表中
回答1:
的所有值为 x 的节点删除,最后输出删除后的链表。
实现思路:
定义一个链表节点结构体,包含一个整型数据域和一个指向下一个节点的指针域。
定义一个函数 createList,用于创建一个长度为 n 的单向链表,并返回链表的头节点指针。
定义一个函数 deleteNode,用于删除链表中所有值为 x 的节点。
在主函数中,先输入 repeat,然后循环 repeat 次,每次输入 n 和 n 个整数,调用 createList 函数创建链表,再输入 x,调用 deleteNode 函数删除链表中所有值为 x 的节点,最后输出删除后的链表。
代码实现:
#include <stdio.h> #include <stdlib.h>
// 链表节点结构体 struct ListNode { int val; struct ListNode *next; };
// 创建链表 struct ListNode* createList(int n) { struct ListNode *head = NULL, *tail = NULL; for (int i = 0; i < n; i++) { int val; scanf("%d", &val); struct ListNode *node = (struct ListNode*)malloc(sizeof(struct ListNode)); node->val = val; node->next = NULL; if (head == NULL) { head = node; tail = node; } else { tail->next = node; tail = node; } } return head; }
// 删除节点 void deleteNode(struct ListNode* head, int x) { struct ListNode *prev = NULL, *curr = head; while (curr != NULL) { if (curr->val == x) { if (prev == NULL) { head = curr->next; } else { prev->next = curr->next; } free(curr); curr = prev == NULL ? head : prev->next; } else { prev = curr; curr = curr->next; } } }
int main() { int repeat; scanf("%d", &repeat); for (int i = 0; i < repeat; i++) { int n, x; scanf("%d", &n); struct ListNode *head = createList(n); scanf("%d", &x); deleteNode(head, x); while (head != NULL) { printf("%d ", head->val); head = head->next; } printf("\n"); } return 0; }
回答2:
这道题目要实现的是链表操作的基本内容,主要是对链表的建立与节点的插入。首先,我们需要了解链表的基本概念。链表是一种常见的数据结构,它由一个个节点组成,每个节点包含数据和指向下一节点的指针。链表有单向链表和双向链表之分,单向链表指每个节点只有向后指向的指针,而双向链表每个节点有指向前一个和后一个节点的指针。
在实现过程中,我们需要定义一个链表节点类,包含数据和指向下一个节点的指针。然后再定义一个链表类,包含新增、插入、删除、遍历等相关方法。下面是此题的具体实现:
在每一次循环中,先输入n和n个整数,然后根据这些数据建立链表。可以使用链表类中的addFirst()方法,将输入的n个整数依次插入到头节点后面。接着,输入一个整数x,再用链表类中的add()方法将x插入到链表的任意位置。最后,输出完整链表。
下面是完整代码实现:
class Node(object):
def __init__(self, data=None):
self.data = data
self.next = None
class LinkedList(object):
def __init__(self):
self.head = Node()
def addFirst(self, data):
new_node = Node(data)
new_node.next = self.head.next
self.head.next = new_node
def append(self, data):
new_node = Node(data)
cur = self.head
while cur.next:
cur = cur.next
cur.next = new_node
def add(self, pos, data):
if pos < 0:
print("Error: invalid position!")
return
new_node = Node(data)
cur = self.head
count = 0
while count < pos and cur.next:
cur = cur.next
count += 1
if count == pos:
new_node.next = cur.next
cur.next = new_node
else:
print("Error: invalid position!")
def remove(self, data):
cur = self.head
while cur.next:
if cur.next.data == data:
cur.next = cur.next.next
return
cur = cur.next
print("Error: the data is not in the linked list!")
def size(self):
count = 0
cur = self.head
while cur.next:
count += 1
cur = cur.next
return count
def search(self, data):
cur = self.head
while cur.next:
if cur.next.data == data:
return True
cur = cur.next
return False
def show(self):
cur = self.head
while cur.next:
print(cur.next.data, end=' ')
cur = cur.next
print("")
repeat = int(input("请输入 repeat 的值:"))
for i in range(repeat):
n = int(input("请输入 n 的值:"))
list = LinkedList()
for j in range(n):
data = int(input("请输入第 %d 个整数:" % (j + 1)))
list.addFirst(data)
x = int(input("请输入要插入的整数:"))
pos = int(input("请输入要插入的位置:"))
list.add(pos, x)
print("插入后,链表内容为:")
list.show()
总之,这道题目其实是想让我们掌握链表的基本操作,如何创建节点,如何在链表中插入和删除节点等。通过这道题目的练习,我们不仅可以提高编程能力,还可以加深对链表的理解和应用。
回答3:
这道题需要实现如下两个过程:
- 建立单向链表
当输入一个正整数 n 和一组整数时,需要建立一个单向链表。可以先定义一个节点结构体,包括整数值和指向下一个节点的指针。通过读入 n 个整数,依次构造节点,并将它们连接起来,形成一个单向链表。
- 插入整数 x
当输入一个整数 x 时,需要将其插入到刚才建立的链表中。可以先遍历链表,寻找一个节点 p,使得 p 的值小于 x,p 的下一个节点 q 的值大于 x。然后构造一个新节点,并将其插入到 p 和 q 之间。
下面是完整的代码实现:
#include <stdio.h> #include <stdlib.h>
// 定义节点结构体 struct Node { int value; struct Node* next; };
// 建立单向链表 struct Node* make_list(int n) { struct Node* head = NULL; struct Node* prev = NULL; int i; for (i = 0; i < n; ++i) { int value; scanf("%d", &value); struct Node* node = (struct Node*)malloc(sizeof(struct Node)); node->value = value; node->next = NULL; if (prev == NULL) { head = node; } else { prev->next = node; } prev = node; } return head; }
// 插入整数 x void insert(struct Node* head, int x) { struct Node* p = head; while (p != NULL) { if (p->next == NULL || p->next->value > x) { struct Node* node = (struct Node*)malloc(sizeof(struct Node)); node->value = x; node->next = p->next; p->next = node; break; } p = p->next; } }
int main() { int repeat; scanf("%d", &repeat); int i; for (i = 0; i < repeat; ++i) { int n; scanf("%d", &n); struct Node* head = make_list(n); int x; scanf("%d", &x); insert(head, x); struct Node* p = head; while (p != NULL) { printf("%d ", p->value); p = p->next; } printf("\n"); } return 0; }
用c++实现:输入一个正整数 repeat (0<repeat<10),做 repeat 次下列运算: 输入一个正整数 n(0<n<=9)和一组(n个)升序的整数,建立单向链表,再输入一个整数 x,把 x 插入到这组数据中,使该组数据仍然有序。
以下是使用C++实现的代码:
#include <iostream>
using namespace std;
struct ListNode {
int val;
ListNode* next;
ListNode(int x) : val(x), next(NULL) {}
};
ListNode* insert(ListNode* head, int x) {
ListNode* node = new ListNode(x);
if (!head || x < head->val) {
node->next = head;
return node;
}
ListNode* curr = head;
while (curr->next && curr->next->val < x) {
curr = curr->next;
}
node->next = curr->next;
curr->next = node;
return head;
}
ListNode* createList(int n) {
ListNode* dummy = new ListNode(-1);
ListNode* curr = dummy;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
curr->next = new ListNode(x);
curr = curr->next;
}
return dummy->next;
}
void printList(ListNode* head) {
ListNode* curr = head;
while (curr) {
cout << curr->val << " ";
curr = curr->next;
}
cout << endl;
}
int main() {
int repeat;
cin >> repeat;
while (repeat--) {
int n, x;
cin >> n;
ListNode* head = createList(n);
cin >> x;
head = insert(head, x);
printList(head);
}
return 0;
}
首先定义了链表节点 ListNode
,包含一个整数值 val
和指向下一个节点的指针 next
。在 insert
函数中,首先创建一个新节点,然后遍历链表找到插入位置,最后插入新节点。在 createList
函数中,根据输入的升序整数序列创建链表。在主函数中,根据输入的 repeat
参数,循环执行多次插入操作,每次创建链表并插入一个新节点,最后输出插入后的链表。
相关推荐













