Skip to content

Commit

Permalink
no message
Browse files Browse the repository at this point in the history
  • Loading branch information
Hyup98 committed Aug 25, 2021
1 parent 366a384 commit 2bc2671
Show file tree
Hide file tree
Showing 17 changed files with 158 additions and 170 deletions.
92 changes: 35 additions & 57 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 1 addition & 5 deletions Percolation/PercolationStats.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
/* *****************************************************************************
* Name: Alan Turing
* Coursera User ID: 123456
* Last modified: 1/1/2019
**************************************************************************** */

import edu.princeton.cs.algs4.StdRandom;

public class PercolationStats {
Expand Down
97 changes: 35 additions & 62 deletions Queues/src/Dequeue.java → Queues/src/Deque.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import javax.print.DocFlavor;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;

public class Dequeue<Item> implements Iterable<Item> {

public class Node<Item> {
public class Deque<Item> implements Iterable<Item> {
private class Node {
private Item item;
private Node next;

Expand All @@ -27,10 +23,11 @@ public Item getItem() {
}
}

private Node<Item> first;
private Node first;
private int size;

// construct an empty deque
public Dequeue() {
public Deque() {
first = null;
}

Expand All @@ -44,53 +41,45 @@ public boolean isEmpty() {

// return the number of items on the deque
public int size() {
Node<Item> tem = first;
if(first == null) {
return 0;
}
int count = 0;
while(true) {
if(tem.getNext() == null) {
break;
}
count++;
tem = tem.getNext();
}
return count;
return size;
}

// add the item to the front
public void addFirst(Item item) {
if(item == null) {
throw new java.lang.IllegalArgumentException();
}
Node<Item> tem = new Node(item);
Node tem = new Node(item);
if(first == null) {
first = tem;
size++;
return;
}
tem.setNext(first);
first = tem;
size++;
}

// add the item to the back
public void addLast(Item item) {
if(item == null) {
throw new java.lang.IllegalArgumentException();
}
Node<Item> tem = new Node(item);
Node tem = new Node(item);
if(first == null) {
first = tem;
size++;
return;
}
Node<Item> iter = first;
Node iter = first;
while(true) {
if(iter.getNext() == null) {
break;
}
iter = iter.getNext();
}
iter.setNext(tem);
size++;
}

// remove and return the item from the front
Expand All @@ -100,6 +89,7 @@ public Item removeFirst() {
}
Item tem = first.getItem();
first = first.getNext();
size--;
return tem;
}

Expand All @@ -108,22 +98,25 @@ public Item removeLast() {
if(first == null) {
throw new java.util.NoSuchElementException();
}
Node<Item> iter = first;
while(true) {
if(iter.getNext() == null) {
break;
}
iter = iter.getNext();
Node iter = first;
Node finder = first.getNext();
if(finder == null) {
Item tem = iter.getItem();
first = null;
size--;
return tem;
}
Node<Item> finder = first;
while(true) {
if(finder.getNext() == iter) {
if(finder.getNext() == null) {
break;
}
finder = first.getNext();
iter = iter.getNext();
finder = finder.getNext();
}
Item tem = iter.getItem();
finder.setNext(null);

Item tem = finder.getItem();
iter.setNext(null);
size--;
return tem;
}

Expand All @@ -134,17 +127,20 @@ public Iterator<Item> iterator() {
}

private class QueueIterator implements Iterator<Item> {
private Node<Item> currnet = first;
private Node current = first;

@Override
public boolean hasNext() {
return currnet != null;
return current != null;
}

@Override
public Item next() {
Item item = currnet.getItem();
currnet = currnet.getNext();
if(hasNext() == false) {
throw new java.util.NoSuchElementException();
}
Item item = current.getItem();
current = current.getNext();
return item;
}

Expand All @@ -157,29 +153,6 @@ public void remove() {

// unit testing (required)
public static void main(String[] args) {
Dequeue<Integer> app = new Dequeue<>();
Scanner sc = new Scanner(System.in);
StringBuilder builder = new StringBuilder();
builder.append("----DEQUEUE----\n")
.append("(1) add (2) print (3) exit\n");
while(true) {
System.out.print(builder.toString());

int menu = sc.nextInt();
if(menu == 1) {
app.addLast(sc.nextInt());
}
else if(menu == 2) {
for(Integer i: app) {
System.out.print(i + " ");
}
System.out.print("\n");
}
else {
break;
}

}

}

Expand Down
21 changes: 0 additions & 21 deletions Queues/src/Node.java

This file was deleted.

Loading

0 comments on commit 2bc2671

Please sign in to comment.