Kraemer - PowerPoint PPT Presentation

1 / 29
About This Presentation
Title:

Kraemer

Description:

Can be modified only by adding and removing items at one end. Queue ... 'insertion or enqueue', which is done at the end of the queue called 'rear' ... – PowerPoint PPT presentation

Number of Views:20
Avg rating:3.0/5.0
Slides: 30
Provided by: eileenk2
Category:
Tags: end | kraemer | rear

less

Transcript and Presenter's Notes

Title: Kraemer


1
Lists Part I
  • CSCI 2720
  • Eileen Kraemer
  • The University of Georgia

2
ADTs
  • ADT abstract data type
  • a mathematical specification of a set of data and
    the set of operations that can be performed on
    the data.
  • the focus is on the definitions of the
    constructor that returns an abstract handle that
    represents the data, and the various operations
    with their arguments.
  • actual implementation is not defined

3
The List ADT
  • List ordered sequence of elements ltx0, ,
    xn-1gt.
  • Length of the list L
  • Read cardinality of L
  • ltx0, , xn-1gt n
  • Can be any non-negative number
  • Li
  • ith element of list L,
  • provided 0 lt i lt L

4
Lists
  • Well define several types of lists, each with
    their own ADT
  • Common operations
  • Access(L,i)
  • Length(L)
  • Concat(L1,L2)
  • MakeEmptyList()
  • IsEmptyList(L)

5
Access(L,i)
  • Return Li
  • Return error if i out of range
  • i lt 0
  • i gt L - 1

6
Length(L)
  • return L

7
Concat(L1,L2)
  • Return the result of concatenating L1 with L2
  • If L1 ltx0, , xn-1gt and L2 lty0, , ym-1gt,
    then Concat (L1, L2) returns the combined list
  • ltx0, , xn-1,y0, , ym-1gt

8
MakeEmptyList()
  • Returns the empty list ltgt

9
IsEmptyList(L)
  • Returns true if l 0
  • Otherwise returns false

10
Special Types of Lists
  • Stack
  • Can be modified only by adding and removing items
    at one end
  • Queue
  • Can be modified only by adding items at one end
    and removing them at the other

11
Stacks
  • A Stack Applet example

12
Stacks
  • Push add new item at the top
  • Pop remove item from the top
  • Top peek at item on the top, but dont remove
    it
  • LIFO lists last in, first out
  • used to implement recursion, reversing of
    strings, bracket-checking, more

13
Stack ADT
  • Top(L)
  • Pop(L)
  • Push(x,L)
  • MakeEmptyStack()
  • IsEmptyStack(L)

14
Top(L)
  • Return last element of L
  • Same as Access(L, L -1)
  • Error results if L is empty

15
Pop(L)
  • Remove and return last element of L
  • Return Top(L) and replace L with ltL0, .LL
    -2gt
  • Error results if L is empty

16
Push(x,L)
  • Add x at the end of L
  • Replace L by Concat(L,ltxgt)

17
MakeEmptyStack()
  • Return the empty list ltgt
  • O(1)

18
IsEmptyStack(L)
  • Return true if L 0
  • Otherwise return false

19
UML for Stack Class
20
Queue ADT
  • similar to stack, except that the first item to
    be inserted is the first one to be removed.
  • This mechanism is called First-In-First-Out
    (FIFO).
  • Placing an item in a queue is called insertion
    or enqueue, which is done at the end of the
    queue called rear.
  • Removing an item from a queue is called deletion
    or dequeue, which is done at the other end of
    the queue called front.
  • Some of the applications are printer queue,
    keystroke queue, etc.

21
Queue Example
  • A queue applet

22
Queue ADT
  • Enqueue(x,L)
  • Dequeue(L)
  • Front(L)
  • MakeEmptyQueue()
  • IsEmptyQueue(L)

23
Enqueue(x,L)
  • Add x at the end of L
  • Replace L by Concat(L,ltxgt)
  • O(1)

24
Dequeue(L)
  • Remove and return the first element of L
  • Replace L by ltL1 LL-1gt and return L0
  • Error results if L is empty

25
Front(L)
  • Return the first element of L
  • Return L0
  • Error results if L is empty

26
MakeEmptyQueue()
  • Return the empty list ltgt

27
IsEmptyQueue(L)
  • Return true if L 0
  • Otherwise return false

28
UML for Queue class
29
List representations
  • Contiguous memory representations
  • Elements stored in table of fixed size (greater
    than max_length)
  • Adjacent items in list are adjacent in storage
  • Linked representations
  • Elements can be scattered in memory
  • Elements carry pointers to next element in list
  • Pro easy to insert/delete
  • Con need to follow links to access
Write a Comment
User Comments (0)
About PowerShow.com