0% found this document useful (0 votes)
92 views24 pages

Adts and List As Adt

The document discusses lists as an abstract data type (ADT). It defines a list as a collection of homogeneous elements and describes key operations on lists like creation, destruction, insertion, deletion and printing of elements. It also discusses different implementations of lists, specifically sequential/array-based implementations where elements are stored in adjacent array slots. The C++ implementation of array-based lists is demonstrated including functions for checking if empty/full and inserting elements at the tail or specific positions.

Uploaded by

Syeda Fatima
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
92 views24 pages

Adts and List As Adt

The document discusses lists as an abstract data type (ADT). It defines a list as a collection of homogeneous elements and describes key operations on lists like creation, destruction, insertion, deletion and printing of elements. It also discusses different implementations of lists, specifically sequential/array-based implementations where elements are stored in adjacent array slots. The C++ implementation of array-based lists is demonstrated including functions for checking if empty/full and inserting elements at the tail or specific positions.

Uploaded by

Syeda Fatima
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

ADTs and List as ADT

 Abstract Data Types


 What is a List
 List as Abstract Data Type (ADT)
 Operations on List
 Sequential implementation of List

Data Structures & Algorithms Lecture 3: Review C++


Abstract Data Types (ADTs)

 Data storage & operations encapsulated by an ADT.


 ADT specifies permitted operations as well as time and space
guarantees.
 User unconcerned with how it’s implemented
► but we are concerned with implementation in this class
 ADT is a concept :
► not something that directly appears in your code
► programming language may provide support for communicating
ADT to users
► (e.g. classes in Java & C++)

Data Structures & Algorithms Lecture 3: Review C++


What is List Data Structure?

 We use Lists all the time


 List of addresses
 List of things to do

 List of guests for a party

 Grocery list

 And so on

List as an ADT
A list is a collection of homogenous elements

Data Structures & Algorithms Lecture 3: Review C++


A Sequential List

 Length of list is 4.
 Maximum number of elements is 6.
 Remember array and list are different things; array can be
used only as a data container for storing list elements

Data A B C D
Index 0 1 2 3 4 5

Data Structures & Algorithms Lecture 3: Review C++


What is List Data Structure?

 At the logical level, each element in the list except


the first one has a unique predecessor and each
element except the last one has a unique
successor.

 Lists can be unordered; elements may be placed


into a list in no particular order.

 List may be ordered in different ways

Data Structures & Algorithms Lecture 3: Review C++


What is List Data Structure

 Lists can also be ordered by value.


► A list of names can be ordered alphabetically.
► A list of grades can be ordered numerically.

 Elements of the list may be records (structures).


► In that case, a list may be ordered on the basis of one or more
fields of a record.
► For example, a list of student records may be ordered based on
their first name or their roll number field.

 The field which is used to sort a list of records, is called


key; such a list is called key ordered list.

Data Structures & Algorithms Lecture 3: Review C++


Operations on the Elements of
List ADT

Data Structures & Algorithms Lecture 3: Review C++


List ADT - Operations

 CreateList (MAX_ELEMENTS)
► Function : Initializes List to empty state

► Input : MAX_ELEMENTS

► Precondition : None

► Output : List

► Post conditions : List exists and is empty

Data Structures & Algorithms Lecture 3: Review C++


List ADT - Operations

 DestroyList (List)
► Function : Destroys all elements , leaving List in empty state.

► Input : List

► Preconditions : List has been created

► Output : Empty List

► Post Conditions : List is empty

Data Structures & Algorithms Lecture 3: Review C++


List ADT - Operations

 EmptyList(List) : Returns Boolean value


► Function : Determines whether List is empty

► Input : List

► Preconditions : List has been created.

► Output : true/false

► Post Conditions : value = Empty()

Data Structures & Algorithms Lecture 3: Review C++


List ADT - Operations

 isFull(List) : Returns Boolean value


► Function : Determines whether List is full.

► Input : List

► Preconditions : List has been created.

► Output : true/false

► Post conditions : value = isFull()

Data Structures & Algorithms Lecture 3: Review C++


List ADT - Operations

 InsertElement ( List , NewElement)


► Function : Adds NewElement to List.

► Input : List
NewElement(ListElementType)

► Preconditions : List is initialized, list is not full and the


NewElement does not already exist in the list.

► Output : List

► Post Conditions : List = Original list + NewElement

Data Structures & Algorithms Lecture 3: Review C++


List ADT - Operations

 ModifyElement(List, ModElement)
► Function : Replace existing list element with same key as
ModElement.

► Input : List,
ModElement(ListElementType)

► Preconditions : Element with same key as ModElement exists


in List.

► Output : List

► Post Conditions: List = original list with value of ModElement


replacing an existing element such that the key value of
ModElement matches the replaced element.

Data Structures & Algorithms Lecture 3: Review C++


List ADT - Operations

 DeleteElement(List,DeleteVal)
► Function : Deletes the element containing the key
DeleteVal from list.

► Input : List
DeleteVal(KeyType)

► Preconditions : Element with key of DeleteVal is in the


list and only appears once

► Output : List

► Postconditions : List = original list with DeleteVal


removed.

Data Structures & Algorithms Lecture 3: Review C++


List ADT - Operations

 PrintList(List)
► Function : Prints all the element in List in order from
smallest to largest key value.

► Input : List

► Preconditions : List has been created.

► Output : List elements ( to standard output)

► Postconditions : List elements have been printed in


order from smallest to largest key value. List is
unchanged.

Data Structures & Algorithms Lecture 3: Review C++


Implementations of List ADT

 Array-based implementation
 Linked-list based implementation
► Singly linked list
► Circular linked list
► Doubly linked list

Data Structures & Algorithms Lecture 3: Review C++


Sequential/Array-based Implementation of the List ADT
 In this implementation list elements are stored
sequentially, in adjacent slots in an array.

 Array-based list implementations are so


common that many people refer to an “array” of
data when they really mean a “list”.

Data Structures & Algorithms Lecture 3: Review C++


The C++ Implementation

19

Data Structures & Algorithms Lecture 3: Review C++


isEmpty() and isFull() Functions

20

Data Structures & Algorithms Lecture 3: Review C++


Display Method

 Prints all elements of a non-empty list.

Data Structures & Algorithms Lecture 3: Review C++


Insertion Operation

22 1. Insertion at the Tail End:


We may add a new item at the end
of the list
► Assign new value at end
► Increment length of list

2. Insertion at a Particular
Position: A user may insert an
item at a particular position in a list.
► Requires a utility method,
makeRoom()to shift elements
towards right.
► Then the new value is inserted at
the vacated position.

Insert Carla as 3rd position in the list


Data Structures & Algorithms Lecture 3: Review C++
Insertion at the Tail End

23

List of length 5

Position in 1 2 3 4 5
the list
Index 0 1 2 3 4 5
Value 8 20 33 44 48

Data Structures & Algorithms Lecture 3: Review C++


Adding Items at any Position in a List
 If list not full, then add a new value at a given
position in the list.
 Note that position here does not refer an index
number of the array.
 Valid positions for a new value are one
(insertion as the first element) to length+1
(insertion at the tail end).
 In the given example, valid positions are 1 to 5.

24
Insert Carla as 3rd position in the list
Search Operation

25 Searches a value in a non-empty list.
 If found, returns position of the element in the list (not in
array).
 If not found, returns a special invalid position e.g. -1.

List of length 5

Position in 1 2 3 4 5
the list
Index 0 1 2 3 4 5
Value 8 20 33 44 48

Data Structures & Algorithms Lecture 3: Review C++

You might also like