Hello people, I need some help regarding the insertion/deletion of data in the middle node. My problem is, if you insert a node on the middle, the existing middle node gets replaced (the nodes between the inserted node in the middle should only move left and right). And when I delete the middle node, it would only change the value to 0. I hope you can help me with this, thanks :D

#include<conio.h>
#include<iostream>
#include<windows.h>

using namespace std;

class node
{
      private:
              int i;
              node *next;
              node *head;
      
      public:
             node();
             ~node();
             void addFirst(int x);
             void display();
             void deleteFirst();
             void addEnd(int x);
             void deleteEnd();
             void addMiddle(int x);
             void deleteMiddle();
             int count();
             };
             
node::node()
{
            head = NULL;
}

node::~node()
{
             delete head;
             }

void node::addFirst(int x)
{
     node *ptr = new node();
     if(head == NULL)
     {
     head = new node();
     head->i = x;
     head->next = NULL;
     }
     
     else
     {
     ptr->i = x;
     ptr->next = head;
     head = ptr;
     }
     
     }
     
void node::display()
{
     node *ptr = new node();
     ptr = head;
     
     while(ptr != NULL)
     {
     cout<<ptr->i<<" -> ";
     ptr = ptr->next;
     }
     
     }
     
void node::deleteFirst()
{
     
     node *temp = new node();
     temp = head;
     head = temp->next;
     delete temp;
     }
     
void node::addEnd(int x)
{
     node *ptr = head;
     node *newnode = new node();
     
     while(ptr->next != NULL)
     {
     ptr = ptr->next;
     }
     
     ptr->next = newnode;
     newnode->i = x;
     newnode->next = NULL;

         
     }
     
void node::deleteMiddle()
{
     node *ptr;
     ptr = head;
     
     for(int a = 0; a < count()/2; a++)
     {
     ptr = ptr->next;
     }
     
     delete ptr;
     
     }
     
void node::addMiddle(int x)
{
     node *ptr = new node();
     ptr = head;
     node *nw = new node();
     
     for(int a = 0; a < count()/2; a++)
     {
     ptr = ptr->next;
     }
     
     
     ptr->i = x;
     
     }
     
void node::deleteEnd()
{
     node *ptr;
     ptr = head;
     
     while(ptr->next != NULL)
     {
     next = ptr;
     ptr = ptr->next;
     }

     next->next = NULL;
     }
     
     
int node::count()
{
    int c=0;
    node *ptr = head;
    
    while(ptr != NULL)
    {
    ptr = ptr->next;
    c++;
    }
    return c;
}

     
main()
{
      node b;
      
       b.addFirst(5);
       b.addFirst(3);
      b.addFirst(2);
      b.addFirst(3);
      b.addFirst(4);
      b.addFirst(8);
      b.addMiddle(1);
      
      cout<<"\n";

      b.display();

      cout<<endl<<endl<<b.count();
      getch();
      }
void node::addMiddle(int x)
{
  node *ptr = new node();
  ptr = head;
  node *nw = new node();

  for(int a = 0; a < count()/2; a++)
  {
    ptr = ptr->next;
  }
 
 
  ptr->i = x;
 
}

Okay, the first thing I see here is a massive memory leak. You declare "ptr" and "nw" and initialize both of them with new nodes. The problem is you then immediately reassign ptr without deleting the node you created. The pointer "nw" is never used again anywhere in the method, then at the end of the method, it goes out of scope. This method creates two (2) nodes every time you call it, then immediately loses access to both of them making those portions of memory unreachable until the program ends.

When you call this method, do you want the new node to be added before or after the node with the index you indicate, or is 'i' the node's data? I think it would be a good idea for you to consider renaming 'i' to something more descriptive, such as "myData" or "myIndex".

Edited 5 Years Ago by Fbody: n/a

Well I was doing something with node *nw then when I sought help here I removed them. Thanks for the reply! Regarding about "i", I don't really like declaring/making long variable names. "i" is the node's data.

Edited 5 Years Ago by kevinyu: n/a

>>Regarding about "i", I don't really like declaring/making long variable names. "i" is the node's data
Not liking long variable names is one thing. Sacrificing clarity for a shorter meaningless name is quite another and is borderline irresponsible. Six months from now, when you have to make a change to your code, are you going to remember what that is? Not likely. The name 'i' means absolutely nothing. What is it; "input", "index", "information" or something else completely off the wall? Do you see what I'm getting at?

There are times when a temporary non-descript identifier such as this is warranted (such as a for-loop index), but it's NOT a good name for a major application variable. Are you really so averse to a short name like "myData" that you would be willing to do something like this? Is that seriously too much for you to type? It's only 6 characters, and there's no confusion. If you're a decent typist, that should only take about 1-second to type.

Edited 5 Years Ago by Fbody: n/a

This article has been dead for over six months. Start a new discussion instead.