java链栈(int型数据)
先实现链表的功能,这里我为了方便使用了双向链表。
通过屏蔽一些链表功能从而实现栈的功能。
直接上菜:::::
//写一个int型的链栈。
//实现了基本的增删改查。
//使用双向链表来够造基本底层逻辑。
//并使用头插法添加新数据
public class Stack {
private Lnode head;
private int length;
int getLength(){
return this.length;
}
//栈的功能区----------------------------------
//初始化栈
public void Init_Stack(){
this.head = new Lnode();
//初始化头结点上下域全设置为空
this.head.setNext(null);
this.head.setSup(null);
//头结点初始化data设置为-1
this.head.setData(-1);
//初始化栈的长度设置为0
this.length = 0;
}
//进栈
public void pop_Stack(int data){
add_Data(data);
}
//出栈
public int push_Stack(){
if (this.length == 0){
System.out.println("出栈失败哦!栈是空的呀!!");
System.exit(-1);
}else{
Lnode p = this.head.getNext();
int cache = p.getData();
Dele_Data(cache);
return cache;
}
return -1;
}
//打印栈内元素
public String Print_Stack(){
if(this.length == 0){
return "[]";
}
Lnode p = head.getNext();
String s ="[";
for (int i=0;i
if(i == this.length - 1){
s = s + p.getData() + "]";
}else{
s = s + p.getData() + " ";
p = p.getNext();
}
}
return s;
}
//链表操作区----------------------------------------
//增加一个结点
public void add_Data(int data){
//创建一个结点
Lnode add_cache = new Lnode();
//将数据添加到新节点中
add_cache.setData(data);
if(this.length == 0){
//将头结点后面的第一个结点找到,并设置一个指针指向它
//空指针异常和c++中的一级指针传参时不能传null一样。
add_cache.setSup(head);
this.head.setNext(add_cache);
add_cache.setNext(null);
}else{
//将头结点后面的第一个结点找到,并设置一个指针指向它
//空指针异常和c++中的一级指针传参时不能传null一样。
Lnode p = this.head.getNext();
p.setSup(add_cache);
add_cache.setNext(p);
add_cache.setSup(this.head);
this.head.setNext(add_cache);
}
this.length++;
}
//查找一个结点
public Lnode Search_Data(int data){
if(length == 0){
System.out.println("链表为空,啥也没有!!!");
return null;
}
Lnode l_cache = this.head.getNext();
int cache = length;
while(cache>0){
if(l_cache.getData() == data){
System.out.println("已经查找到元素!!!");
return l_cache;
}else{
l_cache = l_cache.getNext();
cache--;
}
}
System.out.println("链表里面没有该元素!!!");
return null;
}
//删除指定结点
public boolean Dele_Data(int data){
//查找指定结点位置
if(this.length == 0){
return false;
}
if(this.length == 1){
Lnode l = this.head.getNext();
l.setSup(null);
this.head.setNext(null);
this.length--;
return true;
}
Lnode l_cache = Search_Data(data);
if(l_cache == null){
System.out.println("删除失败。");
return false;
}else{
Lnode pre = l_cache.getSup();
Lnode rear = l_cache.getNext();
pre.setNext(rear);
rear.setSup(pre);
this.length--;
return true;
}
}
//将旧数据找到并改为新数据
public boolean Change_Data(int old_data,int new_data){
Lnode l_cache = Search_Data(old_data);
if (l_cache == null){
System.out.println("没又找到符合的数据,无法修改。");
return false;
}else{
l_cache.setData(new_data);
return true;
}
}
}
//链表结点
//链表结构为双向链表
//结点保存上一级和下一级的地址
class Lnode{
//数据域
private int data;
//下一个指针域
private Lnode next;
//上一个指针域
private Lnode sup;
int getData(){
return this.data;
}
Lnode getNext(){
return this.next;
}
Lnode getSup(){
return this.sup;
}
void setData(int data){
this.data = data;
}
void setNext(Lnode next){
this.next = next;
}
void setSup(Lnode sup){
this.sup = sup;
}
}
测试代码
public class Stack_Text {
public static void main(String[] args) {
Stack s = new Stack();
s.Init_Stack();
int[] num = {1,2,3,5,4,87,5};
for(int i:num){
s.pop_Stack(i);
}
int cache = s.getLength();
for (int i = 0; i
System.out.println(s.push_Stack());
System.out.println(s.Print_Stack());
}
System.out.println("查看length:"+s.getLength());
System.out.println("再出栈一个:"+s.push_Stack());
System.out.println("查看栈内元素:"+s.Print_Stack());
}
}
以下为运行效果图