delete node

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

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

#16 Java Student  Icon User is offline

  • D.I.C Regular
  • member icon

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

Re: delete node

Posted 02 September 2013 - 08:21 AM

I apologize I seem to have missed a few crucial lines when I went to re-edit your function.

Your function should look like this:

void insertAtBack(int vl)//vl mnz value
{
	Node *newptr=new Node(vl,NULL);
	if(isEmpty())
	{
		firstptr=newptr;
	}
	else
	{ 
                Node *tmpptr=firstptr;
                while(tempptr->nxtptr!=NULL){
                   tempptr=tempptr->next;
                }
                tempptr->next=newptr;
	}
}



Feel free to ask questions if something doesn't make sense.
Was This Post Helpful? 0
  • +
  • -

#17 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 663
  • View blog
  • Posts: 2,273
  • Joined: 31-December 10

Re: delete node

Posted 02 September 2013 - 08:54 AM

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

I apologize I seem to have missed a few crucial lines when I went to re-edit your function.

Your function should look like this:

void insertAtBack(int vl)//vl mnz value
{
	Node *newptr=new Node(vl,NULL);
	if(isEmpty())
	{
		firstptr=newptr;
	}
	else
	{ 
                Node *tmpptr=firstptr;
                while(tempptr->nxtptr!=NULL){
                   tempptr=tempptr->next;
                }
                tempptr->next=newptr;
	}
}



Feel free to ask questions if something doesn't make sense.

If you're going to just post the answer, why not at least put some comments in there to explain what's going on?

Annie, this project is for a class correct? You've been posting about linked-lists for a while now, and it doesn't seem like you grasp the idea of what a linked list is and how it operates. What have you read about linked lists?
Was This Post Helpful? 0
  • +
  • -

#18 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2993
  • View blog
  • Posts: 10,342
  • Joined: 08-August 08

Re: delete node

Posted 02 September 2013 - 09:40 AM

View Postvividexstance, on 02 September 2013 - 11:54 AM, said:

Annie, this project is for a class correct? You've been posting about linked-lists for a while now, and it doesn't seem like you grasp the idea of what a linked list is and how it operates. What have you read about linked lists?

True.

I'll quote myself from another topic:

View PostCTphpnwb, on 01 September 2013 - 01:24 PM, said:

Suppose you have a list of items:
Pointer	|	Address in list	|	Name	|	Phone	|	Street address
1	|	00001	|	Jim	|	888-555-1212	|	111 Main Street
2	|	00002	|	Alice	|	888-555-2112	|	333 Broad Steet

You want to sort them, but moving things around takes time and energy that are at a premium. If you move the name, phone, and street address it will be too slow/costly, but if you move the pointer value it will be very cheap:
Pointer	|	Address in list	|	Name	|	Phone	|	Street address
2	|	00001	|	Jim	|	888-555-1212	|	111 Main Street
1	|	00002	|	Alice	|	888-555-2112	|	333 Broad Steet

Now when you go through the list of pointers they point to items in alphabetical order by Name.
Note that with a list of two items the difference in cost doesn't matter much, but when the list is longer and the items contain more information the difference becomes very important.

Do you see how swapping pointer values makes sorting a list easier? Now suppose I have this sorted (by Name) list:
Pointer	|	Address in list	|	Next	|	Name	|	Phone	|	Street address
2	|	1	|	3	|	Jim	|	888-555-1212	|	111 Main Street
1	|	2	|	1	|	Alice	|	888-555-2112	|	333 Broad Steet
3	|	3	|	4	|	Mary	|	888-555-2112	|	444 Maple Steet
4	|	4	|	null	|	Nancy	|	888-555-2112	|	1 Elm Steet

and I want to delete the third item (pointed to: 3, which is Mary). First, I need to change the pointer that points to Mary in the linked list, which is the next pointer for Jim. I need to change it to point to Mary's next (Nancy):
Pointer	|	Address in list	|	Next	|	Name	|	Phone	|	Street address
2	|	1	|	4	|	Jim	|	888-555-1212	|	111 Main Street
1	|	2	|	1	|	Alice	|	888-555-2112	|	333 Broad Steet
3	|	3	|	4	|	Mary	|	888-555-2112	|	444 Maple Steet
4	|	4	|	null	|	Nancy	|	888-555-2112	|	1 Elm Steet

