-
Notifications
You must be signed in to change notification settings - Fork 641
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 #9 from conf1102/master
Basic Data Structure implementation in Java
- Loading branch information
Showing
4 changed files
with
165 additions
and
30 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
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
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,33 @@ | ||
package com.basic.datastructure; | ||
|
||
public class Queue { | ||
LinkedList list = new LinkedList(); | ||
private int size; | ||
|
||
public void enQueue(Object o){ | ||
list.add(o); | ||
size ++; | ||
} | ||
|
||
public Object deQueue(){ | ||
size --; | ||
return list.removeLast(); | ||
} | ||
|
||
public boolean isEmpty(){ | ||
return list.size() == 0; | ||
} | ||
|
||
public int size(){ | ||
return size; | ||
} | ||
|
||
public static void main(String[] args) { | ||
Queue queue = new Queue(); | ||
for (int i = 0; i < 10; i++) { | ||
queue.enQueue(i); | ||
} | ||
queue.deQueue(); | ||
System.out.println("Finished"); | ||
} | ||
} |
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,41 @@ | ||
package com.basic.datastructure; | ||
|
||
public class Stack { | ||
private ArrayList elementData = new ArrayList(); | ||
private int size; | ||
|
||
public void push(Object o){ | ||
elementData.add(o); | ||
size++; | ||
} | ||
public Object pop(){ | ||
size --; | ||
return elementData.remove(elementData.size() - 1); | ||
} | ||
|
||
/** | ||
* Looks at the object at the top of this stack without removing it from the stack. | ||
* @return Object | ||
*/ | ||
public Object peek(){ | ||
return elementData.get(elementData.size() - 1); | ||
} | ||
public boolean isEmpty(){ | ||
return size == 0; | ||
} | ||
public int size(){ | ||
return size; | ||
} | ||
|
||
public static void main(String[] args) { | ||
Stack stack = new Stack(); | ||
for (int i = 0; i < 10; i++) { | ||
stack.push(i); | ||
} | ||
System.out.println(stack.peek()); | ||
|
||
stack.pop(); | ||
System.out.println("Finished"); | ||
} | ||
|
||
} |