Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
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
Old May 22nd, 2005, 5:30 AM   #2
ZenMasterJG
Hobbyist Programmer
 
ZenMasterJG's Avatar
 
Join Date: Nov 2004
Location: Boston, MA
Posts: 145
Rep Power: 4 ZenMasterJG is on a distinguished road
Send a message via AIM to ZenMasterJG
to find the one to delete, you need to "traverse" the list.
this involves checking each node to see if its the one that needs to be deleted, if not, move on to the next. Also, life is easier if you check the node ahead of current. (cuz current.next needs to be set to point to the node after the to-be-deleted-node)
hope that helps, if not, i'll try and find some of my linked list code for you to gander at when its not 6:30 in the morning
ZenMasterJG is offline   Reply With Quote
Old May 22nd, 2005, 8:00 AM   #3
Firebar
Newbie
 
Join Date: May 2005
Posts: 2
Rep Power: 0 Firebar is on a distinguished road
Hi, thanks for your reply. I'm in the UK so didnt realise the time :eek: oops.

I understand the whole concept and what I have to do, just no idea how to implement it. If you do have some code I really would appreciate it, if you could explain it a little then I would be really thankful. A lot to ask, but again, thanks for your help.
Firebar is offline   Reply With Quote
Old May 22nd, 2005, 10:56 AM   #4
OpenLoop
Expert Programmer
 
OpenLoop's Avatar
 
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4 OpenLoop is on a distinguished road
I have a linklist with a remove function but it is implemented in C++.
if you want, checkout my website (in my signature), it should be under "projects". (sorry, no code comments)
OpenLoop is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:15 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC