链表插入/删除
Linked List insertion/deletion
// ConsoleApplication1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
Node* head = NULL;
int size;
Node* tail = NULL;
void printLinkedList() {
Node *search = head;
if (head == NULL) {
cout << "linkedlist is empty" << endl;
}
else {
while (search != NULL){
cout << search->data << endl;
search = search->next;
}
}
}
int sizeLinkedList() {
size = 1;
Node* current = head;
while (current->next != NULL) {
current = current->next;
size = size + 1;
}
cout << size << endl;
return size;
}
Node *getNode(int position){
Node *current = head;
for (int i = 0; i<position; i++)
{
current = current->next;
}
return current;
}
void appendNode(int n) {
Node *newNode = new Node; //creating new node
newNode->data = n;
newNode->next = NULL;
if (head == NULL)
{
head = newNode;
return;
}
else {
Node *current = head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}
}
void insertNode(int n, int position) {
Node *newNode = new Node;
newNode->data = n;
newNode->next = NULL;
int size = sizeLinkedList();
if (position = 0){
if (head == NULL) {
head = newNode;
}
else{
newNode->next = head;
head = newNode;
}
}
else if (position == size) {
appendNode(n);
}
else {
Node *prevNode = getNode(position-1);
Node *nextNode = getNode(position);
prevNode->next = newNode;
newNode->next = nextNode;
}
}
void deleteNode(int position) {
Node *currentNode;
int size = sizeLinkedList();
if (size == 0) {
return;
}
if (position == 0) {
currentNode = head->next;
head = currentNode;
}
else if (position == size-1) {
getNode(position - 1)->next = NULL;
delete getNode(position);
}
else {
getNode(position - 1)->next = getNode(position+1);
delete getNode(position);
}
}
//making a dynamic array only via pointers in VC++
void makeArray() {
int* m = NULL;
int n;
cout << "how many entries are there?"<<endl;
cin >> n;
m = new int[n];
int temp;
for (int x = 0; x < n; x++){
cout << "enter item:"<< x+1<< endl;
cin >> temp;
*(m + x) = temp;
}
for (int x = 0; x < n; x++){
cout << x+1 + ":" << "There is item: "<<*(m+x) << endl;
}
delete[]m;
}
int main() {
int x;
//makeArray();
appendNode(1);
appendNode(2);
appendNode(32);
appendNode(55);
appendNode(66);
//insertNode(2, 0);
printLinkedList();
deleteNode(3);
printLinkedList();
sizeLinkedList();
cin >> x;
}
我只是尝试编写一个带有几个函数的链表进行练习我的 Delete 函数,最后一个 else 语句不起作用,逻辑上我无法弄清楚为什么,至于我的插入函数,没有任何语句有效,即使在头部或位置 0 也是如此。但是,附加项目、返回大小、打印列表、删除第一个和最后一个元素都有效。
谢谢!
- 如果列表为空,
sizeLinkedList
将无法正常工作(它不能返回 0( - 您将
size
用作不同作用域(在主作用域和deleteNode
内(的不同变量。 这非常令人困惑,尽管并非完全错误。 -
在
deleteNode
中,此序列将不起作用:
在节点else if (position == size-1) { getNode(position - 1)->next = NULL; delete getNode(position); }
上设置
next
指针之前position
为 NULL 将干扰在下一行中getNode(position)
的尝试,因为它会根据next
遍历列表。 解决方法是反转这两条线。 -
同样,由于类似的原因,
deleteNode
中的最后一个序列将不起作用,因为您正在修改下一个指针:else { getNode(position - 1)->next = getNode(position+1); delete getNode(position); // this list traversal will skip the node to delete! }
这里的解决方案是这样的:
else { currentNode = getNode(position); getNode(position - 1)->next = getNode(position+1); delete currentNode; }
- 我还重写了
insertNode
函数,其中包含 @0x499602D2 提供的评论。
下面是代码的修改版本,其中当前序列已main
修复:
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
Node* head = NULL;
int size = 0;
Node* tail = NULL;
void printLinkedList() {
Node *search = head;
if (head == NULL) {
cout << "linkedlist is empty" << endl;
}
else {
while (search != NULL){
cout << search->data << endl;
search = search->next;
}
}
}
int sizeLinkedList() {
size = 0;
if (head->next != NULL){
size = 1;
Node* current = head;
while (current->next != NULL) {
current = current->next;
size = size + 1;
}
}
cout << size << endl;
return size;
}
Node *getNode(int position){
Node *current = head;
for (int i = 0; i<position; i++)
{
current = current->next;
}
return current;
}
void appendNode(int n) {
Node *newNode = new Node; //creating new node
newNode->data = n;
newNode->next = NULL;
size++;
if (head == NULL)
{
head = newNode;
return;
}
else {
Node *current = head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}
}
void insertNode(int n, int position) {
Node *newNode = new Node;
newNode->data = n;
newNode->next = NULL;
if (position == 0){
newNode->next = head;
head = newNode;
}
else if (position == sizeLinkedList()) {
appendNode(n);
}
else {
Node *prevNode = getNode(position-1);
Node *nextNode = getNode(position);
prevNode->next = newNode;
newNode->next = nextNode;
}
}
void deleteNode(int position) {
Node *currentNode;
int my_size = sizeLinkedList();
if ((my_size == 0) || (position > my_size)) {
return;
}
if (position == 0) {
currentNode = head->next;
head = currentNode;
}
else if (position == size-1) {
delete getNode(position);
getNode(position - 1)->next = NULL;
}
else {
currentNode = getNode(position);
getNode(position - 1)->next = getNode(position+1);
delete currentNode;
}
}
//making a dynamic array only via pointers in VC++
void makeArray() {
int* m = NULL;
int n;
cout << "how many entries are there?"<<endl;
cin >> n;
m = new int[n];
int temp;
for (int x = 0; x < n; x++){
cout << "enter item:"<< x+1<< endl;
cin >> temp;
*(m + x) = temp;
}
for (int x = 0; x < n; x++){
cout << x+1 + ":" << "There is item: "<<*(m+x) << endl;
}
delete[]m;
}
int main() {
int x;
//makeArray();
appendNode(1);
appendNode(2);
appendNode(32);
appendNode(55);
appendNode(66);
insertNode(2, 0);
printLinkedList();
deleteNode(3);
printLinkedList();
sizeLinkedList();
}
相关文章:
- 插入或删除时获取usb的dos_name
- 如何插入 4 个空格宽的制表符并将它们删除两个空格?
- 数组 X[n] 仅存储名称.编写算法以在数组中插入或删除 ITEM
- 无法使用带有 std::move 的自定义删除器插入 std::unique_ptr
- 插入指针数组会删除上一个结果
- 在 QSet 中插入元素<QString>并检索 ,删除 QSet 的最后一项
- 遍历 STL 映射(集/多集)的最佳方法,同时元素可能会在循环期间被删除并重新插入?
- BST插入,删除,搜索
- 使用链表在堆栈中插入和删除
- 删除空格后如何将其插入字符串中
- 哪个容器对于C++中的多次插入/删除最有效
- 内部源代码 - 在链接列表实现中插入元素上的删除会破坏整个应用程序
- 如何删除第一个节点并将其插入链接列表C 的末尾
- 在拖放 QML 列表中插入/删除项目使用 cpp 模型查看
- 如何插入/删除多音阶中相同值的某些元素
- 插入向量后,删除对WSTRING的引用
- 是否有具有对数时间插入、删除和查找(带距离)的排序数据结构
- 堆用于快速插入和删除O(日志n)
- 在C 中的链接列表中插入/删除
- qt QAbstractItemModel 拖放用于移动项目执行删除/插入