一个c语言的程序题? 100
入法建立单链表来存储该五个数)。
2.编写程序,在第 1 题的基础上再分别在链表中插入一个元素以及删除一个元素,
再输出链表中的所有元素 展开
Option Explicit
Dim head As Node
Private Type Node
data As Integer
nextNode As Node
End Type
Private Sub Command1_Click()
Dim i As Integer
For i = 1 To 5
InputNumber
Next
PrintList
End Sub
Private Sub InputNumber()
Dim newNode As Node
Set newNode = New Node
newNode.data = InputBox("请输入整数:")
If head Is Nothing Then
Set head = newNode
Else
Dim p As Node
Set p = head
Do While p.nextNode Is Not Nothing
Set p = p.nextNode
Loop
Set p.nextNode = newNode
End If
End Sub
Private Sub PrintList()
Dim p As Node
Set p = head
Do While p Is Not Nothing
Debug.Print p.data
Set p = p.nextNode
Loop
End Sub
在上述代码的基础上,您可以添加两个新函数来实现在链表中插入一个元素以及删除一个元素的功能:
Private Sub InsertNumber(data As Integer, pos As Integer)
Dim newNode As Node
Set newNode = New Node
newNode.data = data
If pos = 1 Then
newNode.nextNode = head
Set head = newNode
Else
Dim p As Node
Set p = head
Dim i As Integer
For i = 1 To pos - 2
Set p = p.nextNode
Next
newNode.nextNode = p.nextNode
Set p.nextNode = newNode
End If
End Sub
Private Sub DeleteNumber(pos As Integer)
If pos = 1 Then
Set head = head.nextNode
Else
Dim p As Node
Set p = head
Dim i As Integer
For i = 1 To pos - 2
Set p = p.nextNode
Next
Set p.nextNode = p.nextNode.nextNode
End If
End Sub
您可以在调用这些函数时使用以下代码来插入和删除元素:
InsertNumber 100, 3
DeleteNumber 2
这是第一问的代码:
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node* next;
};
int main() {
int i;
struct node *head, *p, *q;
head = NULL; // 初始化头指针
for (i = 0; i < 5; i++) {
// 分配内存
q = (struct node*)malloc(sizeof(struct node));
scanf("%d", &q->data); // 从键盘输入数据
q->next = NULL; // 设置链表尾
if (head == NULL) {
head = q; // 如果是第一个节点,则将头指针指向该节点
} else {
p->next = q; // 尾插入法,将新节点插入到链表尾
}
p = q; // 将新节点作为下一个节点的前驱
}
p = head; // 设置遍历指针
while (p != NULL) {
printf("%d ", p->data); // 输出节点的数据
p = p->next; // 指向下一个节点
}
return 0;
}
这是第二问的代码:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
int main()
{
struct node *head, *p, *q;
int i;
head = NULL;
for(i=0; i<5; i++)
{
p = (struct node *)malloc(sizeof(struct node));
printf("输入第%d个数:", i+1);
scanf("%d", &p->data);
p->next = NULL;
if(head == NULL)
head = p;
else
q->next = p;
q = p;
}
p = head;
printf("链表中的元素是:\n");
while(p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n");
// 在链表中插入一个元素
p = (struct node *)malloc(sizeof(struct node));
p->data = 99;
p->next = head;
head = p;
// 删除一个元素
p = head;
head = head->next;
free(p);
p = head;
printf("
//定义链表结构
struct Node
{
int data;
Node *next;
};
//定义头结点
Node *head;
//定义尾插入函数
void insertTail(int data)
{
Node *node = new Node;
node->data = data;
node->next = NULL;
if (head == NULL)
{
head = node;
}
else
{
Node *p = head;
while (p->next != NULL)
{
p = p->next;
}
p->next = node;
}
}
//定义输出函数
void printList()
{
Node *p = head;
while (p != NULL)
{
cout << p->data << " ";
p = p->next;
}
cout << endl;
}
int main()
{
int data;
//从键盘输入五个整数
for (int i = 0; i < 5; i++)
{
cin >> data;
//尾插入法建立单链表
insertTail(data);
}
//输出链表中的所有元素
printList();
return 0;
}
2.编写程序,在第 1 题的基础上再分别在链表中插入一个元素以及删除一个元素,再输出链表中的所有元素
//定义插入函数
void insertNode(int data, int pos)
{
Node *node = new Node;
node->data = data;
node->next = NULL;
if (head == NULL)
{
head = node;
}
else
{
Node *p = head;
int i = 0;
while (p->next != NULL && i < pos - 1)
{
p = p->next;
i++;
}
node->next = p->next;
p->next = node;
}
}
//定义删除函数
void deleteNode(int pos)
{
if (head == NULL)
{
return;
}
else
{
Node *p = head;
int i = 0;
while (p->next != NULL && i < pos - 1)
{
p = p->next;
i++;
}
Node *q = p->next;
p->next = q->next;
delete q;
}
}
int main()
{
int data;
//从键盘输入五个整数
for (int i = 0; i < 5; i++)
{
cin >> data;
//尾插入法建立单链表
insertTail(data);
}
//在链表中插入一个元素
insertNode(100, 3);
//在链表中删除一个元素
deleteNode(3);
//输出链表中的所有元素
printList();
return 0;
}
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
# 尾插入法建立链表
def createLinkedList(nums):
head = ListNode() # 定义头节点
cur = head
for num in nums:
cur.next = ListNode(num)
cur = cur.next
return head.next
# 在链表末尾插入一个元素
def insertLinkedList(head, val):
cur = head
while cur.next:
cur = cur.next
cur.next = ListNode(val)
# 从链表中删除一个元素
def deleteLinkedList(head, val):
dummy = ListNode(0)
dummy.next = head
pre, cur = dummy, head
while cur:
if cur.val == val:
pre.next = cur.next
break
pre = cur
cur = cur.next
return dummy.next
# 输出链表中的所有元素
def printLinkedList(head):
cur = head
while cur:
print(cur.val, end=" ")
cur = cur.next
print()
# 主函数
if __name__ == '__main__':
# 输入五个整数并建立链表
nums = []
for i in range(5):
num = int(input("请输入第%d个整数:" % (i+1)))
nums.append(num)
head = createLinkedList(nums)
# 在链表中插入一个元素并输出链表中的所有元素
val = int(input("请输入要插入的整数:"))
insertLinkedList(head, val)
print("插入后链表中的所有元素:")
printLinkedList(head)
# 从链表中删除一个元素并输出链表中的所有元素
val = int(input("请输入要删除的整数:"))
head = deleteLinkedList(head, val)
print("删除后链表中的所有元素:")
printLinkedList(head)
这段代码实现了从键盘中输入五个整数,并用尾插入法建立单链表来存储这五个数。然后,用户可以输入一个整数,程序会在链表末尾插入这个整数,并输出链表中的所有元素。接着,用户可以输入要删除的整数,程序会从链表中删除这个整数,并再次输出链表中的所有元素。
有帮到你的话望采纳 谢谢~