코드비교

Old VersionNew Version
1 1 
  2class ListQueueNode { 
  3private int data; 
  4public ListQueueNode link; 
  5public ListQueueNode(int data) { 
  6this.data = data; 
  7this.link = null; 
  8} 
  9public int getData() { 
  10return this.data; 
  11} 
  12} 
2class Queue { 13class Queue { 
3int[] elements; 3ListQueueNode head = null;
4int[] temp; 4ListQueueNode front = null;
5int size; 5ListQueueNode rear = null;
6int front; 6int size = 0;
7public Queue() { 7Queue() {
8elements = new int[8]; 8this.size = 8;
9size = 0;   
10front = 0;   
11} 20} 
12void enqueue(int n) { 12void enqueue(int v) {
13if(size == elements.length) { 13ListQueueNode newNode = new ListQueueNode(v);
14temp = new int[size * 2]; 14if(empty()) {
15for(int i = 0; i < size; i++) 15this.head = newNode;
16temp[i] = elements[i]; 16this.front = this.head;
17elements = temp; 17this.rear = this.head;
18} 27} 
19elements[size++] = n; 28else {
   29rear.link = newNode;
   30rear = newNode;
   31}
20} 32} 
21int dequeue() { 33int dequeue() { 
22return elements[front++]; 34ListQueueNode tempNode;
   35int frontdata;
   36if(front.link == null) {
   37frontdata = front.getData();
   38head = null;
   39front = null;
   40rear = null;
   41}
   42else {
   43frontdata = front.getData();
   44tempNode = front.link;
   45head = tempNode;
   46front.link = null;
   47front = head;
   48}
   49return frontdata;
23} 50} 
24boolean empty() { 51boolean empty() { 
25if(size == 0 || front == size) 25return (front == null && rear == null);
26return true;   
27else   
28return false;   
29} 53} 
30int getSize() { 54int getSize() { 
31return size; 55return size; 

sample code

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Queue queue = new Queue();
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
for (int i = 0; i < N; i++) {
int n = sc.nextInt();
if (n == -1) {
if (!queue.empty()) queue.dequeue();
} else {
queue.enqueue(n);
}
}
while (!queue.empty())
System.out.println(queue.dequeue());
}
}
class Queue {
int[] elements;
int[] temp;
int size;
int front;
public Queue() {
elements = new int[8];
size = 0;
front = 0;
}
void enqueue(int n) {
if(size == elements.length) {
temp = new int[size * 2];
for(int i = 0; i < size; i++)
temp[i] = elements[i];
elements = temp;
}
elements[size++] = n;
}
int dequeue() {
return elements[front++];
}
boolean empty() {
if(size == 0 || front == size)
return true;
else
return false;
}
int getSize() {
return size;
}
}
class ListQueueNode {
private int data;
public ListQueueNode link;
public ListQueueNode(int data) {
this.data = data;
this.link = null;
}
public int getData() {
return this.data;
}
}
class Queue {
ListQueueNode head = null;
ListQueueNode front = null;
ListQueueNode rear = null;
int size = 0;
Queue() {
this.size = 8;
}
void enqueue(int v) {
ListQueueNode newNode = new ListQueueNode(v);
if(empty()) {
this.head = newNode;
this.front = this.head;
this.rear = this.head;
}
else {
rear.link = newNode;
rear = newNode;
}
}
int dequeue() {
ListQueueNode tempNode;
int frontdata;
if(front.link == null) {
frontdata = front.getData();
head = null;
front = null;
rear = null;
}
else {
frontdata = front.getData();
tempNode = front.link;
head = tempNode;
front.link = null;
front = head;
}
return frontdata;
}
boolean empty() {
return (front ==  null && rear == null);
}
int getSize() {
return size;
}
}