Karan
Karan

Reputation: 1151

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;

}

Im just trying to code a Linked List with a couple of functions for practice My Delete function, the last else statement isnt working, and logically I cant figure out why, as for my insert function none of the statements are working, not even at head or position 0. However appending items, returning size, printing the list, deleting the first and last elements works.

thanks!

Upvotes: 2

Views: 4106

Answers (1)

lone gold
lone gold

Reputation: 238

  1. sizeLinkedList won't work correctly if the list is empty (it cannot return 0)
  2. you are using size as different variables at different scopes (at main scope, and within deleteNode). This is pretty confusing although not strictly wrong.
  3. in deleteNode, this sequence won't work:

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

    setting the next pointer on the node prior to position to NULL will interfere with the attempt to getNode(position) in the very next line, because it traverses the list based on next. The fix is to reverse these two lines.

  4. Likewise, your last sequence in deleteNode won't work for a similar reason, because you are modifying the next pointers:

    else {
      getNode(position - 1)->next = getNode(position+1);
      delete getNode(position);  // this list traversal will skip the node to delete!
      }
    

    the solution here is like this:

    else {
      currentNode = getNode(position);
      getNode(position - 1)->next = getNode(position+1);
      delete currentNode;
      }
    
  5. I've also re-written the insertNode function incorporating the comment provided by @0x499602D2 .

Here's a modified version of your code that has your current sequence in main fixed:

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

Upvotes: 3

Related Questions