Delete a specific node in link-list

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 827 Views - Last Post: 11 July 2013 - 12:17 AM Rate Topic: -----

#1 1helloworld1  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 11-May 13

Delete a specific node in link-list

Posted 07 July 2013 - 05:37 AM

I write a function to delete a node with element name "Henry". But It didn't work why?

void delete_Henry()
{
	node *travel,*del;
	if(head == NULL)
	{
		cout<<"ERROR : EMPTY LIST"<<endl;
	}

	else
	{
		travel = head;
		del = travel->next;

		while(del!=NULL)
		{
			if(del->Name=="Henry")
			{
				travel->next = del->next;
				delete del;
				del = travel->next;
			}
		}
	}

	if(head->Name== "Henry")
	{
		del = head;
		head = head->next;
		delete del;
	}

	for(travel = head ; travel != NULL ; travel=travel->next)
	{
		cout<<"Name: "<<travel->Name<<endl;
		cout<<"Position: "<<travel->Position<<endl;
		cout<<"Salary: "<<travel->Salary<<endl;
	}

}


Is This A Good Question/Topic? 0
  • +

Replies To: Delete a specific node in link-list

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 4018
  • View blog
  • Posts: 12,400
  • Joined: 25-December 09

Re: Delete a specific node in link-list

Posted 07 July 2013 - 05:46 AM

Quote

But It didn't work why?

Good question. Why didn't it work? For us to help you'll need to post more code and provide a sample of any input into the program. Post a small complete program that illustrates the problem.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 1helloworld1  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 11-May 13

Re: Delete a specific node in link-list

Posted 07 July 2013 - 06:42 AM

the whole program
#include <iostream>
#include <cstring>
using namespace std;

struct node 
{
	char Name[25];
	char Position[25];
	double Salary;
	node *next;
}; node *head=NULL;

void insert();

void delete_Henry();
void display();

int main()
{
	insert();
	
	delete_Henry();
	display();
	system("pause");
}

void insert()
{
	node *newnode , *travel;
	int num;

	cout<<"Enter any num to cont (-1 =exit): ";
	cin>>num;

	while(num != -1)
	{
		newnode = new node;

		cout<<"Enter name : ";cin>>newnode->Name;
		cout<<"Enter position : ";cin>>newnode->Position;
		cout<<"Enter salary : ";cin>>newnode->Salary;
		cout<<endl;

		newnode->next=NULL;

		if(head==NULL)
		{
			head = newnode;
		}

		else
		{
			travel = head;
			while(travel->next != NULL)
				travel = travel->next;
				travel->next = newnode;
		}

		cout<<"Enter any num to cont (-1 =exit): ";
		cin>>num;
	}
}

void display()
{
	node *travel;
	for(travel = head ; travel != NULL ; travel=travel->next)
	{
		cout<<"Name: "<<travel->Name<<endl;
		cout<<"Position: "<<travel->Position<<endl;
		cout<<"Salary: "<<travel->Salary<<endl;
	}
}

void delete_Henry()
{
	node *travel,*del;
	if(head == NULL)
	{
		cout<<"ERROR : EMPTY LIST"<<endl;
	}

	else
	{
		travel = head;
		del = travel->next;

		while(del!=NULL)
		{
			if(del->Name=="Henry")
			{
				travel->next = del->next;
				delete del;
				del = travel->next;
			}
		}
	}

	if(head->Name== "Henry")
	{
		del = head;
		head = head->next;
		delete del;
	}

	for(travel = head ; travel != NULL ; travel=travel->next)
	{
		cout<<"Name: "<<travel->Name<<endl;
		cout<<"Position: "<<travel->Position<<endl;
		cout<<"Salary: "<<travel->Salary<<endl;
	}

}

Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 4018
  • View blog
  • Posts: 12,400
  • Joined: 25-December 09

Re: Delete a specific node in link-list

Posted 07 July 2013 - 07:33 AM

Your first problem is that you can't use the comparison operators with C-strings. I suggest you think about switching to std::strings.


Jim
Was This Post Helpful? 0
  • +
  • -

