栈是先进后出,堆列是先进先出,这里需要区分开。
今天学的是数组,单向列表和双向列表。
数组实现
优点:查找块 缺点:插入 删除
链表实现
优点:插入 删除 缺点:查找
对于数组,我们定义了以下的方法,方便主函数进行调用:
public class MyList {
int[] arr = new int[10];
int index = 0;
double r = 0.75;//加载因子 ???
//追加
public void add(int a){
if(((double)index)/arr.length>r){
int[] temp = new int[arr.length+10];
for(int i =0;i<arr.length;i++){
temp[i] = arr[i];
}
arr = temp;
}
arr[index++] = a;
}
//插入
public void insert(int index_,int a){
if(index_>index&&index_<0){
System.out.println("下标越界");
}else{
if(((double)index)/arr.length>r){
int[] temp = new int[arr.length+10];
for(int i =0;i<arr.length;i++){
temp[i] = arr[i];
}
arr = temp;
}
//移位
for(int i=index-1;i>=index_;i--){
arr[i+1] = arr[i];//index-1
}
arr[index_] = a;
index++;
}
}
//修改
public void update(int index_,int a){
if(index_>=index&&index_<0){
System.out.println("下标越界");
}else{
arr[index_] = a;
}
}
//删除
public void delete(int index_){
if(index_>=index&&index_<0){
System.out.println("下标越界");
}else{
for(int i=index_;i<index;i++){
arr[i] = arr[i+1];
}
index--;
}
}
//取值
public int get(int index_){
if(index_>=index&&index_<0){
System.out.println("下标越界");
return -1;
}else{
return arr[index_];
}
}
public int size(){
return index;
}
public void foreach(){
for(int i=0;i<index;i++ ){
System.out.print(arr[i]+" ");
}
}
}
接下来就是单向链表的方法:
public class MyLinked {
Node first; //头接点
int size = 0;
//追加
public void add(int a){
if(size==0){
Node node = new Node();
node.value = a;
node.next = null;
first = node;
}else{
Node node = new Node();
node.value = a;
node.next = null;
Node tail = first;
while (tail.next!=null){
tail = tail.next;
}
tail.next = node;
}
size++;
}
//插入
public void insert(int v,int a){
Node tail = first;
while (tail.value!=v){
tail = tail.next;
}
Node node = new Node();
node.value = a;
node.next = tail.next;
tail.next = node;
size++;
}
//删除
public void delete(int v){
Node tail = first;
while (tail.next.value!=v){ //判断当前结点下一个结点是否是要被删除元素
tail = tail.next;
}
tail.next = tail.next.next;
size--;
}
public int size(){
return size;
}
//修改
public void update(int v,int v1){
Node tail = first;
while (tail.value!=v){ //判断当前结点下一个结点是否是要被删除元素
tail = tail.next;
}
tail.value = v1;
}
public void foreach(){
Node tail = first;
while (tail.next!=null){
System.out.print(tail.value+" ");
tail = tail.next;
}
System.out.print(tail.value);
System.out.println();
}
}
最后是双向链表:
public class MyLinkedList2 {
Node2 first = null;
Node2 tail = null;
int size=0;
//追加
public void add(int a){
//判定是否位空链表
if(first==null){
//空
Node2 node2 = new Node2();
node2.value = a;
node2.pre = null;
node2.next = null;
first = node2;
tail = node2;
}else{
//非空
Node2 node2 = new Node2();
node2.value = a;
node2.pre = tail;
node2.next = null;
tail.next = node2;//原来的尾结点指向下一个接点
tail = node2;
}
//长度加1
size++;
}
//遍历
public void foreach_first(){
Node2 node2 = first;
while (node2.next!=null){
System.out.print(node2.value+" ");
node2 = node2.next;
}
System.out.print(node2.value+" ");//补输出尾结点
System.out.println();
}
public void foreach_tail(){
Node2 node2 = tail;
while (node2.pre!=null){
System.out.print(node2.value+" ");
node2 = node2.pre;
}
System.out.print(node2.value+" ");//补输出头结点
System.out.println();
}
//插入
public void insert_first(int v,int a){
if(first==null){
//空
Node2 node2 = new Node2();
node2.value = a;
node2.pre = null;
node2.next = null;
first = node2;
tail = node2;
}else {//链表长度为1;
if(tail.value==v){
Node2 node2 = new Node2();
node2.value = a;
node2.pre = tail;
node2.next = null;
tail.next = node2;//原来的尾结点指向下一个接点
tail = node2;
}else{
//链表长度>=2
Node2 node2 = first;
while (node2.value != v) {
node2 = node2.next;//步长
}
Node2 node21 = new Node2();
node21.value = a;
node21.pre = node2;
node21.next = node2.next;
node2.next.pre = node21;
node2.next = node21;
}
}
size++;
}
public void insert_tail(int v,int a){
if(tail==null){
Node2 node2 = new Node2();
node2.value = a;
node2.pre = null;
node2.next = null;
first = node2;
tail = node2;
}else if(first.value==v){
Node2 node2 = new Node2();
node2.value = a;
node2.next = first;
node2.pre = null;
first = node2;
}else {
Node2 node2 = tail;
while (node2.value != v){
node2 = node2.pre;
}
Node2 node21 = new Node2();
node21.value = a;
node21.next = node2;
node21.pre = node2.pre;
node2.pre.next = node21;
node2.pre = node21;
}
}
//删除
public void delete_first(int v){
Node2 node2 = first;
while (node2!=null&&node2.value!=v){
node2 = node2.next;
}
if(node2==tail){
tail.pre.next=null;
tail =tail.pre;
}else if(node2==first){
first.next.pre=null;
first=first.next;
}else {
node2.next.pre = node2.pre;
node2.pre.next = node2.next;
}
size--;
}
public void delete_tail(int v){
//默认不是空链表
Node2 node2 = tail;
while (node2!=null&&node2.value!=v){
node2 = node2.pre;
}
if(node2==first){
first.next.pre = null;
first = first.next;
}else if(node2 == tail){
tail.pre.next = null;
tail = tail.pre;
}else{
node2.next.pre = node2.pre;
node2.pre.next = node2.next;
}
size--;
}
//修改
public void update_first(int v, int a){
Node2 node2 = first;
while (node2.value!=v){
node2 = node2.next;
}
node2.value = a;
}
public void update_tail(int v,int a){
Node2 node2 = tail;
while (node2.value!=v){
node2 = node2.pre;
}
node2.value = a;
}
//大小
public int size(){
return size;
}
}
这里调用方法的主函数也给出:
public class day6 {
public static void main(String[] args){
/*MyLinked myLinked = new MyLinked();
myLinked.add(1);
myLinked.add(2);
myLinked.add(3);
myLinked.add(4);
myLinked.add(5);
myLinked.foreach();
myLinked.insert(2,89);
myLinked.foreach();
myLinked.delete(89);
myLinked.foreach();*/
MyLinkedList2 myLinkedList2 = new MyLinkedList2();
myLinkedList2.add(1);
myLinkedList2.add(2);
myLinkedList2.add(3);
myLinkedList2.add(4);
myLinkedList2.add(5);
myLinkedList2.insert_first(2,85);
myLinkedList2.delete_tail(4);
myLinkedList2.foreach_first();
myLinkedList2.foreach_tail();
myLinkedList2.update_first(1,10);
myLinkedList2.foreach_first();
myLinkedList2.delete_first(10);
myLinkedList2.foreach_first();
myLinkedList2.insert_tail(3,100);
myLinkedList2.foreach_first();
//数组
// ArrayList arrayList = new ArrayList();
// arrayList.add(5);
// LinkedList linkedList = new LinkedList();
}
}
我们可以看到需要将方法里的函数转换成静态方法然后调用,今天主要是记住方法的调用,以及Node2 node2 = new Node2();这种类似的定义方式;
static静态方法设计:
- 在实际项目开发中,大部分的类都是非static型的,也就是说大部分的类都是需要实例化对象调用的。
- 而静态方法则不需要实例话对象就可以使用。
- public static void print只需要print就可以调用。 public void print需要用 new javademo().print()调用。
臭宝宝腿短裤子长;