【问题标题】:Linked List insertion/deletion链表插入/删除
【发布时间】:2014-11-14 16:39:13
【问题描述】:
                   // 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 也不起作用。但是附加项目、返回大小、打印列表、删除第一个和最后一个元素都有效。

谢谢!

【问题讨论】:

  • if (position = 0) 应该是if (position == 0)

标签: c++ data-structures singly-linked-list


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

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

    position 之前的节点上的next 指针设置为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();
}

【讨论】:

  • 您错过了上面评论中提到的if (position = 0)
  • 现已修复。我之前的更新尚未在main 中取消注释insertNode,匹配OP 的代码,并且insertNode 未修复。它现在似乎适用于 OP 的测试用例。
猜你喜欢
  • 1970-01-01
  • 2018-03-22
  • 1970-01-01
  • 2011-01-10
  • 2015-01-17
  • 1970-01-01
  • 2018-11-26
  • 1970-01-01
相关资源
最近更新 更多