Merge pull request #75 from earngpi/master
added class for Palindrome and re-edited SinglyLinkedList
This commit is contained in:
commit
0e48afc53b
16
Misc/Palindrome.java
Normal file
16
Misc/Palindrome.java
Normal file
@ -0,0 +1,16 @@
|
||||
class Palindrome {
|
||||
|
||||
public String reverseString(String x){ //*helper method
|
||||
String output = "";
|
||||
for(int i=x.length()-1; i>=0; i--){
|
||||
output += x.charAt(i); //addition of chars create String
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
|
||||
public Boolean isPalindrome(String x){ //*palindrome method, returns true if palindrome
|
||||
return (x.equalsIgnoreCase(reverseString(x)));
|
||||
}
|
||||
|
||||
}
|
@ -14,7 +14,7 @@
|
||||
*/
|
||||
class SinglyLinkedList{
|
||||
/**Head refered to the front of the list */
|
||||
private LinkForLinkedList head;
|
||||
private Node head;
|
||||
|
||||
/**
|
||||
* Constructor of SinglyLinkedList
|
||||
@ -29,9 +29,9 @@ class SinglyLinkedList{
|
||||
* @param x Element to be added
|
||||
*/
|
||||
public void insertHead(int x){
|
||||
LinkForLinkedList newLink = new LinkForLinkedList(x); //Create a new link with a value attached to it
|
||||
newLink.next = head; //Set the new link to point to the current head
|
||||
head = newLink; //Now set the new link to be the head
|
||||
Node newNode = new Node(x); //Create a new link with a value attached to it
|
||||
newNode.next = head; //Set the new link to point to the current head
|
||||
head = newNode; //Now set the new link to be the head
|
||||
}
|
||||
|
||||
/**
|
||||
@ -39,8 +39,8 @@ class SinglyLinkedList{
|
||||
*
|
||||
* @return The element deleted
|
||||
*/
|
||||
public LinkForLinkedList deleteHead(){
|
||||
LinkForLinkedList temp = head;
|
||||
public Node deleteHead(){
|
||||
Node temp = head;
|
||||
head = head.next; //Make the second element in the list the new head, the Java garbage collector will later remove the old head
|
||||
return temp;
|
||||
}
|
||||
@ -58,9 +58,9 @@ class SinglyLinkedList{
|
||||
* Prints contents of the list
|
||||
*/
|
||||
public void display(){
|
||||
LinkForLinkedList current = head;
|
||||
Node current = head;
|
||||
while(current!=null){
|
||||
current.displayLink();
|
||||
System.out.print(current.getValue()+" ");
|
||||
current = current.next;
|
||||
}
|
||||
System.out.println();
|
||||
@ -96,26 +96,26 @@ class SinglyLinkedList{
|
||||
* @author Unknown
|
||||
*
|
||||
*/
|
||||
class LinkForLinkedList{
|
||||
class Node{
|
||||
/** The value of the node */
|
||||
public int value;
|
||||
/** Point to the next node */
|
||||
public LinkForLinkedList next; //This is what the link will point to
|
||||
public Node next; //This is what the link will point to
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param valuein Value to be put in the node
|
||||
*/
|
||||
public LinkForLinkedList(int valuein){
|
||||
public Node(int valuein){
|
||||
value = valuein;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prints out the value of the node
|
||||
* Returns value of the node
|
||||
*/
|
||||
public void displayLink(){
|
||||
System.out.print(value+" ");
|
||||
public int getValue(){
|
||||
return value;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user