delete node

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

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

#31 annie12  Icon User is offline

  • D.I.C Head

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

Re: delete node

Posted 06 September 2013 - 01:14 AM

i have change the deleteatback but it still printing the number like -17891602
void deleteatback()
{
Node *currentptr=firstptr;
Node *secondlast=firstptr;
while(currentptr->nxtptr!=NULL){
if(currentptr==NULL){
break;
}
currentptr=currentptr->nxtptr;


secondlast=secondlast->nxtptr;
}
secondlast->nxtptr=NULL;
delete currentptr;
}


Was This Post Helpful? 0
  • +
  • -

#32 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2834
  • View blog
  • Posts: 9,737
  • Joined: 08-August 08

Re: delete node

Posted 06 September 2013 - 05:18 AM

You don't need to use two pointers. You can do something like this:
if(currentptr->nxtptr->nxtptr == NULL) {
   // Do something
}

Was This Post Helpful? 1
  • +
  • -

#33 annie12  Icon User is offline

  • D.I.C Head

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

Re: delete node

Posted 06 September 2013 - 06:28 AM

View PostCTphpnwb, on 06 September 2013 - 05:18 PM, said:

You don't need to use two pointers. You can do something like this:
if(currentptr->nxtptr->nxtptr == NULL) {
   // Do something
}

its done thnks .......but explain me me if statement twice derefrencing

This post has been edited by annie12: 06 September 2013 - 06:29 AM

Was This Post Helpful? 0
  • +
  • -

#34 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2834
  • View blog
  • Posts: 9,737
  • Joined: 08-August 08

Re: delete node

Posted 06 September 2013 - 06:38 AM

Think of it this way: currentptr->nxtptr->nxtptr breaks down as temporary_pointer = currentptr->nxtptr is the pointer to the next node, and the value of temporary_pointer->nxtptr is the pointer to the note after that. So it's getting the pointer to not the next node, but the one after that. If that's null, then we want to delete the next node and set our current nxtptr to null.

By the way: it's done isn't very descriptive. You should get in the habit of posting relevant code along with a description. Often, just doing that will cause you to see a solution or at least better understand the problem(s).

This post has been edited by CTphpnwb: 06 September 2013 - 06:35 AM

Was This Post Helpful? 1
  • +
  • -

#35 annie12  Icon User is offline

  • D.I.C Head

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

Re: delete node

Posted 06 September 2013 - 08:54 AM

View PostCTphpnwb, on 06 September 2013 - 06:38 PM, said:

Think of it this way: currentptr->nxtptr->nxtptr breaks down as temporary_pointer = currentptr->nxtptr is the pointer to the next node, and the value of temporary_pointer->nxtptr is the pointer to the note after that. So it's getting the pointer to not the next node, but the one after that. If that's null, then we want to delete the next node and set our current nxtptr to null.

By the way: it's done isn't very descriptive. You should get in the habit of posting relevant code along with a description. Often, just doing that will cause you to see a solution or at least better understand the problem(s).

that's the complete code
#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;
Node *secondlast;
while(currentptr->nxtptr!=NULL){
if(currentptr->nxtptr->nxtptr==NULL){
break;
}
currentptr=currentptr->nxtptr;
}
secondlast=currentptr;
secondlast->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;
 }



Quote

i always put the code side by side in this topic and because its error free so i think i should not post that code and where i got problem i ask you

This post has been edited by annie12: 06 September 2013 - 09:02 AM

Was This Post Helpful? 0
  • +
  • -

#36 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 629
  • View blog
  • Posts: 2,105
  • Joined: 31-December 10

Re: delete node

Posted 06 September 2013 - 09:01 AM

One thing I would like to add is that there is some code repetition there. The loops to get the last node of the list could be made into a helper function that would decrease the size of some of the functions and more than likely make them easier to read/understand. Also, you really should be commenting your code to help yourself and others be able to read and understand it better.

*EDIT*:
Here is the above code with a proper indentation style, you don't need to use this style, there are plenty to choose from, this is the one I prefer. This makes the code so much easier to reach and digest:
#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
{
	Node *firstptr;

public:
	list() : firstptr(0) {}
	
	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;
			return;
		}
		
		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;
		Node *secondlast;
	
		while(currentptr->nxtptr!=NULL)
		{
			if(currentptr->nxtptr->nxtptr == NULL)
			{
				break;
			}
		
			currentptr=currentptr->nxtptr;
		}
	
		secondlast = currentptr;
		secondlast->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;
}


Note how I commented out system because you really shouldn't be using that, especially if you want to write portable code. If you're just using that to keep the console window open, then you can replace the system("PAUSE") with cin.get() to do the same thing.

This post has been edited by vividexstance: 06 September 2013 - 09:11 AM

Was This Post Helpful? 1
  • +
  • -

#37 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2834
  • View blog
  • Posts: 9,737
  • Joined: 08-August 08

Re: delete node

Posted 06 September 2013 - 11:39 AM

Look at your while loop:
void deleteatback()
{
	Node *currentptr=firstptr;
	Node *secondlast;

	while(currentptr->nxtptr!=NULL)
	{
		if(currentptr->nxtptr->nxtptr == NULL)
		{
			break;
		}
	
		currentptr=currentptr->nxtptr;
	}

	secondlast = currentptr;
	secondlast->nxtptr = NULL;
	currentptr = currentptr->nxtptr;

	delete currentptr;
}


Why would it need a break? What's wrong with doing this?
void deleteatback()
{
	Node *currentptr=firstptr;

	while(currentptr->nxtptr->nxtptr!=NULL)
	{	
		currentptr=currentptr->nxtptr;
	}
	delete currentptr->nxtptr;
	currentptr->nxtptr = NULL;

}


Of course, you'd need to check that there is a nxtptr object first:
void deleteatback()
{
	Node *currentptr=firstptr;
	if(currentptr->nxtptr!=NULL)
	{
		while(currentptr->nxtptr->nxtptr!=NULL)
		{	
			currentptr=currentptr->nxtptr;
		}
		delete currentptr->nxtptr;
		currentptr->nxtptr = NULL;
	}
}


but now you only need to use currentptr and you've got less logic happening inside the while loop.
Was This Post Helpful? 2
  • +
  • -

#38 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 06 September 2013 - 12:07 PM

View PostCTphpnwb, on 06 September 2013 - 05:18 AM, said:

You don't need to use two pointers. You can do something like this:
if(currentptr->nxtptr->nxtptr == NULL) {
   // Do something
}


I thought of this too, but there was a problem when I thought about it more.

In the case of having 1 node, then calling deleteAtBack(), currentptr will be the head as always and this will happen:

if(head->nxtptr->NULL == NULL) {
   // Do something
}


Causing an access violation.

The solution to this would be changing the deleteAtBack() structure:

From:
if(isEmpty()){
   //no nodes upon call
}else{
   //1 or more node upon call
}



To:
if(isEmpty()){
   //no nodes upon call
}else if(currentptr->nxtptr==NULL){
   //1 node upon call
}else{
   //2 or more upon call
}



This or your above post will work.

This post has been edited by Java Student: 06 September 2013 - 12:10 PM

Was This Post Helpful? 0
  • +
  • -

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