java用链表实现栈_Java数据结构——用链表实现栈

a95150064200e927a3d9ec5aac8bd397.png

7a146026ca5aaf4863d1b4a657a9757f.png

//=================================================

// File Name :LinkStack_demo

//------------------------------------------------------------------------------

// Author :Common

//类名:Link_long

//属性:

//方法:

class Link_long{//链节点类

public long dData;

public Link_long next;//链表中下一个节点的引用

public Link_long(long dData) {

super();

this.dData = dData;

}

public void displayLink(){//显示当前节点的值

System.out.println("dData"+dData);

}

}

//类名:LinkList_long

//属性:

//方法:

class LinkList_long{

private Link_long first;//只需要第一个节点,从第一个节点出发即可定位所有节点

public LinkList_long() {//构造函数

this.first = null;

}

public boolean isEmpty(){

return (first == null);

}

public void insertFirst(long dd){//插入元素是从链表的头开始插入

Link_long newLink = new Link_long(dd);

newLink.next = first;

first = newLink;

}

public long deleteFirst(){//删除temp.dData

Link_long temp = first;//暂存first

first = first.next;//把next设为first

return temp.dData;//返回原来的first

}

public void displayList(){

System.out.println("List(first-->last):");

Link_long current = first;//用于不断改变位置实现遍历

while(current != null){

current.displayLink();

current = current.next;

}

}

public Link_long find(int key){//查找指定的关键字

Link_long current = first;

while(current.dData != key){

if(current.next == null)

return null;

else

current = current.next;

}

return current;

}

public Link_long delete(int key){//如果current的值匹配,则删除

Link_long current = first;

Link_long previous = first;

//没有匹配到值

while(current.dData != key){

if(current.next == null)

return null;

else{//pre和cur向后移动

previous = current;

current = current.next;

}

}

//匹配到值

if(current == first)//只有一个first,并匹配,则把first设成first.next

first = first.next;

else//current的值匹配,则删除,并把cur的next赋给pre的next

previous.next = current.next;

return current;

}

}

//类名:LinkList_long

//属性:

//方法:

class LinkStack{//用链表实现栈,链表从First开始插入,新的元素将变成First,先删除后来元素

private LinkList_long theList;

public LinkStack() {//构造函数

theList = new LinkList_long();

}

public void push(long j){//入栈,即在链表的First插入元素

theList.insertFirst(j);

}

public long pop(){//出栈,即删除链表的First元素

return theList.deleteFirst();

}

public boolean isEmpty(){//判断栈是否为空,即判断链表是否为空

return (theList.isEmpty());

}

public void displayStack(){

System.out.println("Stack(top-->bottom):");

theList.displayList();

}

}

//主类

//Function : LinkStack_demo

public class LinkStack_demo {

public static void main(String[] args) {

// TODO 自动生成的方法存根

LinkStack theStack = new LinkStack();

theStack.push(10);

theStack.push(20);

theStack.push(30);

theStack.displayStack();

theStack.pop();

theStack.pop();

theStack.displayStack();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值