//写一个int型的链栈。//实现了基本的增删改查。//使用双向链表来够造基本底层逻辑。//并使用头插法添加新数据publicclassStack{privateLnode head;privateint length;intgetLength(){returnthis.length;}//栈的功能区----------------------------------//初始化栈publicvoidInit_Stack(){this.head =newLnode();//初始化头结点上下域全设置为空this.head.setNext(null);this.head.setSup(null);//头结点初始化data设置为-1this.head.setData(-1);//初始化栈的长度设置为0this.length =0;}//进栈publicvoidpop_Stack(int data){add_Data(data);}//出栈publicintpush_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;}//打印栈内元素publicStringPrint_Stack(){if(this.length ==0){return"[]";}Lnode p = head.getNext();String s ="[";for(int i=0;i<this.length;i++){if(i ==this.length -1){
s = s + p.getData()+"]";}else{
s = s + p.getData()+" ";
p = p.getNext();}}return s;}//链表操作区----------------------------------------//增加一个结点publicvoidadd_Data(int data){//创建一个结点Lnode add_cache =newLnode();//将数据添加到新节点中
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++;}//查找一个结点publicLnodeSearch_Data(int data){if(length ==0){System.out.println("链表为空,啥也没有!!!");returnnull;}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("链表里面没有该元素!!!");returnnull;}//删除指定结点publicbooleanDele_Data(int data){//查找指定结点位置if(this.length ==0){returnfalse;}if(this.length ==1){Lnode l =this.head.getNext();
l.setSup(null);this.head.setNext(null);this.length--;returntrue;}Lnode l_cache =Search_Data(data);if(l_cache ==null){System.out.println("删除失败。");returnfalse;}else{Lnode pre = l_cache.getSup();Lnode rear = l_cache.getNext();
pre.setNext(rear);
rear.setSup(pre);this.length--;returntrue;}}//将旧数据找到并改为新数据publicbooleanChange_Data(int old_data,int new_data){Lnode l_cache =Search_Data(old_data);if(l_cache ==null){System.out.println("没又找到符合的数据,无法修改。");returnfalse;}else{
l_cache.setData(new_data);returntrue;}}}//链表结点//链表结构为双向链表//结点保存上一级和下一级的地址classLnode{//数据域privateint data;//下一个指针域privateLnode next;//上一个指针域privateLnode sup;intgetData(){returnthis.data;}LnodegetNext(){returnthis.next;}LnodegetSup(){returnthis.sup;}voidsetData(int data){this.data = data;}voidsetNext(Lnode next){this.next = next;}voidsetSup(Lnode sup){this.sup = sup;}}
测试代码
publicclassStack_Text{publicstaticvoidmain(String[] args){Stack s =newStack();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 <cache; 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());}}