java利用单向链表构造队的类,利用单向链表构造栈的类

满意答案

00e27ab806e4881f8254fe7ae8741834.png

vsnytx

2013.08.26

00e27ab806e4881f8254fe7ae8741834.png

采纳率:47%    等级:12

已帮助:6062人

// linkStack.java

// demonstrates a stack implemented as a list

// to run this program: C>java LinkStackApp

class Link

{

public long dData; // data item

public Link next; // next link in list

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

public Link(long dd) // constructor

{ dData = dd; }

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

public void displayLink() // display ourself

{ System.out.print(dData + " "); }

} // end class Link

class LinkList

{

private Link first; // ref to first item on list

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

public LinkList() // constructor

{ first = null; } // no items on list yet

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

public boolean isEmpty() // true if list is empty

{ return (first==null); }

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

public void insertFirst(long dd) // insert at start of list

{ // make new link

Link newLink = new Link(dd);

newLink.next = first; // newLink --> old first

first = newLink; // first --> newLink

}

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

public long deleteFirst() // delete first item

{ // (assumes list not empty)

Link temp = first; // save reference to link

first = first.next; // delete it: first-->old next

return temp.dData; // return deleted link

}

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

public void displayList()

{

Link current = first; // start at beginning of list

while(current != null) // until end of list,

{

current.displayLink(); // print data

current = current.next; // move to next link

}

System.out.println("");

}

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

} // end class LinkList

class LinkStack

{

private LinkList theList;

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

public LinkStack() // constructor

{

theList = new LinkList();

}

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

public void push(long j) // put item on top of stack

{

theList.insertFirst(j);

}

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

public long pop() // take item from top of stack

{

return theList.deleteFirst();

}

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

public boolean isEmpty() // true if stack is empty

{

return ( theList.isEmpty() );

}

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

public void displayStack()

{

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

theList.displayList();

}

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

} // end class LinkStack

class LinkStackApp

{

public static void main(String[] args)

{

LinkStack theStack = new LinkStack(); // make stack

theStack.push(20); // push items

theStack.push(40);

theStack.displayStack(); // display stack

theStack.push(60); // push items

theStack.push(80);

theStack.displayStack(); // display stack

theStack.pop(); // pop items

theStack.pop();

theStack.displayStack(); // display stack

} // end main()

} // end class LinkStackApp

00分享举报

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值