import java.util.Scanner;
import java.io.*;
public class linkedList {
// MAIN METHOD -----------------------------------//
public static void main(String[] args) {
linkedList myLinkedList = new linkedList();
myLinkedList.displayList();
myLinkedList.insert("vinny");
myLinkedList.insert("darrell");
myLinkedList.insert("mcdowell");
myLinkedList.insert("zzz");
myLinkedList.displayList();
myLinkedList.printSection("abchijqsx");
System.out.println("The first link is " + myLinkedList.head.next.value + ". The tail link is " + myLinkedList.tail.value + ".");
myLinkedList.delete("darrell");
myLinkedList.delete("vinny");
myLinkedList.delete("zzz");
}//-----------------------------------------------//
DataInputStream in;
String fname, line, alphabet = "abcdefghijklmnopqrstuvwxyz";;
boolean looping, searching;
int linkCount = 0;
link head = new link();
link tail = new link();
link temp = new link();
link current = new link();
// link[] index = new link[26];
public linkedList() {
System.out.println("...starting linkedList() method...");
getFileName();
readFileContents();
}
public class link {
public String value; // value of element
public link next; // referene to next
// constructor
public link() {
}
public link(String val) {
value = val;
}
public link(String val, link n) {
value = val;
next = n;
}
}
/////////////////////////// PROMPT USER FOR .TXT FILE NAME ///////////////////
public void getFileName() {
Scanner in = new Scanner(System.in);
System.out.println("What file do you want to turn into a linked list?");
fname = in.nextLine();
System.out.println("you entered: "+fname);
}
//////////////////////////////////////////////////////////////////////////////
////// TRANSFER .TXT FILE'S CONTENTS INTO AN ALPHABETICALLY LINKED LIST //////
public void readFileContents() {
System.out.println("...starting readFileContents() method...");
try {
in = new DataInputStream(new FileInputStream(fname));
looping = true;
while(looping) {
if (null == (line = in.readLine())) {
looping = false;
in.close();
}
else {
insert(line); // call insert method
}// end else
}// end while
}// end try
catch(IOException e) {
System.out.println("Error " + e);
}// end catch
}// end readFileContents()
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////// ALPHABETICALLY INSERT A LINK INTO THE LIST ///
public void insert(String line) {
link newLink = new link(line, null);
temp = head;
current = head.next;
if (linkCount == 0) { // create first link
head.next = newLink;
current = newLink;
temp = head;
tail = newLink;
searching = false;
}
else if (linkCount > 0) { // create additional link & insert
searching = true;
while (searching) {
int compare = line.compareTo(current.value);
if (current.next == null) {
if (compare > 0) {
current.next = newLink;
temp = current;
current = newLink;
tail = newLink;
searching = false;
}
if (compare < 0) {
temp.next = newLink;
temp = newLink;
newLink.next = current;
tail = current;
searching = false;
}
}
else if (current.next != null) {
if (compare > 0) {
temp = current;
current = current.next;
}
if (compare < 0) {
temp.next = newLink;
newLink.next = current;
temp = newLink;
searching = false;
}
}
}
}
linkCount++; // increment linkCount
System.out.println("'" + newLink.value + "' link was created and added to the list!");
System.out.println(" linkCount is now: " + linkCount);
}////////////////////////////////////////////////////////////////////////
///////////////////////////////// DELETE A PARTICULAR LINK //////////////
public void delete(String val) {
current = head.next;
temp = head;
boolean looking = true;
while (looking) {
if (current.value == val) {
temp.next = current.next;
current = current.next;
looking = false;
}
else if (current.value != val) {
temp = current;
current = current.next;
}
}
linkCount--; // decrement linkCount
System.out.println("'" + val + "' link was DELETED from the list!");
System.out.println(" linkCount is now: " + linkCount);
}/////////////////////////////////////////////////////////////////////////
////////////////////////////// DISPLAY THE WHOLE LINKED LIST /////////////////////////////
public void displayList() {
String listString = new String("\ndisplayList() of all " + linkCount + " links : ");
current = head.next;
while (current.next != null) { //concatenate link values into listString
listString = listString + current.value + ", ";
current = current.next;
}
listString = listString + current.value + "\n";
System.out.println(listString); //print listString }
}/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////// PRINT SPECIFIC SECTIONS OF THE LINKED LIST ///
public void printSection(String section) {
int sectionCount = 0;
String sectionString = new String();
for (int i = 0; i < section.length(); i++) {
current = head.next;
boolean printing = true;
while (printing) {
if (current.value.charAt(0) != section.charAt(i) && current.next == null) {
System.out.println("WARNING!!! NO STRING LINKS BEGINNING WITH '" + section.charAt(i) + "'!");
printing = false;
}
else if (current.value.charAt(0) == section.charAt(i)) {
while (current.value.charAt(0) == section.charAt(i)) { //concatenate section values into sectionString
sectionString = sectionString + current.value + ", ";
sectionCount++; // increment sectionCount
current = current.next;
}
printing = false;
}
else if (current.value.charAt(0) != section.charAt(i)) {
current = current.next;
}
}
if (current.next == null && current.value.charAt(0) == section.charAt(i)) {
sectionString = sectionString + current.value;
}
}
String output = new String("printSection(" + section + ") contains " + sectionCount + ": " + sectionString);
System.out.println(output); // print sectionString
}/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}