0% found this document useful (0 votes)
108 views

Array-Based Lists: Ias1223 Data Structures and Algorithms

This document discusses array-based lists and their implementation in Java. It describes how lists store and manipulate elements through operations like insertion, removal and retrieval. Array-based lists use arrays to store elements and require all elements to be of the same type. The document defines classes like ArrayListClass, UnorderedArrayList and DataElement to implement generic lists and their common operations in a clear and organized manner.

Uploaded by

Muhammad Hashim
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
108 views

Array-Based Lists: Ias1223 Data Structures and Algorithms

This document discusses array-based lists and their implementation in Java. It describes how lists store and manipulate elements through operations like insertion, removal and retrieval. Array-based lists use arrays to store elements and require all elements to be of the same type. The document defines classes like ArrayListClass, UnorderedArrayList and DataElement to implement generic lists and their common operations in a clear and organized manner.

Uploaded by

Muhammad Hashim
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 39

1

Chapter 3

Array-based lists

IAS1223 DATA STRUCTURES AND ALGORITHMS


Chapter Objectives
2

 Learn about lists


 Explore how various operations, such as search,
insert, and remove, on lists are implemented
 Learn how to design and implement a generic
class to process various types of lists
 Become aware of the class Vector

Data Structures Using Java


Array-Based Lists
3

 List: A collection of elements of the same type


 Length of list is number of elements in list

Data Structures Using Java


Operations Performed on a List
4

 Create the list: initialize to an empty state


 Determine whether the list is empty
 Determine whether the list is full
 Find the size of the list
 Destroy (clear) the list
 Determine whether an item is the same as a given
list element

Data Structures Using Java


Operations Performed on a List
5

 Insert an item in the list at the specified location


 Remove an item from the list at the specified location
 Replace an item at the specified location with another
item
 Retrieve an item from the list at the specified location
 Search the list for a given item

Data Structures Using Java


Type of List Elements
6

 class Object directly or indirectly becomes a


superclass of every Java class, user defined or
built in
 Reference variable of Object type can refer to any
object of any class
 class DataElement, superclass of every class
specifying data type of list elements
 Abstract methods of class DataElement: equals,
compareTo, makeCopy, and getCopy

Data Structures Using Java


Type of List Elements
7

Data Structures Using Java


Type of List Elements
8

 class IntElement used when list of integers is


manipulated
 class DoubleElement used when list of decimal
numbers is manipulated
 class StringElement, designed in chapter, used
when list of strings is manipulated

Data Structures Using Java


class IntElement
9

Data Structures Using Java


class StringElement
10

Data Structures Using Java


class ArrayListClass
11

 An abstract class
 Is superclass of the classes that implement a list
 Has three instance variables
 length: specifies the number of elements currently in the
list
 maxSize: specifies the maximum number of elements that
can be processed by the list
 list: an array of reference variables

Data Structures Using Java


class ArrayListClass
12

Data Structures Using Java


Definitions of Nonabstract Methods of
ArrayListClass
13

public boolean isEmpty() public int listSize()


{ {
return (length == 0); return length;
} }

public boolean isFull() public int maxListSize()


{ {
return (length == maxSize); return maxSize;
} }

Data Structures Using Java


Definitions of Nonabstract
14
Methods of ArrayListClass

public void print()


{
for(int i = 0; i < length; i++)
System.out.print(list[i] + “ “);
System.out.println();
}

public boolean isItemAtEqual(int location, DataElement item)


{
return (list[location].equals(item));
}
Data Structures Using Java
Definitions of Nonabstract
15
Methods of ArrayListClass
public void insertAt(int location, DataElement insertItem)
{
if(location < 0 || location >= maxSize)
System.err.println(“The position of the item to “
+ “be inserted is out of range”);
else
if(length >= maxSize) //list is full
System.err.println(“Cannot insert in a full list.”);
else
{
for(int i = length; i > location; i--)
list[i] = list[i - 1]; //move the elements down
list[location] = insertItem.getCopy(); //insert the
//item at the specified position
length++; //increment the length
}
}//end insertAt

Data Structures Using Java


Definitions of Nonabstract
16
Methods of ArrayListClass
public void insertEnd(DataElement insertItem)
{
if(length >= maxSize) //the list is full
System.err.println(“Cannot insert in a full list.”);
else
{
list[length] = insertItem.getCopy(); //insert item
//at end
length++; //increment the length
}
}//end insertEnd

Data Structures Using Java


Definitions of Nonabstract
17
Methods of ArrayListClass
public void removeAt(int location)
{
if(location < 0 || location >= length)
System.err.println(“The location of the item to “
+ “be removed is out of range.”);
else
{
for(int i = location; i < length - 1; i++)
list[i] = list[i + 1];
list[length - 1] = null;
length--;
}
}//end removeAt

Data Structures Using Java


Definitions of Nonabstract
18
Methods of ArrayListClass
public DataElement retrieveAt(int location)
{
if(location < 0 || location >= length)
{
System.err.println("The location of the item to be "
+ "retrieved is out of range.");
return null;
}
else
return list[location].getCopy();
}//end retrieveAt

Data Structures Using Java


Definitions of Nonabstract
19
Methods of ArrayListClass
public void replaceAt(int location, DataElement repItem)
{
if(location < 0 || location >= length)
System.err.println(“The location of the item to “
+ “be replaced is out of range.”);
else
list[location].makeCopy(repItem);
}//end replaceAt
public void clearList()
{
for(int i = 0; i < length; i++)
list[i] = null;
length = 0;
System.gc();
}//end clearList

Data Structures Using Java


Definition of ArrayListClass
20

public abstract class ArrayListClass


{
protected int length; //to store the length
//of the list
protected int maxSize; //to store the maximum
//size of the list
protected DataElement[] list; //array to hold
//list elements
//Place the definitions of the instance
// methods and abstract methods here.
}

Data Structures Using Java


Unordered List
21

 class UnorderedArrayList is a subclass of the class


ArrayListClass
 Elements are not necessarily sorted
 class UnorderedList implements operations search,
insert, and remove

Data Structures Using Java


class UnorderedArrayList
22

Data Structures Using Java


Search
23

 Necessary components to search a list:


 Array containing the list
 Length of the list
 Item for which you are searching
 After search completed:
 If item found, report “success”, return location in array
 If item not found, report “failure”

Data Structures Using Java


Search
24

public int seqSearch(DataElement searchItem)


{
int loc;
boolean found = false;
for(loc = 0; loc < length; loc++)
if(list[loc].equals(searchItem))
{
found = true;
break;
}
if(found)
return loc;
else
return -1;
}//end seqSearch

Data Structures Using Java


Insert and Remove
25

 Insert
 Inserts a new item in the list
 Uses method seqSearch to determine whether
insertItem is already in list
 Remove
 deletes an item from the list
 uses the methods seqSearch and removeAt to remove
an item from the list

Data Structures Using Java


Insert
26
public void insert(DataElement insertItem)
{
int loc;
if(length == 0) //list is empty
list[length++] = insertItem; //insert the item and
//increment the length
else
if(length == maxSize)
System.err.println(“Cannot insert in a full list.”);
else
{
loc = seqSearch(insertItem);
if(loc == -1) //the item to be inserted
//does not exist in the list
list[length++] = insertItem.getCopy();
else
System.err.println(“The item to be inserted is “
+ “already in the list. No “
+ “duplicates are allowed.”);
}
}//end insert Data Structures Using Java
Remove
27
public void remove(DataElement removeItem)
{
int loc;
if(length == 0)
System.err.println(“Cannot delete from an empty
list.”);
else
{
loc = seqSearch(removeItem);
if(loc != -1)
removeAt(loc);
else
System.out.println(“The item to be deleted is “
+ “not in the list.”);
}
}//end remove
Data Structures Using Java
Vectors
28

 Class Vector can be used to implement a list


 Unlike array, size of Vector object can grow/shrink
during program execution
 Do not need to worry about number of data
elements in vector

Data Structures Using Java


Members of the class Vector
29

 protected int elementCount;


 protected Object[] elementData;

 public Vector()
 public Vector (int size)
 public void addElement (Object insertObj)
 public void insertElementAt (Object insertObj, int index)
 public Object clone ( )
 public boolean contains (Object obj)

Data Structures Using Java


Members of the class Vector
30

 public void copyInto (Object[] dest)


 public Object elementAt (int index)
 public Object firstElement ()
 public Object lastElement ()
 public int indexOf (Object obj)
 public int indexOf (Object obj, int index)
 public boolean isEmpty ()
 public int lastIndexOf (Object obj)
 public int lastIndexOf (Object obj)

Data Structures Using Java


Members of the class Vector
31

 public int lastIndexOf (Object item, int index)


 public void removeAllElements ( )
 public boolean removeElement (Object obj)
 public void removeElementAt (int index)
 public void setElementAt (Object obj, int index)
 public int size ( )
 public String toString ( )

Data Structures Using Java


Vectors
32

 Every element of Vector object is reference


variable of type Object
 To add element into Vector object
 Create appropriate object
 Store data into object
 Store address of object holding data into Vector object
element

Data Structures Using Java


Vector StringList
33

stringList.addElement(“Spring”);
stringList.addElement(“Summer”);
stringList.addElement(“Fall”);
stringList.addElement(“Winter”);

Data Structures Using Java


Vector StringList
34

stringList.addElement(“Cool”, 1);

Data Structures Using Java


Programming Example:
35
Polynomials

Purpose: To design and implement the class Polynomial to


perform various polynomial operations in a program
Program implements the following polynomial operations:
1. Evaluate a polynomial at a given value
2. Add polynomials
3. Subtract polynomials
4. Multiply polynomials

Data Structures Using Java


Programming Example:
36
Polynomials

Data Structures Using Java


Programming Example:
37 Polynomials

Data Structures Using Java


Chapter Summary
38

 Operations performed on a list


 Type of list elements
 Abstract class DataElement
 Classes IntElement, DoubleElement, StringElement
 class ArrayListClass
 Definitions of Nonabstract Methods of ArrayListClass
 Definition of ArrayListClass

Data Structures Using Java


Chapter Summary
39

 Unordered List
 Class UnorderedArrayList
 Implementations of search, insert and remove
 Time Complexity of List Operations
 Vectors
 Members of the class Vector
 Programming examples

Data Structures Using Java

You might also like