#5 1helloworld1  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 11-May 13

Re: Delete a specific node in link-list

Posted 07 July 2013 - 08:33 AM

Issit like this? using string but the problem still there

#include <iostream>
#include <string>
using namespace std;

struct node 
{
	string Name;
	char Position[25];
	double Salary;
	node *next;
}; node *head=NULL;

void insert();

void delete_Henry();
void display();

int main()
{
	insert();
	display();
	delete_Henry();

	system("pause");
}

void insert()
{
	node *newnode , *travel;
	int num;

	cout<<"Enter any num to cont (-1 =exit): ";
	cin>>num;

	while(num != -1)
	{
		newnode = new node;

		cout<<"Enter name : ";
		cin.ignore();
		getline(cin,newnode->Name);
		cout<<"Enter position : ";cin>>newnode->Position;
		cout<<"Enter salary : ";cin>>newnode->Salary;
		cout<<endl;

		newnode->next=NULL;

		if(head==NULL)
		{
			head = newnode;
		}

		else
		{
			travel = head;
			while(travel->next != NULL)
				travel = travel->next;
				travel->next = newnode;
		}

		cout<<"Enter any num to cont (-1 =exit): ";
		cin>>num;
	}
}

void display()
{
	node *travel;
	for(travel = head ; travel != NULL ; travel=travel->next)
	{
		cout<<"Name: "<<travel->Name<<endl;
		cout<<"Position: "<<travel->Position<<endl;
		cout<<"Salary: "<<travel->Salary<<endl;
	}
}

void delete_Henry()
{
	node *travel,*del;
	string str1="Henry";

	if(head == NULL)
	{
		cout<<"ERROR : EMPTY LIST"<<endl;
	}

	else
	{
		travel = head;
		del = travel->next;

		while(del!=NULL)
		{
			if(del->Name==str1)
			{
				travel->next = del->next;
				delete del;
				del = travel->next;
			}
		}
	}

	if(head->Name== str1)
	{
		del = head;
		head = head->next;
		delete del;
	}

	for(travel = head ; travel != NULL ; travel=travel->next)
	{
		cout<<"Name: "<<travel->Name<<endl;
		cout<<"Position: "<<travel->Position<<endl;
		cout<<"Salary: "<<travel->Salary<<endl;
	}

}

Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is offline

  • member icon


Reputation: 4018
  • View blog
  • Posts: 12,400
  • Joined: 25-December 09

Re: Delete a specific node in link-list

Posted 07 July 2013 - 08:53 AM

Have you run your program thru your debugger, setting a break point early in main() and single stepping thru your program watching the variables as you step?

Jim
Was This Post Helpful? 0
  • +
  • -

#7 1helloworld1  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 11-May 13

Re: Delete a specific node in link-list

Posted 07 July 2013 - 09:05 AM

dont understand
Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg  Icon User is offline

  • member icon


Reputation: 4018
  • View blog
  • Posts: 12,400
  • Joined: 25-December 09

Re: Delete a specific node in link-list

Posted 07 July 2013 - 09:37 AM

What don't you understand? Do you know what a debugger is and how to use it?

What compiler are you using?

Jim
Was This Post Helpful? 0
  • +
  • -

#9 1helloworld1  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 11-May 13

Re: Delete a specific node in link-list

Posted 08 July 2013 - 12:05 AM

Quote

Do you know what a debugger is and how to use it?

Don't know :helpsmilie:

Quote

What compiler are you using?

i using microsoft visual studio 2010.
Was This Post Helpful? 0
  • +
  • -

#10 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: Delete a specific node in link-list

Posted 08 July 2013 - 04:00 AM

I noticed you partially used Jim's suggestion to use C++ strings ...
(since you are using C++ ... why not go all the way?)


This may help you get started ... (using a C++ style)

// delNodeHenry.cpp //

#include <iostream>
#include <sstream> // re. istringstream obj's
#include <string>

#include <cctype> // re. tolower


