Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 1031 Linked Lists Definition of Linked Lists Examples of Linked Lists Operations on Linked Lists Linked List as a Class Linked Lists as Implementations.

Similar presentations


Presentation on theme: "CS 1031 Linked Lists Definition of Linked Lists Examples of Linked Lists Operations on Linked Lists Linked List as a Class Linked Lists as Implementations."— Presentation transcript:

1 CS 1031 Linked Lists Definition of Linked Lists Examples of Linked Lists Operations on Linked Lists Linked List as a Class Linked Lists as Implementations of Stacks, Sets, etc.

2 CS 1032 Definition of Linked Lists A linked list is a sequence of items (objects) where every item is linked to the next. Graphically: data head_ptr tail_ptr

3 CS 1033 Definition Details Each item has a data part (one or more data members), and a link that points to the next item One natural way to implement the link is as a pointer; that is, the link is the address of the next item in the list It makes good sense to view each item as an object, that is, as an instance of a class. We call that class: Node The last item does not point to anything. We set its link member to NULL. This is denoted graphically by a self-loop

4 CS 1034 Examples of Linked Lists (A Waiting Line) A waiting line of customers: John, Mary, Dan, Sue (from the head to the tail of the line) A linked list of strings can represent this line: JohnMaryDanSue head_ptr tail_ptr

5 CS 1035 Examples of Linked Lists (A Stack of Numbers) A stack of numbers (from top to bottom): 10, 8, 6, 8, 2 A linked list of ints can represent this stack: 10862 head_ptr tail_ptr 8

6 CS 1036 Examples of Linked Lists ( A Set of Non-redundant Elements ) A set of characters: a, b, d, f, c A linked list of chars can represent this set: abdc head_ptr tail_ptr f

7 CS 1037 Examples of Linked Lists ( A Sorted Set of Non-redundant Elements ) A set of characters: a, b, d, f, c The elements must be arranged in sorted order: a, b, c, d, f A linked list of chars can represent this set: abcf head_ptr tail_ptr d

8 CS 1038 Examples of Linked Lists ( A Polynomial ) A polynomial of degree n is the function P n (x)=a 0 +a 1 x+a 2 x 2 +…+a n x n. The a i ’s are called the coefficients of the polynomial The polynomial can be represented by a linked list ( 2 data members and a link per item ): a 0,0a 1,1a 2,2a n,n head_ptr tail_ptr

9 CS 1039 Operations on Linked Lists Insert a new item – At the head of the list, or – At the tail of the list, or – Inside the list, in some designated position Search for an item in the list – The item can be specified by position, or by some value Delete an item from the list – Search for and locate the item, then remove the item, and finally adjust the surrounding pointers size( ); isEmpty( )

10 CS 10310 Insert– At the Head Insert a new data A. Call new: newPtr List before insertion: After insertion to head: data head_ptrtail_ptr Adata head_ptr tail_ptr A The link value in the new item = old head_ptr The new value of head_ptr = newPtr

11 CS 10311 Insert – at the Tail Insert a new data A. Call new: newPtr List before insertion After insertion to tail: data head_ptrtail_ptr Adata head_ptr tail_ptr A The link value in the new item = NULL The link value of the old last item = newPtr

12 CS 10312 Insert – inside the List Insert a new data A. Call new: newPtr List before insertion: After insertion in 3 rd position: data head_ptrtail_ptr data A head_ptr tail_ptr data The link-value in the new item = link-value of 2 nd item The new link-value of 2 nd item = newPtr

13 CS 10313 Delete – the Head Item List before deletion: List after deletion of the head item: data head_ptr tail_ptr data head_ptr tail_ptr data The new value of head_ptr = link-value of the old head item The old head item is deleted and its memory returned data

14 CS 10314 Delete – the Tail Item List before deletion: List after deletion of the tail item: data head_ptr tail_ptr data head_ptr tail_ptr New value of tail_ptr = link-value of the 3 rd from last item New link-value of new last item = NULL. data

15 CS 10315 Delete – an inside Item List before deletion: List after deletion of the 2 nd item: data head_ptr tail_ptr data head_ptr tail_ptr New link-value of the item located before the deleted one = the link-value of the deleted item data

16 CS 10316 size() and isEmpty() We need to scan the items in the list from the head_ptr to the last item marked by its link-value being NULL Count the number of items in the scan, and return the count. This is the size(). Alternatively, keep a counter of the number of item, which gets updated after each insert/delete. The function size( ) returns that counter If head_ptr is NULL, isEmpty() returns true; else, it returns false.

17 CS 10317 Searching for an Item Suppose you want to find the item whose data value is A You have to search sequentially starting from the head item rightward until the first item whose data member is equal to A is found. At each item searched, a comparison between the data member and A is performed.


Download ppt "CS 1031 Linked Lists Definition of Linked Lists Examples of Linked Lists Operations on Linked Lists Linked List as a Class Linked Lists as Implementations."

Similar presentations


Ads by Google