Then I can delete the object containing Mary and delete the pointer to her from the list of pointers:
Pointer	|	Address in list	|	Next	|	Name	|	Phone	|	Street address
2	|	1	|	4	|	Jim	|	888-555-1212	|	111 Main Street
1	|	2	|	1	|	Alice	|	888-555-2112	|	333 Broad Steet
4	|	4	|	null	|	Nancy	|	888-555-2112	|	1 Elm Steet

Note that I didn't move Nancy at all. All I did was free up the memory used by Mary and shorten the list of pointers (and change the Next value for one node. Note: coding this would be easy if I were keeping track of previous pointers (doubly linked list) or at least knew the starting pointer address, but I'm just trying to show the concept here.

By the way, if you copy/paste these lists into a spreadsheet they should be easier to see.
Was This Post Helpful? 2
  • +
  • -

#19 Java Student  Icon User is offline

  • D.I.C Regular
  • member icon

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

Re: delete node

Posted 02 September 2013 - 09:55 AM

View Postvividexstance, on 02 September 2013 - 08:54 AM, said:

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

I apologize I seem to have missed a few crucial lines when I went to re-edit your function.

Your function should look like this:

void insertAtBack(int vl)//vl mnz value
{
	Node *newptr=new Node(vl,NULL);
	if(isEmpty())
	{
		firstptr=newptr;
	}
	else
	{ 
                Node *tmpptr=firstptr;
                while(tempptr->nxtptr!=NULL){
                   tempptr=tempptr->next;
                }
                tempptr->next=newptr;
	}
}



Feel free to ask questions if something doesn't make sense.

If you're going to just post the answer, why not at least put some comments in there to explain what's going on?

Annie, this project is for a class correct? You've been posting about linked-lists for a while now, and it doesn't seem like you grasp the idea of what a linked list is and how it operates. What have you read about linked lists?



Uhhh. I've tried to help for 5+ replies, plus the OP was getting very close, so I hardly think I "Just posted the answer." Comments? For what use? Maybe the OP understood the answer. If the OP did not understand, that's why I said "Feel free to ask questions if something doesn't make sense." Thanks for the downvote, though.
Was This Post Helpful? 0
  • +
  • -

#20 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 663
  • View blog
  • Posts: 2,273
  • Joined: 31-December 10

Re: delete node

Posted 02 September 2013 - 10:10 AM

Compare what you posted to what annie had posted right before, and it's not even close.
Was This Post Helpful? 0
  • +
  • -

#21 Java Student  Icon User is offline

  • D.I.C Regular
  • member icon

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

Re: delete node

Posted 02 September 2013 - 10:13 AM

View Postvividexstance, on 02 September 2013 - 10:10 AM, said:

Compare what you posted to what annie had posted right before, and it's not even close.


There is different sections of the function being discussed in different replies. You know that, don't play games.
Was This Post Helpful? 0
  • +
  • -

#22 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 663
  • View blog
  • Posts: 2,273
  • Joined: 31-December 10

Re: delete node

Posted 02 September 2013 - 10:25 AM

So this:
void insertAtBack(int vl)//vl mnz value
             {
                 Node *tmpptr=new Node(vl);
                 if(isEmpty())
                 {
                     firstptr=tmpptr;
                     }
                  else
                  {   
                    do
                    {
                      tmpptr=tmpptr->nxtptr;                         
                      
                     }while(tmpptr!=NULL);
                  }  
              }


and this:
void insertAtBack(int vl)//vl mnz value
{
	Node *newptr=new Node(vl,NULL);
	if(isEmpty())
	{
		firstptr=newptr;
	}
	else
	{ 
                Node *tmpptr=firstptr;
                while(tempptr->nxtptr!=NULL){
                   tempptr=tempptr->next;
                }
                tempptr->next=newptr;
	}
}


are "different sections" of the same function? Other than allocating a new node and checking if the list is empty, those functions are not the same.
Was This Post Helpful? 0
  • +
  • -

#23 Java Student  Icon User is offline

  • D.I.C Regular
  • member icon

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

Re: delete node

Posted 02 September 2013 - 10:29 AM

I'm not going to argue the point with you on the OP's thread. If you want to talk about it, PM me.
Was This Post Helpful? 0
  • +
  • -

#24 annie12  Icon User is offline

  • D.I.C Head

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

Re: delete node

Posted 03 September 2013 - 04:43 AM

