Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,142 +2,131 @@

public class LinkedList implements List,Iterator {

private Node head;

private Node current;
public Node head;

private int size=0;

public void add(Object o){
Node n = new Node();
n.next=this.head;
n.data=o;
this.head=n;
private static class Node{
Object data;
Node next;
}
public void add(int index , Object o){


public Iterator iterator(){
return null;
}

public boolean hasNext(){
return false;
}

public Object next(){
return null;
}

public void add(Object o){
if (head == null){
head = new Node();
Node n = new Node();
n.data = o;
head.next = n;
size++;
head.data=size;
}else{
Node e = head.next;
while (e.next!=null){
e=e.next;
}
Node n = new Node();
n.data = o;
e.next=n;
size++;
head.data=size;
}
}

public Object get(int index){
Node c = this.head;
int len=Integer.parseInt(head.data.toString());
if (index>len||index<=0){
throw new RuntimeException("下标不存在"+index);
}
Node e = head;
int i=0;
while(c!=null){
while (e.next != null){
i++;
if (index==i){
return c.data;
if (i == index){
return e.next.data;
}
c=c.next;
e=e.next;
}
return null;
}

public Object remove(int index){
int s=size();
if (s==index){
Node n=head.next;
head=n;
return null;
int len=Integer.parseInt(head.data.toString());
if (index>len||index<=0){
throw new RuntimeException("下标不存在"+index);
}
Node n=head;
Node p = null;
while (n!=null){
s--;
if (s==index){
p=n.next;
n.next=p.next;
break;
Node e = head;
Object data = null;
int i=0;
while (e.next != null){
i++;
if (i == index){
len--;
head.data = len;
data = e.next.data;
e.next = e.next.next;
return data;
}
n=n.next;
e=e.next;
}
return null;
}

public void listNode(){
Node c = this.head;
while(c!=null){
System.out.print(c.data+ " -> ");
c=c.next;
}
System.out.println();
public Object removeFirst(){
return remove(1);
}

public int size(){
size=0;
Node c = this.head;
while(c!=null){
size++;
c=c.next;
}
return size;
public Object removeLast(){
return remove(Integer.parseInt(head.data.toString()));
}

public void addFirst(Object o){
if (this.head==null){
this.head = new Node();
this.head.data=o;
}else{
Node f = new Node();
Node n=head;
Node p=null;
while (n!=null){
p=n;
n=n.next;
}
f.data=o;
p.next=f;
}
Node e = head.next;
Node n = new Node();
n.data=o;
n.next=e;
size++;
head.next=n;
head.data=size;
}

public void addLast(Object o){
add(o);
}
public Object removeFirst(){
remove(1);
return null;
}
public Object removeLast(){
remove(size());
return null;
}

public Iterator iterator(){
LinkedList l = new LinkedList();
l.head=this.head;
return l;
public int size(){
return Integer.parseInt(head.data.toString());
}

public boolean hasNext(){
current = head;
if (current!=null){
head = current.next;
return true;
public void listNode(){
Node n = head;
StringBuffer buffer = new StringBuffer();
while (n.next!=null){
buffer.append(n.next.data + " -> ");
n=n.next;
}
if(buffer.length()>0){
System.out.print(buffer.substring(0,buffer.length()-3));
System.out.println();
}
return false;
}

public Object next(){
return current.data;
}


private static class Node{
Object data;
Node next;

}

/**
* 把该链表逆置
* 例如链表为 3->7->10 , 逆置后变为 10->7->3
*/
public void reverse(){

}

/**
* 删除一个单链表的前半部分
* 例如:list = 2->5->7->8 , 删除以后的值为 7->8
* 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10

*/
public void removeFirstHalf(){
public void reverse(){

}

Expand All @@ -146,9 +135,33 @@ public void removeFirstHalf(){
* @param i
* @param length
*/
public void remove(int i, int length){

public void remove(int i, int length){
if (i <= 0 || length<=0){
throw new RuntimeException("起始值、结束值都不能小于0等于");
}
int len = length + i;
if (len > size){
throw new RuntimeException("删除索引长度超过了链表长度");
}
Node e = head;
int y = 0;
while (e.next != null){
y++;
if (y == i){
Node n = e.next;
while (n!=null){
n = n.next;
if (y == length){
break;
}
y++;
n=n.next;
}
}
e=e.next;
}
}

/**
* 假定当前链表和list均包含已升序排列的整数
* 从当前链表中取出那些list所指定的元素
Expand All @@ -157,8 +170,40 @@ public void remove(int i, int length){
* 返回的结果应该是[101,301,401,601]
* @param list
*/
public static int[] getElements(LinkedList list){
return null;
public int[] getElements(LinkedList list){
if (list==null||list.head==null){
throw new RuntimeException("集合没有初始化");
}
int[] elements = new int[Integer.parseInt(list.head.data.toString())];
Node l = list.head;
Node n = head;
int len = 0;
int i = 0;
while (l.next!=null){
len = 0;
n=head;
while(n.next!=null){
len++;
if(len==Integer.parseInt(l.next.data.toString())){
elements[i]=Integer.parseInt(n.next.data.toString());
i++;
break;
}
n=n.next;
}
l = l.next;
}
return elements;
}

/**
* 删除一个单链表的前半部分
* 例如:list = 2->5->7->8 , 删除以后的值为 7->8
* 如果list = 2->5->7->8->10 ,删除以后的值为7,8,10

*/
public void removeFirstHalf(){

}

/**
Expand All @@ -168,26 +213,61 @@ public static int[] getElements(LinkedList list){
* @param list
*/

public void subtract(LinkedList list){

public void subtract(LinkedList list){
if (list==null||list.head==null){
throw new RuntimeException("集合没有初始化");
}
Node l = list.head;
Node n = head;
int i = 0;
while (l.next!=null){
n=head;
i=0;
while(n.next!=null){
i++;
if(n.next.data.equals(l.next.data)){
remove(i);
break;
}
n=n.next;
}
l = l.next;
}
}

/**
* 已知当前链表中的元素以值递增有序排列,并以单链表作存储结构。
* 删除表中所有值相同的多余元素(使得操作后的线性表中所有元素的值均不相同)
*/
public void removeDuplicateValues(){
public void removeDuplicateValues(){

}

/**
* 已知链表中的元素以值递增有序排列,并以单链表作存储结构。
* 试写一高效的算法,删除表中所有值大于min且小于max的元素(若表中存在这样的元素)
* 11->101->201->301->401->501->601->701
* @param min
* @param max
*/
public void removeRange(int min, int max){

public void removeRange(int min, int max){
if (min<=0||max<=0||min>max){
throw new RuntimeException("录入不正确:"+min+","+max+" 应该大于min且小于max的元素");
}
Node n = head;
int data = 0;
Node p = null;
while(n.next != null){
data=Integer.parseInt(n.next.data.toString()); //11
if(data>min&&data<max){
p = n.next.next; //101
n.next = p;
size--;
}else{
n = n.next;
}
}
head.data=size;
}

/**
Expand All @@ -196,7 +276,12 @@ public void removeRange(int min, int max){
* @param list
*/
public LinkedList intersection( LinkedList list){
return null;
return null;
}

public void add(int index, Object o) {
// TODO Auto-generated method stub

}

}