Delete element method in Linked List

Need to delete element in "last" node

Page 1 of 1

4 Replies - 12308 Views - Last Post: 20 November 2007 - 11:45 AM Rate Topic: -----

#1 CrazyJ  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 15-October 07

Delete element method in Linked List

Posted 14 November 2007 - 11:05 PM

Hi, I have this linked list in C# whereupon I am overloading binary operators based on int input from the user, as well as manipulating the list using a menu whinch allows the user to choose which operators to use. My issue right now is in my "DeleteLast" method. I need to enable the deletion of the last node element if so chosen. My "DeleteFirst" method works just fine, deleting an element each time the user chooses the DeleteFirst menu option. If anyone can take a peek at my gob of code and tell me what I'm doing wrong in this particular method, it would really be appreciated. Thanks!

This is just the portion of my class where I am having the issue with DeleteLast...
//	  ==========================
		public void DeleteFirst( )
		{
			if (Length() >= 1){

				if (Length() == 1){
					first = null;
					last = null;
				}
				else { // Length() > 1
					first = first.next;
				} // else

			} // if then			 
		} // DeleteFirst
//	  ========================
		public void DeleteLast()
		{
			if (Length() >= 1)
			{

				if (Length() == 1)
				{
					first = null;
					last = null;
				}
				else
				{ // Length() > 1
					last = last.next; 
				} // else

			}//if then
		}//DeleteLast



and my Node class

   public class Node
	{

//	  ====================
		internal int  datum;
		internal Node next;
	   
//	  ===================
		
//  =============
//  Constructors:
//  =============

//	  ============
//	  Constructor:
//	  ==============================
		public Node( int  nodeElement,
					 Node n )
		{
			datum = nodeElement;
			next = n; 
		} // constructor Node
//	  =====================

	} // class Node
//  ===============



