构造一个空的单链表L,在单链表L的第i个元素之后插入新的元素e,在单链表L中删除第i个元素,并用e返回其值,写好这3个函数,并且在main函数中调用这3个函数,显示结果
时间: 2024-09-22 17:08:27 浏览: 55
在编程中,我们可以使用结构体或类来表示链表节点,这里假设我们有一个名为`ListNode`的结构体,包含`value`和`next`两个字段,其中`value`存储元素值,`next`指向下一个节点。
首先,我们需要创建构造函数来初始化一个空链表:
```c++
struct ListNode {
int value;
ListNode* next;
};
ListNode* createEmptyList() {
return new ListNode{0, nullptr};
}
```
接下来,我们分别编写插入、删除和获取值的函数:
1. 插入新元素:
```c++
void insertElement(ListNode*& head, int i, int e) {
if (head == nullptr || i <= 0) return; // 如果链表为空或索引错误,直接返回
ListNode* current = head;
for (int j = 1; j < i; ++j) {
if (current->next == nullptr) break; // 找到位置并插入
current = current->next;
}
// 创建新节点
ListNode* newNode = new ListNode{e, nullptr};
if (current == nullptr) { // 如果这是第一个节点
newNode->next = head;
head = newNode;
} else {
newNode->next = current->next;
current->next = newNode;
}
}
```
2. 删除第i个元素:
```c++
ListNode* deleteElement(ListNode*& head, int i) {
if (head == nullptr || i <= 0) return nullptr;
ListNode* current = head;
ListNode* previous = nullptr;
for (int j = 1; j < i && current != nullptr; ++j) {
previous = current;
current = current->next;
}
if (current == nullptr) return head; // 没有找到要删除的位置,返回原链表头
if (previous == nullptr) { // 如果要删除的是头节点
head = current->next;
} else {
previous->next = current->next;
}
delete current; // 释放内存
return head;
}
```
3. 获取第i个元素的值:
```c++
int getElementValue(ListNode* head, int i) {
if (head == nullptr || i <= 0) return -1; // 空链表或非法索引,返回错误值
ListNode* current = head;
for (int j = 0; j < i; ++j) {
if (current->next == nullptr) return -1; // 未到达目标位置,或者已达到结尾
current = current->next;
}
return current->value; // 返回值
}
```
最后,在`main`函数中调用这三个函数:
```c++
int main() {
ListNode* list = createEmptyList();
insertElement(list, 1, 5); // 在第二个位置插入5
insertElement(list, 3, 10); // 在第四个位置插入10
printf("After inserting elements:\n");
printf("%d\n", getElementValue(list, 1)); // 输出5
printf("%d\n", getElementValue(list, 3)); // 输出10
printf("\ndeleting element at index 1:\n");
list = deleteElement(list, 1);
printf("%d\n", getElementValue(list, 1)); // 输出10
// ...其他操作...
return 0;
}
```
请注意,实际程序需要处理边界情况和异常,以上代码仅作为演示示例。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)