1 / 20

Lecture 7 of Computer Science II

Lecture 7 of Computer Science II. Linked Lists. Instructor: Mr.Ahmed Al Astal. LINKED LISTS. Definition. A sequence of nodes, each containing arbitrary data fields and one or two references ("links") pointing to the next and/or previous nodes. LINKED LISTS. Definition.

lani-wade
Download Presentation

Lecture 7 of Computer Science II

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 7 ofComputer Science II Linked Lists Instructor: Mr.Ahmed Al Astal

  2. LINKED LISTS Definition A sequence of nodes, each containing arbitrary data fields and one or two references ("links") pointing to the next and/or previous nodes.  Page 2

  3. LINKED LISTS Definition • A linked list is a self-referential datatype because it contains a pointer or link to another datum of the same type. • Linked lists permit insertion and removal of nodes at any point in the list in constant time,[1] but do not allow random access. • Several different types of linked list exist: singly-linked lists, doubly-linked lists, and circularly-linked lists.  Page 3

  4. tail head LINKED LISTS Applications • Navigating pages in a novel. • Presenting test questions. • Moving through a list of user settings on an application.  Page 4

  5. tail head LINKED LISTS Vs. Arrays • Space usage: • Indexing • Insertion • Deletion  Page 5

  6. tail head SINGLY LINKED LISTS Definition • A singly linked list is a concrete data structure consisting of a sequence of nodes • Each node stores • element • link to the next node  Page 6

  7. SINGLY LINKED LISTS Implementing a Singly Linked List To implement a singly linked list, we define a Node class,, which specifies the type of objects stored at the nodes of the list. Given the Node class, we can define a class, SLinkedList,, defining the actual linked list. This class keeps a reference to the head node and a variable counting the total number of nodes.  Page 7

  8. SINGLY LINKED LISTS Inserting at the Head 1. Allocate a new node 2. Insert new element 3. Have new node point to old head 4. Update head to point to new node  Page 8

  9. SINGLY LINKED LISTS Removing at the Head 1. Update head to point to next node in the list 2. Allow garbage collector to reclaim the former first node  Page 9

  10. SINGLY LINKED LISTS Inserting at the Tail 1. Allocate a new node 2. Insert new element 3. Have new node point to null 4. Have old last node point to new node 5. Update tail to point to new node  Page 10

  11. SINGLY LINKED LISTS Removing at the Tail • Removing at the tail of a singly linked list is not efficient! • There is no constant-time way to update the tail to point to the previous node  Page 11

  12. DOUBLY LINKED LISTS Doubly Linked Lists • There is a type of linked list that allows us to go in both directions—forward and reverse—in a linked list. It is the doubly linked list. • A node in a doubly linked list stores two references: • next link, which points to the next node in the list, • and a prev link, which points to the previous node in the list.  Page 12

  13. DOUBLY LINKED LISTS Header and Trailer Sentinels • To simplify programming, it is convenient to add special nodes at both ends of a doubly linked list: • A header node just before the head of the list, • and a trailer node just after the tail of the list. • These "dummy" or sentinel nodes do not store any elements. The header has a valid next reference but a null prev reference, while the trailer has a valid prev reference but a null next reference.  Page 13

  14. DOUBLY LINKED LISTS Removing the node at the end of a doubly linked list • Inserting or removing elements at either end of a doubly linked list is straight- forward to do. • Indeed, the prev links eliminate the need to traverse the list to get to the node just before the tail.  Page 14

  15. DOUBLY LINKED LISTS Adding an element at the front • Likewise, we can easily perform an insertion of a new element at the beginning of a doubly linked list, as shown in the following Figure.  Page 15

  16. DOUBLY LINKED LISTS Insertion in the Middle of a Doubly Linked List • Given a node v of a doubly linked list (which could be possibly the header but not the trailer), we can easily insert a new node z immediately after v. • Specifically, let w the be node following v. We execute the following steps: • make z's prev link refer to v • make z's next link refer to w • make w's prev link refer to z • make v's next link refer to z  Page 16

  17. DOUBLY LINKED LISTS Removal in the Middle of a Doubly Linked List • We access the nodes u and w on either side of v using v's getPrev and getNext methods. • To remove node v, we simply have u and w point to each other instead of to v. • We refer to this operation as the linking out of v. • We also null out v's prev and next pointers so as not to retain old references into the list.  Page 17

  18. Circularly Linked Lists Circularly Linked Lists • A circularly linked list has the same kind of nodes as a singly linked list. • But there is no head or tail in a circularly linked list. • For instead of having the last node's next pointer be null, in a circularly linked list, it points back to the first node. • Even though a circularly linked list has no beginning or end, we nevertheless need some node to be marked as a special node, which we call the cursor.  Page 18

  19. Circularly Linked Lists Circularly Linked Lists • We can then define some simple update methods for a circularly linked list: • add(v): Insert a new node v immediately after the cursor; if the list is empty, then v becomes the cursor and its next pointer points to itself. • remove(): Remove and return the node v immediately after the cursor (not the cursor itself, unless it is the only node); if the list becomes empty, the cursor is set to null. • advance(): Advance the cursor to the next node in the list. • Look at the Java implementation of a circularly linked list Page No. 153 (Text Book)  Page 19

  20. Circularly Linked Lists Sorting a Linked List Following is the pseudo code for the insertion-sort algorithm for a doubly linked list.  Page 20

More Related