delete node

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

37 Replies - 1420 Views - Last Post: 06 September 2013 - 12:07 PM Rate Topic: -----

#1 annie12  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 141
  • Joined: 27-July 13

delete node

Posted 31 August 2013 - 05:43 AM

i dont understand in the following code how to delete my node ..please explain me how to delete node from front
#include<iostream>
#include<stdio.h>
using namespace std;
class Node{
      public:
             int data;
             Node *nxtptr;
             Node(){
             nxtptr=NULL;       
                    }
             Node(int val=1,Node *obj=NULL)
             {
                      data=val;
                      nxtptr=obj;
                      }
            };
class list{
      private:
             Node *firstptr;
             Node *lastptr;
       public:
             list():firstptr(0),lastptr(0)
             {
               //empty body                           
               }
             void insertAtFront(int vl)//vl mnz value
             {
                  Node *newptr=new Node(vl);
                  if(isEmpty())
                  {
                      firstptr=lastptr=newptr;
                      }
                   else
                   { 
                       newptr->nxtptr=firstptr;
                       firstptr=newptr;
                       }
              }
              void insertAtBack(int vl)//vl mnz value
              {
                  Node *newptr=new Node(vl);
                  if(isEmpty())
                  {
                      firstptr=lastptr=newptr;
                      }
                   else
                   { 
                       newptr->nxtptr=lastptr;
                       lastptr=newptr;
                       }
               }
               bool isEmpty()
               {
                      if(firstptr==NULL)
                      return true;
                      else
                      return false;
                      }
               void print()
               {    
                    Node *currentptr=firstptr;
                    if(isEmpty())
                     {  
                       cout<<"List is empty"<<endl;
                       }
                     while(currentptr!=0)
                     {
                            cout<<currentptr->data<<"  ";
                            currentptr=currentptr->nxtptr;
                            }
                    }
                    };
 int main()
 {
     list obj;
     obj.insertAtFront(8);
     obj.print();
     obj.insertAtFront(9);
     obj.print();
     system("PAUSE");
     return 0;
     }                                       
      

i feel this function works but i am nor sure
void list::deletefromfront()
{
Node *currentptr;
currentptr=firstptr;
delete currentptr;
firstptr=nextptr;
}



Is This A Good Question/Topic? 0
  • +

Replies To: delete node

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3549
  • View blog
  • Posts: 10,993
  • Joined: 05-May 12

Re: delete node

Posted 31 August 2013 - 06:08 AM

You know, with modern computers you could actually try running the code and stepping through it with a debugger to see if it works.
Was This Post Helpful? 1
  • +
  • -

#3 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 656
  • View blog
  • Posts: 2,246
  • Joined: 31-December 10

Re: delete node

Posted 31 August 2013 - 07:44 AM

It's basically the exact opposite of inserting a node.
Was This Post Helpful? 0
  • +
  • -

#4 Java Student  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 480
  • Joined: 05-February 10

Re: delete node

Posted 31 August 2013 - 11:17 AM

I'm guessing your making a FIFO queue circular LL from the looks of your variables and because you're deleting from the front.

I recommend making a function that outputs the structure of your LL after each operation.

The function would look something like this:
void list::Print() {
   
   Node temp_head* = head;

   if(temp_head!=NULL){
      while(temp_head!=NULL){
         cout << temp_head->getData() << " ";
         temp_head->getNext();
      }
   }else{
      cout << "List is empty" << endl;
   }
   
   cout << endl;
   
}





Implement a function like this, run your program, and tell us if your function works or if you get errors.
Was This Post Helpful? 1
  • +
  • -

#5 annie12  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 141
  • Joined: 27-July 13

Re: delete node

Posted 01 September 2013 - 12:12 AM

i dont understand why my print function make one extra printing
void print()
               {    
                    Node *currentptr=firstptr;
                    if(isEmpty())
                     {  
                       cout<<"List is empty"<<endl;
                       }
                     while(currentptr!=0)
                     {
                            cout<<currentptr->data<<"  ";
                            currentptr=currentptr->nxtptr;
                            }
                    }

Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3549
  • View blog
  • Posts: 10,993
  • Joined: 05-May 12

Re: delete node

Posted 01 September 2013 - 01:46 AM

How many times did you call insertAtFront()? Is the extra nodes only printed out after you've attempted a delete?

By the way, your insertAtBack() has a problem: it doesn't chain the last node to the new node you are adding.
Was This Post Helpful? 0
  • +
  • -

#7 annie12  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 141
  • Joined: 27-July 13

Re: delete node

Posted 01 September 2013 - 04:41 AM

View PostSkydiver, on 01 September 2013 - 01:46 PM, said:

How many times did you call insertAtFront()? Is the extra nodes only printed out after you've attempted a delete?

By the way, your insertAtBack() has a problem: it doesn't chain the last node to the new node you are adding.

no i didnot use delete function.without using delete function it prints one extra number.....ok really that function not working....please tell me changes i have to do to insert back node

This post has been edited by annie12: 01 September 2013 - 04:46 AM

Was This Post Helpful? 0
  • +
  • -

