顺序表和链表实现及泛型实现
就当复习,再写一遍,有空把队列和栈也改改
1、顺序表(数组)
public class MyArrayList {
public int[] elem;
public int usedsize;
public MyArrayList() {
this.elem = new int[10];
this.usedsize = 0;
}
public MyArrayList(int cap) {
this.elem = new int[cap];
this.usedsize = 0;
}
//插入
public void add(int val) {
if (isFull()) {
return;
}
this.elem[this.usedsize] = val;
this.usedsize++;
}
public void add(int index,int val) {
if (isFull()) {
resize();
}
if (index < 0 || index > this.usedsize) {
return;
}
for (int i = this.usedsize; i > index; i--) {
this.elem[i] = this.elem[i - 1];
}
this.elem[index] = val;
this.usedsize++;
}
//删除第一次遇到的元素
private int findindex(int key) {
if (!contains(key)) {
return -1;
}
for (int i = 0; i < this.usedsize; i++) {
if (this.elem[i] == key) {
return i;
}
}
return -1;
}
public void remove(int key) {
if (isEmpty()) { //为空返回
return;
}
int find = findindex(key); //找到要删除的key的第一个下标
if (find == -1) { //不存在key
return;
}
if (find == this.usedsize - 1) { //如果要删除的是最后一个
this.usedsize--;
return;
}
//删除的不是最后一个
for (int i = find; i < this.usedsize - 1; i++) {
this.elem[i] = this.elem[i + 1];
}
this.usedsize--;
}
//删除遇到的所有关键字
public void removeAll(int key) {
if (isEmpty()) {
return;
}
while (contains(key)) {
remove(key);
}
}
//扩容
public void resize() {
if (isFull()) {
this.elem = Arrays.copyOf(this.elem,this.elem.length * 2);
}
}
//判断是否包含某个元素
public boolean contains(int key) {
if (isEmpty()) {
return false;
}
for (int i = 0; i < this.usedsize; i++) {
if (this.elem[i] == key) {
return true;
}
}
return false;
}
//清空
public void clear() {
this.usedsize = 0;
}
//返回指定下标的元素
public int get(int index) {
if (isEmpty()) {
return -1;
}
if (index < 0 || index > this.usedsize) {
return -1;
}
return this.elem[index];
}
//判空
public boolean isEmpty() {
if (this.usedsize == 0) {
return true;
}
return false;
}
//判满
public boolean isFull() {
if (this.elem.length == this.usedsize) {
return true;
}
return false;
}
//显示
public void display() {
for (int i = 0; i < this.usedsize; i++) {
System.out.print(this.elem[i] + " ");
}
System.out.println();
}
}
1.1泛型顺序表
注意:泛型顺序表需要修改参数、返回值以及比较相等时使用equals()
public class GenericArrayList<T> {
public T[] elem;
public int usedsize;
public GenericArrayList() {
this.elem = (T[]) new Object[10]; //使用Object[],强转为T[]
this.usedsize = 0;
}
public GenericArrayList(int cap) {
this.elem = (T[])new Object[cap];
this.usedsize = 0;
}
//插入
public void add(T val) {
if (isFull()) {
resize();
}
this.elem[this.usedsize++] = val;
}
public void add(int index,T val) {
if (isFull()) {
resize();
}
if (index < 0 || index > this.usedsize) {
return;
}
for (int i = this.usedsize; i > index; i--) {
this.elem[i] = this.elem[i - 1];
}
this.elem[index] = val;
this.usedsize++;
}
//删除第一次遇到的元素
private int findindex(T key) {
if (!contains(key)) {
return -1;
}
for (int i = 0; i < this.usedsize; i++) {
if ( key.equals(this.elem[i])) { //==变为equlas
return i;
}
}
return -1;
}
public void remove(T key) {
if (isEmpty()) { //为空返回
return;
}
int find = findindex(key); //找到要删除的key的第一个下标
if (find == -1) { //不存在key
return;
}
if (find == this.usedsize - 1) { //如果要删除的是最后一个
this.usedsize--;
return;
}
//删除的不是最后一个
for (int i = find; i < this.usedsize - 1; i++) {
this.elem[i] = this.elem[i + 1];
}
this.usedsize--;
}
//删除遇到的所有关键字
public void removeAll(T key) {
if (isEmpty()) {
return;
}
while (contains(key)) {
remove(key);
}
}
//扩容
public void resize() {
if (isFull()) {
this.elem = Arrays.copyOf(this.elem,this.elem.length * 2);
}
}
//判断是否包含某个元素
public boolean contains(T key) {
if (isEmpty()) {
return false;
}
for (int i = 0; i < this.usedsize; i++) {
if (key.equals(this.elem[i] )) { //判断值相等,需要重写equals
return true;
}
}
return false;
}
//清空
public void clear() {
this.usedsize = 0;
}
//返回指定下标的元素
public T get(int index) {
if (isEmpty()) {
return null;
}
if (index < 0 || index > this.usedsize) {
return null;
}
return this.elem[index];
}
//判空
public boolean isEmpty() {
if (this.usedsize == 0) {
return true;
}
return false;
}
//判满
public boolean isFull() {
if (this.elem.length == this.usedsize) {
return true;
}
return false;
}
//显示
public void display() {
for (int i = 0; i < this.usedsize; i++) {
System.out.print(this.elem[i] + " ");
}
System.out.println();
}
}
2、单链表
class singleNode{
public int val;
public singleNode next;
public singleNode(int val) {
this.val = val;
}
}
public class SingleList {
public singleNode head;
//插入
public void addFirst(int val) {
singleNode node = new singleNode(val);
if (isEmpty()) {
this.head = node;
return;
}
node.next = this.head;
this.head = node;
}
public void addLast(int val) {
singleNode node = new singleNode(val);
if (isEmpty()) {
this.head = node;
}else {
singleNode cur = this.head;
while(cur.next != null) {
cur = cur.next;
}
cur.next = node;
}
}
public void addIndex(int index,int val) {
if (index < 0 || index > size()) {
return;
}
if (index == 0) {
addFirst(val);
return;
}
if (index == size()) {
addLast(val);
return;
}
singleNode node = new singleNode(val);
singleNode pre = searchPre(index);
node.next = pre.next;
pre.next = node;
}
private singleNode searchPre(int index) {
singleNode cur = this.head;
while(index - 1 > 0) {
cur = cur.next;
index--;
}
return cur;
}
//删除第一个遇到的关键字
public void remove(int key) {
if (isEmpty()) {
return;
}
if (this.head.val == key) {
this.head = this.head.next;
return;
}
singleNode cur = this.head;
while(cur.next != null) {
if (cur.next.val == key) {
cur.next = cur.next.next;
return;
}
cur = cur.next;
}
}
public void removeAll(int key) {
if (isEmpty()) {
return;
}
if (this.head.val == key) {
this.head = this.head.next;
return;
}
singleNode cur = this.head;
while(cur.next != null) {
if (cur.next.val == key) {
cur.next = cur.next.next;
}
cur = cur.next;
}
}
//包含某个元素
public boolean contains(int key) {
if (isEmpty()) {
return false;
}
singleNode cur = this.head;
while(cur != null) {
if (cur.val == key) {
return true;
}
cur = cur.next;
}
return false;
}
//清空链表
public void clear() {
this.head = null;
}
//判空
public boolean isEmpty() {
if (this.head == null) {
return true;
}
return false;
}
//返回大小
public int size() {
if (isEmpty()) {
return 0;
}
singleNode cur = this.head;
int size = 0;
while(cur != null) {
size++;
cur = cur.next;
}
return size;
}
//打印
public void display() {
if (isEmpty()) return;
singleNode cur = this.head;
while (cur != null) {
System.out.print(cur.val + " ");
cur = cur.next;
}
System.out.println();
}
}
2.1泛型单链表
public class GenericSingleList<T> {
static class singleNode<T>{
public T val;
public singleNode<T> next;
public singleNode(T val) {
this.val = val;
}
}
public singleNode<T> head;
//插入
public void addFirst(T val) {
singleNode<T> node = new singleNode<T>(val);
if (isEmpty()) {
this.head = node;
return;
}
node.next = this.head;
this.head = node;
}
public void addLast(T val) {
singleNode<T> node = new singleNode<T>(val);
if (isEmpty()) {
this.head = node;
}else {
singleNode<T> cur = this.head;
while(cur.next != null) {
cur = cur.next;
}
cur.next = node;
}
}
public void addIndex(int index,T val) {
if (index < 0 || index > size()) {
return;
}
if (index == 0) {
addFirst(val);
return;
}
if (index == size()) {
addLast(val);
return;
}
singleNode<T> node = new singleNode<T>(val);
singleNode<T> pre = searchPre(index);
node.next = pre.next;
pre.next = node;
}
private singleNode<T> searchPre(int index) {
singleNode<T> cur = this.head;
while(index - 1 > 0) {
cur = cur.next;
index--;
}
return cur;
}
//删除第一个遇到的关键字
public void remove(T key) {
if (isEmpty()) {
return;
}
if (key.equals(this.head.val)) {
this.head = this.head.next;
return;
}
singleNode<T> cur = this.head;
while(cur.next != null) {
if ( key.equals(cur.next.val)) {
cur.next = cur.next.next;
return;
}
cur = cur.next;
}
}
public void removeAll(T key) {
if (isEmpty()) {
return;
}
if (key.equals(this.head.val)) {
this.head = this.head.next;
return;
}
singleNode<T> cur = this.head;
while(cur.next != null) {
if (key.equals(cur.next.val)) {
cur.next = cur.next.next;
}
cur = cur.next;
}
}
//包含某个元素
public boolean contains(T key) {
if (isEmpty()) {
return false;
}
singleNode<T> cur = this.head;
while(cur != null) {
if (key.equals(cur.val)) {
return true;
}
cur = cur.next;
}
return false;
}
//清空链表
public void clear() {
this.head = null;
}
//判空
public boolean isEmpty() {
if (this.head == null) {
return true;
}
return false;
}
//返回大小
public int size() {
if (isEmpty()) {
return 0;
}
singleNode<T> cur = this.head;
int size = 0;
while(cur != null) {
size++;
cur = cur.next;
}
return size;
}
//打印
public void display() {
if (isEmpty()) return;
singleNode<T> cur = this.head;
while (cur != null) {
System.out.print(cur.val + " ");
cur = cur.next;
}
System.out.println();
}
}
3、双向链表
class DoubleNode{
public int val;
public DoubleNode pre;
public DoubleNode next;
public DoubleNode(int val) {
this.val = val;
}
}
public class DoubleHeadList {
public DoubleNode head;
public DoubleNode tail;
//插入
public void addFirst(int val) {
DoubleNode node = new DoubleNode(val);
if (isEmpty()) {
this.head = node;
this.tail = node;
return;
}
node.next = this.head;
this.head.pre = node;
this.head = node;
}
public void addLast(int val) {
DoubleNode node = new DoubleNode(val);
if (isEmpty()) addFirst(val);
this.tail.next = node;
node.pre = this.tail;
this.tail = node;
}
public void addIndex(int index,int val) {
if (isEmpty()) addFirst(val);
if (index < 0 || index > size()) return;
DoubleNode node = new DoubleNode(val);
if (index == 0) {
addFirst(val);
return;
}
if (index == size()) {
addLast(val);
return;
}
DoubleNode cur = this.head;
while(index > 0) {
cur = cur.next;
index--;
}
node.next = cur;
node.pre = cur.pre;
cur.pre.next = node;
cur.pre = node;
}
//删除
public void remove(int key) {
if (isEmpty()) return;
if (!contains(key)) return;
DoubleNode cur = this.head;
while(cur != null) {
if (cur.val == key) {
if (cur == this.head) {
this.head.next.pre = null;
this.head = this.head.next;
}else {
cur.pre.next = cur.next;
if (cur == this.tail) {
this.tail = this.tail.pre;
}else {
cur.next.pre = cur.pre;
}
}
return;
}
cur = cur.next;
}
}
public void removeAll(int key) {
if (isEmpty()) return;
if (!contains(key)) return;
DoubleNode cur = this.head;
while(cur != null) {
if (cur.val == key) {
if (cur == this.head) {
this.head.next.pre = null;
this.head = this.head.next;
}else {
cur.pre.next = cur.next;
if (cur == this.tail) {
this.tail = this.tail.pre;
}else {
cur.next.pre = cur.pre;
}
}
}
cur = cur.next;
}
}
//是否包含某个元素
public boolean contains(int key) {
if (isEmpty()) return false;
DoubleNode cur = this.head;
while (cur != null ) {
if (cur.val == key) {
return true;
}
cur = cur.next;
}
return false;
}
//返回指定下标的元素
public DoubleNode get(int index) {
if (isEmpty()) return null;
if (index < 0 || index > size()) {
return null;
}
DoubleNode cur = this.head;
while (index > 0 ) {
cur = cur.next;
index--;
}
return cur;
}
//判空
public boolean isEmpty() {
if (this.head == null) {
return true;
}
return false;
}
//大小
public int size() {
if (isEmpty()) return 0;
DoubleNode cur = this.head;
int size = 0;
while(cur != null) {
size++;
cur = cur.next;
}
return size;
}
//清空链表
public void clear() {
this.head = null;
this.tail = null;
}
//打印链表
public void display() {
if (isEmpty()) return;
DoubleNode cur = this.head;
while (cur != null) {
System.out.print(cur.val + " ");
cur = cur.next;
}
System.out.println();
}
}
3.1泛型双向链表
public class GenericDoubleHeadList<T>{
static class DoubleNode<T>{ //内部类
public T val;
public DoubleNode<T> pre;
public DoubleNode<T> next;
public DoubleNode(T val) {
this.val = val;
}
}
public DoubleNode<T> head;
public DoubleNode<T> tail;
//插入
public void addFirst(T val) {
DoubleNode<T> node = new DoubleNode<>(val);
if (isEmpty()) {
this.head = node;
this.tail = node;
return;
}
node.next = this.head;
this.head.pre = node;
this.head = node;
}
public void addLast(T val) {
DoubleNode<T> node = new DoubleNode<>(val);
if (isEmpty()) addFirst(val);
this.tail.next = node;
node.pre = this.tail;
this.tail = node;
}
public void addIndex(int index,T val) {
if (isEmpty()) addFirst(val);
if (index < 0 || index > size()) return;
DoubleNode<T> node = new DoubleNode<>(val);
if (index == 0) {
addFirst(val);
return;
}
if (index == size()) {
addLast(val);
return;
}
DoubleNode<T> cur = this.head;
while(index > 0) {
cur = cur.next;
index--;
}
node.next = cur;
node.pre = cur.pre;
cur.pre.next = node;
cur.pre = node;
}
//删除
public void remove(T key) {
if (isEmpty()) return;
if (!contains(key)) return;
DoubleNode<T> cur = this.head;
while(cur != null) {
if ( key.equals(cur.val)) {
if (this.head.equals(cur)) {
this.head.next.pre = null;
this.head = this.head.next;
}else {
cur.pre.next = cur.next;
if (this.tail.equals(cur)) {
this.tail = this.tail.pre;
}else {
cur.next.pre = cur.pre;
}
}
return;
}
cur = cur.next;
}
}
public void removeAll(T key) {
if (isEmpty()) return;
if (!contains(key)) return;
DoubleNode<T> cur = this.head;
while(cur != null) {
if (key.equals(cur.val)) {
if (this.head.equals(cur)) {
this.head.next.pre = null;
this.head = this.head.next;
}else {
cur.pre.next = cur.next;
if (this.tail.equals(cur)) {
this.tail = this.tail.pre;
}else {
cur.next.pre = cur.pre;
}
}
}
cur = cur.next;
}
}
//是否包含某个元素
public boolean contains(T key) {
if (isEmpty()) return false;
DoubleNode<T> cur = this.head;
while (cur != null ) {
if (key.equals(cur.val)) {
return true;
}
cur = cur.next;
}
return false;
}
//返回指定下标的元素
public DoubleNode<T> get(int index) {
if (isEmpty()) return null;
if (index < 0 || index > size()) {
return null;
}
DoubleNode<T> cur = this.head;
while (index > 0 ) {
cur = cur.next;
index--;
}
return cur;
}
//判空
public boolean isEmpty() {
if (this.head == null) {
return true;
}
return false;
}
//大小
public int size() {
if (isEmpty()) return 0;
DoubleNode<T> cur = this.head;
int size = 0;
while(cur != null) {
size++;
cur = cur.next;
}
return size;
}
//清空链表
public void clear() {
this.head = null;
this.tail = null;
}
//打印链表
public void display() {
if (isEmpty()) return;
DoubleNode<T> cur = this.head;
while (cur != null) {
System.out.print(cur.val + " ");
cur = cur.next;
}
System.out.println();
}
}