// some utilities //
char takeInChar( const std::string& msg )
{
    std::cout << msg << std::flush;
    std::string reply;
    getline( std::cin, reply );
    if( reply.size() ) return reply[0];
    // else ...
    return 0;
}

bool more()
{
    if( tolower( takeInChar( "\nMore (y/n) ? " )) == 'n' )
        return false;
    // else ...
    return true;
}


struct Node
{
    std::string name;
    std::string position;
    double salary;
    Node* next;

    // default ctor
    Node() { salary = 0.0; next = NULL; }
} ;

// here is where you define how to display a node //
void print( const Node* nd )
{
    std::cout << nd->name << ", " << nd->position << ", " << nd->salary;
}

// print all the Nodes using above print def'n //
void printAll( const Node* head )
{
    const Node* cur = head;
    while( cur )
    {
        print( cur );
        std::cout << std::endl;

        cur = cur->next;
    }
}

// clean un dynamic memory when done //
void clearAll( Node*& head )
{
    while( head )
    {
        Node* cur = head;
        head = head->next;
        delete cur;
    }
}

// returns new head address by ref...//
void push_front( Node*& head, Node* newNode )
{
    newNode->next = head;
    head = newNode;
}

Node* takeInNewNode()
{
    Node tmp;
    std::string dblStr;

    std::cout << "Enter name     : ";
    getline( std::cin, tmp.name );
    
    std::cout << "Enter position : ";
    getline( std::cin, tmp.position );

    std::cout << "Enter salary   : ";
    getline( std::cin, dblStr );
    std::istringstream iss( dblStr ); // construct iss from dblStr
    iss >> tmp.salary; // get double value ... or zero if bad data

    // check if data entered is good data ? before accepting it

    std::cout << "\nYou entered: ";
    print( &tmp );
    if( tolower( takeInChar( ", Ok ... (y/n) ? " )) == 'y' )
        return new Node( tmp ); // uses default copy ctor

    // else ...
    return NULL;
}

//void delete_Henry();


int main()
{
    Node* head = NULL;
    Node* tmp;

    do
    {
        std::cout << std::endl;
        if( ( tmp = takeInNewNode() ) != NULL )
            push_front( head, tmp );
        else
            std::cout << "\nNot accepted ...\n";

    }
    while( more() );

    std::cout << "\n\nShowing all the 'goods' ...\n";
    printAll( head );

    //delete_Henry();

    clearAll( head );

    std::cout << "\nPress 'Enter' to continue/exit ... " << std::flush;
    std::cin.get();
}

Was This Post Helpful? 0
  • +
  • -

#11 1helloworld1  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 11-May 13

Re: Delete a specific node in link-list

Posted 08 July 2013 - 04:18 AM

Quote

(since you are using C++ ... why not go all the way?)

because I only want to delete the element in NAME with "Henry" this string so i only used string on Name.
My problem nw is even with string the element "Henry" still there didn't delete from the node. :online2long:
Was This Post Helpful? 0
  • +
  • -

#12 jimblumberg  Icon User is offline

  • member icon


Reputation: 4018
  • View blog
  • Posts: 12,400
  • Joined: 25-December 09

Re: Delete a specific node in link-list

Posted 08 July 2013 - 04:29 AM

Quote

Don't know :helpsmilie:

You need to learn about the important piece of your IDE.

You may want to start by studying this tutorial on how to use your debugger.

Jim
Was This Post Helpful? 0
  • +
  • -

#13 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: Delete a specific node in link-list

Posted 08 July 2013 - 10:19 AM

View Post1helloworld1, on 08 July 2013 - 07:18 AM, said:

Quote

(since you are using C++ ... why not go all the way?)

because I only want to delete the element in NAME with "Henry" this string so i only used string on Name.
My problem nw is even with string the element "Henry" still there didn't delete from the node. ...



> because I only want to delete the element in NAME with "Henry" this string so i only used string on Name.

If you can use one C++ string in a struct, it (usually/often) only makes sense to use C++ strings every-where in that data stuct (record) ...

> ... My problem ... is ... the element "Henry" (is) still there ... (i.e. node) didn't delete ...

