java-Data Structure Exercises

Source: Internet
Author: User

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

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.