Dynamic Array using a linked list -Task: Create a class that implements a Dynami
ID: 3730422 • Letter: D
Question
Dynamic Array using a linked list -Task: Create a class that implements a Dynamic Array using a link list. The class should implement the following method/constructor .A constructor that creates and initializes the link list addElementFront: Adds an integer to the front of the array addElementBack: Adds an integer to the back of the array insertElement: Adds or inserts an integer at a given location deleteElement: Deletes an integer from the element. removeFront: Deletes first element in the array. removeBack: Deletes last element in the array. size: Prints out the current size of the array print: Prints out the elements in an arrayExplanation / Answer
import java.util.*;
class Node{ //Node class for an element in linked list with data and link to next node
Node next = null;
int data = 0;
public Node(){ //default constuctor if no values are passed
next = null;
data = 0;
}
public Node(int data,Node next){ //constuctor with values defined
this.data = data;
this.next = next;
}
}
class linkedList { //linked list class for performing various operations required for implementing dynamic array
Node start = null; //start points to the starting node of the list
Node end = null; //end points to the end node of the list
int size = 0; //size stores the size of the list
public linkedList() { //default constuctor for creating the list
start = null;
end = null;
size = 0;
}
public void addElementFront(int data) { //adding element at front
Node newnode = new Node(data, null); //creating new node
if (start == null) { //if list is empty
start = newnode;
end = start;
} else {
newnode.next = start;
start = newnode;
}
size++;
}
public void addElementBack(int data) { //adding element at end
Node newnode = new Node(data, null); //creating new node
if (start == null) { //if list is empty
start = newnode;
end = newnode;
} else {
end.next = newnode;
end = newnode;
}
size++;
}
public void insertElement(int location, int data) { //adding element at particular position
Node newnode = new Node(data, null); //creating new node
Node ptr = start; //temporary pointer for looping through the list
location = location - 1;
for (int i = 0; i < size; i++) {
//System.out.println(i);
if (i == location) { //if the position is reached
Node temp = ptr.next;
ptr.next = newnode;
newnode.next = temp;
size++;
break;
}
ptr = ptr.next;
}
}
public void deleteElement(int data) { //deleting the element by given value
if (start == null) { //if the list is empty
System.out.println("Empty!");
return;
}
if (start.data == data && start.next == null) { //if the list has only one element
start = null;
end = null;
size--;
return;
}
if (start.data == data && start.next != null) { //if the element is first element and list has more than one element
start = start.next;
size--;
return;
}
if (end.data == data) { //if the element is at end of the list
Node sptr = start;
Node eptr = start;
sptr = sptr.next;
while (sptr.next != null) {
eptr = sptr;
sptr = sptr.next;
}
end = eptr;
end.next = null;
size--;
return;
}
Node sptr = start;
Node prev = sptr;
sptr = sptr.next;
while (sptr.data != data && sptr.next != null) {
prev = sptr;
sptr = sptr.next;
}
if (sptr.data == data) {
Node temp = prev.next;
temp = temp.next;
prev.next = temp;
size--;
return;
}
System.out.println(data + " not found!");
}
public void removeFront() { //deleting the first element of array
if (start == null) { //if the list is empty
System.out.println("Empty!");
return;
}
if (start.next == null) { //if the list has only one element
start = null;
end = null;
size--;
return;
}
if (start.next != null) { //if the list has more than one element
start = start.next;
size--;
return;
}
}
public void removeBack() { //deleting the last element of array
if (start == null) { //if the list is empty
System.out.println("Empty!");
return;
}
if (start.next == null) { //if the list has only one element
start = null;
end = null;
size--;
return;
}
if (start.next != null) { //if the list has more than one element
Node ptr = start;
while(ptr.next != end){
ptr = ptr.next;
}
ptr.next = null;
return;
}
}
public void print() { //to display elements of the array
if (start == null) { //if list is empty
System.out.println("Empty!");
return;
}
Node ptr = start;
while (ptr != null) {
System.out.print(ptr.data + " ");
ptr = ptr.next;
}
System.out.println();
}
public void size() {
System.out.println("Size of array is: " + size);
}
}
public class dynamicArray {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
linkedList list = null;
String val;
System.out.println("Create");
System.out.println("addFront <num>");
System.out.println("addBack <num>");
System.out.println("insert <location>,<num>");
System.out.println("del <num>");
System.out.println("delFront");
System.out.println("delBack");
System.out.println("size");
System.out.println("print");
System.out.println("exit");
System.out.println();
do {
System.out.print("Enter Command: ");
String com = in.nextLine();
//System.out.println(com);
StringTokenizer st = new StringTokenizer(com," ,");
val = st.nextToken();
if(val.equalsIgnoreCase("Create")){
list = new linkedList();
}
else if(val.equalsIgnoreCase("addFront")) {
int num = Integer.parseInt(st.nextToken());
list.addElementFront(num);
}
else if(val.equalsIgnoreCase("addBack")) {
int num = Integer.parseInt(st.nextToken());
list.addElementBack(num);
}
else if(val.equalsIgnoreCase("insert")) {
int loc = Integer.parseInt(st.nextToken());
int num = Integer.parseInt(st.nextToken());
list.insertElement(loc,num);
}
else if(val.equalsIgnoreCase("del")) {
int num = Integer.parseInt(st.nextToken());
list.deleteElement(num);
}
else if(val.equalsIgnoreCase("delFront")) {
list.removeFront();
}
else if(val.equalsIgnoreCase("delBack")) {
list.removeBack();
}
else if(val.equalsIgnoreCase("size")) {
list.size();
}
else if(val.equalsIgnoreCase("print")) {
list.print();
}
} while (!(val.equalsIgnoreCase("exit")));
}
}
output:
Create
addFront <num>
addBack <num>
insert <location>,<num>
del <num>
delFront
delBack
size
print
exit
Enter Command: Create
Enter Command: print
Empty!
Enter Command: addFront 4
Enter Command: print
4
Enter Command: addFront 9
Enter Command: print
9 4
Enter Command: addBack 10
Enter Command: print
9 4 10
Enter Command: insert 1,6
Enter Command: print
9 6 4 10
Enter Command: del 4
Enter Command: print
9 6 10
Enter Command: delFront
Enter Command: print
6 10
Enter Command: delBack
Enter Command: print
6
Enter Command: exit
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.