Algorithm (4th edition) -1.3.3 linked list

Source: Internet
Author: User

Summary: This section describes the concept of linked lists and the process and pros and cons of using lists to implement stack, queue, and bag.

Focus:

1. Definition: A linked list is a recursive data structure that is either empty (null), or a reference to a node (node) that contains a generic element and a reference to another linked list.

2. We first use a nested class to define the abstract data type of the node:

Private class Node {    item item;    Node next;}

They do not implement abstract data types, because we use their instance variables directly.

3. The required time and the length of the linked list are independent of three operations:

· Inserting nodes in the table header;

· Delete nodes from the table header;

· Inserts a node at the end of the table.

This is the point, be sure to understand!

4. The standard solution for any insert and delete operation is to use a doubly linked list, where each node contains two links pointing in different directions.

5. Implementation of the Stack (reason: 3):

When pressing an element with push (), we will add the element to the table header according to the code discussed in the 1.3.3.3 section;

When you delete an element using pop (), we delete the element from the header according to the code discussed in the 1.3.3.4 section.

 Public classStack<item> {    PrivateNode first;//Top of stack (recently added element)    Private intN//Number of elements    Private classNode {//defining nesting classes for endpointsitem Item;    Node Next; }     Public BooleanIsEmpty () {returnFirst = =NULL; }     Public intsize () {returnN; }     Public voidpush (item item) {//adding elements to the top of the stackNode Oldfirst =First ; First=NewNode (); First.item=item; First.next=Oldfirst; N++; }     PublicItem Pop () {//Delete an element from the top of the stackItem item =First.item; First=First.next; N--; returnitem; }}

The direction of the list: Stack top and bottom

6. Implementation of the queue (reason: 3):

To into row an element (Enqueue ()), we add it to the footer (the first and last points need to point to the new node when the list is empty);

To dequeue an element (Dequeue ()), we delete the node of the table header (the last value needs to be updated when the list is empty).

 Public classQueue<item> {    PrivateNode first;//links to the earliest added nodes    PrivateNode last;//links to recently added nodes    Private intN//number of elements in the queue    Private classNode {//defining nesting classes for endpointsitem Item;    Node Next; }     Public BooleanIsEmpty () {returnFirst = =NULL;//or: N = = 0.    }     Public intsize () {returnN; }     Public voidEnqueue (item item) {//add an element to the end of a tableNode Oldlast =Last ; Last=NewNode (); Last.item=item; Last.next=NULL; if(IsEmpty ()) First =Last ; ElseOldlast.next =Last ; N++; }     PublicItem dequeue () {//Delete an element from a table headerItem item =First.item; First=First.next; if(IsEmpty ()) last =NULL; N--; returnitem; }}

Link List direction: Team head--team tail

7. the use of linked lists has achieved our optimal design objectives:

· It can handle any type of data;

· The space required is always proportional to the size of the set;

· The time required for an operation is always independent of the size of the collection.

8. A milestone in the history of programming languages is the Lisp language invented by McCathy in the the 1950s, and the linked list is the main structure of the program and data in this language.

9. The implementation of the backpack:

You only need to rename the push () in the stack to add () and remove the implementation of the pop ().

ImportJava.util.Iterator; Public classBag<item>ImplementsIterable<item> {    PrivateNode first;//The first node of a linked list    Private intN; Private classNode {Item item;    Node Next; }     Public BooleanIsEmpty () {returnFirst = =NULL; }     Public intsize () {returnN; }     Public voidAdd (item item) {//exactly the same as the stack's push () methodNode Oldfirst =First ; First=NewNode (); First.item=item; First.next=Oldfirst; N++; }     PublicIterator<item>iterator () {return NewListiterator (); }    Private classListiteratorImplementsIterator<item> {        PrivateNode current =First ;  Public BooleanHasnext () {returnCurrent! =NULL; }         Public voidremove () {} PublicItem Next () {Item Item=Current.item; Current=Current.next; returnitem; }    }}

The *hasnext () method detects if current is null.

Algorithm (4th edition) -1.3.3 linked list

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.