You are on page 1of 47

General Lists

DATA STRUCTURES
USING C++
General Lists
Why a general lists?
 New values are added in location determined by the user.
 Element (item) is removed from the location determined by
the user.

Important notice (Special case of lists):


 If we keep adding and removing from the first location, the
list will behave as a stack, LIFO.
 If we keep adding from one end and removing from front,
the list will behave as a queue, FIFO.
Two Ways of Implementing
General Lists

Array-Based Lists.

Linked-Based Lists.
Array-Based Lists

DATA STRUCTURES
USING C++
Array-Based Lists
 List: A collection (a finite sequence) of elements (items) of the
same type T together with operations.
 The length of a list is the number of elements in the list.
list
0 maxSize - 1
length - 1
 This is list (just a logical view, no implementation yet) with number
of items (elements) equal to length.
 We can add a new element in location 0 ≤ location ≤ length.
 However, we delete from 0 ≤ location ≤ length – 1.
 Now, let us define list with its’ operations.
 length represents the logical length of list (number of elements).
 maxSize represents the physical length of list (number of memory
locations located for array)
Operations performed on a list:
1. Create the list. The list is initialized to an empty state.
2. Determine whether the list is empty.
3. Determine whether the list is full.
4. Find the size of the list.
5. Destroy, or clear, the list.
6. Determine whether an item is the same as a given list element.
7. Insert an item in the list at the specified location.
8. Remove an item from the list at the specified location.
9. Replace an item at the specified location with another item.
10. Retrieve an item from the list from the specified location.
11. Search the list for a given item.
Figure shows
the UML class
diagram of the
class
arrayListType
Class arrayListType
• Data members are:
1) int *list ; //array to hold the list elements
2) int length; //to store the length of the list
3) int maxSize; //to store the maximum size of the list

• Public member methods (functions) each represent operation on


the array-based list.
Code

arrayListType
class

header file
(arrayListType.h)
Code

arrayListType
class

header file
(arrayListType.h)
List operations : Create the list

• Constructor used to create a list and the user specify the size of created list. The default size
value used here is 100.
• The data member maxSize specifies the maximum size of the list.
• The data member length of the class stores the number of elements currently in the list, in
the created new list the length of list is equal to 0.
• The list data member in the class arrayListType is a dynamic array created to hold list
elements.
• The complexity of constructor is O(1).
List operations : Destroy the list

• Destructor used to destroy the list..


• Delete operation used to destroy the dynamic array (list).
• The complexity of destructor is O(1).
print
is a member function used to print
out the elements of list
List operations : isEmpty isFull

• The data member length of the class stores the number of elements currently
in the list.
• maxSize specifies the maximum size of the list.
• Note: the size of the array holding the list elements is stored in the data
member maxSize.
List operations : list size and max-size

• What is the complexity of these functions????


O(1)
Code

arrayListType class

implementation file
(arrayListType.cpp)
Continue…

Code

arrayListType class

implementation file
(arrayListType.cpp)
Code
main program
implementation file (testProgArrayBasedList.cpp)