#8 Java Student  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 480
  • Joined: 05-February 10

Re: delete node

Posted 01 September 2013 - 08:13 AM

If you have trouble if indentation, want to know the correct form, and you're using Visual Studio, you can highlight your print function for example, then click
"Edit"->"Format Selection".

The result is this nicer form of the print function. Also, this function looks correct, assuming the correct implementation of isEmpty().
void print()
{    
	Node *currentptr=firstptr;
	if(isEmpty())
	{  
		cout<<"List is empty"<<endl;
	}
	while(currentptr!=0)
	{
		cout<<currentptr->data<<"  ";
		currentptr=currentptr->nxtptr;
	}
}



In your deleteFromFront(),
void list::deletefromfront()
{
   Node *currentptr;
   currentptr=firstptr;
   delete currentptr;
   firstptr=nextptr;//compiler error
}


this function doesn't work and won't compile. Where does nextptr come from? It is a member of data, not list. Before the compiler error, you have successfully deleted the head, now you want to link firstptr's next to the second in line, but keeping in mind of the scenario where you have only a head(one node).

View Postannie12, on 01 September 2013 - 04:41 AM, said:

please tell me changes i have to do to insert back node


Let's take a look at your insertAtBack(),
void insertAtBack(int vl)//vl mnz value
{
	Node *newptr=new Node(vl);
	if(isEmpty())
	{
		firstptr=lastptr=newptr;
	}
	else
	{ 
		newptr->nxtptr=lastptr;
		lastptr=newptr;
	}
}



Keep in mind the scenario where inserting at back means inserting into the head because you may have no elements in the LL. Otherwise, you'll want a while a simple while look like we did before: while currentPtr points is not null, currentptr equal currentptr's next. At this point, currentPtr==NULL(currenPtr is at the end of LL), so currentptr equal netptr(your variable).
Was This Post Helpful? 0
  • +
  • -

#9 annie12  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 141
  • Joined: 27-July 13

Re: delete node

Posted 01 September 2013 - 11:24 AM

View PostJava Student, on 01 September 2013 - 08:13 PM, said:

If you have trouble if indentation, want to know the correct form, and you're using Visual Studio, you can highlight your print function for example, then click
"Edit"->"Format Selection".

The result is this nicer form of the print function. Also, this function looks correct, assuming the correct implementation of isEmpty().
void print()
{    
	Node *currentptr=firstptr;
	if(isEmpty())
	{  
		cout<<"List is empty"<<endl;
	}
	while(currentptr!=0)
	{
		cout<<currentptr->data<<"  ";
		currentptr=currentptr->nxtptr;
	}
}



In your deleteFromFront(),
void list::deletefromfront()
{
   Node *currentptr;
   currentptr=firstptr;
   delete currentptr;
   firstptr=nextptr;//compiler error
}


this function doesn't work and won't compile. Where does nextptr come from? It is a member of data, not list. Before the compiler error, you have successfully deleted the head, now you want to link firstptr's next to the second in line, but keeping in mind of the scenario where you have only a head(one node).

View Postannie12, on 01 September 2013 - 04:41 AM, said:

please tell me changes i have to do to insert back node


Let's take a look at your insertAtBack(),
void insertAtBack(int vl)//vl mnz value
{
	Node *newptr=new Node(vl);
	if(isEmpty())
	{
		firstptr=lastptr=newptr;
	}
	else
	{ 
		newptr->nxtptr=lastptr;
		lastptr=newptr;
	}
}



Keep in mind the scenario where inserting at back means inserting into the head because you may have no elements in the LL. Otherwise, you'll want a while a simple while look like we did before: while currentPtr points is not null, currentptr equal currentptr's next. At this point, currentPtr==NULL(currenPtr is at the end of LL), so currentptr equal netptr(your variable).

i dont understand what you are saying about insert at back function
one thing more i try to make a single link list
and i just got on idea on front and back by focusing on pointers
i felt link list work inm the following way but i dont understand how to then but AT back
............... ..................
.nextptr=NULL . .nextptr=firstptr.
.firstptr=NULL..firstptr=newpt .
............... ..................
when at starting firstptr is pointing to null so why we dontuse lastptr in linking and it also points to null :dontgetit:/>

This post has been edited by annie12: 01 September 2013 - 11:25 AM

Was This Post Helpful? 0
  • +
  • -

#10 Java Student  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 480
  • Joined: 05-February 10

Re: delete node

Posted 01 September 2013 - 12:21 PM

*If you're making a singly LL, there's no need for a lastptr. Singly LL's only have a head node. The reason I assumed a circular LL was because you had a lastptr, and your node structure didn't indicate a doubly LL.

Ok, in your insertAtBack(),
void insertAtBack(int vl)//vl mnz value
{
	Node *newptr=new Node(vl);
	if(isEmpty())
	{
		firstptr=lastptr=newptr;
	}
	else
	{ 
		newptr->nxtptr=lastptr;
		lastptr=newptr;
	}
}



I would strongly advise against trying to keep track of a lastptr as the last node in the list throughout all your LL operations, because it is unnecessary you don't gain anything from it.

