Init Queue
This commit is contained in:
commit
0d6c151e53
38
.gitignore
vendored
Normal file
38
.gitignore
vendored
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
target/
|
||||||
|
!.mvn/wrapper/maven-wrapper.jar
|
||||||
|
!**/src/main/**/target/
|
||||||
|
!**/src/test/**/target/
|
||||||
|
|
||||||
|
### IntelliJ IDEA ###
|
||||||
|
.idea/modules.xml
|
||||||
|
.idea/jarRepositories.xml
|
||||||
|
.idea/compiler.xml
|
||||||
|
.idea/libraries/
|
||||||
|
*.iws
|
||||||
|
*.iml
|
||||||
|
*.ipr
|
||||||
|
|
||||||
|
### Eclipse ###
|
||||||
|
.apt_generated
|
||||||
|
.classpath
|
||||||
|
.factorypath
|
||||||
|
.project
|
||||||
|
.settings
|
||||||
|
.springBeans
|
||||||
|
.sts4-cache
|
||||||
|
|
||||||
|
### NetBeans ###
|
||||||
|
/nbproject/private/
|
||||||
|
/nbbuild/
|
||||||
|
/dist/
|
||||||
|
/nbdist/
|
||||||
|
/.nb-gradle/
|
||||||
|
build/
|
||||||
|
!**/src/main/**/build/
|
||||||
|
!**/src/test/**/build/
|
||||||
|
|
||||||
|
### VS Code ###
|
||||||
|
.vscode/
|
||||||
|
|
||||||
|
### Mac OS ###
|
||||||
|
.DS_Store
|
||||||
17
pom.xml
Normal file
17
pom.xml
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<groupId>ps.edu</groupId>
|
||||||
|
<artifactId>Queue2</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>23</maven.compiler.source>
|
||||||
|
<maven.compiler.target>23</maven.compiler.target>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
</project>
|
||||||
13
src/main/java/ps/edu/GenericClass.java
Normal file
13
src/main/java/ps/edu/GenericClass.java
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
package ps.edu;
|
||||||
|
|
||||||
|
public class GenericClass {
|
||||||
|
String name;
|
||||||
|
|
||||||
|
public GenericClass(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
}
|
||||||
13
src/main/java/ps/edu/Held.java
Normal file
13
src/main/java/ps/edu/Held.java
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
package ps.edu;
|
||||||
|
|
||||||
|
public class Held {
|
||||||
|
private String name = "";
|
||||||
|
|
||||||
|
public Held(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
}
|
||||||
30
src/main/java/ps/edu/Main.java
Normal file
30
src/main/java/ps/edu/Main.java
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
package ps.edu;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Queue queue = new Queue();
|
||||||
|
|
||||||
|
Held held = new Held("Hero");
|
||||||
|
Held held1 = new Held("Hero1");
|
||||||
|
Held held2 = new Held("Hero2");
|
||||||
|
Held held3 = new Held("Hero3");
|
||||||
|
GenericClass genericClass = new GenericClass("Generic");
|
||||||
|
|
||||||
|
System.out.println(queue.isEmpty());
|
||||||
|
queue.enqueue(held);
|
||||||
|
System.out.println(queue.size());
|
||||||
|
queue.enqueue(held1);
|
||||||
|
queue.enqueue(held2);
|
||||||
|
queue.enqueue(held3);
|
||||||
|
queue.enqueue(genericClass);
|
||||||
|
System.out.println(queue.peek());
|
||||||
|
System.out.println(queue.size());
|
||||||
|
System.out.println(queue.dequeue());
|
||||||
|
System.out.println(queue.dequeue());
|
||||||
|
System.out.println(queue.dequeue());
|
||||||
|
System.out.println(queue.dequeue());
|
||||||
|
System.out.println(queue.dequeue());
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
75
src/main/java/ps/edu/Queue.java
Normal file
75
src/main/java/ps/edu/Queue.java
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
package ps.edu;
|
||||||
|
|
||||||
|
public class Queue {
|
||||||
|
private class Node<T> {
|
||||||
|
private T content = null;
|
||||||
|
private Node nextNode = null;
|
||||||
|
|
||||||
|
private void setContent(T content) {
|
||||||
|
this.content = content;
|
||||||
|
}
|
||||||
|
|
||||||
|
private T getContent() {
|
||||||
|
return this.content;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setNextNode(Node nextNode) {
|
||||||
|
this.nextNode = nextNode;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Node getNextNode() {
|
||||||
|
return this.nextNode;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Node head = null;
|
||||||
|
private Node tail = null;
|
||||||
|
private int count = 0;
|
||||||
|
|
||||||
|
public void enqueue(Object content) {
|
||||||
|
Node node = new Node();
|
||||||
|
node.setContent(content);
|
||||||
|
|
||||||
|
if (head == null) {
|
||||||
|
head = node;
|
||||||
|
tail = node;
|
||||||
|
} else {
|
||||||
|
tail.setNextNode(node);
|
||||||
|
tail = node;
|
||||||
|
}
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object dequeue() {
|
||||||
|
if (!isEmpty()) {
|
||||||
|
Object content = head.getContent();
|
||||||
|
if (head != tail) {
|
||||||
|
head = head.getNextNode();
|
||||||
|
} else {
|
||||||
|
head = null;
|
||||||
|
tail = null;
|
||||||
|
}
|
||||||
|
count--;
|
||||||
|
return content;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isEmpty() {
|
||||||
|
if (head == null) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object peek() {
|
||||||
|
if (head != null) {
|
||||||
|
return head.getContent();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int size() {
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user