Java data Structure--implementing stacks with linked lists

Source: Internet
Author: User

=================================================//File name:linkstack_demo//-------------------------------- ----------------------------------------------//author:common//class name: link_long//attribute://Method: Class link_long{//chain node classes Publ IC Long ddata;public Link_long next;//a reference to the next node in the list public Link_long (long DData) {super (); this.ddata = DData;} public void DisplayLink () {//Displays the value of the current node System.out.println ("DData" +ddata);}} Class Name: linklist_long//property://Method: Class Linklist_long{private Link_long first;//only need the first node, from the first node to locate all nodes public linklist _long () {//constructor this.first = null;} public Boolean isEmpty () {return (first = = null);} public void Insertfirst (long dd) {//Insert element is inserted from the head of the linked list Link_long NewLink = new Link_long (dd); newlink.next = First;first = Newli NK;} Public long Deletefirst () {//delete Temp.ddatalink_long temp = first;//Staging Firstfirst = first.next;//Set next to Firstreturn temp.ddata;//returns the original first}public void Displaylist () {System.out.println ("List (first-->last):"); Link_long current = first;//is used to continuously change the position of the traverse while (CurreNT! = null) {Current.displaylink (); current = Current.next;}} Public Link_long find (int key) {//find the specified keyword Link_long current = First;while (current.ddata! = key) {if (Current.next = = null) return null;elsecurrent = Current.next;} return current;} Public Link_long Delete (int key) {///If the value of current matches, delete link_long current = first; Link_long previous = first;//does not match to the value while (current.ddata! = key) {if (Current.next = null) return null;else{// Pre and cur move backwards previous = Current;current = Current.next;}} Match to value if (current = = first)//only one first and match, then set first to First.nextfirst = first.next;else//current value matches, then delete, and assign cur next to the pre Nextprevious.next = Current.next;return Current;}} Class Name: linklist_long//property://Method: Class linkstack{//with a linked list implementation of the stack, the list is inserted from first, the new element will become first, delete the later element private Linklist_long Thelist;public Linkstack () {//constructor thelist = new Linklist_long ();} public void push (long j) {//In the stack, that is, the first INSERT element in the list Thelist.insertfirst (j);} Public long Pop () {//out of the stack, that is, delete the first element of the list return Thelist.deletefirst ();} public Boolean isEmpty () {//Determines whether the stack is empty, that is, whether the linked list is empty return (Thelist.isempty ());} public void Displaystack () {System.out.println ("Stack (Top-->bottom):"); Thelist.displaylist ();}} Main class//function:linkstack_demopublic class Linkstack_demo {public static void main (string[] args) {//TODO auto-generated method Stub linkstack thestack = new Linkstack (); Thestack.push (n); Thestack.push (+); Thestack.push (+); thestack.displaystack (); Thestack.pop (); Thestack.pop (); Thestack.displaystack ();}}

Java data Structure--implementing stacks with linked lists

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.