Lists - PowerPoint PPT Presentation

Loading...

PPT – Lists PowerPoint presentation | free to download - id: 19fea8-NGE3N



Loading


The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
About This Presentation
Title:

Lists

Description:

Empty Check if it is empty. Traverse Go through list, process elements in order stored ... Carve it up into pieces of size required for a node of a list T . 34 ... – PowerPoint PPT presentation

Number of Views:33
Avg rating:3.0/5.0
Slides: 41
Provided by: steve1791
Learn more at: http://www.letu.edu
Category:
Tags: carve | lists

less

Write a Comment
User Comments (0)
Transcript and Presenter's Notes

Title: Lists


1
Lists
  • Chapter 8

2
Linked Lists
  • As an ADT, a list is
  • finite sequence (possibly empty) of elements
  • Operations commonly include
  • Construction Allocate initialize object
  • Empty Check if it is empty
  • Traverse Go through list, process elements in
    order stored
  • Insert Add an item to list at any point
  • Delete Remove an item from the list at any
    point

3
Array/Vector Implementation of List
  • Data Members
  • Store the list items in consecutive array or
    vector locations

a1, a2, a3 , . . . an?
? ? ? a0 a1
a2 ... an-1, an ...
aCAPACITY-1 For an array, add a mySize member
to store the length (n) of the list.
4
Array/Vector Implementation of List
  • Operations

Construction Set mySize to 0 if run-time
array, allocate memory for it For vector let
its constructor do the work Empty mySize
0 For vector Use its empty() operation
5
Array/Vector Implementation of List
  • Traverse
  • or

for (int i 0 i lt size i) Process(ai)

i 0while (i lt size) Process(ai) i
6
Array/Vector Implementation of List
  • Insert
  • Delete

Insert 6 after 5 in 3, 5, 8, 9, 10, 12, 13, 15
3, 5, 6, 8, 9, 10, 12, 13, 15 Have to
shift array elements to make room.
Delete 5 from 3, 5, 6. 8, 9, 10, 12, 13, 15
3, 6, 8, 9, 10, 12, 13, 15 Have to
shift array elements to close the gap.
7
Array/Vector Implementation of List
  • This implementation of lists is inefficient for
    dynamic lists
  • Those that change frequently
  • Those with many insertions and deletions
  • So
  • We look for an alternative implementation.

8
Linked List
  • For the array/vector-based implementation
  • First element is at location 0
  • Successor of item at location i is at location i
    1
  • End is at location size 1
  • Fix
  • Remove requirement that list elements be stored
    in consecutive location.
  • But then need a "link" that connects each element
    to its successor

Linked Lists !!
9
Linked Lists
  • Definition ltgt A linked list of self-referential
    class objects
  • called nodes
  • connected by pointer links (thus, a "linked"
    list)
  • Subsequent nodes accessed via link-pointer member
    stored in each member
  • Link pointer in last node set to null (zero)
  • marks the end of the list
  • Nodes created dynamically, as needed

10
Self-Referential Classes
  • A self-referential class contains a pointer
    member that points to a class object of the same
    class type

class Part_node public Part_node ( )
private char part_num8, descrip20
int qty float price Part_node
next_part
11
Self-Referential Classes
  • This pointer to an object of the type being
    declared enables class objects to be linked
    together in various ways
  • This is how we get linked lists

0
12
Dynamic Memory Allocation
  • If the data structures are to be dynamic, then
    dynamic memory allocation is required
  • operators new and delete are essential

part_node newPtr new part_node
0
13
Dynamic Memory Allocation
  • The delete operator deallocates memory allocated
    with the new
  • Note newPtr is not itself deleted -- rather the
    space newPtr points to

delete newPtr

14
Linked Lists Operations
  • Construction first null_value
  • Empty first null_value?
  • Traverse

ptr first while (ptr ! null_value)
Process data part of node pointed to by ptr ptr
next part of node pointed to by ptr
15
Traverse
ptr first while (ptr ! null_value) Process
data part of node pointed to by ptr ptr
next part of node pointed to by ptr
16
Operations Insertion
9
17
26
34
22
first
  • Insertion
  • To insert 20 after 17
  • Need address of item before point of insertion
  • predptr points to the node containing 17
  • Get a new node pointed to by newptr and store 20
    in it
  • Set the next pointer of this new node equal to
    the next pointer in its predecessor, thus making
    it point to its successor.
  • Reset the next pointer of its predecessor to
    point to this new node

And voila! The node is inserted (linked) into
the list
17
Operations Insertion
  • Note insertion also works at end of list
  • pointer member of new node set to null
  • Insertion at the beginning of the list
  • predptr must be set to first
  • pointer member of newptr set to that value
  • first set to value of newptr
  • ? Note In all cases, no shifting of list
    elements is required !

18
Operations Deletion
ptr
5
17
22
29
34
20
9
first
  • Delete node containing 22 from list.
  • Suppose ptr points to the node to be deleted
  • predptr points to its predecessor (the 20)
  • Do a bypass operation
  • Set the next pointer in the predecessor to point
    to the successor of the node to be deleted
  • Deallocate the node being deleted.

19
Linked Lists - Advantages
  • Access any item as long as external link to first
    item maintained
  • Insert new item without shifting
  • Delete existing item without shifting
  • Can expand/contract as necessary

