这个项目是编写Linkedlist中的一些算法比如添加,插入删除,建立linkedlist,以下是我写的Java代码。
public class LinkedStr implements Comparable<LinkedStr>{
private static final Exception IndexOutOfBoundException = null;
private int size;
private Node head;
private Node tail;
//inner class of the node in LinkedStr
class Node <T> {
T data;
Node prev;
Node next;
Node (T element) {
this.data = element;
next = null;
}
Node () {
data = null;
prev = null;
next = null;
}
}
//this is a default constructor of this class
public LinkedStr() {
head = null;
tail = null;
size = 0;
}
//this is a deep copy constructor for current linkedlist
//onject
public LinkedStr (LinkedStr otherStr) {
if (otherStr.head == null) {
head = null;
}
else {
head = new Node(otherStr.head.data);
size++;
Node curr = head;
Node t = otherStr.head.next;
while(t!=null) {
Node n = new Node(t.data);
curr.next = n;
n.prev = curr;
curr = curr.next;
t = t.next;
size++;
}
}
}
//This method adds ch to the end of current linkedlsit
//object
public LinkedStr append (char ch) {
LinkedStr list=new LinkedStr();
Node newOne = new Node(ch);
// When there is no items
if (size == 0) {
head = newOne;
tail = newOne;
} else {
newOne.prev = tail;
tail.next = newOne;
tail = newOne;
}
size++;
return this;
}
//This method returns the number of characters in current
//Linkedstr
public int length() {
return size;
}
//This method returns the character located at the position
//parameter
public Character getCharAt(int position) {
if (size == 0 || position >= size ) {
return null;
}
if (position == 0) {
return (Character) head.data;
}
Node current = head;
for (int i = 0 ; i < size ; i++) {
if (i == position) {
break;
}
current = current.next;
}
return (Character) current.data;
}
//This method removes all characters from current linkedlist
//object
public void reset() {
head = null;
}
//method helper for insert method below
public LinkedStr addToFront (char data) {
Node newOne = new Node(data);
if ( size == 0) {
head = newOne;
tail = newOne;
size++;
return this;
} else {
newOne.next = head;
head.prev = newOne;
head = newOne; }
size++;
return this;
}
//this method inserts ch at the position indicated by position
//parameter
public void insert(int position, char ch) throws IndexOutOfBoundsException {
Node newOne = new Node(ch);
// When adding to the fist position
if (position == 0) {
this.addToFront(ch);
}
else if (position == size) {
this.append(ch);
}
else if(position<size) {
int num = 0;
Node current = head;
while (num < position) {
num++;
current = current.next;
}
// Find the Node that is before the index Node
Node before = current.prev;
newOne.prev = before;
newOne.next = current;
before.next = newOne;
current.prev = newOne;
size++;
}
//the case when index is out of bounds
else {
throw new IndexOutOfBoundsException();
}
}
//This method modifies the current LinkedStr object by
//adding another LinkedStr object after it
public void concat(LinkedStr otherStr) {
if (otherStr.head == null) {
return;
}
else {
Node curr = otherStr.head;
while (curr != null) {
append((Character) curr.data);
curr = curr.next;
}
}
}
//this method returns the index of the first occurence of
//ch in current linkedlist object
public int findFirst(int position, char ch) {
if (head == null || position < 0 || position > size) {
return -1;
}
Node curr = head;
int num = 0;
while (curr != null) {
if ( (Character) curr.data!=ch) {
curr = curr.next;
num++;
}
else if (num == 0) {
return -1;
}
else {
return num;
}
}
return -1;
}
//this method removes number of chars of numChars in the
//beginning of posistion of current linkedlist object
public void deleteCharsAt (int position, int numChars) {
if (position < size) {
int count = 0;
int count2 = 0;
Node curr = head;
Node temp = curr.prev;
if (position == 0) {
while (count != numChars) {
head = head.next;
count++;
size--;
}
}
else if (position + numChars > size) {
while (count != position-1) {
curr = curr.next;
count++;
}
temp = curr;
tail = temp.prev;
size = count+1;
}
else {
while (count != position-1) {
curr = curr.next;
count++;
}
temp = curr;
while (count2<=numChars) {
curr=curr.next;
count2++;
}
size = size-numChars;
temp.next = curr;
}
}
}
public <T> void deleteAfter(T x) {
Node current= head;
while (current != null && current.data != x)
current= current.next;
if (current != null && current != tail) {
current.next.next.prev= current;
current.next= current.next.next;
}
}
//this method compares two linkedlists' string
//alphabetically and lexicographically
public int compareTo (LinkedStr otherStr) {
Node curr = head;
Node curr2 = otherStr.head;
while (curr != null && curr2 != head) {
if ( (Character) curr.data == (Character) curr2.data) {
curr = curr.next;
curr2 = curr2.next;
}
else {
return (Character) curr.data- (Character) curr2.data;
}
}
return size-otherStr.size;
}
}