the MainProgram

 class MainProgram
	{
		
//	  ===============================
		static void Main(string[] args)
		{
			int  menuNumber;
			int  datumValue;

			bool goAgain = true;

			SinglyLinkedList listA = new SinglyLinkedList("listA");
			SinglyLinkedList listB = new SinglyLinkedList("listB");
			SinglyLinkedList listC = new SinglyLinkedList("listC");

//		  ===============
			do // User loop
			{
				UtilityMethods.Menu( out menuNumber );
			   
//			  =====================
				switch ( menuNumber )
				{
					case 1: 
						UtilityMethods.CaseBanner("Initialize ListA.");
						listA = new SinglyLinkedList("listA");
						break;

					case 2: 
						UtilityMethods.CaseBanner("Initialize ListB.");
						listB = new SinglyLinkedList("listB");
						break;

					case 3: 
						UtilityMethods.CaseBanner("Output ListA.");
						listA.Print();
						break;

					case 4: 
						UtilityMethods.CaseBanner("Output ListB.");
						listB.Print();
						break;

					case 5: 
						UtilityMethods.CaseBanner("Output ListC.");
						listC.Print();
						break;

					case 6: 
						UtilityMethods.CaseBanner("Insert First ListA.");
						UtilityMethods.GetInteger( out datumValue );
						listA.InsertFirst( datumValue );
						break;

					case 7:
						UtilityMethods.CaseBanner("Delete First ListA.");
						listA.DeleteFirst();
						break;

					case 8: 
						UtilityMethods.CaseBanner("Insert First ListB.");
						UtilityMethods.GetInteger(out datumValue);
						listB.InsertFirst( datumValue );
						break;

					case 9:
						UtilityMethods.CaseBanner("Delete First ListB.");
						listB.DeleteFirst();
						break;

					case 10: 
						UtilityMethods.CaseBanner("Insert Last ListA.");
						UtilityMethods.GetInteger( out datumValue );
						listA.InsertLast(datumValue);
						break;

					case 11:
						UtilityMethods.CaseBanner("Delete Last ListA.");
						listA.DeleteLast();
						break;


					case 12: 
						UtilityMethods.CaseBanner("Insert Last ListB.");
						UtilityMethods.GetInteger(out datumValue);
						listB.InsertLast( datumValue );
						break;

					case 13:
						UtilityMethods.CaseBanner("Delete Last ListB.");
						listB.DeleteLast();
						break;

					case 14: 
						UtilityMethods.CaseBanner("Deep Copy ListA to ListB.");
						listB.DeepCopy(listA);
						break;

					case 15: 
						UtilityMethods.CaseBanner("Deep Copy ListB to ListA.");
						listA.DeepCopy(listB);
						break;

					case 16: 
						UtilityMethods.CaseBanner("ListC = ListA + ListB.");
						listC = new SinglyLinkedList("listC");
						listC = listA + listB;
						break;

					case 17:
						UtilityMethods.CaseBanner("ListC = ListA - ListB.");
						listC = new SinglyLinkedList("listC");
						listC = listA - listB;
						break;

					case 18:
						if (listA > listB)
						{
							Console.WriteLine("ListA is larger than ListB");
						}
						else if (listA < listB)
						{
							Console.WriteLine("ListB is larger than ListA");
						}
							
						else
						{
							Console.WriteLine("ListA and ListB must be equal.");
						}   
						break;

					case 19:
						if (listA == listB)
						{
							Console.WriteLine("ListA and ListB are equal length.");
						}
						else
						{
							Console.WriteLine("ListA and ListB are not equal length.");
						}
						break;

					case 20:
						if (listA >= listB)
						{
							Console.WriteLine("ListA is greater than or equal to ListB.");
						}
						else if (listA <= listB)
						{
							Console.WriteLine("ListA is less than or equal to ListB.");
						}

						else
						{
							Console.WriteLine("ListA and ListB must be equal.");
						}   
						break;

					case 21: 
						UtilityMethods.CaseBanner("Exit Program.");
						goAgain = false;
						break;


					default:						   
						Console.WriteLine("Invalid menu item.");
						break;
							
				} // switch
//			  ===========

			} while ( goAgain );
//		  ====================
			
		} // method Main
//	  ================

	} // class MainProgram
//  ======================

} // namespace SinglyLinkedListProject



Thanks so much for any advice :D

Is This A Good Question/Topic? 0
  • +

Replies To: Delete element method in Linked List

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 3872
  • View blog
  • Posts: 11,405
  • Joined: 18-April 07

Re: Delete element method in Linked List

Posted 15 November 2007 - 11:55 AM

I am going to take a stab at this since I am not completely sure what first and last may exactly mean. But I think the problem is with your statement last = last.next. First thing I want to point out is that if this is the last node, last.next will be null. Which is fine, but I wanted to make sure you knew that. What you need to do is get the reference to the node BEFORE the last and set its "next" to last.next. The idea is that the node before the node to be deleted will have to have its "next" pointer point to the one AFTER the node being deleted. In this case again, it will be null if this is the last element. So navigate to the node before the last node, lets call it secondtoLast and set it up like...

secondtoLast.next = last.next;



The image of this would look like so....

Posted Image

Hope that clarifies some things.

Enjoy!

"At DIC we be node changing code ninjas!" :snap:
Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 4880
  • View blog
  • Posts: 11,270
  • Joined: 16-October 07

Re: Delete element method in Linked List

Posted 15 November 2007 - 02:14 PM

I have to ask, you have to implement DeleteFirst and DeleteLast? Are you sure this is supposed to be a singly linked list? Why would be delete it from both ends?

Anyway, Martyr2's description is great. If you have a Length() function that works, it presumablely traverses all the next nodes? With the same code, have it hang on to the prior node and have the prior node ophan it's next when you get to the bottom.

Note, you absolutely should not have to check a Length() function for either of these opperations. e.g. DeleteFirst is as simple as if(first!=null) { first=first.next; }

Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#4 CrazyJ  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 15-October 07