20
Linked Lists - Disadvantages
  • Overhead of links
  • used only internally, pure overhead
  • If dynamic, must provide
  • destructor
  • copy constructor
  • No longer have direct access to each element of
    the list
  • Many sorting algorithms need direct access
  • Binary search needs direct access
  • O(1) access becomes O(n) access
  • must go through first element, and then second,
    and then third, etc.

21
Linked Lists - Disadvantages
  • List-processing algorithms that require fast
    access to each element cannot be done as
    efficiently with linked lists.
  • Consider adding an element at the end of the list

22
Using C Pointers and Classes
  • To Implement Nodes
  • class Node public
  • DataType data
  • Node next
  • Note The definition of a Node is recursive
  • (or self-referential)
  • It uses the name Node in its definition
  • The next member is defined as a pointer to a Node

23
Working with Nodes
  • Declaring pointers Node ptr or typedef
    Node NodePointer NodePointer ptr
  • Allocate and deallocate ptr new Node delete
    ptr
  • Access the data and next part of node(ptr).data
    and (ptr).nextor ptr-gtdata and
    ptr-gtnext

24
Working with Nodes
class Node public DataType data
Node next
  • Note data members are public
  • This class declaration will be placed inside
    another class declaration for LinkedList
  • The data members data and next of struct Node
    will be public inside the class
  • will accessible to the member and friend
    functions
  • will be private outside the class

25
Class Template LinkedList
  • template lttypename DataTypegtclass LinkedList
    private class Node public DataType
    data Node next typedef Node
    NodePointer . . .
  • data is public inside class Node
  • class Node is private inside LinkedList

26
Data Members for LinkedList
  • A linked list will be characterized by
  • A pointer to the first node in the list.
  • Each node contains a pointer to the next node in
    the list
  • The last node contains a null pointer
  • As a variation first may
  • be a structure
  • also contain a count of the elements in the list

27
Function Members for LinkedList
  • Constructor
  • Make first a null pointer and
  • set mySize to 0
  • Destructor
  • Nodes are dynamically allocated by new
  • Default destructor will not specify the delete
  • All the nodes from that point on would be
    "marooned memory"
  • A destructor must be explicitly implemented to do
    the delete

28
Function Members for LinkedList
L
9
17
22
26
34
first
mySize
5
copyOfL
first
mySize
5
  • Copy constructor
  • By default, when a copy is made of a LinkedList
    object, it only gets the head pointer
  • Copy constructor will make a new linked list of
    nodes to which copyOfL will point

29
Variations
  • An empty head node
  • Every node has a predecessor
  • Does away with special case insertions
  • An empty trailer node
  • Every node has a successor
  • Doubly linked list

30
The STL listltTgt Class Template
  • A sequential container
  • Optimized for insertion and erasure at arbitrary
    points in the sequence.
  • Implemented as a circular doubly-linked list with
    head node.

31
The STL listltTgt Class Template
  • Node structure
  • struct list_node
  • pointer next, prev T data

32
The STL listltTgt Class Template
  • But it's allo/deallo-cation scheme is complex
  • Does not simply using new and delete operations.
  • Using the heap manager is inefficient for large
    numbers of allo/deallo-cations
  • Thus it does it's own memory management.

33
The STL listltTgt Memory Management
  • When a node is allocated
  • If there is a node on the free list, allocate it.
  • This is maintained as a linked stack
  • If the free list is empty
  • Call the heap manager to allocate a block of
    memory (a "buffer", typically 4K)
  • Carve it up into pieces of size required for a
    node of a listltTgt.

34
The STL listltTgt Memory Management
  • When a node is deallocated
  • Push it onto the free list.
  • When all lists of this type T have been destroyed
  • Return it to the heap

35
Comparing Listlttgt With Other Containers
  • Note listltTgt does not support direct access
  • does not have the subscript operator .

36
listlttgt Iterators
  • listltTgt's iterator is "weaker" than that for
    vectorltTgt.
  • vectorltTgt random access iterators
  • listltTgt bidirectional iterators
  • Operations in common
  • Move iterator to next element (like ptr
    ptr-gt next)
  • -- Move iterator to preceding element (like
    ptr ptr-gt prev)
  • dereferencing operator (like ptr-gt data)

37
listlttgt Iterators
  • Operators in common
  • assignment (for same type iterators)
    it1 it2 makes it1 positioned at same
    element as it2
  • and ! (for same type iterators)
    checks whether iterators are positioned at the
    same element

38
Using listlttgt Iterators
  • Example Construct a list containing first 4
    even integers then output the list.
  • listltintgt l
  • for (int i 1 i lt 4 i) l.push_back(2i)
  • for (listltintgtiterator it l.begin()
    it ! l.end() it)
  • cout ltlt it ltlt " "
  • cout ltlt endl

39
Limitations of listlttgt Iterators
  • Directional iterators don't have , -, ,
    -,
  • listlttgt iterators cannot do "jumping"
  • No iterator n
  • No direct access
  • Result, cannot implement some sort() algorithms
  • Solution listltTgt has it's own sort() operation

40
Basic listlttgt Operations
  • See page 451,2
  • Constructors
  • Destructors
  • Empty, Size
  • Push, insert, pop, remove
  • Front, back
  • Iterator functions
  • begin, end,
  • Sort
  • Merge, splice
  • Comparisons
About PowerShow.com