键盘输入五个数存储到链表中并能实现链表相关操作C语言版
时间: 2023-09-03 21:05:00 浏览: 87
### 回答1:
好的,这个问题我可以回答。首先,我们需要定义一个链表节点的结构体,包含一个整型数据和一个指向下一个节点的指针。然后,我们可以使用 scanf 函数从键盘输入五个整数,并将它们存储到链表中。接下来,我们可以实现链表的相关操作,比如遍历链表、插入节点、删除节点等等。具体实现可以参考以下代码:
```
#include <stdio.h>
#include <stdlib.h>
// 定义链表节点结构体
struct Node {
int data;
struct Node* next;
};
// 遍历链表并打印每个节点的数据
void printList(struct Node* head) {
struct Node* current = head;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
// 在链表头部插入一个节点
void insertAtHead(struct Node** head, int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = *head;
*head = newNode;
}
// 在链表尾部插入一个节点
void insertAtTail(struct Node** head, int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
if (*head == NULL) {
*head = newNode;
} else {
struct Node* current = *head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}
}
// 删除链表中第一个值为 data 的节点
void deleteNode(struct Node** head, int data) {
struct Node* current = *head;
struct Node* previous = NULL;
while (current != NULL && current->data != data) {
previous = current;
current = current->next;
}
if (current == NULL) {
return;
}
if (previous == NULL) {
*head = current->next;
} else {
previous->next = current->next;
}
free(current);
}
int main() {
// 定义链表头指针
struct Node* head = NULL;
// 从键盘输入五个整数并存储到链表中
int i, num;
for (i = ; i < 5; i++) {
scanf("%d", &num);
insertAtTail(&head, num);
}
// 遍历链表并打印每个节点的数据
printList(head);
// 在链表头部插入一个节点
insertAtHead(&head, );
printList(head);
// 在链表尾部插入一个节点
insertAtTail(&head, 6);
printList(head);
// 删除链表中第一个值为 3 的节点
deleteNode(&head, 3);
printList(head);
return ;
}
```
### 回答2:
链表是由一系列节点组成的数据结构,每个节点包含一个数据域和一个指针域,指针域指向链表中的下一个节点。
首先,需要定义一个链表节点的结构体:
```c
struct Node {
int data;
struct Node* next;
};
```
接下来,定义一个函数来创建链表,并将五个数存储到链表中:
```c
struct Node* createLinkedList(int num) {
struct Node* head = NULL;
struct Node* current = NULL;
for (int i = 0; i < num; i++) {
int value;
printf("请输入第%d个数:", i+1);
scanf("%d", &value);
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;
if (head == NULL) {
head = newNode;
current = newNode;
} else {
current->next = newNode;
current = newNode;
}
}
return head;
}
```
以上代码使用动态内存分配,通过循环输入五个数并创建相应的节点,将节点插入链表中。
接下来,可以实现链表的一些常用操作,例如在链表末尾插入一个新节点:
```c
void insertAtEnd(struct Node** head, int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;
if (*head == NULL) {
*head = newNode;
} else {
struct Node* current = *head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}
}
```
在链表头部插入一个新节点:
```c
void insertAtBeginning(struct Node** head, int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = *head;
*head = newNode;
}
```
删除链表中指定数值的节点:
```c
void deleteNode(struct Node** head, int value) {
struct Node* current = *head;
struct Node* previous = NULL;
if (current != NULL && current->data == value) {
*head = current->next;
free(current);
return;
}
while (current != NULL && current->data != value) {
previous = current;
current = current->next;
}
if (current == NULL) {
return;
}
previous->next = current->next;
free(current);
}
```
遍历链表并打印出所有节点的值:
```c
void printLinkedList(struct Node* head) {
struct Node* current = head;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
```
最后,在主函数中调用以上函数实现链表的相关操作:
```c
int main() {
struct Node* head = createLinkedList(5);
printLinkedList(head);
insertAtEnd(&head, 6);
printLinkedList(head);
insertAtBeginning(&head, 0);
printLinkedList(head);
deleteNode(&head, 3);
printLinkedList(head);
return 0;
}
```
### 回答3:
首先,我们需要定义一个链表的结构体,包含一个整型数据域和一个指向下一个节点的指针。定义如下:
```c
typedef struct Node
{
int data;
struct Node *next;
} Node;
```
接下来,我们可以编写一个函数来创建一个链表节点,将输入的值存储到该节点中。
```c
Node* createNode(int value)
{
Node *newNode = (Node*)malloc(sizeof(Node));
newNode->data = value;
newNode->next = NULL;
return newNode;
}
```
然后,我们可以编写一个函数来向链表中添加节点。如果链表为空,即头指针为NULL,则将新节点作为链表的第一个节点;否则,遍历到链表的最后一个节点,将新节点插入到最后。
```c
void addToLinkedList(Node **head, int value)
{
Node *newNode = createNode(value);
if(*head == NULL)
{
*head = newNode;
return;
}
Node *temp = *head;
while(temp->next != NULL)
{
temp = temp->next;
}
temp->next = newNode;
}
```
接着,我们可以编写打印链表的函数,以便查看链表中的值。
```c
void printLinkedList(Node *head)
{
printf("链表中的数值为:");
Node *temp = head;
while(temp != NULL)
{
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
```
最后,我们可以编写释放链表内存的函数,以免内存泄漏。
```c
void freeLinkedList(Node *head)
{
Node *temp;
while(head != NULL)
{
temp = head;
head = head->next;
free(temp);
}
}
```
现在,我们可以在主函数中调用这些函数来实现链表相关操作。
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{
int data;
struct Node *next;
} Node;
Node* createNode(int value)
{
Node *newNode = (Node*)malloc(sizeof(Node));
newNode->data = value;
newNode->next = NULL;
return newNode;
}
void addToLinkedList(Node **head, int value)
{
Node *newNode = createNode(value);
if(*head == NULL)
{
*head = newNode;
return;
}
Node *temp = *head;
while(temp->next != NULL)
{
temp = temp->next;
}
temp->next = newNode;
}
void printLinkedList(Node *head)
{
printf("链表中的数值为:");
Node *temp = head;
while(temp != NULL)
{
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
void freeLinkedList(Node *head)
{
Node *temp;
while(head != NULL)
{
temp = head;
head = head->next;
free(temp);
}
}
int main()
{
Node *head = NULL;
int num;
printf("请输入五个数字:");
for(int i=0; i<5; i++)
{
scanf("%d", &num);
addToLinkedList(&head, num);
}
printLinkedList(head);
freeLinkedList(head);
return 0;
}
```
以上代码实现了将5个数字存储到链表中,并可以打印链表中的数值。可以根据需求进行进一步扩展链表的操作,例如插入节点、删除节点等。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)