Re: Delete element method in Linked List

Posted 20 November 2007 - 12:09 AM

View PostMartyr2, on 15 Nov, 2007 - 11:55 AM, said:

I am going to take a stab at this since I am not completely sure what first and last may exactly mean. But I think the problem is with your statement last = last.next. First thing I want to point out is that if this is the last node, last.next will be null. Which is fine, but I wanted to make sure you knew that. What you need to do is get the reference to the node BEFORE the last and set its "next" to last.next. The idea is that the node before the node to be deleted will have to have its "next" pointer point to the one AFTER the node being deleted. In this case again, it will be null if this is the last element. So navigate to the node before the last node, lets call it secondtoLast and set it up like...

secondtoLast.next = last.next;



The image of this would look like so....

Posted Image

Hope that clarifies some things.

Enjoy!

"At DIC we be node changing code ninjas!" :snap:


Thanks Martyr2, that is a superb explanation of the way the next operator works. I actually got it figured out later on that morning so thanks are in order for all who replied...Thanks! I have another question though on the same program. I am trying to implement a "Search" operator that will iterate through the list and output the value in a given node based on input e.g. user types 5 and the program displays the value(element) in 5. I am having a problem calling it from my Main program. Also, shown below is code for an ObtainFirst and ObtainLast operators that will display elements in the first and last nodes, similar issue. Any suggestions?

Search, ObtainFirst & ObtainLast Methods:
 public void Search(SinglyLinkedList list,
						   int nodeVal,
						   ref int nodeLocation)
		{

			int counter = 1;

			Node current;

			current = list.first;

			while (current != null)
			{

				if (current.datum == nodeVal)
				{

					nodeLocation = counter;

				}

				current = current.next;

				counter++;

			}// while loop

		}//method Search
/	  ===============
//	  Method ObtainFirst:
//	  ========================================
		public void ObtainFirst(ref int nodeVal)
		{
			nodeVal = first.datum;
		}//method ObtainFirst
//	  =====================
//	  Method ObtainLast:
//	  ========================================
		public void ObtainLast(ref int nodeVal)
		{
			nodeVal = last.datum;
		}//method ObtainLast
//	  ====================



Main program (where I am trying to call them):
 case 14:
						UtilityMethods.CaseBanner("ObtainFirst.");
						UtilityMethods.GetInteger(out nodeVal);
						listA.ObtainFirst(ref nodeVal);   //   <------Issue here?
						break;

					case 15:
						UtilityMethods.CaseBanner("ObtainLast.");
						UtilityMethods.GetInteger(out nodeVal);
						listA.ObtainLast(ref nodeVal);   //   <-------
						break;

//					=====================
 case 24:
						UtilityMethods.CaseBanner("Search List.");
						UtilityMethods.GetInteger(out nodeVal);
						listA.Search();  //		<--------	 <------What do I need to put here?
						break;




The GetInteger Utility:
//	  ================================================
		public static void GetInteger(out int intValue )
		{
			string intString;
 
			Console.WriteLine();
			Console.WriteLine("=======================");
			Console.WriteLine("Enter an integer value.");

			intString = Console.ReadLine();
			intValue = int.Parse(intString);

			Console.WriteLine("GetInteger terminating.");
			Console.WriteLine("=======================");
			Console.WriteLine();

		} // method IterateAgain
//	  ========================


I hope this is sufficient, I didn't want to include ALL of the code again :rolleyes: Thanks!
Was This Post Helpful? 0
  • +
  • -

#5 CrazyJ  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 15-October 07

Re: Delete element method in Linked List

Posted 20 November 2007 - 11:45 AM

I've got this one all wrapped up now :^: . Thanks alot Martyr2 and baavgai for your help. I appreciate your objective view, and thought provoking suggestions. Thanks.

"node changing code ninjas..." LOL... :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1