package linklist;
import java.util.*;
public class SLinkedList {
protected StringNode head;
public SLinkedList() {
head = new StringNode();
}
public static void main(String[] args) {
// set up a linked list of 4 integers
SLinkedList greenline = new SLinkedList();
greenline.addFirst("Dejvicka");
greenline.addMid("Hradcanska","Dejvicka");
greenline.addMid("Malostranska","Hradcanska");
greenline.addMid("Staromestska","Malostranska");
greenline.addMid("Mustek","Staromestska");
greenline.addMid("Muzeum","Mustek");
greenline.addMid("Namesti","Muzeum");
greenline.addMid("Jirihoz Podebrad","Namesti");
greenline.addMid("Flora","Jirihoz Podebrad");
greenline.addMid("Zelivskeho","Flora");
greenline.addMid("Stransnicka","Zelivskeho");
greenline.addMid("Skalka","Stransnicka");
greenline.addLast("Depo Hostivar");
printList(greenline);
SLinkedList yellowline = new SLinkedList();
yellowline.addFirst("Zlicin");
yellowline.addMid("Stodulky","Zlicin");
yellowline.addMid("Luka","Stodulky");
yellowline.addMid("Luziny","Luka");
yellowline.addMid("Hurka","Luziny");
yellowline.addMid("Nove Butovice","Hurka");
yellowline.addMid("Jinonice","Nove Butovice");
yellowline.addMid("Radlicka","Jinonice");
yellowline.addMid("Smichovske Nadrazi","Radlicka");
yellowline.addMid("Andel","Smichovske Nadrazi");
yellowline.addMid("Karlovo Namesti","Andel");
yellowline.addMid("Nardonitrada","Karlovo Namesti");
yellowline.addMid("Mustek","Nardonitrada");
yellowline.addMid("NR","Mustek");
yellowline.addMid("Florence","NR");
yellowline.addMid("Krizikova","Florence");
yellowline.addMid("Invalidovna","Krizikova");
yellowline.addMid("Palmovka","Invalidovna");
yellowline.addMid("CM","Palmovka");
yellowline.addMid("Vysocanska","CM");
yellowline.addMid("Kolbenova","Vysocanska");
yellowline.addMid("Hloubetin","Kolbenova");
yellowline.addMid("Rajska Zahrada","Hloubetin");
yellowline.addLast("Cerry Most");
printList(yellowline);
SLinkedList redline = new SLinkedList();
redline.addFirst("Ladvi");
redline.addMid("Kobylisy","Ladvi");
redline.addMid("Nadrazi Holesovic","Kobylisy");
redline.addMid("Vltavska","Nadrazi Holesovic");
redline.addMid("Florence","Vltavska");
redline.addMid("Hlavni","Florence");
redline.addMid("Muzeum","Hlavni");
redline.addMid("I.P.Pavlova","Muzeum");
redline.addMid("Vysehrad","I.P.Pavlova");
redline.addMid("Prazskeho Povstani","Vysehrad");
redline.addMid("Pankrac","Prazskeho Povstani");
redline.addMid("Budejovicka","Pankrac");
redline.addMid("Kacerov","Budejovicka");
redline.addMid("Roztyly","Kacerov");
redline.addMid("Chodov","Roztyly");
redline.addMid("Opatov","Chodov");
redline.addLast("Haje");
printList(redline);
}
//add a new node to the head of the list
private void addFirst(String element) {
// make variable head point to new node
head = new StringNode(element,head);
}
private void addLast(String element) {
StringNode tail;
tail = head;
while (tail.getNext() != null) {
tail = tail.getNext();
}
//insert new node at end of list
tail.setNext( new StringNode(element));
}
//add a new node after position of curnode
private void addMid(String element, String entryafter) {
StringNode curnode;
curnode = head;
//go to last node and remember previous node at all times
while (curnode != null && curnode.getElement() != entryafter) {
curnode = curnode.getNext();
}
//if first occurrence of element entryafter was located then insert new node
if (curnode != null) {
StringNode newnode = new StringNode(element,curnode.getNext());
curnode.setNext(newnode);
}
}
private boolean isEmpty() {
return head == null;
}
private void removeFirst() {
StringNode oldhead;
oldhead = head;
//remove first node from linked list
if (head != null) {
head = head.getNext();
oldhead.setNext(null);
}
else {
throw new NoSuchElementException();
}
}
private void removeLast() {
StringNode temp, previous;
temp = head;
previous = temp;
//go to last node and remember previous node at all times
while (temp != null && temp.getNext() != null) {
previous = temp;
temp = temp.getNext();
}
if (previous != null) {
//remove last node
previous.setNext(null);
}
else {
throw new NoSuchElementException();
}
}
//very similar to removeLast except we are looking for element i
private void removeMid(String element) {
StringNode temp, previous;
temp = head.getNext();
previous = null;
//go to node containing element and rermember previous node at all times
while (temp.getElement() != element && temp.getNext() != null) {
previous = temp;
temp = temp.getNext();
}
if (previous != null && temp.getNext() != null) {
//not first or last node so we can remove node defined by temp.
// set the previous node to that after temp
previous.setNext(temp.getNext());
temp.setNext(null);
}
else {
throw new NoSuchElementException();
}
}
public static void printList(SLinkedList thelist) {
StringNode temp;
if(thelist.isEmpty())
System.out.println("List is empty");
else {
temp = thelist.head;
while (temp != null) {
System.out.print(temp.getElement()+" ");
temp = temp.getNext();
}
System.out.println();
}
}
}