Algorithm (fourth edition) Java implementation stack and queue for learning notes (linked list implementation)

Source: Internet
Author: User
Tags iterable

Down-Stack (linked list implementation):

Import Java.util.iterator;public class Linkedstack<item> implements Iterable<item>{public class Node{Item Item Node Next;} Private Node frist;private int N = 0;public Boolean isEmpty () {return N = = 0;} public int size () {return N;} public void push (item item) {Node Oldfrist = Frist;frist = new Node (); frist.next = Oldfrist;frist.item = Item; n++;} Public Item Pop () {Item item = Frist.item;frist = Frist.next; N--;return item;} @Overridepublic iterator<item> Iterator () {//TODO auto-generated method Stubreturn new Linkedstackiterator ();} Private class Linkedstackiterator implements Iterator<item>{private Node current = frist; @Overridepublic Boolean Hasnext () {//TODO auto-generated method Stubreturn Current! = NULL;} @Overridepublic Item Next () {//TODO auto-generated method Stubitem item = Current.item;current = Current.next;return Item ;} @Overridepublic void Remove () {//TODO auto-generated Method stub}}}

FIFO Queue (linked list implementation):

Import Java.util.iterator;public class Linkedqueue<item> implements Iterable<item>{public class Node{Item Item Node Next;} Private node frist;private node last;private int N = 0;public Boolean isEmpty () {return frist = null;} public int size () {return N;}  public void Enqueue (item item) {Node Oldlast = Last;last = new Node (); last.item = Item;last.next = Null;if (IsEmpty ()) {frist = Last;} else {oldlast.next = last;} n++;} Public Item dequeue () {Item item = Frist.item;frist = Frist.next;if (IsEmpty ()) {last = null;} N--;return item;} @Overridepublic iterator<item> Iterator () {//TODO auto-generated method Stubreturn new Linkedqueueiterator ();} Private class Linkedqueueiterator implements Iterator<item>{private Node current = frist; @Overridepublic Boolean Hasnext () {//TODO auto-generated method Stubreturn Current! = NULL;} @Overridepublic Item Next () {//TODO auto-generated method Stubitem item = Current.item;current = Current.next;return Item ;} @Overridepublic void Remove () {//ToDo auto-generated Method stub}}} 


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Algorithm (fourth edition) Java implementation stack and queue for learning notes (linked list implementation)

Related Article

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.