forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from CottonChou/master
Queue&Stack
- Loading branch information
Showing
2 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.coding.work; | ||
|
||
import java.util.NoSuchElementException; | ||
|
||
|
||
|
||
public class Queue { | ||
private Object[] elementdate; | ||
private int size; | ||
private int index; | ||
|
||
public void enQueue(Object o) { | ||
if(o == null){ | ||
throw new IllegalArgumentException(); | ||
} | ||
elementdate[index++] = o; | ||
size = index + 1; | ||
} | ||
|
||
public Object deQueue(){ | ||
if (size == 0) { | ||
throw new NoSuchElementException(); | ||
}else{ | ||
Object out = elementdate[0]; | ||
System.arraycopy(elementdate, 1, elementdate, 0, size - 1); | ||
return out; | ||
} | ||
} | ||
|
||
public boolean isEmpty(){ | ||
|
||
return size == 0; | ||
} | ||
|
||
public int size() { | ||
return size; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.coding.work; | ||
|
||
import java.util.NoSuchElementException; | ||
|
||
public class Stack { | ||
private Object[] elementdata; | ||
private int size; | ||
private int index; | ||
|
||
public void push(Object o) { | ||
if (o == null) { | ||
throw new IllegalArgumentException(); | ||
|
||
} | ||
size = index + 1; | ||
elementdata[index++] = o; | ||
} | ||
|
||
public Object pop() { | ||
if (size == 0) { | ||
throw new NoSuchElementException(); | ||
} | ||
size --; | ||
return elementdata[index--]; | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
private boolean isEmpty() { | ||
return size == 0; | ||
} | ||
|
||
private int size() { | ||
return size; | ||
} | ||
|
||
} |