Skip to content

Instantly share code, notes, and snippets.

@id4ehsan
Last active September 8, 2019 00:41
Show Gist options
  • Save id4ehsan/4ec9d256a8f500e1c48ca09ee00623ea to your computer and use it in GitHub Desktop.
Save id4ehsan/4ec9d256a8f500e1c48ca09ee00623ea to your computer and use it in GitHub Desktop.
DataStructure in Java
public class Array {
public static void main(String[] args) {
int nums[] = {28, 14, 17, 30 };
}
// Array Limitation
// 1-we need to know number of elements when the array is created
// too small: can't add more element
// too large: wasted space
// 2-Cannot easily insert an element(or remove element) except at the end
// need to shift all element, assuming enough space!
// 3-some operations can be very slow: for example determining whether contain an element(we should go one by one)
public boolean contains(int[] nums, int target) {
for (int i = 0; i < nums.length; i++) {
if (nums[i] == target) {
return true; //found it
}
}
return false; // couldn't find it
}
}
public class LinkedList {
class Node {
int value;
Node next = null;
Node(int value) {
this.value = value;
}
}
protected Node head = null;
protected Node tail = null;
public void addToFront(int value) {
Node newNode = new Node(value);
newNode.next = head;
head = newNode;
if (newNode.next == null) { //the only element
tail = newNode;
}
}
public void addToBack(int value) {
Node newNode = new Node(value);
if (tail == null) { //the only element
head = newNode;
} else {
tail.next = newNode;
}
tail = newNode;
}
public void addAtIndex(int index, int value) {
if (index < 0) { //we can't have negative index
throw new IndexOutOfBoundsException();
} else if (index == 0) { //index zero
addToFront(value);
} else {
Node newNode = new Node(value);
Node current = head;
for (int i = 0; i < index - 1; i++) {
if (current == null) { //index larger than number of elements, we can't go to far
throw new IndexOutOfBoundsException();
}
current = current.next;
}
if (current.next == null) { //index at the tail
tail = newNode;
} else {
newNode.next = current.next;
current.next = newNode;
}
}
}
// Linkedlist Compare to Array
// don't need to know the number of element when the linkedlist is created
// can easily insert element in front of others
public boolean contains(int value) {
Node current = head;
while (current != null) {
if (current.value == value) {
return true;
}
current = current.next;
}
return false;
}
public int getByIndex(int index) {
if (index < 0) {
throw new IndexOutOfBoundsException();
} else {
Node current = head;
for (int i = 0; i < index; i++) {
if (current == null | current.next == null) {
throw new IndexOutOfBoundsException();
}
current = current.next;
}
return current.value;
}
}
public void removeFromFront() {
if (head != null) {
head = head.next;
}
if (head == null) {
tail = null;
}
}
public void removeFromBack() {
if (head == null) { // empty list
return;
} else if (head.equals(tail)) { // single element list
head = null;
tail = null;
} else {
Node current = head;
while (current.next != tail) {
current = current.next;
}
tail = current;
current.next = null;
}
}
public void removeAtIndex(int index) {
if (index < 0) {
throw new IndexOutOfBoundsException();
} else if (index == 0) {
removeFromFront();
} else {
Node current = head;
for (int i = 0; i < index - 1; i++) {
if (current == null) {
throw new IndexOutOfBoundsException();
}
current = current.next;
}
current.next = current.next.next;
if (current.next == null) {
tail = null;
}
}
}
public boolean contains(int value) {
Node current = head;
while (current != null) {
if (current.value == value) {
return true;
}
current = current.next;
}
return false;
}// Still may need to compare against all element in the oreder to search for an element
public int getByIndex(int index) {
if (index < 0) { //we can't have negative index
throw new IndexOutOfBoundsException();
} else {
Node current = head;
for (int i = 0; i < index; i++) {
if (current == null | current.next == null) {//index larger than number of elements, we can't go to far
throw new IndexOutOfBoundsException();
}
current = current.next;
}
return current.value;
}
}
//Getting an element by index requires to following links
public void removeFromFront(){
if(head != null){
head = head.next;
}
if(head == null){
tail = null;
}
}
public void removeFromBack(){
if(head == null){ // empty list
return;
}else if(head.equals(tail)){ // single element list
head = null;
tail = null;
}else{
Node current = head;
while(current.next != tail){
current = current.next;
}
tail = current;
current.next = null;
}
}
public void removeAtIndex(int index){
if(index < 0){
throw new IndexOutOfBoundsException();
}else if(index == 0){
removeFromFront();
}else{
Node current = head;
for(int i=0; i < index-1; i++){
if(current == null){
throw new IndexOutOfBoundsException();
}
current = current.next;
}
current.next = current.next.next;
if(current.next == null){
tail = null;
}
}
}
}
import java.util.ArrayList;
import java.util.Scanner;
public class useAL{
static ArrayList<Integer> numbers = new ArrayList<Integer>();
static Scanner in = new Scanner(System.in);
static int input =0;
public static void main(String[] args) {
while (true) {
System.out.print("Enter a number: ");
input = in.nextInt();
if(input == 0){
break;
}
numbers.add(input);
}
System.out.print("Enter another number: ");
input = in.nextInt();
if(numbers.contains(input)){
System.out.println(input + " is in the list");
}else{
System.out.println(input + " is not in the list");
}
}
}
import java.util.List;
import java.util.LinkedList;
import java.util.Scanner;
public class useLL {
static LinkedList < Integer > numbers = new LinkedList < Integer > ();
static Scanner in = new Scanner(System.in);
static int input = 0;
public static void main(String[] args) {
while (true) {
System.out.print("Enter a number: ");
input = in .nextInt();
if (input == 0) {
break;
}
numbers.add(input);
}
System.out.print("Enter another number: ");
input = in.nextInt();
if (numbers.contains(input)) {
System.out.println(input + " is in the list");
} else {
System.out.println(input + " is not in the list");
}
System.out.println(findMax(numbers)+ "is the Maximum number in list");
}
public static int findMax(List<Integer> values){
int maxValue = values.get(0);
for(Integer value : values){
if(maxValue < value){
maxValue = value;
}
}
return maxValue;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment