[go: up one dir, main page]

0% found this document useful (0 votes)
10 views6 pages

Codes

Uploaded by

Ariq Alwin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views6 pages

Codes

Uploaded by

Ariq Alwin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Class: ArrayList.

java

public class ArrayList{


private int [] array;
private int length; //length of array
private int size; //number of content

public ArrayList(int length){


this.length=length;
array = new int[this.length];
}

public void addFront(int data){


addAtIndex(0, data);
}

public void addBack(int data){


addAtIndex(this.size, data);
}

public void addAtIndex(int index, int data){


if(this.length==this.size){
this.length = this.length*2;

int [] b = new int[this.length];


for(int i=0;i<index;i++){
b[i] = this.array[i];
}
b[index]=data;

for(int i=index;i<size;i++){
b[i+1] = this.array[i];
}

this.array=b;

} else {
for(int i=this.size;i>index;i--){
array[i]=array[i-1];
}
array[index]=data;
}
this.size++;
}

public int removeFront(){


//complete your code here
return 0;
}

public int removeBack(){


//complete your code here
return 0;
}

public int removeFromIndex(int index){


//complete your code here
return 0;
}

public String toString(){


String o="";
for(int i=0;i<this.size;i++){
o+=this.array[i]+ " ";
}
return o;
}

class: ArrayListG.java

public class ArrayListG{


private int [] array;
private int length; //length of array
private int size; //number of content

public ArrayListG(int length){


this.length=length;
array = new int[this.length];
}

public void addFront(int data){


addAtIndex(0, data);
}

public void addBack(int data){


addAtIndex(this.size, data);
}
public void addAtIndex(int index, int data){
if(this.length==this.size){
this.length = this.length*2;

int [] b = new int[this.length];


for(int i=0;i<index;i++){
b[i] = this.array[i];
}
b[index]=data;

for(int i=index;i<size;i++){
b[i+1] = this.array[i];
}

this.array=b;

} else {
for(int i=this.size;i>index;i--){
array[i]=array[i-1];
}
array[index]=data;
}
this.size++;
}

public int removeFront(){


//complete your code here
return 0;
}

public int removeBack(){


//complete your code here
return 0;
}

public int removeFromIndex(int index){


//complete your code here
return 0;
}

public String toString(){


String o="";
for(int i=0;i<this.size;i++){
o+=this.array[i]+ " ";
}
return o;
}

class: LinkedList.java

/**
* ES234317 - Algorithms and Data Structures
* Gasal | 2024-2025
* Coursework : 03
* Student ID :
* Full Name :
* Class :
*/

public class LinkedList{


private Node head;
private int size;

public LinkedList(){
this.size=0;
}

public void addFront(int data){


if (this.size==0){
this.head = new Node(data,null);
} else{
Node newHead = new Node(data,this.head);
this.head = newHead;
}
this.size++;
}

public void addBack(int data){


if (this.size==0){
this.head = new Node(data,null);
} else{
Node curr= this.head;
while(curr.link!=null){
curr=curr.link;
}
curr.link = new Node(data,null);
}
this.size++;
}

public void addAtIndex(int index, int data){


if (index==0){
addFront(data);
}
else if(index==size){
addBack(data);
}

else{
Node curr=this.head;
for(int i=0;i<index-1;i++){
curr=curr.link;
}
Node newNode = new Node(data,curr.link);
curr.link=newNode;
}
this.size++;
}

public int removeFromIndex(int index){


//complete your code here
return 0;
}

public int removeFront(){


//complete your code here
return 0;
}

public int removeBack(){


//complete your code here
return 0;
}

public String toString(){


String o="";
Node n = this.head;
do {
o+= n.value+" ";
n= n.link;
}while(n!=null);
return o;
}
}
class: Node.java
public class Node{
int value;
Node link;

public Node(int value, Node link){


this.value = value;
this.link = link;
}
}

Main class:

/**
* Main class of the Java program.
*/

public class Main {

public static void main(String[] args) {


ArrayList myData = new ArrayList(7);
myData.addFront(10);
myData.addFront(20);
myData.addFront(30);
myData.addBack(100);
myData.addAtIndex(1, 200);
myData.addAtIndex(3, 99);
myData.addBack(1000);
myData.addFront(88);
myData.addAtIndex(5, 111);
System.out.println(myData);

LinkedList yourData = new LinkedList();


yourData.addFront(1);
yourData.addFront(2);
yourData.addBack(3);
yourData.addBack(5);
yourData.addAtIndex(1,10);
System.out.println(yourData);
}
}

You might also like