4 Replies - 3372 Views - Last Post: 21 September 2006 - 09:08 PM Rate Topic: -----

#1 Addicted  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 20-September 06

Linked list contact list

Posted 21 September 2006 - 12:16 AM

Ok I redid my whole code. I want to know how I could possibly store strings into the linked nodes.

 import java.io.*;
import java.util.*;
import java.lang.*;

public class linked {
				public int value;
				public linked next;
		public String sex;


				public linked(int n,linked ln){
						value = n;
						next = ln;
			sex = m;
			
				}

public static void main(String args[]){
	Scanner in = new Scanner(System.in);
		int choice = 0;
	linked head = null;
	int phone;
	int list;
	String gender;
while(choice != 6){
	System.out.print("\n Welcome to the techie contact manager\n");
		System.out.print("\nWhat would you like to do (please choose a number)?\n");
	System.out.print("1) Add a contact\n");
	System.out.print("2) Remove a contact\n");
	System.out.print("3) Search for a contact\n");
	System.out.print("4) Print all contacts\n");
	System.out.print("5)Print all female contacts\n");
	System.out.print("6) Exit\n");

		choice = in.nextInt();

if(choice == 1){
		System.out.print("Please enter the contact's list number. (If they are first enter a '1' and so on.\n");
		System.out.print(">>");
		list = in.nextInt();
		head = new linked(list, head);
		System.out.print("Please enter the contact's phone number\n");
		System.out.print(">>");	
		phone =  in.nextInt();

				head = new linked(phone, head);
		/*System.out.print("Please enter the contact's gender\n");
		System.out.print(">>");	
		gender =  in.nextLine();
		head = new linked(gender, head);
		~~~~How do I do this????~~~~~*/



						}
if(choice == 4){
				linked p = head;
				while (p != null) {
								System.out.println(p.value);
								p = p.next;
				}
}
		}
}
}


I think that I would have to play with my class linked to have it accept strings but I do not know how. Thanks

This post has been edited by Addicted: 21 September 2006 - 08:19 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Linked list contact list

#2 Addicted  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 20-September 06

Re: Linked list contact list

Posted 21 September 2006 - 08:20 AM

redid the code didn't think i should create a new thread.
Was This Post Helpful? 0
  • +
  • -

#3 Videege  Icon User is offline

  • rÍvant.toujours
  • member icon

Reputation: 6
  • View blog
  • Posts: 1,413
  • Joined: 25-March 03

Re: Linked list contact list

Posted 21 September 2006 - 09:38 AM

So which one are you in the class, and can I ask how you found out about DIC? We would like to know if our advertising is working here.

The assignment is practically already due, so I hope you got it :D.
Was This Post Helpful? 0
  • +
  • -

#4 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Linked list contact list

Posted 21 September 2006 - 11:17 AM

View PostAddicted, on 21 Sep, 2006 - 12:16 AM, said:

I think that I would have to play with my class linked to have it accept strings but I do not know how. Thanks

You can create as many constructors as you need to handle different sets of parameters.

So in this case, create another constructor that takes a string in addition to the parameters in your first constructor.

Don't forget you still need to create methods in order to access the next node and methods to access the data.
public class linked {
	public int value;
	public linked next;
	public String sex;

   public linked(int n,linked ln){
	value = n;
	next = ln;
	sex = m;
   }

   public linked(String str, int n, linked ln){
		//code here to assign values into node
   }
}


Was This Post Helpful? 0
  • +
  • -

#5 Addicted  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 20-September 06

Re: Linked list contact list

Posted 21 September 2006 - 09:08 PM

yea I figured it out. just in time too. i was directed from a current member.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1