#include <iostream>
#include "arrayListType.h"
using namespace std;
int main()
{
arrayListType list;
.
.
Call the previous member functions
.
List operations : insertAt
0 length - 1 maxSize - 1

list
• The function insertAt inserts an item at a specific location in the list.
• insertAt member function inputs are:- Location of insertion and Item to be
inserted.
•We can add a new element in location 0 ≤ location ≤ length.
• Preconditions:
- Can we insert (add) in the location less than 0 or more than maxSize?
- Can we insert (add) item if the list is full?

•Actions:
- Shift the list items one location from the location of inserted item to length
index.
- Modify the length data member value by increasing one to it.
List operations : insertAt
The function insertAt inserts an item at a specific location in the list.

What is the complexity of these functions???? O(n)


In main program
List operations : insertEnd
The function insertEnd can be implemented by using the function
insertAt. However, the function insertEnd does not require the
shifting of elements. Therefore, we give its definition directly.

What is the complexity of these functions???? O(1)


List operations : removeAt
0 length - 1 maxSize - 1

list
•The function removeAt is the opposite of the function insertAt.
•The function removeAt remove an item at a specific location in the list.
• removeAt member function input is:- Location of deletion.
•We can remove an element from location 0 ≤ location ≤ length-1 .
• Preconditions:
- Can we remove (delete) from location less than 0 or more than maxSize?

•Actions:
-Shift the list items one location toward the location of removed item.
- Modify the length data member value by decreasing one from it.
List operations : removeAt

What is the complexity of these functions???? O(n)


List operations : retrieveAt
The definition of the function retrieveAt is straightforward. The
location of the item in list to be retrieved, and the address (&)
where to store the retrieved item, are passed as parameters to
this function.

What is the complexity of these functions???? O(1)


List operations : replaceAt
The definition of the function replaceAt is straightforward. The
location of the item in list to be replaced, and the new item need to be
store in the provided location, are passed as parameters to this
function.

What is the complexity of these functions???? O(1)


In main program
Search
List operations :
Search: linear search, sequential
• The function seqSearch Input list of n elements and item

• seqSearch member function input is:- Item to be found.

•Return the location of the item if found, -1 indicates that the item does not
exist in the list.

• Search in the locations 0 ≤ location < length.


Search: linear search, sequential
Complexity Sequential Search

 Two Kinds of result


1) Successful 2) UnSuccessful
Best Case - 1st element
O(1)
Worst Case - nth element
O(n) (last)

What is the complexity of these functions????


O(n)
Insert
0
Insert length - 1 maxSize - 1

list
• The function insert inserts a new item in the list. Because duplicates are
not allowed, this function first searches the list to determine whether the
item to be inserted is already in the list.
•The function insert inserts an item at the end of list.
• insert function input is:- Item to be inserted.
• Preconditions:
- If list is empty insert the item.
- If list is full, can’t insert.

•Actions:
- Search for the item (by sequential search function). If it is not in the list add
(insert) it. And modify the length data member value by increasing one to it.
- Else the item in the list, can’t duplicate it.
Complexity
What is the complexity of these functions????
It is O(n)

Why?
Because it calls seqSearch member function which
need O(n) complexity time.
In main
Remove
remove
0 length - 1 maxSize - 1

list
•Function remove deletes an item from the list. To delete the item, the
function calls the member function seqSearch to determine whether the item
to be deleted is in the list.
•remove member function input is:- Item to be deleted.
• Preconditions:
- If list is empty can’t remove the item.

•Actions:
-Call seqSearch function to check whether the item in the list or not.
- Then pass the return location value from seqsearch function to removeAt
function .
Complexity
What is the complexity of remove function????
It is O(n)

Why?
Because it calls seqSearch member function which
need O(n) complexity time. Also it calls removeAt
member function which need O(n).
Sorted Array
 Which member function specifications and implementations
must change to ensure that any instance of the Sorted List
ADT remains sorted at all times??
 InsertItem
 DeleteItem
Insert Item algorithm for Sorted List ADT
 Find proper location for the newelement in the sorted list.
 Create space for the new element by moving up all the list
elements that will follow it.
 Put the new element in the list.
 Increment length.
Delete Item algorithm for Sorted List ADT
 Find the location of the element to be deleted from the
sorted list.
 Eliminate space occupied by the item being deleted by
moving down all the list elements that follow it.
 Decrement length.
Case Study:
Insertion Sort
 Pick the first card: it forms the trivially sorted list {a1}.
 Pick the next card from the table, and insert it into the correct
place (i.e., before or after a1), giving the sorted list {a1, a2}.
 Pick a third cards and insert it into the correct place in the list.
Now we have sorted {a1, a2, a3}.
 In general, at step j , insert aj into the correct location of {a1,
a2, … ,aj-1}.

You might also like