Lists - PowerPoint PPT Presentation

1 / 31
About This Presentation
Title:

Lists

Description:

Lists List: finite sequence of data elements all elements have the same data type The operations depend on the type of the list and not on the data type – PowerPoint PPT presentation

Number of Views:58
Avg rating:3.0/5.0
Slides: 32
Provided by: TheGre5
Category:
Tags: lists | queues | stacks

less

Transcript and Presenter's Notes

Title: Lists


1
Lists
  • List finite sequence of data elements
  • all elements have the same data type
  • The operations depend on the type of the list and
    not on the data type
  • List the most general type
  • Ordered elements is ascending order
  • Stacks, Queues not all operations are allowed

2
(No Transcript)
3
(No Transcript)
4
(No Transcript)
5
(No Transcript)
6
Terminology
  • empty contains no elements
  • length number of elements in list
  • head, or list pointer to the beginning of the
    list
  • tail pointer to the last element
  • current pointer to current element
  • ordered elements in ascending or descending order

7
Operations
  • setFirst set current to head
  • setPos(i) sets current to the i-th element
  • currValue returns value of current element
  • next/prev current points to next/previous
    element
  • clear delete all elements
  • insert inserts an element at/after current
    position
  • append inserts an element at tail
  • remove delete the element at/after current
    position
  • isInList true/false if current position is in
    list
  • isEmpty true/false if list is empty

8
ADT List
  • interface List // List ADT in Java
  • public void clear() // Remove all Objects
  • public void insert(Object item) // Insert at
    curr pos
  • public void append(Object item) // Insert at
    tail
  • public Object remove() // Remove/return curr
  • public void setFirst() // Set to first pos
  • public void next() // Move to next pos
  • public void prev() // Move to prev pos
  • public int length() // Return curr length
  • public void setPos(int pos) // Set curr
    position
  • public void setValue(Object val) // Set
    current value
  • public Object currValue() // Return curr
    value
  • public boolean isEmpty() // True if empty
    list
  • public boolean isInList() // True if in list
  • // interface List

9
Iteration
  • Iterate through the whole list MyList
  • for (MyList.first( ) MyList.isInList( )
    MyList.next( ))
  • DoSomething(MyList.currValue( ))
  • If MyList (12 32 15) and current points to 32
    then MyList.insert(90) changes the list to be (12
    32 90 15)

10
List Implementations
  • Array-based very fast
  • the elements are stored in array
  • static actual number of elements less than size
    allocated
  • Dynamic Memory slower, more efficient
  • allocates memory for new elements
  • dynamic no restriction on number of elements
    (except memory size)

11
Array Implementation (1)
  • Elements in continuous array positions
  • Head of list at pos 0
  • Insertion and Deletion cause shifting of elements

12
  • class AList implements List // Array-based
    list class
  • private static final int defaultSize 10
  • private int msize // Maximum size of list
  • private int numInList // Actual list size
  • private int curr // Position of curr
  • private Object listArray // Array holding
    list
  • AList() setup(defaultSize) // Constructor
  • AList(int sz) setup(sz) // Constructor
  • private void setup(int sz) // Do
    initializations
  • msize sz
  • numInList curr 0
  • ListArray new Objectsz // Create
    listArray
  • public void clear() // Remove all Objects from
    list