If at any point you need to find a pointer to the last node in the list, just incorporate a simple while loop:
node* temp_ptr=currentptr;
while(tempPtr!=NULL){
  tempPtr=tempPtr->next; 
}
/*at this point, temp_ptr points to the last node in the list as if it were lastptr and you can do what you wish with it*/



So you have the correct implementation for the special case of having of no nodes in the list. Now i'll give you the steps you need to take majority case:
void insertAtBack(int vl)//vl mnz value
{
	Node *newptr=new Node(vl);
	if(isEmpty())
	{
		firstptr=newptr;
	}
	else
	{ 
		//create a temporary pointer that starts at the front
                //iterate temporary pointer through the list until it's nextptr==NULL
                //set temporary pointer's next to newPtr 
	}
}


[/code]
Was This Post Helpful? 1
  • +
  • -

#11 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3549
  • View blog
  • Posts: 10,993
  • Joined: 05-May 12

Re: delete node

Posted 01 September 2013 - 02:37 PM

View PostJava Student, on 01 September 2013 - 03:21 PM, said:

I would strongly advise against trying to keep track of a lastptr as the last node in the list throughout all your LL operations, because it is unnecessary you don't gain anything from it.


Actually, you do gain something from it. <sarcasm>You get O(1) insertion performance that linked lists supposedly have.</sarcasm> (See this other thread for the source of my sarcasm.)
Was This Post Helpful? 0
  • +
  • -

#12 Java Student  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 480
  • Joined: 05-February 10

Re: delete node

Posted 01 September 2013 - 02:48 PM

View PostSkydiver, on 01 September 2013 - 02:37 PM, said:

View PostJava Student, on 01 September 2013 - 03:21 PM, said:

I would strongly advise against trying to keep track of a lastptr as the last node in the list throughout all your LL operations, because it is unnecessary you don't gain anything from it.


Actually, you do gain something from it. <sarcasm>You get O(1) insertion performance that linked lists supposedly have.</sarcasm> (See this other thread for the source of my sarcasm.)


Hahaha very nice Sky ;)
Was This Post Helpful? 0
  • +
  • -

#13 annie12  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 141
  • Joined: 27-July 13

Re: delete node

Posted 02 September 2013 - 05:18 AM

View PostJava Student, on 02 September 2013 - 12:21 AM, said:


i change the code as follows but it still not printing backward number and my print make one extra printing

[quote name='Java Student' date='02 September 2013 - 12:21 AM' timestamp='1378063288' post='1895666']
i change the code as you say but its still not printing and print function make also extra printing
  void insertAtBack(int vl)//vl mnz value
              {
              
                  if(isEmpty())
                  {
                      firstptr=lastptr;
                      }
                   else
                   { 
                     Node *tmptr=firstptr;
                     while(tmptr!=NULL)
                     {
                      tmptr=tmptr->nxtptr;                   
                      }
                      Node *newptr=(vl,tmptr);
                     }
              }


Was This Post Helpful? 0
  • +
  • -

#14 Java Student  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 480
  • Joined: 05-February 10

Re: delete node

Posted 02 September 2013 - 05:53 AM

If you absolutely need to make sure lastptr is assigned to the end node at end of all your LL operations, then at least try to get insertAtBack() working with firstptr first. Baby steps.

You seem to be going off track with your new insertAtBack(), so let's go back to your old implementation of it:
void insertAtBack(int vl)//vl mnz value
{
        Node *tmptr=new Node(v1);
	if(isEmpty())
	{
		firstptr=tmptr;//correct
	}
	else
	{ 
		
		while(tmptr!=NULL)//incorrect. while tmptr's next does not equal null, because we already tested if the head does not equal null in isEmpty()
		{
			tmptr=tmptr->nxtptr;//correct                   
		}
		Node *newptr=(vl,tmptr);//incorrect. we don't need to create more than 1 node, because we only adding 1 node. change this newptr to tmpptr which we have up top declared. also this statement is a compiler error I think you mean "Node *newptr(vl,tmptr);". the arguments are also incorrect. why? because, if this node is added to the end of the list, shouldn't it's next be NULL, not tmptr?
                //once you've set newptr correctly, tmptr->nextptr holds the correct position for where we want to store our newptr.
                //done
	}
}



You are getting close though, good job
Was This Post Helpful? 1
  • +
  • -

#15 annie12  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 141
  • Joined: 27-July 13

Re: delete node

Posted 02 September 2013 - 07:55 AM

View PostJava Student, on 02 September 2013 - 05:53 PM, said:


i try to change it again program is running but still not printing puk bavk node element :nottalkingtoyou:/>
 void insertAtBack(int vl)//vl mnz value
              {
                  Node *tmpptr=new Node(vl);
                  if(isEmpty())
                  {
                      firstptr=tmpptr;
                      }
                   else
                   {   
                     do
                     {
                       tmpptr=tmpptr->nxtptr;                         
                       
                      }while(tmpptr!=NULL);
                   }  
               }


This post has been edited by annie12: 02 September 2013 - 07:56 AM

Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3