链表插入/删除

Linked List insertion/deletion

本文关键字:删除 插入 链表      更新时间:2023-10-16
                   // 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 也是如此。但是,附加项目、返回大小、打印列表、删除第一个和最后一个元素都有效。

谢谢!

  1. 如果列表为空,sizeLinkedList将无法正常工作(它不能返回 0(
  2. 您将size用作不同作用域(在主作用域和deleteNode内(的不同变量。 这非常令人困惑,尽管并非完全错误。
  3. deleteNode 中,此序列将不起作用:

    else if (position == size-1) {
      getNode(position - 1)->next = NULL;
      delete getNode(position);
      }
    
    在节点

    上设置 next 指针之前position为 NULL 将干扰在下一行中getNode(position)的尝试,因为它会根据 next 遍历列表。 解决方法是反转这两条线。

  4. 同样,由于类似的原因,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;
      }
    
  5. 我还重写了 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();
}