12.07.2015 Views

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

108 Chap. 4 Lists, Stacks, <strong>and</strong> Queuesclass Link {private E element;private Link next;// Singly linked list node// Value for this node// Pointer <strong>to</strong> next node in list// Construc<strong>to</strong>rsLink(E it, Link nextval){ element = it; next = nextval; }Link(Link nextval) { next = nextval; }Link next() { return next; }Link setNext(Link nextval){ return next = nextval; }E element() { return element; }E setElement(E it) { return element = it; }} // class LinkFigure 4.4 A simple singly linked list node implementation.headcurrtailhead20 23 12 15(a)currtail2023101215(b)Figure 4.5 Illustration of a faulty linked-list implementation where curr pointsdirectly <strong>to</strong> the current node. (a) Linked list prior <strong>to</strong> inserting element withvalue 10. (b) Desired effect of inserting element with value 10.of the list. The position of the current element is indicated by another pointer,named curr. Finally, because there is no simple way <strong>to</strong> compute the length of thelist simply from these three pointers, the list length must be s<strong>to</strong>red explicitly, <strong>and</strong>updated by every operation that modifies the list size. The value cnt s<strong>to</strong>res thelength of the list.Note that LList’s construc<strong>to</strong>r maintains the optional parameter for minimumlist size introduced for Class AList. This is done simply <strong>to</strong> keep the calls <strong>to</strong> theconstruc<strong>to</strong>r the same for both variants. Because the linked list class does not need<strong>to</strong> declare a fixed-size array when the list is created, this parameter is unnecessaryfor linked lists. It is ignored by the implementation.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!