13
public void insert(Object it) // Insert at
curr pos Assert.notFalse(numInList lt msize,
"List is full") Assert.notFalse((curr gt0)
(curr lt numInList), "Bad value for curr")
for (int inumInList igtcurr i--) // Shift
up listArrayi listArrayi-1
listArraycurr it numInList //
Increment list size public void
append(Object it) // Insert at tail
Assert.notFalse(numInList lt msize, "List is
full") listArraynumInList it //
Increment list size public Object remove()
// Remove and return Object
Assert.notFalse(!isEmpty(), "No delete list
empty") Assert.notFalse(isInList(), "No
current element") Object it
listArraycurr // Hold removed Object for
(int icurr iltnumInList-1 i) // Shift down
listArrayi listArrayi1
numInList-- // Decrement list size
return it
14
public void setFirst() curr 0 // Set to
first public void prev() curr-- // Move
curr to prev public void next() curr //
Move curr to next public int length() return
numInList public void setPos(int pos) curr
pos public boolean isEmpty() return
numInList 0 public void setValue(Object
it) // Set current value Assert.notFalse(isIn
List(), "No current element") listArraycurr
it public boolean isInList() // True if
curr within list return (curr gt 0) (curr lt
numInList) // Array-based list
implementation
15
Array Implementation (2)
16
(No Transcript)
17
1 26 0
2 11 10
3 5 16
4 1 25
5 17 1
6 13 2
7
8 19 19
9 14 13
10 4 22
11
12 31 8
13 6 3
14
15
16 37 24
17 3 12
18
19 32 0
20
21 7 9
22 15 0
23
24 12 0
25 18 6
18
  • class AList implements List // Array-based
    list class
  • private static final int defaultSize 10
  • private int msize // Maximum size of list
  • private int numInList // Actual list size
  • private int curr // Position of curr
  • private int avail // next available position
  • private Object listArray // Array holding
    list
  • private int listarray_next // array holding
    pointers to next Object
  • private void setup(int sz) // Do
    initializations
  • msize sz
  • numinlist curr 0
  • listarray new Objectsz
  • listarray_next new intsz
  • avail 0 // the first available element
  • for (i0 i lt msize i)
  • listarray_nexti i1 // each elem
    points to its successor
  • listarray_nextmsize-1 nothing // the
    last elem has no next

19
  • private int get_node( ) // Get next
    available node
  • if (avail nothing) // from
    stack
  • error(list overflow)
  • else
  • int pos avail
  • avail listarray_nextavail
  • return pos
  • private void free_node (int p) // make node
    available
  • listarray_nextp avail // push
    node back to stack
  • avail p
  • private void insert(int p, Object x) // insert
    after node pointed to by p
  • if (p lt 0 p gt msize)
  • error (void insertion)
  • else

20
  • private void delete(int p, Object x)
  • if ( p gt 0 p gt msize) //
    deletes elem after elem
  • error (void deletion) //
    pointed to by p
  • else
  • int q listarray_nextp
  • x listarrayq
  • listarray_nextp listarray_nextq
  • free_node(q)
  • public AList() setup(defaultSize) //
    Constructor
  • public AList(int sz) setup(sz) //
    Constructor
  • public void clear( ) // remove all ELEMs
    from list
  • public void insert(Object it) // insert ELEM
    at current position
  • public void append(Object it) // insert ELEM
    at tail of list
  • public Object remove( ) // remove and return
    current ELEM
  • public void setFirst( ) // set curr to first
    position
  • public void prev( ) // move curr to previous
    position

21
Dynamic Memory
  • Allocates memory for new elements as needed
  • Each node is a distinct object
  • The node class
  • class Link // A linked-list node
  • private Object element // Object for this
    node
  • private Link next // Pointer to next node
  • Link(Object it, Link nextval) // Constructor
  • element it next nextval
  • Link(Link nextval) next nextval //
    Constructor
  • Link next() return next
  • Link setNext(Link nextval) return next
    nextval
  • Object element() return element
  • Object setElement(Object it) return element
    it

22
  • class LList implements List // Linked list
    class
  • private Link head // Pointer to list header
  • private Link tail // Pointer to last Object
    in list
  • protected Link curr // Position of current
    Object
  • LList(int sz) setup() // Constructor
  • LList() setup() // Constructor
  • private void setup()
  • tail head curr new Link(null)
  • public void setFirst() curr head
  • public void next() if (curr ! null) curr
    curr.next()
  • public void prev() // Move to previous
    position
  • Link temp head
  • if ((curr null) (curr head)) // No
    prev
  • curr null return // so return
  • while ((temp ! null) (temp.next() !
    curr))
  • temp temp.next()

23
  • public Object currValue() // Return current
    Object
  • if (!isInList()) return null
  • return curr.next().element()
  • public boolean isEmpty() // True if list is
    empty
  • return head.next() null
  • public void insert(Object it) // Insert Object
    at current position
  • Assert.notNull(curr, "No current element")
  • curr.setNext(new Link(it, curr.next()))
  • if (tail curr) // Appended new Object
  • tail curr.next()
  • public Object remove() // Remove/return curr
    Object
  • if (!isInList()) return null
  • Object it curr.next().element() // Remember
    value
  • if (tail curr.next()) tail curr // Set
    tail

24
  • public void append(Object it) // insert Elem at
    tail of list
  • tail.setNext(new Link(it, NULL))
  • public int length( ) // return current length
    of list
  • int cnt 0
  • for (Link temp head.next() temp ! NULL
    temp temp.next())
  • cnt // count Elems
  • return cnt
  • public void setPos(int pos) // set curr to
    position
  • curr head
  • for (int i 0 (curr ! NULL) (i lt pos)
    i) curr curr.next()
  • public void setValue(Object val) // set
    current Elem's value
  • Assert.notFalse(isInList(), "No current
    element")
  • curr.next().setElement(val)

25
Comparison
  • Array-Based Lists
  • insertion and deletion are ?(n)
  • prev and direct access are ?(1)
  • fixed space allocated in advance
  • space reorganization if the array is full
  • faster in most cases
  • Linked Lists
  • insertion and deletion are ?(1)
  • prev and direct access are ?(n)
  • space grows with number of elements
  • every element requires overhead
  • slower

26
Doubly Linked List
  • Allows for direct access to both next and
    previous elements of the current pointer
  • insert (delete) operations update both next and
    prev pointers
  • easy implementation

27
Doubly linked list node
  • class DLink // A doubly-linked list node
  • private Object element // Object for this
    node private
  • DLink next // Ptr to next node
  • DLink prev // Ptr to previous node
  • DLink(Object it, DLink n, DLink p) // Constr.
    1
  • element it next n prev p
  • DLink(DLink n, DLink p) next n prev p
    // Constr. 2
  • DLink next() return next
  • DLink setNext(DLink nextval) return next
    nextval
  • DLink prev() return prev
  • DLink setPrev(DLink prevval) return prev
    prevval
  • Object element() return element
  • Object setElement(Object it) return element
    it

28
Insertion in Doubly Linked List
29
Deletion in Doubly Linked List
current
current
30
Insertion
  • curr.setNext(new DLink(it, curr.next(), curr))
  • if (curr.next().next() ! null)
  • curr.next().next().setPrev(curr.next())
  • if (tail curr) // Appended new Object
  • tail curr.next()

31
Circular Linked Lists
  • The next pointer of the last element points to
    the first element
Write a Comment
User Comments (0)
About PowerShow.com