Skip to content

Adding a Dynamic List in Java #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
166 changes: 166 additions & 0 deletions DynamicList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
import java.util.Scanner;

public class DynamicList {

public static Scanner sc = new Scanner(System.in);
public static void main(String [] args) throws Exception{

List list = new List();

list.insertEnd(3);
list.insertBegin(0);
list.insert(1, 1);
list.insert(2, 2);
list.insertEnd(5);

list.print();

list.remove(4);

list.print();

}

}

class Cell {

public int element;
public Cell next;

public Cell(){
this(0);
}

public Cell(int element) {
this.element = element;
this.next = null;
}
}

class List {

private Cell first;
private Cell last;

public List() {
first = new Cell();
last = first;
}

public void insertBegin(int x) {
Cell tmp = new Cell(x);
tmp.next = first.next;
first.next = tmp;
if (first == last) {
last = tmp;
}
tmp = null;
}

public void insertEnd(int x) {
last.next = new Cell(x);
last = last.next;
}

public int removeBegin() throws Exception {

if (first == last) {
throw new Exception("Error! List is empty!");
}

Cell tmp = first;
first = first.next;
int resp = first.element;
tmp.next = null;
tmp = null;

return resp;
}


public int removeEnd() throws Exception {

if (first == last) {
throw new Exception("Error! List is empty!");
}

Cell i;
for(i = first; i.next != last; i = i.next);

int resp = last.element;
last = i;
i = last.next = null;

return resp;
}

public void insert(int x, int pos) throws Exception {

int size = size();

if(pos < 0 || pos > size){
throw new Exception("Error! Position (" + pos + " / size = " + size + ") invalid!");
} else if (pos == 0){
insertBegin(x);
} else if (pos == size){
insertEnd(x);
} else {
Cell i = first;
for(int j = 0; j < pos; j++, i = i.next);

Cell tmp = new Cell(x);
tmp.next = i.next;
i.next = tmp;
tmp = i = null;
}
}

public int remove(int pos) throws Exception {

int resp;
int size = size();

if (first == last){
throw new Exception("Error! List is empty!");

} else if(pos < 0 || pos >= size){
throw new Exception("Error! Position (" + pos + " / size = " + size + ") invalid!");
} else if (pos == 0){
resp = removeBegin();
} else if (pos == size - 1){
resp = removeEnd();
} else {
Cell i = first;
for(int j = 0; j < pos; j++, i = i.next);

Cell tmp = i.next;
resp = tmp.element;
i.next = tmp.next;
tmp.next = null;
i = tmp = null;
}

return resp;
}

public void print() {

int position = 0;

for (Cell i = first.next; i != null; i = i.next) {

System.out.print("[" +position+ "]" + i.element + " ");
position++;
}

System.out.println();
}

public int size() {
int size = 0;
for(Cell i = first; i != last; i = i.next, size++);
return size;
}

}