[quote name='Java Student' date='02 September 2013 - 08:21 PM' timestamp='1378135271' post='1896076']
[\quote]

one more problem its printing back node but when i use insertatfront function and then i use print function it makes twice printing

View PostJava Student, on 02 September 2013 - 08:21 PM, said:

I apologize I seem to have missed a few crucial lines when I went to re-edit your function.

Your function should look like this:

void insertAtBack(int vl)//vl mnz value
{
	Node *newptr=new Node(vl,NULL);
	if(isEmpty())
	{
		firstptr=newptr;
	}
	else
	{ 
                Node *tmpptr=firstptr;
                while(tempptr->nxtptr!=NULL){
                   tempptr=tempptr->next;
                }
                tempptr->next=newptr;
	}
}



Feel free to ask questions if something doesn't make sense.

thnks its running.....but can you explain me about the while loop you made in the code
[code]
while(tmpptr->nxtptr!=NULL){
tmpptr=tmpptr->nxtptr;
}
[\code]

one more problem its printing back node but when i use insertatfront function and then i use print function it makes twice printing

This post has been edited by annie12: 03 September 2013 - 04:45 AM

Was This Post Helpful? 0
  • +
  • -

#25 annie12  Icon User is offline

  • D.I.C Head

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

Re: delete node

Posted 03 September 2013 - 04:51 AM

View Postvividexstance, on 02 September 2013 - 08:54 PM, said:

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

I apologize I seem to have missed a few crucial lines when I went to re-edit your function.

Your function should look like this:

void insertAtBack(int vl)//vl mnz value
{
	Node *newptr=new Node(vl,NULL);
	if(isEmpty())
	{
		firstptr=newptr;
	}
	else
	{ 
                Node *tmpptr=firstptr;
                while(tempptr->nxtptr!=NULL){
                   tempptr=tempptr->next;
                }
                tempptr->next=newptr;
	}
}



Feel free to ask questions if something doesn't make sense.

If you're going to just post the answer, why not at least put some comments in there to explain what's going on?

Annie, this project is for a class correct? You've been posting about linked-lists for a while now, and it doesn't seem like you grasp the idea of what a linked list is and how it operates. What have you read about linked lists?

no i was trying to make link list.link list is a chain of nodes which is linking with the help of pointers.its last node points to null and second last node pointer points to third last node...and i think its a kind of train
Was This Post Helpful? 0
  • +
  • -

#26 Java Student  Icon User is offline

  • D.I.C Regular
  • member icon

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

Re: delete node

Posted 03 September 2013 - 11:01 AM

View Postannie12, on 03 September 2013 - 04:43 AM, said:

thnks its running.....but can you explain me about the while loop you made in the code
while(tmpptr->nxtptr!=NULL){
tmpptr=tmpptr->nxtptr;
}

one more problem its printing back node but when i use insertatfront function and then i use print function it makes twice printing


Yes of course.

