Program - Array-list Implementation
Implement an abstract datatype ListADT with the following methods.
Note: There are exceptions, handle those using throw.
Ex: throws("Index out of range").
# Methods:
1) ListADT(): Constructor
2) add(element): Takes an int element to add into Array. Return type is void.
3) add(index, element): Takes an int index and int element to add the given element at the given index in Array. Return type is void.
4) get(index): Takes an int index and returns the element of that index. Return type is int.
5) indexOf(element): Takes an int element and returns its first index. Return type is int.
6) lastIndexOf(element): Takes an int element and returns its last index. Return type is int.
7) sublist(startIndex, endIndex): takes two int indexes, startIndex and endIndex and returns a sublist of the elements excluding the last index. Return type is int[] in Java vector<int> in CPP.
8) contains(element): Takes an int element and returns whether it is present in the Array or not. 10) Return type is boolean.
9) size(): Returns the size of Array. Return type is int.
10) resize(): Resizes the Array. Use this method to resize the array when the number of elements is greater that the array size. Return type is void.
11) toString(): Converts the given input into String format.
Program:
import java.lang.ArrayIndexOutOfBoundsException;
class ListADT
{
int[] arr;
int size;
int capacity;
public ListADT()
{
size = 0;
capacity = 1;
arr = new int[capacity];
}
public void add(int el)
{
if (size == capacity) resize();
arr[size++] = el;
}
public void add(int index, int el)
{
if(size == capacity) resize();
if (index >= 0 && index <= size)
{
for(int i = size - 1; i >= index; i--)
{
arr[i + 1] = arr[i];
}
arr[index] = el;
size++;
}
else
{
throw new ArrayIndexOutOfBoundsException("Index out of range");
}
}
public int get(int index)
{
if (index > 0 && index < size) return arr[index];
throw new ArrayIndexOutOfBoundsException("Index out of range");
}
public int indexOf(int el)
{
for(int i = 0; i < size; i++)
{
if (arr[i] == el) return i;
}
return -1;
}
public int lastIndexOf(int el)
{
for(int i = size - 1; i >= 0; i--)
{
if (arr[i] == el) return i;
}
return -1;
}
public int[] sublist(int startIndex, int endIndex)
{
if(startIndex >= 0 && startIndex < endIndex && endIndex > 0 && endIndex <= size)
{
int res[] = new int[endIndex - startIndex];
for(int i = startIndex; i < endIndex; i++)
{
res[i - startIndex] = arr[i];
}
return res;
}
else
{
throw new ArrayIndexOutOfBoundsException("Index out of range");
}
}
public boolean contains(int el)
{
for(int i = 0; i < size; i++)
{
if(arr[i] == el) return true;
}
return false;
}
public int size()
{ return size; }
public void resize()
{
capacity = 2 * capacity;
int[] temp = new int[capacity];
for(int i = 0; i < size; i++)
{
temp[i] = arr[i];
//System.out.print(arr[i] + " ");
}
System.out.println();
arr = temp;
}
public String toString()
{
String str = "";
for(int i = 0; i < size; i++)
{
if (i < size - 1) str += arr[i] + ", ";
else str += arr[i];
}
return str;
}
}
public class ListADTDemo
{
public static void main(String[] args)
{
ListADT listADT = new ListADT();
//listADT.get(20);
listADT.add(1);
listADT.add(2);
listADT.add(3);
listADT.add(4);
listADT.add(5);
System.out.println(listADT.lastIndexOf(5));
System.out.println(listADT.indexOf(5));
listADT.add(0, 0);
System.out.println(listADT.lastIndexOf(5));
//listADT.add(-1, -1);
//listADT.add(20, -20);
System.out.println(listADT.indexOf(-1));
System.out.println(listADT.lastIndexOf(5));
System.out.println(listADT.size());
System.out.println(listADT.sublist(2,4));
for(int i=6; i<20; i++)
listADT.add(i, i);
System.out.println(listADT);
System.out.println(listADT.size());
System.out.println(listADT.sublist(0,20));
}
}
Note: There are exceptions, handle those using throw.
Ex: throws("Index out of range").
# Methods:
1) ListADT(): Constructor
2) add(element): Takes an int element to add into Array. Return type is void.
3) add(index, element): Takes an int index and int element to add the given element at the given index in Array. Return type is void.
4) get(index): Takes an int index and returns the element of that index. Return type is int.
5) indexOf(element): Takes an int element and returns its first index. Return type is int.
6) lastIndexOf(element): Takes an int element and returns its last index. Return type is int.
7) sublist(startIndex, endIndex): takes two int indexes, startIndex and endIndex and returns a sublist of the elements excluding the last index. Return type is int[] in Java vector<int> in CPP.
8) contains(element): Takes an int element and returns whether it is present in the Array or not. 10) Return type is boolean.
9) size(): Returns the size of Array. Return type is int.
10) resize(): Resizes the Array. Use this method to resize the array when the number of elements is greater that the array size. Return type is void.
11) toString(): Converts the given input into String format.
Program:
import java.lang.ArrayIndexOutOfBoundsException;
class ListADT
{
int[] arr;
int size;
int capacity;
public ListADT()
{
size = 0;
capacity = 1;
arr = new int[capacity];
}
public void add(int el)
{
if (size == capacity) resize();
arr[size++] = el;
}
public void add(int index, int el)
{
if(size == capacity) resize();
if (index >= 0 && index <= size)
{
for(int i = size - 1; i >= index; i--)
{
arr[i + 1] = arr[i];
}
arr[index] = el;
size++;
}
else
{
throw new ArrayIndexOutOfBoundsException("Index out of range");
}
}
public int get(int index)
{
if (index > 0 && index < size) return arr[index];
throw new ArrayIndexOutOfBoundsException("Index out of range");
}
public int indexOf(int el)
{
for(int i = 0; i < size; i++)
{
if (arr[i] == el) return i;
}
return -1;
}
public int lastIndexOf(int el)
{
for(int i = size - 1; i >= 0; i--)
{
if (arr[i] == el) return i;
}
return -1;
}
public int[] sublist(int startIndex, int endIndex)
{
if(startIndex >= 0 && startIndex < endIndex && endIndex > 0 && endIndex <= size)
{
int res[] = new int[endIndex - startIndex];
for(int i = startIndex; i < endIndex; i++)
{
res[i - startIndex] = arr[i];
}
return res;
}
else
{
throw new ArrayIndexOutOfBoundsException("Index out of range");
}
}
public boolean contains(int el)
{
for(int i = 0; i < size; i++)
{
if(arr[i] == el) return true;
}
return false;
}
public int size()
{ return size; }
public void resize()
{
capacity = 2 * capacity;
int[] temp = new int[capacity];
for(int i = 0; i < size; i++)
{
temp[i] = arr[i];
//System.out.print(arr[i] + " ");
}
System.out.println();
arr = temp;
}
public String toString()
{
String str = "";
for(int i = 0; i < size; i++)
{
if (i < size - 1) str += arr[i] + ", ";
else str += arr[i];
}
return str;
}
}
public class ListADTDemo
{
public static void main(String[] args)
{
ListADT listADT = new ListADT();
//listADT.get(20);
listADT.add(1);
listADT.add(2);
listADT.add(3);
listADT.add(4);
listADT.add(5);
System.out.println(listADT.lastIndexOf(5));
System.out.println(listADT.indexOf(5));
listADT.add(0, 0);
System.out.println(listADT.lastIndexOf(5));
//listADT.add(-1, -1);
//listADT.add(20, -20);
System.out.println(listADT.indexOf(-1));
System.out.println(listADT.lastIndexOf(5));
System.out.println(listADT.size());
System.out.println(listADT.sublist(2,4));
for(int i=6; i<20; i++)
listADT.add(i, i);
System.out.println(listADT);
System.out.println(listADT.size());
System.out.println(listADT.sublist(0,20));
}
}
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment