-
-
Save dbc2201/d97471995e25aa0672ae0a10889d25c0 to your computer and use it in GitHub Desktop.
CodeRelease #1:
Integer List ADT in Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package release; | |
import java.util.Scanner; | |
public class ListADT | |
{ | |
private int[] listArray; | |
// integer array to hold 'int' type list elements | |
private int listBottom; | |
// integer variable listBottom to keep track of the listBottom of the list | |
// constructor to initialize values for the ADT class | |
public ListADT(int numberOfElements) | |
{ | |
listArray = new int[numberOfElements]; | |
// add memory to the array for n elements, n = numberOfElements | |
listBottom = -1; | |
// since the list is currently empty, the index of listBottom is -1 | |
} | |
// push method for the list | |
public void insert(int element) | |
{ | |
if (isFull()) | |
// check whether the list is already full | |
{ | |
System.out.println("List is already full!"); | |
// if the list is already full, print the message | |
} | |
else | |
{ | |
listBottom++; | |
// increase the listBottom index | |
listArray[listBottom] = element; | |
// add the new element at the modified index | |
} | |
} | |
// isFull method for the list | |
public boolean isFull() | |
{ | |
boolean response = false; | |
// response variable to return the value of the method | |
if (listBottom == listArray.length - 1) | |
// check whether the listBottom index is same as the last index of the array | |
{ | |
response = true; | |
// if true, set the response variable to true | |
} | |
return response; | |
} | |
// method to remove the last element of the list | |
public int remove() | |
{ | |
int response = 0; | |
if (isEmpty()) | |
{ | |
System.out.println("The list is already empty!"); | |
} | |
else | |
{ | |
response = listArray[listBottom]; | |
listArray[listBottom] = 0; | |
// set the last element of the list to 0, indicating it has been deleted | |
listBottom--; | |
// decrease the list bottom index | |
} | |
return response; | |
} | |
// removeAt() method to remove a specific index from the list | |
public int removeAt(int index) | |
{ | |
int response = 0; | |
if (isEmpty()) // check whether the list is empty | |
{ | |
System.out.println("List is already empty"); | |
} | |
else | |
{ | |
if (listBottom < index) // if bottom is less than index | |
// then the index is not present in the list | |
{ | |
System.out.println("No such index in the list!"); | |
} | |
else | |
{ | |
response = listArray[index]; | |
listArray[index] = 0; // set the element at 'index' to 0 (delete) | |
// shift the elements to the left | |
int i; | |
for (i = index; i < listBottom - 1; i++) | |
{ | |
listArray[i] = listArray[i + 1]; | |
} | |
listArray[i] = 0; // delete the last redundant value | |
listBottom--; | |
} | |
} | |
return response; | |
} | |
// isEmpty method for the list | |
public boolean isEmpty() | |
{ | |
boolean response = false; | |
if (listBottom == -1) | |
// check whether the bottom index is -1 (empty condition for the list) | |
{ | |
response = true; | |
} | |
return response; | |
} | |
// a display method for the list | |
public void display() | |
{ | |
System.out.println("---"); | |
if (isEmpty()) | |
{ | |
System.out.println("List is empty!"); | |
} | |
else | |
{ | |
for (int i : listArray) | |
{ | |
if (i != 0) | |
{ | |
System.out.println(i); | |
} | |
} | |
} | |
System.out.println("---"); | |
} | |
public static void main(String[] args) | |
{ | |
Scanner scanner = new Scanner(System.in); | |
System.out.println("How many elements would you like in your list?"); | |
int n = scanner.nextInt(); // input the number of elements in the list | |
ListADT listObject = new ListADT(n); | |
/* | |
* n is passed inside the constructor to the class to specify that | |
* the listArray must have n elements. | |
* */ | |
listObject.display(); | |
// iterate the loop 'n' times to input 'n' elements | |
for (int i = 0; i < n; i++) | |
{ | |
System.out.print("Enter the " + i + "th element:"); | |
int value = scanner.nextInt(); | |
listObject.insert(value); | |
} | |
System.out.println("List after inserting " + n + " elements."); | |
listObject.display(); | |
System.out.println(listObject.remove() + " was removed from the list."); | |
System.out.println("List after removing the last element."); | |
listObject.display(); | |
System.out.println("Enter the index you want to remove:"); | |
int removedElement = listObject.removeAt(scanner.nextInt()); | |
System.out.println(removedElement + " was removed"); | |
System.out.println("List after removing " + removedElement); | |
listObject.display(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment