LinkedList Help

Can't get delete case to work

Page 1 of 1

6 Replies - 955 Views - Last Post: 09 November 2008 - 02:07 AM Rate Topic: -----

#1 bigdavemtz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 23-October 08

LinkedList Help

Posted 07 November 2008 - 02:21 PM

Hi this a project for my CS111
class and I am having trouble
getting my delete a contact case to work.


/**
 * David Martinez
 * Project 0
 * IT/CS 111
 */
 
import java.util.Scanner;
import java.io.*;
import java.util.InputMismatchException;
enum data_type{ID, SSN}
 class LinkedListNode
{
	private int num=0;
	private LinkedListNode next;
 Contact info = new Contact();
 
	public LinkedListNode()
{
		setNode(null);
}
public LinkedListNode(Contact data, LinkedListNode myNode)
{
  
	setItem(data);
	setNode(myNode);
}
public void setItem(Contact data)
{
	info = data;
}
public void setNode(LinkedListNode myNode)
{
	next = myNode;
}

public Contact getItem()
{
	return info;
}
public LinkedListNode getNode()
{
	return next;
}
public String toString()
{
	return " " + info;
}
}
 class LinkedList{
	private String list;
	LinkedListNode head;
 public LinkedList(LinkedListNode obj)
{
	list = " ";
	head = obj;
	
}
public String toString()
{
	list = "";
	LinkedListNode tempNode = head.getNode();
	System.out.println();
	while(tempNode != null)
{
	list = list + " " + tempNode;
	tempNode = tempNode.getNode();
}
	return list;
}
}
//Making the classes needed for program
	class Contact
   {
	  public String first;
		public String last;
		
		public String toString () {
			
			return first + " " + last + " " + phone + " " + data + " " + address;
			
		}
	
	Phone phone = new Phone();
	Data data = new Data();
	Address address = new Address();
   }
  
   class  Phone
   {
	
	 int area_code=0;
	 int prefix=0;
	 int suffix=0;
	 public String toString(){
	  return " " + area_code+ " " + prefix + " " + suffix;
	 }
   }
   
   class Address{
	
	 
	int housenumber=0;
	public String street;
	public String city;
	public String state;
	int zipcode=0;
	
	public String toString(){
	 
	 return housenumber + " " + street + " " + city + " " + state + " " + zipcode;
	} 
   }
   
  
   class Data
   {
	char idorssn;
	data_type x;
	
	public int value = 0;
	public String toString(){
	 if (x == data_type.ID){
	  return "900" + value;
	 }else{
	  return "" + value;
	 }
	  
	}
	}
 

 
  
public class project0 {
 
 private static Scanner keyboard = new Scanner(System.in);
	
	public static void main(String[] args) {
	 
	 
	 String temp;
	 LinkedList start;
	 LinkedListNode tN, tN1, pos, head;
	 LinkedListNode newNode = null;
	 head = new LinkedListNode();
	 start = new LinkedList(head);
	 pos = head;
	 char idorssn=0,findContact=0;
	 int user_choice = 0;
	 String first, last, street, city, state, zipcode;
	 
	 keyboard.useDelimiter("\\n");
	 
	 System.out.println("David Martinez");
	 System.out.println("Project 0");
	 System.out.println("20081107");
	 
	 while (user_choice != 5){
	  System.out.println("1)Create new contacts:");
	  System.out.println("2)Delete Contacts:");
	  System.out.println("3)Show Contacts:");
	  System.out.println("4)Find Contacts:");
	  System.out.println("5)End Program");
	  
	  try
	  {
	   user_choice = keyboard.nextInt();
	  }
	  catch (InputMismatchException e)
	  {
	   keyboard.next();
	  }
	  finally {}
	  
	 
	 switch (user_choice){
	
		case 1:
			
			 newNode = new LinkedListNode();
			 
			 System.out.println("Enter First Name:");
				newNode.info.first = keyboard.next();
				
				System.out.println("Enter Last Name:");
				newNode.info.last = keyboard.next();
				
				keyboard.useDelimiter("\\s");
				System.out.print("Enter the Phone Number of this student: (###)-###-####:");
				
				newNode.info.phone.area_code = keyboard.nextInt();
				newNode.info.phone.prefix = keyboard.nextInt();
				newNode.info.phone.suffix = keyboard.nextInt(); 
				keyboard.useDelimiter("\\n");
				
				System.out.println("Enter House/Apartment number:");				
				newNode.info.address.housenumber = keyboard.nextInt();
				
				System.out.println("Enter Street name:");
				newNode.info.address.street = keyboard.next();
				
				System.out.println(" Enter city:");
				newNode.info.address.city = keyboard.next();
				
				System.out.println("Enter state:");
				newNode.info.address.state = keyboard.next();
				
				System.out.println("Enter zip code:");
				newNode.info.address.zipcode = keyboard.nextInt();
				
				System.out.println("I)Enter the ID for the contact:");
				System.out.println("S)Enter this contacts social security number: ###-##-####:");
				
				newNode.info.data.idorssn = keyboard.next().charAt(0);
				
				idorssn = newNode.info.data.idorssn;
				
				switch (idorssn){
						case 'i': 
						case 'I':
						 System.out.println("ID number: 900");
				   newNode.info.data.x=data_type.ID;
						 break;
							
						case 's':
						case 'S':
						System.out.println("SNN:");
							newNode.info.data.x=data_type.SSN;
				   break;
						   
						default:
							System.out.println("User error please try again");
				 }
				 
			  newNode.info.data.value = keyboard.nextInt();
			  pos.setNode(newNode);
			  pos = newNode;
			  break;
				
		case 2:
		 
		  System.out.println("Select a contact to be deleted:");
			  
		 tN = head;
			temp = keyboard.next();
			tN1 = head.getNode();
			while(tN1 != null){
		
				 if (tN1.getItem() == temp){
				 
					  tN.setNode(tN1.getNode());
					  tN1 = tN.getNode();
						   
				 }else{
	 
				 tN = tN.getNode();
				
				  tN1 = tN1.getNode();
					 }
			}
			pos = tN;	
			break;  
		  
		   
		  
			 
		case 3:
		 System.out.println(start);
		 break;
		 
		case 4:
		 System.out.println("F) Find contact with first name:");
		 System.out.println("L) Find contact with last name:");
		 System.out.println("I) Find contact with ID number:");
		 System.out.println("S) Find contact with SSN:");
		 System.out.println("P) Find contact with Phone number:");
		 
		 switch (findContact){
		  case 'f':
		  case 'F':
		   break;
		  case 'l':
		  case 'L':
		   break;
		  case 'i':
		  case 'I':
		   break;
		  case 's':
		  case 'S':
		   break;
		  case 'p':
		  case 'P':
		   break; 
		 }
		 break
		  
		case 5:
		 System.out.println("Goodbye");
		 break;
			 
	  }
	 }
				
			  
	 
 }
}


