簡體   English   中英

鏈接列表的插入/刪除

[英]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;

}

我只是想用一些函數編寫鏈接列表以進行練習我的刪除功能,最后一個else語句不起作用,從邏輯上講,我無法弄清楚為什么,對於我的插入函數,這些語句都不起作用,甚至開頭也不起作用或位置0。但是附加項目,返回大小,打印列表,刪除第一個和最后一個元素都可以。

謝謝!

  1. 如果列表為空, sizeLinkedList將無法正常工作(無法返回0)
  2. 您將size用作不同作用域(在主作用域和deleteNode內)的不同變量。 盡管並非嚴格錯誤,但這確實令人困惑。
  3. deleteNode ,此序列不起作用:

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

    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();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM