4 Replies - 1887 Views - Last Post: 20 June 2009 - 10:23 PM Rate Topic: -----

#1 kingdom   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 16-June 09

How to delete a Node in LinkedList?

Posted 19 June 2009 - 12:03 AM

I want to delete the Node based on the text user typed in the textbox.
Lets say the user type to delete the album titled "Wonders", my code will use this information provided by the user and start traversing the linkedlist to delete the node that contain the album titled "Wonders"...

What I'm experiencing now is that the message box showed that its deleted successfully but the song details is still being displayed on the TreeView...

Can someone help me in this?

Thanks.

Button click action code:
private void btnDeleteByAlbum_Click(object sender, EventArgs e)
		{
			string album2Delete = txtBxDeleteAlbum.Text;
			bool returnResult = list.deleteAlbum(album2Delete);

			if (returnResult == true)
			{
				MessageBox.Show("Album deleted.");
				txtBxDeleteAlbum.Clear();
			}
			else
			{
				MessageBox.Show("Album was not deleted successfully.");
			}

			//Referesh the screen and counter
			this.Visualize(list.Head); //Takes link list and display it inside the white space form.

			//visualize the linked list
			lblLinkedListSizeValue.Text = list.Count.ToString();
		}




Actual deletion code:
public bool deleteAlbum(string album)
		{
			Item tempA = null;
			Node removeNode = null;
			//Node previous = null;
			bool deleted = false;

			for (Node cursor = head; cursor != null; cursor = cursor.Link) //traverse the linkklist 
			{
				tempA = (Item)cursor.Data;

				if (album.ToLower() == tempA.Album.ToLower())
				{
					removeNode = cursor;
					cursor.Link = removeNode.Link;
					removeNode = null;
					Count--;
					deleted = true;
				}
			}
			return deleted;
		}



Is This A Good Question/Topic? 0
  • +

Replies To: How to delete a Node in LinkedList?

#2 SwiftStriker00   User is offline

  • No idea why my code works
  • member icon

Reputation: 439
  • View blog
  • Posts: 1,617
  • Joined: 25-December 08

Re: How to delete a Node in LinkedList?

Posted 19 June 2009 - 05:35 AM

removeNode = cursor; is the problem. You are then setting its link to it's link. effectively making a small loop.

You need a another Node, previousNode. It will always be one Node behind cursor. Then when cursor hits the Node you want to remove you can do the following:
previous.Link = cursor.Link;
cursor.Link = null;
Count--;



Then let the garbage collector do its magic to your de-referenced Node :^:

Edit: you also dont need the deleted bool, since you can return true when you find the similar object. So end-all could be something like this:
public bool deleteAlbum(string album) {
			Item tempA = null;
			Node cursor = head;
			Node previous = cursor;

			while( cursor != null ) {
				tempA = (Item)cursor.Data;
				if( album.ToLower() == tempA.Album.ToLower() ) {
					previous.Link = cursor.Link;
					cursor.Link = null;
					Count--;
					return true;
				}
				previous = cursor;
				cursor = cursor.Link;
			}
			return false;
		}


This post has been edited by SwiftStriker00: 19 June 2009 - 05:44 AM

Was This Post Helpful? 0
  • +
  • -

#3 kingdom   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 16-June 09

Re: How to delete a Node in LinkedList?

Posted 20 June 2009 - 02:29 AM

I have modified my code again cos I realise I can use the remove at position method to do it and now it won't delete the last node.
E.g: I have a song in the playlist and I wanna delete it, it just can't be deleted..

public bool deleteA(string album, LinkedList list)
		{
			Item tempA = null;
			Node cursor = head;
			bool deleted = false;
			int position = 0;

			for (int i = 1; i < list.Count; i++)
			{
				tempA = (Item)cursor.Data;

				while (list != null)
				{
				 
					if (album.ToLower() == tempA.Album.ToLower())
					{
						position = i;
						list.RemoveAt(position);
						deleted = true;
					}
					break;
				}
			}
			return deleted;
		}


This post has been edited by kingdom: 20 June 2009 - 07:00 AM

Was This Post Helpful? 0
  • +
  • -

#4 Smaini   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 19-June 09

Re: How to delete a Node in LinkedList?

Posted 20 June 2009 - 07:07 AM

Hey there! Erm.. Yeah. Good guess. I am from TP. How did you know? I'm guessing that you're from Dip IT or MWC or GET?? I'm from MWC year 2. And the assignment?? I'm like SUPER stuck and lost?? How about yourself?? I do really need alot of help a.s.a.p. Nice knowing ya!
Was This Post Helpful? 0
  • +
  • -

#5 masteryee   User is offline

  • D.I.C Regular

Reputation: 40
  • View blog
  • Posts: 271
  • Joined: 16-May 09

Re: How to delete a Node in LinkedList?

Posted 20 June 2009 - 10:23 PM

Start your for loop with i = 0 instead of 1.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1