数组
数组的代码
java
public class Myarr<T> {
private int capacity=0;
private int size=0;
private T[]arr;
//如果是空的则强制将容量扩充为10
public Myarr(int capacity) {
if (this.size == 0) {
this.capacity = 10;
this.arr=(T[]) new Object[this.capacity];
}
}
//按顺序添加数据
public void add(T value){
if(this.size>=capacity){
T[]arr1=Arrays.copyOf(arr,2*this.capacity);
this.arr=arr1;
this.capacity=this.capacity*2;
}
arr[this.size++]=value;
}
//按下标删除数据
public void delete(int index){
if(index>=this.size||index<0){
System.out.println("输入有误");
return;
}
if((this.size<=capacity/4)&&(capacity/4>0)){
T []arr1=Arrays.copyOf(arr,this.capacity/4);
this.arr=arr1;
this.capacity=this.capacity/4;
}
for(int i=index+1;i<this.size;i++){
arr[i-1]=arr[i];
}
this.size--;
}
//查找指定下标的数据
public void search(int index){
if(index>=this.size||index<0){
System.out.println("输入有误");
return;
}
System.out.println(index+"处的值为:"+arr[index]);
}
//修改指定下标的值
public void modify(int index,T value){
if(index>=this.size||index<0){
System.out.println("输入有误");
return;
}
arr[index]=value;
}
//将数组转为字符串输出
@Override
public String toString() {
StringBuffer stringBuffer=new StringBuffer();
stringBuffer.append('[');
for(int i=0;i<this.size;i++){
stringBuffer.append(arr[i]);
if(i!=this.size-1){
stringBuffer.append(",");
}
}
stringBuffer.append("]");
return stringBuffer.toString();
}
//向数组指定位置添加数值
public void aimedAdd(int index,T value){
if(index<0||index>this.capacity){
throw new IllegalArgumentException("索引越界!");
}
if(this.size>=capacity){
T []arr1=Arrays.copyOf(arr,2*this.capacity);
this.arr=arr1;
this.capacity=this.capacity*2;
}//当添加的数值大于数组实际长度时将进行扩容
this.size++;
for(int i=this.size;i>index;i--){
arr[i]=arr[i-1];
}
arr[index]=value;
}
//获取数组长度
public int getsize(){
return this.size;
}
//获取数组容积
public int getCapacity(){
return this.capacity;
}
public T remove(){
if((this.size<=capacity/4)&&(capacity/4>0)){
T []arr1=Arrays.copyOf(arr,this.capacity/4);
this.arr=arr1;
this.capacity=this.capacity/4;
}//当数组实际使用长度小于容积的四分之一时将对容积进行缩容
arr[size]=null;
this.size--;
return arr[size];
}
public T gethead(){
return arr[size-1];
}
}
链表
链表的代码
java
public class MyLinkList<T> {
class Node<T>{//使用内部类创建节点
T data;//节点本身的数据
Node next;//指向下一个节点
public Node(T data,Node node){//内部类的构造
this.data=data;
this.next=node.next;
}
public Node(T data){
this.data=data;
this.next=null;
}
}
//链表相关的属性和方法
private Node head;//链表的头结点
private int size;
public MyLinkList(){
this.head=new Node(null);
this.size=0;
}
//判断链表是否为空
public boolean isEmpty(){
return this.size==0;
}
//头部插入元素
public void addHead(T data){
//1.创建结点
Node node=new Node(data);
node.next=node;
//2.挂接
node.next=this.head;
this.head=node;
//3.更新size
this.size++;
}
public void addTail(T data){
Node node=new Node(data);
Node curnode=this.head;
if(this.head==null){
this.head=node;
}else {
while (curnode.next!=null){
curnode=curnode.next;
}
curnode.next=node;
}
this.size++;
}
//获取链表大小
public int getsize(){
return this.size;
}
// 重写toString 打印链表中的数据
@Override
public String toString(){
//从头结点开始向后遍历
Node curnode=this.head;
StringBuffer stringBuffer=new StringBuffer();
while(curnode!=null){
stringBuffer.append(curnode.data+"--->");
curnode=curnode.next;
}
stringBuffer.append("null");
return stringBuffer.toString();
}
//根据索引获得链表的值
public T getByIndex(int index){
//1.判断索引是否有效
if(index<0||index>this.size){
throw new IllegalArgumentException("索引无效");
}
Node<T> curNode=this.head.next;
int i=0;
while(i<index){
curNode=curNode.next;
i++;
}
return curNode.data;
}
//从链表头部删除元素
public T deleteHead(){
if(this.isEmpty()){
return null;
}
Node<T> delNode=this.head.next;
this.head.next=delNode.next;
delNode.next=null;
this.size--;
return delNode.data;
}
//从链表尾部删除元素
public T deletetail(){
if (this.isEmpty()){
return null;
}
Node<T> pre=this.head;
while(pre.next.next!=null){
pre=pre.next;
}
this.size--;
Node<T> delNode=pre.next;
pre.next=delNode.next;
delNode.next=null;
return delNode.data;
}
//在链表任意位置删除
public T deleteByIndex(int index){
int count=1;
if(index<0||index>this.size){
return null;
}
Node<T> pre=this.head;
while(pre.next!=null) {
pre = pre.next;
count++;
if (count == index) {
break;
}
}
Node<T> delNode=pre.next;
pre.next=delNode.next;
delNode.next=null;
return delNode.data;
}
//根据值在链表中删除元素
public int removeByValue(T value){
int count=0;
//定义前驱结点
Node<T> pre=this.head;
while(pre.next!=null){
Node<T> curNode=pre.next;
if(curNode.data.equals(value)){
pre.next=pre.next.next;
curNode.data=null;
this.size--;
count++;
}else{
pre=pre.next;
}
}
return count;
}
}
栈
-
线性结构
-
数据只能从栈顶进,栈顶出
图示
基于数组实现的栈
数组的代码:
java
public class Myarr<T> {
private int capacity=0;
private int size=0;
private T[]arr;
//如果是空的则强制将容量扩充为10
public Myarr(int capacity) {
if (this.size == 0) {
this.capacity = 10;
this.arr=(T[]) new Object[this.capacity];
}
}
//按顺序添加数据
public void add(T value){
if(this.size>=capacity){
T[]arr1=Arrays.copyOf(arr,2*this.capacity);
this.arr=arr1;
this.capacity=this.capacity*2;
}
arr[this.size++]=value;
}
//按下标删除数据
public void delete(int index){
if(index>=this.size||index<0){
System.out.println("输入有误");
return;
}
if((this.size<=capacity/4)&&(capacity/4>0)){
T []arr1=Arrays.copyOf(arr,this.capacity/4);
this.arr=arr1;
this.capacity=this.capacity/4;
}
for(int i=index+1;i<this.size;i++){
arr[i-1]=arr[i];
}
this.size--;
}
//查找指定下标的数据
public void search(int index){
if(index>=this.size||index<0){
System.out.println("输入有误");
return;
}
System.out.println(index+"处的值为:"+arr[index]);
}
//修改指定下标的值
public void modify(int index,T value){
if(index>=this.size||index<0){
System.out.println("输入有误");
return;
}
arr[index]=value;
}
//将数组转为字符串输出
@Override
public String toString() {
StringBuffer stringBuffer=new StringBuffer();
stringBuffer.append('[');
for(int i=0;i<this.size;i++){
stringBuffer.append(arr[i]);
if(i!=this.size-1){
stringBuffer.append(",");
}
}
stringBuffer.append("]");
return stringBuffer.toString();
}
//向数组指定位置添加数值
public void aimedAdd(int index,T value){
if(index<0||index>this.capacity){
throw new IllegalArgumentException("索引越界!");
}
if(this.size>=capacity){
T []arr1=Arrays.copyOf(arr,2*this.capacity);
this.arr=arr1;
this.capacity=this.capacity*2;
}
this.size++;
for(int i=this.size;i>index;i--){
arr[i]=arr[i-1];
}
arr[index]=value;
}
//获取数组长度
public int getsize(){
return this.size;
}
//获取数组容积
public int getCapacity(){
return this.capacity;
}
public T remove(){
if((this.size<=capacity/4)&&(capacity/4>0)){
T []arr1=Arrays.copyOf(arr,this.capacity/4);
this.arr=arr1;
this.capacity=this.capacity/4;
}
arr[size]=null;
this.size--;
return arr[size];
}
public T gethead(){
return arr[size-1];
}
}
数组实现的栈的代码:
java
public class ArrStack<T> implements Mystack<T>{
private Myarr<T> data;
public ArrStack(Myarr data) {
this.data = data;
}
@Override
public void push(T value) {
this.data.add(value);
}
@Override
public T pop() {
return this.data.remove();
}
@Override
public T peek() {
return this.data.gethead();
}
@Override
public boolean IsEmpty() {
if(this.data.getsize()==0){
return true;
}else{
return false;
}
}
}
基于链表实现的栈
链表的代码
java
public class MyLinkList<T> {
class Node<T>{//使用内部类创建节点
T data;//节点本身的数据
Node next;//指向下一个节点
public Node(T data,Node node){//内部类的构造
this.data=data;
this.next=node.next;
}
public Node(T data){
this.data=data;
this.next=null;
}
}
//链表相关的属性和方法
private Node head;//链表的头结点
private int size;
public MyLinkList(){
this.head=new Node(null);
this.size=0;
}
//判断链表是否为空
public boolean isEmpty(){
return this.size==0;
}
//头部插入元素
public void addHead(T data){
//1.创建结点
Node node=new Node(data);
node.next=node;
//2.挂接
node.next=this.head;
this.head=node;
//3.更新size
this.size++;
}
public void addTail(T data){
Node node=new Node(data);
Node curnode=this.head;
if(this.head==null){
this.head=node;
}else {
while (curnode.next!=null){
curnode=curnode.next;
}
curnode.next=node;
}
this.size++;
}
//获取链表大小
public int getsize(){
return this.size;
}
// 重写toString 打印链表中的数据
@Override
public String toString(){
//从头结点开始向后遍历
Node curnode=this.head;
StringBuffer stringBuffer=new StringBuffer();
while(curnode!=null){
stringBuffer.append(curnode.data+"--->");
curnode=curnode.next;
}
stringBuffer.append("null");
return stringBuffer.toString();
}
//根据索引获得链表的值
public T getByIndex(int index){
//1.判断索引是否有效
if(index<0||index>this.size){
throw new IllegalArgumentException("索引无效");
}
Node<T> curNode=this.head.next;
int i=0;
while(i<index){
curNode=curNode.next;
i++;
}
return curNode.data;
}
//从链表头部删除元素
public T deleteHead(){
if(this.isEmpty()){
return null;
}
Node<T> delNode=this.head.next;
this.head.next=delNode.next;
delNode.next=null;
this.size--;
return delNode.data;
}
//从链表尾部删除元素
public T deletetail(){
if (this.isEmpty()){
return null;
}
Node<T> pre=this.head;
while(pre.next.next!=null){
pre=pre.next;
}
this.size--;
Node<T> delNode=pre.next;
pre.next=delNode.next;
delNode.next=null;
return delNode.data;
}
//在链表任意位置删除
public T deleteByIndex(int index){
int count=1;
if(index<0||index>this.size){
return null;
}
Node<T> pre=this.head;
while(pre.next!=null) {
pre = pre.next;
count++;
if (count == index) {
break;
}
}
Node<T> delNode=pre.next;
pre.next=delNode.next;
delNode.next=null;
return delNode.data;
}
//根据值在链表中删除元素
public int removeByValue(T value){
int count=0;
//定义前驱结点
Node<T> pre=this.head;
while(pre.next!=null){
Node<T> curNode=pre.next;
if(curNode.data.equals(value)){
pre.next=pre.next.next;
curNode.data=null;
this.size--;
count++;
}else{
pre=pre.next;
}
}
return count;
}
}
链表实现的栈的代码
java
public class LinkedStack<T> implements Mystack{
private MyLinkList<T> data;
public LinkedStack(){
this.data=new MyLinkList<>();
}
@Override
public void push(Object value) {
this.data.addTail((T) value);
}
@Override
public T pop() {
return this.data.deleteHead();
}
@Override
public Object peek() {
return this.data.getByIndex(data.getsize());
}
@Override
public boolean IsEmpty() {
return this.data.isEmpty();
}
}
栈的接口
java
public interface Mystack <T>{
void push(T value);
T pop();
T peek();
boolean IsEmpty();
}