Question: This is my code but i need for it test class and the time complexity public class Arrayy { private char car[]; private final static
This is my code but i need for it test class and the time complexity
public class Arrayy {
private char car[];
private final static int MAX = 5;
private int size;
public Arrayy() {
car = new char[MAX];
size = 0;
}
public int findElement(char key) {
int i;
for (i=0; i if (car[i] == key) return i; return -1; } public void add(char c) { if(size >= MAX) System.out.println("Lane already full!!"); else car[size ++] = c; } public void remove(char key) { int pos = findElement(key); if (pos == -1) { System.out.println("Car not found"); return; } int i; for (i=pos; i car[i] = car[i+1]; size --; } public void display() { System.out.print(" Stack = "); if (size == 0) { System.out.print("Empty "); return ; } for (int i = 0; i < size; i++) System.out.print(car[i]+" "); } } ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- import java.util.NoSuchElementException; public class Queue { private char cars[] ; private int front, rear, size; private static final int MAX = 5; public Queue() { int size = 0; cars = new char[MAX]; front = -1; rear = -1; } public boolean isEmpty() { return front == -1; } public boolean isFull() { return front==0 && rear == MAX -1 ; } public int getSize() { return size; } public int peek() { if (isEmpty()) throw new NoSuchElementException("Underflow Exception"); return cars[front]; } public void add(char c) { if (rear == -1) { front = 0; rear = 0; cars[rear] = c; } else if (size >= MAX) throw new IndexOutOfBoundsException("Overflow Exception"); else if ( rear + 1 < MAX) cars[++rear] = c; size ++ ; } public char remove() { if (isEmpty()) throw new NoSuchElementException("Underflow Exception"); else { size -- ; char ele = cars[front]; if ( front == rear) { front = -1; rear = -1; } else front++; return ele; } } public int findElement(char key) { int i; for (i=0; i if (cars[i] == key) return i; return -1; } public void remove(char key) { int pos = findElement(key); if (pos == -1) { System.out.println("Car not found"); return; } int i; for (i=pos; i cars[i] = cars[i+1]; size --; } public void display() { System.out.print(" Queue = "); if (size == 0) { System.out.print("Empty "); return ; } for (int i = front; i <= rear; i++) System.out.print(cars[i]+" "); } } ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ import java.util.NoSuchElementException; public class Stack { private char cars[]; private int top, size; private static final int MAX = 5; public Stack() { size = 0; cars = new char[MAX]; top = -1; } public boolean isEmpty() { return top == -1; } public boolean isFull() { return top == MAX -1 ; } public int getSize() { return size ; } public char peek() { if( isEmpty() ) throw new NoSuchElementException("Underflow Exception"); return cars[top]; } public void push(char c) { if(size >= MAX) throw new IndexOutOfBoundsException("Overflow Exception"); if(top + 1 < MAX ) cars[++top] = c; size ++ ; } public char pop() { if( isEmpty() ) throw new NoSuchElementException("Underflow Exception"); size-- ; return cars[top--]; } public int findElement(char key) { int i; for (i=0; i if (cars[i] == key) return i; return -1; } public void remove(char key) { int pos = findElement(key); if (pos == -1) { System.out.println("Car not found"); return; } int i; for (i=pos; i cars[i] = cars[i+1]; size --; } public void display() { System.out.print(" Stack = "); if (size == 0) { System.out.print("Empty "); return ; } for (int i = top; i >= 0; i--) System.out.print(cars[i]+" "); System.out.println(); } } ------------------------------------------------------------------------------------------------------------------------------------------------ public class SLL { private Node head; private static int size = 0; private final static int MAX = 5; class Node { char data; Node next; Node(char d) { data = d; next = null; } } public void add(char car) { if(size == MAX) { System.out.println("Lane already full!!"); return; } Node new_node = new Node(car); if (head == null) { head = new Node(car); return; } new_node.next = null; Node last = head; while (last.next != null) last = last.next; last.next = new_node; return; } public void remove(char key) { Node temp = head, prev = null; if (temp != null && temp.data == key) { head = temp.next; size --; return; } while (temp != null && temp.data != key) { prev = temp; temp = temp.next; } if (temp == null) return; prev.next = temp.next; size --; } public void display() { Node tnode = head; while (tnode != null) { System.out.print(tnode.data + " "); tnode = tnode.next; } System.out.println(); } } ON Data Structures
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