If the node you wish to delete is the first node ...
you have: CASE 1

If the node you wish to delete is NOT the first node,
i.e. there MIGHT exist MORE than one node,
you have: CASE 2



A (first) approach could be ... to code something like this:

in CASE 1,
(the node you wish to delete is the head node
and note: head != NULL && head->data == key )

//to delete head
Node* cur = head;
head = head->next;
delete cur;



in CASE 2 ( i.e. head != NULL && head->data != key )

Node* prev = head; // Note: here head != NULL
Node* cur = head->next; // Note: cur could be NULL
while( cur && cur->data != key)
{
   prev = cur; // update prev since not found yet
   cur = cur->next; // traverse to next cur ... which may be NULL
}

// ok ... test end condition //
if( cur ) // then node was found
{
   prev->next = cur->next;
   delete cur;
}
else // report NOT found


Was This Post Helpful? 0
  • +
  • -

#14 1helloworld1  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 11-May 13

Re: Delete a specific node in link-list

Posted 09 July 2013 - 03:28 AM

View PostDavid W, on 08 July 2013 - 10:19 AM, said:

View Post1helloworld1, on 08 July 2013 - 07:18 AM, said:

Quote

(since you are using C++ ... why not go all the way?)

because I only want to delete the element in NAME with "Henry" this string so i only used string on Name.
My problem nw is even with string the element "Henry" still there didn't delete from the node. ...



> because I only want to delete the element in NAME with "Henry" this string so i only used string on Name.

If you can use one C++ string in a struct, it (usually/often) only makes sense to use C++ strings every-where in that data stuct (record) ...

> ... My problem ... is ... the element "Henry" (is) still there ... (i.e. node) didn't delete ...

If the node you wish to delete is the first node ...
you have: CASE 1

If the node you wish to delete is NOT the first node,
i.e. there MIGHT exist MORE than one node,
you have: CASE 2



A (first) approach could be ... to code something like this:

in CASE 1,
(the node you wish to delete is the head node
and note: head != NULL && head->data == key )

//to delete head
Node* cur = head;
head = head->next;
delete cur;



in CASE 2 ( i.e. head != NULL && head->data != key )

Node* prev = head; // Note: here head != NULL
Node* cur = head->next; // Note: cur could be NULL
while( cur && cur->data != key)
{
   prev = cur; // update prev since not found yet
   cur = cur->next; // traverse to next cur ... which may be NULL
}

// ok ... test end condition //
if( cur ) // then node was found
{
   prev->next = cur->next;
   delete cur;
}
else // report NOT found



I did that
void delete_Henry()
{
	node *travel,*del;
	string str1="Henry";

	if(head == NULL)
	{
		cout<<"ERROR : EMPTY LIST"<<endl;
	}

	else
	{
		travel = head;
		del = travel->next;

		while(del!=NULL)
		{
			if(del->Name==str1)
			{
				travel->next = del->next;
				delete del;
				del = travel->next;
			}
		}
	}

	if(head->Name== str1)
	{
		del = head;
		head = head->next;
		delete del;
	}


View Postjimblumberg, on 08 July 2013 - 04:29 AM, said:

Quote

Don't know :helpsmilie:/>

You need to learn about the important piece of your IDE.

You may want to start by studying this tutorial on how to use your debugger.

Jim

thanks for the tutorial , i have check the variable i enter using the debugger it show no error and when del->Name is == to Henry it just don't care and show the output without delete it.
Was This Post Helpful? 0
  • +
  • -

#15 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: Delete a specific node in link-list

Posted 09 July 2013 - 09:20 AM

View Post1helloworld1, on 09 July 2013 - 06:28 AM, said:

...

I did that

...


If you did, would you have the problem, which you say you have?

So, your code is problematic still, is it not?

Your logic could flow like this ...


void delete_Henry( Node*& head )
{
    if( head )
    {
        if( head->name == "Henry" ) // case 1 //
        {
            // code for case 1

        }
        else // case 2 //
        {
            // code for case 2
        }
        
    }
    else
        std::cout << "\nList was empty ...\n";
}


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2