用C++将链表L中第i个数据元素删除,然后输出删除后的链表
时间: 2023-08-31 15:18:40 浏览: 76
### 回答1:
可以使用以下代码来删除链表L中的第i个元素,并输出删除后的链表:
```
// 定义链表结点
typedef struct node {
int data;
struct node *next;
} Node;
// 删除链表L中的第i个元素
Node* deleteNode(Node *L, int i) {
Node *p = L;
int j = 1;
while (p && j < i) {
p = p->next;
j++;
}
if (!p || !p->next) {
return L;
}
Node *q = p->next;
p->next = q->next;
free(q);
return L;
}
// 输出链表L
void printList(Node *L) {
Node *p = L;
while (p) {
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
// 示例程序
int main() {
// 创建链表L
Node *L = (Node*)malloc(sizeof(Node));
L->data = 1;
L->next = NULL;
Node *p = L;
for (int i = 2; i <= 5; i++) {
Node *node = (Node*)malloc(sizeof(Node));
node->data = i;
node->next = NULL;
p->next = node;
p = node;
}
// 删除链表L中的第3个元素,并输出删除后的链表
L = deleteNode(L, 3);
printList(L);
return 0;
}
```
输出结果为:1 2 4 5
### 回答2:
首先,我们需要创建一个链表结构体,其中包括数据域和指针域:
```
typedef struct Node{
int data;
struct Node* next;
}Node;
```
然后,我们可以定义一个函数来删除链表中第i个数据元素,并输出删除后的链表:
```
void deleteNode(Node** head, int i){
if(*head == NULL){
printf("链表为空!");
return;
}
Node* temp = *head;
Node* pre = NULL;
// 找到第i个节点和它的前驱节点
for(int j=1; j<i && temp!=NULL; j++){
pre = temp;
temp = temp->next;
}
// 如果i超出链表长度
if(temp == NULL){
printf("第%d个节点不存在!", i);
return;
}
// 删除第i个节点
if(pre == NULL){ // 要删除的是头节点
*head = temp->next;
}else{
pre->next = temp->next;
}
free(temp); // 释放内存
// 输出删除后的链表
printf("删除后的链表为:");
Node* cur = *head;
while(cur != NULL){
printf("%d ", cur->data);
cur = cur->next;
}
}
```
接下来我们需要创建一个测试链表,并调用函数deleteNode来删除第i个数据元素:
```
int main(){
// 创建链表 1->2->3->4->5->6
Node* head = (Node*)malloc(sizeof(Node));
Node* second = (Node*)malloc(sizeof(Node));
Node* third = (Node*)malloc(sizeof(Node));
Node* fourth = (Node*)malloc(sizeof(Node));
Node* fifth = (Node*)malloc(sizeof(Node));
Node* sixth = (Node*)malloc(sizeof(Node));
head->data = 1;
head->next = second;
second->data = 2;
second->next = third;
third->data = 3;
third->next = fourth;
fourth->data = 4;
fourth->next = fifth;
fifth->data = 5;
fifth->next = sixth;
sixth->data = 6;
sixth->next = NULL;
// 删除第3个节点
deleteNode(&head, 3);
return 0;
}
```
运行程序,结果会输出删除后的链表:1 2 4 5 6
这样,我们就成功地用C语言将链表L中的第i个数据元素删除,并输出了删除后的链表。
### 回答3:
要在使用C语言中删除链表L中的第i个数据元素,首先需要遍历链表找到目标节点。接下来,将目标节点的前一个节点的next指针指向目标节点的下一个节点,然后释放目标节点的内存空间。
下面是一个示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
// 定义链表节点结构体
typedef struct Node {
int data;
struct Node* next;
} Node;
// 删除链表中第i个数据元素
void deleteNode(Node** head, int i) {
Node* current = *head;
Node* previous = NULL;
int count = 1;
// 如果要删除的是链表的第一个节点
if (i == 1) {
*head = current->next;
free(current);
return;
}
// 遍历链表找到目标节点
while (current != NULL && count < i) {
previous = current;
current = current->next;
count++;
}
// 如果找到了目标节点
if (current != NULL) {
previous->next = current->next;
free(current);
}
}
// 输出链表的数据
void printList(Node* head) {
Node* current = head;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
int main() {
// 创建一个示例链表: 1 -> 2 -> 3 -> 4 -> 5
Node* head = (Node*)malloc(sizeof(Node));
head->data = 1;
head->next = (Node*)malloc(sizeof(Node));
head->next->data = 2;
head->next->next = (Node*)malloc(sizeof(Node));
head->next->next->data = 3;
head->next->next->next = (Node*)malloc(sizeof(Node));
head->next->next->next->data = 4;
head->next->next->next->next = (Node*)malloc(sizeof(Node));
head->next->next->next->next->data = 5;
head->next->next->next->next->next = NULL;
int i = 3; // 要删除的节点位置
printf("删除前链表的数据:");
printList(head);
deleteNode(&head, i);
printf("删除后链表的数据:");
printList(head);
// 释放链表的内存空间
Node* current = head;
Node* next;
while (current != NULL) {
next = current->next;
free(current);
current = next;
}
return 0;
}
```
运行结果:
```
删除前链表的数据:1 2 3 4 5
删除后链表的数据:1 2 4 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_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)