Skip to content

Instantly share code, notes, and snippets.

@ad-m
Created October 28, 2015 14:19
Show Gist options
  • Select an option

  • Save ad-m/82f4fa64fa5a742aa3cb to your computer and use it in GitHub Desktop.

Select an option

Save ad-m/82f4fa64fa5a742aa3cb to your computer and use it in GitHub Desktop.
public class ListTwo<T> implements IList<T> {
ElemTwo<T> first;
ElemTwo<T> last;
@Override
public void addFirst(T newData) {
ElemTwo niu = new ElemTwo(newData, this.first, null);
this.first = niu;
if(this.last == null){
this.last = niu;
}
}
@Override
public void addLast(T newData) {
if(this.first == null){
this.addFirst(newData);
return;
}
ElemTwo<T> cur = this.first;
while(cur.getNext() !=null){
cur = cur.getNext();
}
ElemTwo niu = new ElemTwo(newData, cur, null);
cur.setNext(niu);
}
@Override
public void addAtPosition(T newData, int position) throws ListException {
if(position == 0){
this.addFirst(newData);
return;
} else if(this.size() == position){
this.addLast(newData);
return;
}
ElemTwo<T> cur = this.first;
while(cur.getNext() !=null){
cur = cur.getNext();
if(cur.getData().equals(newData)){
ElemTwo<T> niu = new ElemTwo(newData, cur.getPrev(), cur.getNext());
cur.getNext().setPrev(niu);
cur.getPrev().setNext(niu);
}
}
throw new ListException("Out of list");
}
@Override
public int size() {
if(this.first==null){
return 0;
}
int pos = 1;
ElemTwo<T> cur = this.first;
while(cur.getNext() !=null){
cur = cur.getNext();
pos+=1;
}
return pos;
}
@Override
public T removeFirst() throws ListException {
if(this.first == null) throw new ListException("List are empty!");
T data = this.first.getData();
this.first = this.first.getNext();
return data;
}
@Override
public T removeLast() throws ListException {
if(this.first == null){
throw new ListException("Empty list");
};
if(this.first.getNext() == null){
return this.removeFirst();
}
ElemTwo<T> cur = this.first;
while(cur.getNext() !=null){
cur = cur.getNext();
}
if(this.first.getNext() == cur){
this.first = null;
}
cur.setNext(null);
return cur.getData();
}
@Override
public T remove(int position) throws ListException {
if(position == 0){
return this.removeFirst();
}else if(this.size() == position){
return this.removeLast();
}
int pos = 0;
ElemTwo<T> cur = this.first;
while(cur.getNext() !=null){
if(pos == position){
cur.getNext().setPrev(cur.getNext());
cur.getPrev().setNext(cur.getPrev());
return cur.getData();
}
cur = cur.getNext();
pos++;
}
throw new ListException("Out of list");
}
@Override
public int find(T dataToFind) {
ElemTwo<T> cur = this.first;
int pos = 0;
while(cur!=null){
if(cur.getData().equals(dataToFind)){
return pos;
}
cur = cur.getNext();
pos++;
}
throw new IllegalStateException("Not found");
}
@Override
public boolean contains(T data) {
try{
this.find(data);
}catch(IllegalStateException e){
return false;
}
return true;
}
@Override
public void print() {
ElemTwo<T> cur = this.first;
while(cur !=null){
System.out.print("[" + cur.getData() + "]");
cur = cur.getNext();
}
return;
}
public static void main(String[] args) {
try {
ListTwo<Integer> list = new ListTwo<>();
assert(list.size() == 0);
list.addFirst(25);
assert(list.size() == 1);
list.addFirst(10);
assert(list.size() == 2);
list.addLast(100);
assert(list.size() == 3);
list.removeFirst();
assert(list.size() == 2);
list.removeLast();
assert(list.size() == 1);
assert(list.contains(100) == false);
assert(list.contains(25) == true);
list.addLast(77);
list.addLast(33);
assert(list.find(25) == 0);
assert(list.find(77) == 1);
assert(list.find(33) == 2);
list.remove(0);
assert(list.find(77) == 0);
list.print();
} catch (ListException e) {
}
}
public void join(IList<T> list) {
while(list.size()!=0){
try {
this.addLast(this.removeLast());
} catch (ListException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment