0% found this document useful (0 votes)
40 views27 pages

6 Lists Array

The document discusses lists and their implementation using arrays. It defines lists as collections of ordered items that can be accessed by position. Common list operations like insertion, removal and retrieval of items are presented. An array-based implementation of lists is shown using templates in C++ with methods like insert, remove, getEntry and clear. The implementation shifts elements in the array to maintain list positions during insertions and removals.

Uploaded by

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

6 Lists Array

The document discusses lists and their implementation using arrays. It defines lists as collections of ordered items that can be accessed by position. Common list operations like insertion, removal and retrieval of items are presented. An array-based implementation of lists is shown using templates in C++ with methods like insert, remove, getEntry and clear. The implementation shifts elements in the array to maintain list positions during insertions and removals.

Uploaded by

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

Lists

ERIN KEITH

6_LISTS 1
Topics
1. List ADT
2. Using Lists
3. Array Based

6_LISTS 2
Lists
Things you make lists of
• Chores
• Addresses
• Groceries
Lists contain items of the same type
Operations
• Count items
• Add, remove items
• Retrieve

6_LISTS 3
List ADT
Qualities of an ADT List:
• Finite number of objects
• Not necessarily distinct
• Same data type
• Ordered by position as determined by client

6_LISTS 4
List ADT
Qualities of an ADT List:
• Items are referenced by
• Position in list
• Each item has
• A unique predecessor
• Head (or front) does not have a predecessor
• A unique successor
• Tail (or end) does not have a successor

6_LISTS 5
List ADT
Operations
• isEmpty(): boolean
• getLength(): integer
• insert(newPosition: integer, newEntry: ItemType): boolean
• remove(position: integer): boolean
• clear(): void
• getEntry(position: integer): ItemType
• replace(position: integer, newEntry: ItemType): ItemType

6_LISTS 6
List Use
ArrayList<string> groceryList;

Item Position

6_LISTS 7
List Use
ArrayList<string> groceryList;

groceryList.insert(1, "Apples");

Item Position
Apples 1

6_LISTS 8
List Use
ArrayList<string> groceryList;

groceryList.insert(1, "Apples");
groceryList.insert(2, "Cheese");

Item Position
Apples 1
Cheese 2

6_LISTS 9
List Use
ArrayList<string> groceryList;

groceryList.insert(1, "Apples");
groceryList.insert(2, "Cheese");
groceryList.insert(1, "Eggs");

Item Position
Eggs 1
Apples 2
Cheese 3

6_LISTS 10
List Use
ArrayList<string> groceryList;

groceryList.insert(1, "Apples");
groceryList.insert(2, "Cheese");
groceryList.insert(1, "Eggs");
grocertList.remove(1);

Item Position
Apples 1
Cheese 2

6_LISTS 11
List Use
ArrayList<string> groceryList;

groceryList.insert(1, "Apples");
groceryList.insert(2, "Cheese");
groceryList.insert(1, "Eggs");
grocertList.remove(1);

for(int pos = 1; pos < groceryList.getLength(); pos++){


cout << groceryList.getEntry(pos) << endl;
}

6_LISTS 12
List ADT
Axioms
1. (List()).isEmpty = true
2. (List()).getLength() = 0
3. aList.getLength() = (aList.insert(i, item)).getLength() – 1
4. aList.getLength() = (aList.remove(i)).getLength() + 1
5. (aList.insert(i, item)).isEmpty() = false
6. (List()).remove(i) = false
7. (aList.insert(i, item)).remove(i) = true
8. (aList.insert(i, item)).remove(i) = aList
9. (List()).getEntry(i) => error
10. (aList.insert(i, item)).getEntry(i) = item
11. aList.getEntry(i) = (aList.insert(i, item)).getEntry(i+1)
12. aList.getEntry(i+1) = (aList.remove(i)).getEntry(i)
13. (List()).replace(i, item) => error
14. (aList.replace(i, item)).getEntry(i) = item

6_LISTS 13
List Implementation
#ifndef LIST_INTERFACE
#define LIST_INTERFACE

template<class ItemType>
class ListInterface {
public:
virtual bool isEmpty() const = 0;
virtual int getLength() const = 0;
virtual bool insert(int newPosition, const ItemType& newEntry) = 0;
virtual bool remove(int position) = 0;
virtual void clear() = 0;
virtual ItemType replace(int position, const ItemType& newEntry) = 0;
virtual ~ListInterface() { }
};
#endif

6_LISTS 14
Array Implementation
Very similar to the Bag, so start with an array of a fixed size. But
now we must keep track of “position”.

Index 0 1 2 3
Item Eggs Apples Cheese
Position 1 2 3 4

6_LISTS 15
Array Implementation
Wouldn’t it just be easier if the index and position lined up?

Index 0 1 2 3
Item Eggs Apples Cheese
Position 1 2 3

6_LISTS 16
Array Implementation
#ifndef ARRAY_LIST
#define ARRAY_LIST

#include "ListInterface.h"

template<class ItemType>
class ArrayList : public ListInterface {
private:
static const int DEFAULT_CAPACITY = 100;
ItemType items[DEFAULT_CAPACITY + 1]; //(ignore items[0])
int itemCount;
int maxCount;

6_LISTS 17
Array Implementation
public:
ArrayList();

bool isEmpty() const;


int getLength() const;
bool insert(int newPosition, const ItemType& newEntry);
bool remove(int position);
void clear();
ItemType replace(int position, const ItemType& newEntry);
ItemType getEntry(const ItemType& newEntry) const;

~ArrayList() { }
};
#include "ArrayList.cpp"
#endif

6_LISTS 18
Array Implementation
#include "ArrayList.h"

template<class ItemType>
ArrayList<ItemType>::ArrayList(){
itemCount = 0;
maxItems = DEFAULT_CAPACITY;
}

6_LISTS 19
List Implementation
template<class ItemType>
int ArrayList<ItemType>::getLength() const{
return itemCount;
}

template<class ItemType>
bool ArrayList<ItemType>::isEmpty() const{
return itemCount == 0;
}

6_LISTS 20
List Use
ArrayList<string> groceryList;

groceryList.insert(1, "Apples");
groceryList.insert(2, "Cheese");
groceryList.insert(1, "Eggs");

Item Position
Eggs 1
Apples 2
Cheese 3

6_LISTS 21
Array Implementation
template<class ItemType>
bool ArrayList<ItemType>::insert(int newPos, const ItemType& newEntry){
bool canInsert = itemCount < maxItems
&& newPos >= 1 && newPos <= itemCount + 1;

if(canInsert){
for(int pos = itemCount; pos >= newPos; pos--){
items[pos + 1] = items[pos];
}
items[newPos] = newEntry;
itemCount++;
}

return canInsert;
}

6_LISTS 22
Array Implementation
template<class ItemType>
ItemType ArrayList<ItemType>::getEntry(int position){
bool canGet = position >= 1 && position <= itemCount;

if(canGet){
return items[position];
}
else{
throw "invalid position";
}
}

6_LISTS 23
Array Implementation
template<class ItemType>
ItemType ArrayList<ItemType>::replace(int position,
const ItemType& newEntry){
bool canReplace = position >= 1 && position <= itemCount;

if(canReplace){
ItemType oldEntry = items[position];
items[position] = newEntry;
return oldEntry;
}
else{
throw "invalid position";
}
}

6_LISTS 24
Array Implementation
template<class ItemType>
bool ArrayList<ItemType>::remove(int position){
bool canRemove = position >= 1 && position <= itemCount;

if(canRemove)){
for(int pos = position; pos < itemCount; pos++){
items[pos] = items[pos + 1];
}
itemCount--;
}

return canRemove;
}

6_LISTS 25
Array Implementation
template<class ItemType>
void ArrayList<ItemType>::clear(){
itemCount = 0;
}

6_LISTS 26
Next Class
Stacks
Textbook:
• Chapters 6
Internet:
•https://www.autonomousrobotslab.com/uploads/5/8
/4/4/58449511/cs302-31-stack-adt.pdf
•https://www.autonomousrobotslab.com/uploads/5/8
/4/4/58449511/cs302-32-using-the-adt-stack.pdf
•https://www.autonomousrobotslab.com/uploads/5/8
/4/4/58449511/cs302-33-implementation-adt-stack.
pdf

6_LISTS 27

You might also like