Ok, so in the while loop, the tmpptr is basically acting as the train conductor checking if its nextptr(next train in line) is NOT NULL(it's nxtptr is NOT the last train in line). So the while loop will stop when tmpptr's nxtptr(the conductor's next train in line) IS NULL. So once the while loop breaks, we know that tmpptr's nxtptr(tmpptr->nxtptr) is the position at which we need to place the next train(our newNode).

Let's tackle your insertAtFront(), which is slightly easier since we don't have to traverse, but there is a problem with it:

void insertAtFront(int vl)//vl mnz value
{
	Node *newptr=new Node(vl);//correct
	if(isEmpty())//correct
	{
                //if, in this case, the newptr we add acts as the first AND last node, what should we make newptr's nxtptr equal to?
		firstptr=newptr;//correct, but missing 1 line before it
	}
	else//correct
	{ 
		newptr->nxtptr=firstptr;//correct
		firstptr=newptr;//correct
	}
}



I hope that analogy works for you.

This post has been edited by Java Student: 03 September 2013 - 11:06 AM

Was This Post Helpful? 0
  • +
  • -

#27 annie12  Icon User is offline

  • D.I.C Head

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

Re: delete node

Posted 04 September 2013 - 04:43 AM

[quote name='Java Student' date='03 September 2013 - 11:01 PM' timestamp='1378231308' post='1896651']

Quote

insertatffront function is right running i have just misunderstood print function..
i have made the complete code plus deleteatfront and back .....delete at front is right printing but at the place of deleteatback function compiler prints a number like -17891602

#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;

 public:
 list():firstptr(0)
 {
 //empty body                           
 }
 void insertAtFront(int vl)//vl mnz value
 {
       Node *newptr=new Node(vl);
       if(isEmpty())
       {
       firstptr=newptr;
       }
       else
       { 
       newptr->nxtptr=firstptr;
       firstptr=newptr;
       }
 }
 void insertAtBack(int vl)//vl mnz value
 {
 Node *newptr=new Node(vl,NULL);
if(isEmpty())
{
firstptr=newptr;
}
else
{ 
Node *tmpptr=firstptr;
while(tmpptr->nxtptr!=NULL){
tmpptr=tmpptr->nxtptr;
}
tmpptr->nxtptr=newptr;
}
}

bool isEmpty()
{
if(firstptr==NULL)
return true;
else
return false;
}
void print()
{    
Node *currentptr=firstptr;
if(isEmpty())
{  
cout<<"List is empty"<<endl;
}
cout<<"now list have";
while(currentptr!=0)
{     // cout<<"now list have";
cout<<currentptr->data<<"  ";
currentptr=currentptr->nxtptr;
}
cout<<endl;  
}
void deleteatfront()
{
Node *currentptr=firstptr;
firstptr=firstptr->nxtptr;
delete currentptr;
}   
void deleteatback()
{
Node *currentptr=firstptr;             
while(currentptr->nxtptr!=NULL){
currentptr=currentptr->nxtptr;
} 
delete currentptr;
}               
};
 int main()
 {
 list obj;
 obj.insertAtFront(8);
 obj.print();
 obj.insertAtFront(9);
 obj.print();  
 obj.insertAtBack(7);
 obj.print();
 obj.deleteatfront();
 obj.print();
 obj.deleteatback();
 obj.print();
 system("PAUSE");
 return 0;
 }


Was This Post Helpful? 0
  • +
  • -

#28 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2993
  • View blog
  • Posts: 10,342
  • Joined: 08-August 08

Re: delete node

Posted 04 September 2013 - 07:09 AM

If you simply delete the last item in the list what do you think will happen when your code tries to access the next item from the one before it?
Starting list:

Quote

Item 1 ->next item: 2
Item 2 ->next item: 3
Item 3 ->next item: 4
Item 4 ->next item: null

Delete item 4:

Quote

Item 1 ->next item: 2
Item 2 ->next item: 3
Item 3 ->next item: 4
Is this right?
Was This Post Helpful? 0
  • +
  • -

#29 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 663
  • View blog
  • Posts: 2,273
  • Joined: 31-December 10

Re: delete node

Posted 04 September 2013 - 08:54 AM

This is why just giving people code is against the rules and it doesn't actually help them to learn the problem at hand.
Was This Post Helpful? 0
  • +
  • -

#30 Java Student  Icon User is offline

  • D.I.C Regular
  • member icon

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

Re: delete node

Posted 04 September 2013 - 09:47 AM

View Postvividexstance, on 04 September 2013 - 08:54 AM, said:

This is why just giving people code is against the rules and it doesn't actually help them to learn the problem at hand.


This is not the appropriate thread to express your hate for other people's possible rule breaking. If you think myself, CT or anyone else on this thread has broken a rule, take it up with a MOD(Macos is always a favorite). Let's not troll the thread of those who wish to learn.

View Postannie12, on 04 September 2013 - 04:43 AM, said:

Quote

insertatffront function is right running i have just misunderstood print function..
i have made the complete code plus deleteatfront and back .....delete at front is right printing but at the place of deleteatback function compiler prints a number like -17891602


Your deleteatfront is correct. Your deleteatback is incorrect, but it is one of the trickier problems with a many solutions. Let's look at your current deleteatback(),

void deleteatback()
{
        //What happens you deleteatback() when there is only 1 node in the list? Your program will crash because you're accessing NULL->nxtptr in while loop. 
	Node *currentptr=firstptr;      
        //This will not work. We need another pointer to keep track of the second last in line. So we can #1-delete the last point(correct), then #2-set the new last node(second last node) to null, because it is now the last node after the delete. But you can't do this because you haven't kept track of the second last node. 
	while(currentptr->nxtptr!=NULL){
		currentptr=currentptr->nxtptr;
                //something needs to go here
	} 
	delete currentptr;
}      


Was This Post Helpful? -1
  • +
  • -

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