Is This A Good Question/Topic? 0
  • +

Replies To: LinkedList Help

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8323
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: LinkedList Help

Posted 07 November 2008 - 03:37 PM

Every node in a LinkedList should have a backward and a forward Node;
You only have a forward (next) pointer
Deleteting a node implies:
- set the forward pointer of the previous node to my forward pointer (instead of to myself)
- set the backward pointer of the next node to my backward pointer (instead of myself)
Was This Post Helpful? 0
  • +
  • -

#3 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2618
  • View blog
  • Posts: 11,062
  • Joined: 20-September 08

Re: LinkedList Help

Posted 07 November 2008 - 04:29 PM

Quote

Every node in a LinkedList should have a backward and a forward Node


This one again ;-) This is generally true of doubly linked lists. A simple linked list only requires a 'next' pointer. You can implement node deletion quite easily with this simple design
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8323
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: LinkedList Help

Posted 07 November 2008 - 07:55 PM

View Postg00se, on 7 Nov, 2008 - 03:29 PM, said:

Quote

Every node in a LinkedList should have a backward and a forward Node


This one again ;-) This is generally true of doubly linked lists. A simple linked list only requires a 'next' pointer. You can implement node deletion quite easily with this simple design

OK told you in another post... I always implement double-linked list
for which architectural considerations would you make a single-linked list ?

OK my mistake... sorry
Was This Post Helpful? 0
  • +
  • -

#5 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2618
  • View blog
  • Posts: 11,062
  • Joined: 20-September 08

Re: LinkedList Help

Posted 08 November 2008 - 12:53 AM

Quote

for which architectural considerations would you make a single-linked list ?


Well i wouldn't make one at all - there are already lists in the JDK (doubly-linked) ;-)

Seriously, CS students are more likely to be asked for singly-linked ones, along the lines of the typical CS question 'Here's something you'll never need to do in a real programming job, but we'll ask you to do it anyway. Anything you do need to do in real life you can find out on your own, as we're not covering it here'

This post has been edited by g00se: 08 November 2008 - 12:54 AM

Was This Post Helpful? 0
  • +
  • -

#6 bigdavemtz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 23-October 08

Re: LinkedList Help

Posted 08 November 2008 - 04:21 PM

View Postg00se, on 7 Nov, 2008 - 11:53 PM, said:

Quote

for which architectural considerations would you make a single-linked list ?


Well i wouldn't make one at all - there are already lists in the JDK (doubly-linked) ;-)

Seriously, CS students are more likely to be asked for singly-linked ones, along the lines of the typical CS question 'Here's something you'll never need to do in a real programming job, but we'll ask you to do it anyway. Anything you do need to do in real life you can find out on your own, as we're not covering it here'


Hey I don't have to have a doubly linked list for my project that is extra credit I just need to figure out how to be able to get my delete case I am working on to work I have tried many different things and still can't get it to work. I can't say I really understand the delete code that I have in my delete case because it was given to me in my last lab for LinkedList so I have been trying to modify it so that it will work with this project and I am failing at that. Once I get the delete code to work and I understand it I should have no problems getting my find a contact case to work. Please any help is apperiated!!
Was This Post Helpful? 0
  • +
  • -

#7 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2618
  • View blog
  • Posts: 11,062
  • Joined: 20-September 08

Re: LinkedList Help

Posted 09 November 2008 - 02:07 AM

View Postbigdavemtz, on 8 Nov, 2008 - 03:21 PM, said:

Once I get the delete code to work and I understand it I should have no problems getting my find a contact case to work. Please any help is apperiated!!


I would do it exactly the other way around
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1