Stacks can be viewed as a special type of linear table, where elements can be accessed, inserted, and deleted only at the end of the stack (top of the stack).
A queue represents a waiting linear table, which can also be seen as a special type of linear table where elements can only be inserted from the end of the queue (the end of the queue) and accessed and deleted from the beginning (queue header).
———— Advanced Java Language Programming (original book 8th edition)
The stack is advanced back-out (LIFO), while the queue is first-out (FIFO).
Code that implements the data structure of the stack
Packagestruct;//late in first Out,lifo Public classMystack<e>{ PrivateNode<e> head =NULL; PublicMystack () {} PublicMystack (E Element) {Node<E> NewNode =NewNode<e>(Element); Head=NewNode; } Private classNode<e>{E element; Node<E>Next; PublicNode (E element) { This. Element =element; } } //pop out a element PublicE Pop () {Node<E> popout =Head; Head=Head.next; returnpopout.element; } //push a new element into stack Public voidpush (E Element) {Node<E> NewNode =NewNode<e>(Element); if(head!=NULL) {Newnode.next=Head; Head=NewNode; } Else{Head=NewNode; } } //show The first element PublicE Peek () {returnhead.element; } //empty or not Public Booleanempty () {if(head!=NULL) return false; Else return true; } Public Static voidMain (string[] args) {//About StringMystack<string> Stack1 =NewMystack<string>(); Stack1.push ("SSS"); Stack1.push ("DDDD"); Stack1.push ("DSDs"); System.out.println ("Begin"); while(Stack1.empty () = =false) {System.out.print (Stack1.pop ()+" "); } System.out.println (); System.out.println ("End"); //About IntegerMystack<integer> Stack2 =NewMystack<integer>(); Stack2.push (212); Stack2.push (545); Stack2.push (54643); Stack2.push (000); System.out.println ("Begin"); while(Stack2.empty () = =false) {System.out.print (Stack2.pop ()+" "); } System.out.println (); System.out.println ("End"); //no matter of the typeMystack Stack3 =NewMystack (); Stack3.push (212); Stack3.push ("Sdad"); Stack3.push (54643.787f); Stack3.push (0.98989); System.out.println ("Begin"); while(Stack3.empty () = =false) {System.out.print (Stack3.pop ()+" "); } System.out.println (); System.out.println ("End"); //LIFOMystack<string> Stack4 =NewMystack<string> ("First"); Stack4.push ("Second"); Stack4.push ("Third"); Stack4.push ("Forth"); System.out.println ("Begin"); while(Stack4.empty () = =false) {System.out.print (Stack4.pop ()+" "); } System.out.println (); System.out.println ("End"); } }
In the process of writing this data structure, also a little review of generics. Then notice that the generic type is not the basic type, at least the corresponding wrapper class with the base type.
A generic type must be a reference type. You cannot replace a generic type with a base type such as int, double, or char. Instead, the corresponding integer, double, or character should be used instead.
———— Advanced Java Language Programming (original book 8th edition)
java-Data Structure Exercises