Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write Algorithm of these methods: 1 : Arraylist package GUI; import java.util.Arrays; import java.util.stream.IntStream; import java.util.List; import java.io.*; import java.lang.*; import java.util.*; public class Arraylist

Write Algorithm of these methods:

1 : Arraylist

package GUI;
import java.util.Arrays;
import java.util.stream.IntStream;

import java.util.List;
import java.io.*;
import java.lang.*;
import java.util.*;
public class Arraylist {


public T[] data;

public int size;

public Arraylist()
{
this(1);
}

public Arraylist(int inicap) {
if(inicap>0)
{
this.data= (T[]) new Object[inicap];
}
else if(inicap==0)
{
this.data= (T[]) new Object[0];
}
else
{
throw new IllegalArgumentException("Capacity cannot be negative");
}

}

public void ensurecapacity()
{
if(data.length<=size)
{
int oldcap=data.length;
int newcap=oldcap +1;

T[] temp= (T[]) new Object[newcap];
for (int i = 0; i < data.length; i++) {
temp[i]=data[i];
}
data=temp;
}
}



public int add(T value)
{
ensurecapacity();
data[size]=value;
size++;
return size ;
 


}

public int add(int index, T value)
{
if(index >size || index<0)
{
throw new ArrayIndexOutOfBoundsException("crossing size limit");
}

ensurecapacity();
T temp=data[size-1];
for (int i = size-1; i >= index; i--) {
data[i+1]= data[i];
}
data[index]=value;
return size++;

}

public T get(int index)
{
if(index >size || index<0)
{
throw new ArrayIndexOutOfBoundsException("crossing size limit");
}
 
return data[index];
}


public T remove(int index)
{
if(index >size || index<0)
{
throw new ArrayIndexOutOfBoundsException("crossing size limit");
}
 
T temp=data[index];
for (int i = index; i < data.length-1; i++) {
data[i]=data[i+1];
}
size--;
data[size]=null;

return temp;
}

public boolean contains(T value) {
boolean contain = false;
for(int i=0;iif(data[i]==value) {
contain=true;
break;
}
else {
contain=false;
}
}
return contain;
}
public int indexof(T value) {
 int index=0;
 for (int i = 0; i < data.length; i++) {
 if (data[i] == value) {
 index =index+i;
 }
 }
 return index;
 }




}
 
2: Stack:

package GUI;

import java.util.ArrayList;

public class stack {
public static void main(String[] args) {

stack s1=new stack(5);
//s1.push(8);


}

public int[] arr;
int count=0;

stack(int size)
{
arr=new int[size];
}

void push(int val)
{
if(count==arr.length)
{
System.out.println("stack is full");
}else
{
arr[count]=val;
count++;
}
}

int pop()
{
if(count==0)
{
System.out.print("stack is empty");
return -1;
}
else
{
return arr[--count];
}
}

int peek()
{
if(count==0)
{
System.out.print("stack is empty");
return -1;
}
return arr[count-1];

}
public boolean Contains(int value){
boolean contain =false;
for(int i=0;iif(arr[i]==value){
contain=true;
break;
}
else {
contain=false;
}}
return contain ;
}



public void display(int[] st, int max_size)
{ int i;
System.out.println("Stack Elements:");
for(i=0;i<=max_size;i++)
System.out.println(st[i]);
}

}

3: Queue:

package GUI;


public class Queue {
public static void main(String[] args) {

//Queue q1=new Queue(5);
//s1.push(8);
//s1.push(56);
//q1.enqueue(24);//-------------------1st

//q1.enqueue(58);//-------------------2nd

//System.out.println("first out "+q1.dequeue());

//System.out.println("last out "+q1.dequeue());
//System.out.println(q1.peek());
}


public int[] arr;
int count=0;
int top=0;
int size=4;
int queue[]=new int[size];

Queue(int size)
{
arr=new int[size];
}

void enqueue(int val)
{
if(count==arr.length)
{
System.out.println("Queue is full");
}else
{
arr[count]=val;
count++;
}
}

int dequeue()
{

if(count==0)
{
System.out.print("stack is empty");
return -1;
}
else
{
count--;
return arr[top++];
}
}

int peek()
{
if(count==0)
{
System.out.print("queue is empty");
return -1;
}
return arr[count-1];

}

public boolean isempty()
{
if(count==0)
{
System.out.println("Queue is empty");
return true;
}
return false;
}



public void display()
{
for (int i = 0; i < queue.length; i++) {
System.out.println("Queue [" + i + "] " + queue[i]);
}
}

}

Step by Step Solution

3.37 Rating (163 Votes )

There are 3 Steps involved in it

Step: 1

Java program to convert a ArrayList to an array using streams impo... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Information Technology Auditing

Authors: James A. Hall

4th edition

1133949886, 978-1305445154, 1305445155, 978-1133949886

More Books

Students also viewed these Algorithms questions

Question

Compare and contrast the two dimensions of product quality.

Answered: 1 week ago

Question

What is ITF?

Answered: 1 week ago

Question

What is the purpose of the data definition language?

Answered: 1 week ago