简体   繁体   中英

Phonebook using hash table

I need help creating a small phone book application that uses hash table. This is for my school assignment. I'm very new to Java so I can't really get my head around this.

I have a basic layout of the code on how the application would function, i'm just lost on how to implement the hash table.

I'm not allowed to use the built in data structures in Java, so I have to build the hash table from scratch. I am, however, able to use the native hashCode() function for the hash table.

here is my code and some notes in it:

import java.util.Scanner;

public class PhoneBook {

public static void main(String[] args) {

    boolean exitPhoneBook = false;
    Scanner userInput = new Scanner(System.in);

    while (exitPhoneBook == false) {

        System.out.println("What do you want to do?");
        System.out.println("1. Add a contact");
        System.out.println("2. Show a contact");
        System.out.println("3. Delete a contact");
        System.out.println("4. Show all contacts");
        System.out.println("5. Exit");
        System.out.print("Select a number: ");

        int action = userInput.nextInt();

        switch (action){
        case 1:
            addContact();
            break;

        case 2:
            showContact();
            break;

        case 3:
            deleteContact();
            break;

        case 4:
            showAll();
            break;

        case 5:
            System.out.println("Goodbye!");
            exitPhoneBook = true;
            break;

        default:
            System.out.println("Invalid option.");
            System.out.print("Select a number: ");
            break;
        }
    }

}

static void addContact(){
    //takes in four strings from user (first name, last name, phone number, email)
}

static void showContact(){
    //takes in two strings from user (first name, last name)
}

static void deleteContact(){
    //takes in two strings from user (first name, last name)
}

static void showAll(){
    //prints out all the contact in the hash table
}

}

I apologize for posting this as an answer. I don't know how to add a comment with added codes.

EDIT: I figured out the multi-values. my issue right now is with deleting an entry that has the same index/hashed key. For example. There are three values that has the same key, I can delete first and second value, but not the third one.

here is my code for it:

public void deleteContact(String key) {
    int location = hashFunction(key);
    if (contactsArray[location] == null) { //if the contact doesn't exist
        System.out.println("Contact not found.\n");
        return;
    }

    if (contactsArray[location].key.equals(key)) {
        contactsArray[location] = contactsArray[location].next; //if contact is on first item
        System.out.println("Contact has been removed\n");
        return;
    }

    //If contact is not the first item of the same key
    ContactList prev = contactsArray[location];
    ContactList curr = prev.next;
    while (curr != null && ! curr.key.equals(key)) {
        curr = curr.next;
        prev = curr;
    }

    if (curr != null) {
        prev.next = curr.next;
        System.out.println("Contact has been removed");
        return;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM