View Single Post
Old May 21st, 2005, 4:06 PM   #1
Firebar
Newbie
 
Join Date: May 2005
Posts: 2
Rep Power: 0 Firebar is on a distinguished road
Singly Linked List Help

Hi everyone, new to these forums

I'm currently working on making and manipulating a singly linked list, using books and websites etc etc. There is just one thing I'm not too sure about, and thats deleting a SPECIFIC item from the list. Below is the code so far:

import java.io.*;

class Link
   {
   public int iData;              // data item
   public Link next;              // next link in list
// -------------------------------------------------------------
   public Link(int id)
      {
      iData = id;                 // initialize data
      }                           //  set to null)
// -------------------------------------------------------------
   public void displayLink()      // display ourself
      {
      System.out.print("-[" + iData + "]-" );
      }
   }  // end class Link
////////////////////////////////////////////////////////////////
class LinkList
   {
   private Link first;            // ref to first link on list

// -------------------------------------------------------------
   public LinkList()              // constructor
      {
      first = null;               // no links on list yet
      }
// -------------------------------------------------------------
   public boolean isEmpty()       // true if list is empty - inbuilt java function
      {
      return (first==null);
      }
// -------------------------------------------------------------

   public void insertFirst(int id)// insert data at the start of the list
      {
      Link newLink = new Link(id);// make a new link
      newLink.next = first;       // newLink --> old first new link is now first
      first = newLink;            // first --> newLink
      }
// -------------------------------------------------------------
   public Link deleteFirst()      // delete first item
      {                           // (assumes list not empty)
      Link temp = first;          // save reference to link
      first = first.next;         // delete it: first-->old next
      return temp;                // return deleted link
      }
// -------------------------------------------------------------
   public void deleteLink()       // delete a user specified link - IN PROGRESS
   	  {
	  Link temp2 = first;		  // save reference to link
	  Link current = first;
	  current = current.next;
	  }

// -------------------------------------------------------------
   public void displayList()
      {
      System.out.print("LinkedList [First Item -> Last Item]: ");
      Link current = first;       // start at beginning of list
      while(current != null)      // until end of list,
         {
         current.displayLink();   // print data
         current = current.next;  // move to next link
         }
      System.out.println("");
      }
// -------------------------------------------------------------
   }  // end class LinkList
////////////////////////////////////////////////////////////////

class LinkListApp
{
	      public static void main(String[] args)
	      {

		int aans, bans, cans, qans;

		String asans, bsans, csans, qsans = "";

	  	asans = "";
	  	aans = 0;
	  	bans = 0;
		cans = 0;
		qans = 0;


      LinkList theList = new LinkList();  // make new list

      		System.out.print("\n\nLinked List Menu!\n");           //first menu heading
		System.out.print("----------------------------------------\n");
		System.out.print("This LinkedList is 3 items long\n");
      		System.out.print("Please enter the first item for the linkedlist....\n\n");

      		InputStreamReader stdin = new InputStreamReader(System.in); //creates the input buffer and reader
	      	BufferedReader console = new BufferedReader(stdin);

			while (asans == ""){
	  		try
	       {
	         asans = console.readLine();     //These 2 lines take the input, which is by default a string, and convert it to an integer
	         aans = Integer.parseInt(asans);
	       }
	         catch(IOException ioex1)		//This line is important, CATCHES errors.
	          {
	           System.out.println("Input error");  //allow for input output errors and display as appropriate.
	           System.exit(1);

        }
	}


      	theList.insertFirst(aans);
		theList.displayList();


					System.out.print("Please enter the second item for the linkedlist....\n\n");

					  	try
					        {
					         bsans = console.readLine();     //These 2 lines take the input, which is by default a string, and convert it to an integer
					         bans = Integer.parseInt(bsans);
					        }
					         catch(IOException ioex1)		//This line is important, CATCHES errors.
					          {
					           System.out.println("Input error");  //allow for input output errors and display as appropriate.
					           System.exit(1);
		        }



		 theList.insertFirst(bans); //CHECK HERE, INSERT FIRST MUST BE CHANGED TO ADD ON TO THE END
		 theList.displayList();



				System.out.print("Please enter the third item for the linkedlist....\n\n");

			  		try
			        {
			         csans = console.readLine();     //These 2 lines take the input, which is by default a string, and convert it to an integer
			         cans = Integer.parseInt(csans);
			        }
			         catch(IOException ioex1)		//This line is important, CATCHES errors.
			          {
			           System.out.println("Input error");  //allow for input output errors and display as appropriate.
			           System.exit(1);
        }

		theList.insertFirst(cans);
		theList.displayList();


	System.out.print("\n\n\n-----------------------------------------------------------------------\n");
	System.out.print("Press 2 to delete a specific item from the list\n\n");
	System.out.print("Press 3 to delete the entire list\n\n\n");

	try
					{
					qsans = console.readLine();     //These 2 lines take the input, which is by default a string, and convert it to an integer
					qans = Integer.parseInt(qsans);
				    }
						         catch(IOException ioex1)		//This line is important, CATCHES errors.
						           {
						           System.out.println("Input error");  //allow for input output errors and display as appropriate.
						           System.exit(1);
		        				   }

    if (qans == 2) {


		System.out.print("Now deleting the first item in the list.........\n\n");
		Link aLink = theList.deleteFirst();
		theList.displayList();
	}

    if (qans == 3) {




      				while( !theList.isEmpty() )         // while loop to delete first item until whole list is empty
        				{
        				Link aLink = theList.deleteFirst();   // delete link
        				System.out.print("\nDeleted   ");         // display it
        				aLink.displayLink();
        				System.out.println("\n");
        				theList.displayList();              // display list
						}
					}


   //   theList.displayList();              // display list
      }
   }

I appologise for it being so damn untidy, but its just a rough really whilst I get it fully functioning. I then want to perhaps use AWT to make it all graphical and lovely.

Question is, can anyone help me or steer me in the right direction as to deleting a single link. As you can see, I can delete the first item quite easily, but not quite sure how to find the node before the one I delete etc etc.

As you may have guessed I'm not very good at Java (programming in general).

Thanks for any help anyone can give.
Firebar is offline   Reply With Quote