linkylist class

 avatar
unknown
java
4 years ago
7.5 kB
11
Indexable
package com.company;

import javax.swing.*;
import java.util.ArrayList;

// create the functions for a linked list
public class linkyList {

    node front; // from the node class
    wordNode dfront; // from the wordNode class

    linkyList(){}

    public void inIt() // set the front equal to null
    {
        front = null;
        dfront = null;
    }



    node makeANode(int num) // make a single node from the node class
    {
        node node = new node(num);
        node.data = num;
        node.next = null;


        if (front == null)
        {
            front = node; // create the front is empty
        }
        else // node is not empty
            {
                node n = front; // set n equal to front

                while (n.next != null) // while the list is still populated add node
                {
                    n = n.next;
                }
                n.next = node;
            }


        return node;
        }

        wordNode makeAWordNode (String word) // this creates a node from the word Node class
        {
            wordNode node = new wordNode();
            node.wordCode = wordCode(word); // generate the word code
            node.word = word;
            node.count = 1;
            node.next = null;
            return node;
        }


        void nodeDef(node spot, String def)
        {
            // this methods add a definition to a node

            // find the node you want to add the definition to and attach it to the node
        }


    node findTail () // find the last node
    {
        node current = front;
        while (current.next != null)
        {
            current = current.next;
        }
        return current;
    }

// commented out word stuff
/*
    void buildAList(String word) // b
    {

        int j;
        node tail;

        for(j = 0; j < word.length(); j++ )
        {
            if (front == null)
            {
                inIt();
                front = makeANode(word); // create a front
            }
            else
                {
                    tail = findTail();
                    tail.next = makeANode(word);
                }
        }


    } // end build a list

 */



    void showMe() // show the list
    {
       node current = front;
       while (current.next != null )
       {
           System.out.print(current.data + " "); // print out the word
           current = current.next;
       }
    }

    void showWordList ()
    {
        int j = 0;
        wordNode curr;
        curr = dfront;

        while (curr != null)
        {
            System.out.print(curr.word + " ");
            curr = curr.next;
            j++;
        }
    }

    void addAfter (wordNode spot, String word) // add after a specific node
    {
        wordNode nN = new wordNode();
        nN.next = spot.next;
        spot.next= nN;
    }


    wordNode findSpot (String word) // find  a string within the linked list

    {
        int n = wordCode(word);
        wordNode current, previous;
        current = dfront; // set the front up
        previous = current;

        while ((current != null && current.wordCode <= n)) // as long as ll isn't empty keep going && the current word code is less than num
        {
            previous = current;
            current = current.next;
        }
        return previous; // return the prev node
    }

    void deleteNode (String word)
    {
        // node head
        int num = wordCode(word);
        // if the node is at the end set it to null
        wordNode curr;
        curr = dfront;
        while (curr.next != null) // go through the list
        {
            if (curr.wordCode == num ) {

                curr.wordCode = curr.next.wordCode;
                curr.next = curr.next.next;
            }
            curr = curr.next;
        }


    }

    void returnCharacter (String a)
    {
        //have a character to compare to

        int i = 0; 
        wordNode curr = dfront;

        while (curr.next != null)
        {
            if (( curr.word.startsWith(a) )) // a has to be between these values and equal to the first character of the node
            {

                    System.out.print(curr.word + " ");
            }

            curr = curr.next; // go through the list


        }

    }

    wordNode searchList (wordNode node, String words)
    {
       int num = wordCode(words); // get the word code to comapare

        wordNode curr;
        curr = dfront;
        boolean search = true;
        while (search)
        {
            if (curr.wordCode == num)
            {
                System.out.print("Found your word: " + curr.word);
                curr = curr.next;
                search = false;
            }
        }
        return curr;
    }


    void insertWordNode (String words) {


        wordNode prev = null, curr = null, t;
        boolean searching;
        int num;

        num = wordCode(words);

        if (dfront == null) {
            dfront = makeAWordNode(words); // make a new front if empty
        } else if (num < dfront.wordCode) {
            t = makeAWordNode(words);
            t.next = dfront;
            dfront = t;
        } else {
            searching = true;
            curr = dfront;
            prev = curr;

            while (searching) // while search is true
            {
                if (curr.wordCode == num)
                {
                    //System.out.print("Found a duplicate!");
                    searching = false; // turn searching off

                }
                else if (curr.wordCode < num) {
                    if (curr.next == null) {
                        // add to the back of the list 
                        curr.next = makeAWordNode(words);
                        searching = false;

                    } else {
                        // move to next node
                        prev = curr;
                        curr = curr.next;
                    }
                } else if (curr.wordCode > num) {
                    // insert node into list after prev
                    t = makeAWordNode(words);
                    t.next = curr;
                    prev.next = t;
                    searching = false;
                }
            }
        }
    } // end the insert word node class


    int  wordCode (String words)
    {

        int wordCode = 0;
        int x, y, z; // for the first three letters
        int size = words.length();
        int[] array = new int [size];
        String firstTheeChar = " ";

        if (words.length() > 3) {

            for (int i = 0; i < words.length(); i++) {

                char c = words.charAt(i); // go through the string
                if (c >= 'a' && c <= 'z') // if c is lower case
                {
                    char c1 = words.charAt(0);
                    char c2 = words.charAt(1);
                    char c3 = words.charAt(2);


                    //wordCode = (first letter – ‘a’) * 262 + (second letter – ‘a’) * 261 + (third letter – ‘a’)*260

                    x = (c1 - 'a') * (int) Math.pow(26, 2);
                    y = (c2 - 'a') * (int) Math.pow(26, 1);
                    z = (c3 - 'a') * (int) Math.pow(26, 0);

                    wordCode = x + y + z; // holds the word code for each string
                }


            }
        }
    return wordCode;


    } // end word count method


        
} // end class linky
Editor is loading...