Download presentation
Presentation is loading. Please wait.
Published byHarold Fox Modified over 9 years ago
1
September 05 Kraemer UGA/CSCI 2720 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. Length of the list: |L| –Read “cardinality of L” –| | = n –Can be any non-negative number L[i] –ith element of list L, provided 0 <= i <= |L|
4
Lists We’ll define several types of lists, each with their own ADT Common operations: –Access(L,i) –Length(L) –Concat(L 1,L 2 ) –MakeEmptyList() –IsEmptyList(L)
5
Access(L,i) Return L[i] –Return error if i out of range i < 0 i > |L| - 1
6
Length(L) return | L |
7
Concat(L 1,L 2 ) Return the result of concatenating L 1 with L 2 If L 1 = and L 2 =, then Concat (L 1, L 2 ) returns the combined list
8
MakeEmptyList() Returns the empty list <>
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 A Stack Applet example 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 don’t 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 Return Top(L) and replace L with Error results if L is empty
16
Push(x,L) Add x at the end of L Replace L by Concat(L, )
17
MakeEmptyStack() Return the empty list <> 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 A queue applet 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, ) O(1)
24
Dequeue(L) Remove and return the first element of L Replace L by and return L[0] Error results if L is empty
25
Front(L) Return the first element of L Return L[0] Error results if L is empty
26
MakeEmptyQueue() Return the empty list <>
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.