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{
|
class SinglyLinkedList{
|
||||||
/**Head refered to the front of the list */
|
/**Head refered to the front of the list */
|
||||||
private LinkForLinkedList head;
|
private Node head;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor of SinglyLinkedList
|
* Constructor of SinglyLinkedList
|
||||||
@ -29,9 +29,9 @@ class SinglyLinkedList{
|
|||||||
* @param x Element to be added
|
* @param x Element to be added
|
||||||
*/
|
*/
|
||||||
public void insertHead(int x){
|
public void insertHead(int x){
|
||||||
LinkForLinkedList newLink = new LinkForLinkedList(x); //Create a new link with a value attached to it
|
Node newNode = new Node(x); //Create a new link with a value attached to it
|
||||||
newLink.next = head; //Set the new link to point to the current head
|
newNode.next = head; //Set the new link to point to the current head
|
||||||
head = newLink; //Now set the new link to be the head
|
head = newNode; //Now set the new link to be the head
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -39,8 +39,8 @@ class SinglyLinkedList{
|
|||||||
*
|
*
|
||||||
* @return The element deleted
|
* @return The element deleted
|
||||||
*/
|
*/
|
||||||
public LinkForLinkedList deleteHead(){
|
public Node deleteHead(){
|
||||||
LinkForLinkedList temp = head;
|
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
|
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;
|
return temp;
|
||||||
}
|
}
|
||||||
@ -58,9 +58,9 @@ class SinglyLinkedList{
|
|||||||
* Prints contents of the list
|
* Prints contents of the list
|
||||||
*/
|
*/
|
||||||
public void display(){
|
public void display(){
|
||||||
LinkForLinkedList current = head;
|
Node current = head;
|
||||||
while(current!=null){
|
while(current!=null){
|
||||||
current.displayLink();
|
System.out.print(current.getValue()+" ");
|
||||||
current = current.next;
|
current = current.next;
|
||||||
}
|
}
|
||||||
System.out.println();
|
System.out.println();
|
||||||
@ -96,26 +96,26 @@ class SinglyLinkedList{
|
|||||||
* @author Unknown
|
* @author Unknown
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
class LinkForLinkedList{
|
class Node{
|
||||||
/** The value of the node */
|
/** The value of the node */
|
||||||
public int value;
|
public int value;
|
||||||
/** Point to the next node */
|
/** 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
|
* Constructor
|
||||||
*
|
*
|
||||||
* @param valuein Value to be put in the node
|
* @param valuein Value to be put in the node
|
||||||
*/
|
*/
|
||||||
public LinkForLinkedList(int valuein){
|
public Node(int valuein){
|
||||||
value = valuein;
|
value = valuein;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Prints out the value of the node
|
* Returns value of the node
|
||||||
*/
|
*/
|
||||||
public void displayLink(){
|
public int getValue(){
|
||||||
System.out.print(value+" ");
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user