Java uses LinkedList to simulate a stack or queue data structure __java base

Source: Internet
Author: User

Recently in the review of the Java Foundation, looked at the Java Foundation, in the see the collection when suddenly found that the interview has a written question: simulation of a stack or queue data structure, was not done, today to write, the first to understand the stack and the data structure of the queue

Stack: Advanced later out
Queue: Advanced First Out

LinkedList is just the AddFirst (), AddLast () method, now write a simple tool class package under the OH

public class Demo4 {public
		static void Main (string[] args) {
			tools tools = new tools ();
			Tools.add ("a");
			Tools.add ("B");
			Tools.add ("C");
			Tools.add ("D");
			
			Tools.romove ();
		}
Class tools{
	private linkedlist<string> ll;
	Public Tools () {
		ll = new linkedlist<string> ();
	}
	
	public void Add (String str) {
		ll.addfirst (str);
	}
	
	public void Romove () {for
		(int i=0;i<ll.size (); i++) {
				String content = Ll.get (i);
				SYSTEM.OUT.PRINTLN (content);}}
		

Print results:

D
C
B
A

This is the advanced, this is the data structure of the stack, the queue is simple, as long as the AddFirst () method to AddLast () method on the line

The code is as follows:

public class Demo4 {public
		static void Main (string[] args) {
			tools tools = new tools ();
			Tools.add ("a");
			Tools.add ("B");
			Tools.add ("C");
			Tools.add ("D");
			
			Tools.romove ();
		}
Class tools{
	private linkedlist<string> ll;
	Public Tools () {
		ll = new linkedlist<string> ();
	}
	
	public void Add (String str) {
		ll.addlast (str);
	}
	
	public void Romove () {for
		(int i=0;i<ll.size (); i++) {
				String content = Ll.get (i);
				SYSTEM.OUT.PRINTLN (content);}}
		

The result is not printed,





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.