去一公司面试,由于HR没说清地址,找到此公司可谓历经九九八十一难,终于找到,结果又等了面试的人半个到一小时的样子,一肚子火,本来想走的,后来想想来了看看情况吧!面试...[很墨迹],完了那哥们居然说去上机,还问我不介意吧,我说好吧...
那哥们摆弄了半天电脑终于把环境整好,给我题了:继承List做一个LinkedList实现add,remove,isEmpty,size功能。
憋了一肚子火,怎么也静不下心来。再者之前也很长时间没编码了,整了半天我也没做出来,也很郁闷!今天静下心来,实现了。主要代码如下:
写道
public class LinkedList extends LinkedListImpl{
public Node head;
public Node tail;
public int size;
@Override
public void add(Node node) {
// TODO Auto-generated method stub
node.front = this.tail;
node.next = null;
this.tail.next = node;
this.tail.front = this.tail;
this.tail = node;
this.size++;
}
@Override
public Object remove(int index) {
// TODO Auto-generated method stub
Node tmpNode = new Node();
tmpNode = this.head;
int tmp = 0;
while(tmpNode != null){
tmp++;
//当删除第一个元素时
if(index == 1){
this.head = tmpNode.next;
//list大小减1
this.size--;
break;
}
//当删除最后一个元素时
if(this.size() == index){
this.tail = this.tail.front;
this.tail.next = null;
//list大小减1
this.size--;
break;
}
//删除中间元素时
if(tmp == index){
tmpNode.front.next = tmpNode.next;
tmpNode.next.front = tmpNode.front;
//list大小减1
this.size--;
break;
}
tmpNode = tmpNode.next;
}
return null;
}
@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return this.head == null;
}
@Override
public int size() {
// TODO Auto-generated method stub
Node tmpNode = new Node();
tmpNode = this.head;
int count = 0;
while(tmpNode != null){
count++;
tmpNode = tmpNode.next;
}
return count;
}
public Node head;
public Node tail;
public int size;
@Override
public void add(Node node) {
// TODO Auto-generated method stub
node.front = this.tail;
node.next = null;
this.tail.next = node;
this.tail.front = this.tail;
this.tail = node;
this.size++;
}
@Override
public Object remove(int index) {
// TODO Auto-generated method stub
Node tmpNode = new Node();
tmpNode = this.head;
int tmp = 0;
while(tmpNode != null){
tmp++;
//当删除第一个元素时
if(index == 1){
this.head = tmpNode.next;
//list大小减1
this.size--;
break;
}
//当删除最后一个元素时
if(this.size() == index){
this.tail = this.tail.front;
this.tail.next = null;
//list大小减1
this.size--;
break;
}
//删除中间元素时
if(tmp == index){
tmpNode.front.next = tmpNode.next;
tmpNode.next.front = tmpNode.front;
//list大小减1
this.size--;
break;
}
tmpNode = tmpNode.next;
}
return null;
}
@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return this.head == null;
}
@Override
public int size() {
// TODO Auto-generated method stub
Node tmpNode = new Node();
tmpNode = this.head;
int count = 0;
while(tmpNode != null){
count++;
tmpNode = tmpNode.next;
}
return count;
}
附上实现在附件中。