In this week’s class, we have learnt about the dynamic data structures. Their size are not set before hand, so that it can be used with the increase input of the user. Queue, with its first in first out characteristics can be used in the printing system, while stack, with its first in last out characteristics can be used in creating the action of retrieve.
The following poster includes knowledge about primitive data type, abstract data type, static data structure, and dynamic data structure.
Primitive data types are data types that have pre-defined size inside the computer, for instance, int, short, long, and float are pre-defined data types. The computer knows how much data should be allocated to the data type once they are established.
Abstract data types are the same use as classes. They have their own methods, their own traits, etc.
Static data structure are pre-defined data structure that when user use the structure the computer knows about the size of memory that needs to be allocated.
Dynamic data structure would earn a size once the input or calculation is done.
The following would be the practices we did in learning the data structures.
//Used to learn about singly linked list
import java.util.*;
public class SinglyLinkedListNode {
public int value;
public SinglyLinkedListNode next;
public SinglyLinkedListNode(int value){
this.value=value;
next = null;
}
}
//Implementation of the previous class SinglyLinkedListNode in class SinglyLinkedList
import java.util.*;
public class SinglyLinkedList {
private SinglyLinkedListNode head;
private SinglyLinkedListNode tail;
public SinglyLinkedList(){
head = null;
tail = null;
}
public int traverse(){
int sum = 0;
SinglyLinkedListNode current = head;
SinglyLinkedListNode previous = null;
while ( current != null ) {
sum += current.value;
previous = current;
current = current.next;
}
return sum;
}
public void addFirst ( SinglyLinkedListNode newNode ){
if ( newNode == null )
return;
else{
if( head == null ){
newNode.next = null;
head = newNode;
tail = newNode;
}
else{
newNode.next = null;
head = newNode;
}
}
}
public void addLast ( SinglyLinkedListNode newNode ){
if( newNode == null )
return;
else{
if( head == null){
newNode.next = null;
head = newNode;
tail = newNode;
}
else{
tail.next = newNode;
tail = newNode;
}
}
}
public void addInMiddle ( SinglyLinkedListNode previous, SinglyLinkedListNode newNode ){
if ( newNode == null )
return;
else{
if ( previous == null ) {
addFirst( newNode );
}
else if ( tail == null ){
addLast( newNode );
}
else{
SinglyLinkedListNode next = newNode.next;
previous.next = newNode;
newNode.next = next;
}
}
}
public void removeFirst(){
if( head == null )
return;
else{
if( head == tail ){
head = null;
tail = null;
}
else{
head = head.next;
}
}
}
public void removeLast(){
if( head == null )
return;
else{
if( head == tail ){
head = null;
tail = null;
}
else{
head = head.next;
}
}
}
public void removeInMiddle(SinglyLinkedListNode previous){
if (previous == null)
removeFirst();
else if (previous.next == tail) {
tail = previous;
tail.next = null;
}
else if (previous == tail)
return;
else {
previous.next = previous.next.next;
}
}
}
//Player Class
import java.util.*;
public class Player {
private int id;
private String name;
private String game;
public Player(int i,String n, String g){
id = i;
name = n;
game = g;
}
public int getID(){
return id;
}
public String getName(){
return name;
}
public String getGame(){
return game;
}
public void setID(int i){
id = i;
}
public void setName(String n){
name = n;
}
public void setGame(String g){
game = g;
}
public boolean equals(Object p){
if(!(p instanceof Player)){
return false;
}
else{
Player objPlayer = (Player) p;
return (id==objPlayer.id&&name.equals(objPlayer.name)&&game.equals(objPlayer.game));
}
}
public String toString(int id, String name, String game){
return ("id: "+id+"\n"+"name: "+name+"\n"+"game: "+game);
}
}
//A Player Linked List Node
import java.util.*;
public class PlayerNode {
private Player player;
private PlayerNode next;
public PlayerNode(){
player = null;
next = null;
}
public PlayerNode(Player p){
setPlayer(p);
next = null;
}
public Player getPlayer(){
return player;
}
public PlayerNode getNext(){
return next;
}
public void setPlayer(Player p){
player = p;
}
public void setNext(PlayerNode pn){
next = pn;
}
}
//Linked List Abstract Class
import java.util.*;
public abstract class ShellLinkedList {
protected PlayerNode head;
protected int numberOfItems;
public ShellLinkedList(){
head = null;
numberOfItems = 0;
}
public int getNumberOfItems(){
return numberOfItems;
}
public boolean isEmpty(){
return (numberOfItems == 0);
}
public String toString(){
String listString = "";
PlayerNode current = head;
while(current != null){
listString += current.getPlayer().toString()+"\n";
current = current.getNext();
}
return listString;
}
}
//Data Structure Exception class
import java.util.*;
public class DataStructureException extends NoSuchElementException {
public DataStructureException(String s){
super(s);
}
}
//Player Linked List, inherited from ShellLinkedList class
import java.util.*;
public class PlayerLinkedList extends ShellLinkedList{
public PlayerLinkedList(){
super();
}
public void insert(Player p){
PlayerNode pn = new PlayerNode(p);
pn.setNext(head);
head = pn;
numberOfItems++;
}
public Player delete(int searchID)throws DataStructureException{
PlayerNode current = head;
PlayerNode previous = null;
while (current != null && current.getPlayer().getID() != searchID){
previous = current;
current = current.getNext();
}
if(current == null){
throw new DataStructureException(searchID + "not found: cannot be deleted.");
}
else{
if(current == head){
head = head.getNext();
}
else
previous.setNext(current.getNext());
numberOfItems --;
return current.getPlayer();
}
}
public Player peek(int searchID)throws DataStructureException{
PlayerNode current = head;
while(current != null && current.getPlayer().getID() != searchID){
current = current.getNext();
}
if(current == null){
throw new DataStructureException(searchID + "not found: cannot be deleted.");
}
else{
return current.getPlayer();
}
}
}
//Player Queue
import java.util.*;
public class ArrayQueue {
private static final int QUEUE_SIZE = 8;
private Player[] queue;
private int front;
private int back;
private int numbersOfItems;
public ArrayQueue(){
queue = new Player[QUEUE_SIZE];
front = 0;
back = QUEUE_SIZE-1;
numbersOfItems = 0;
}
public boolean isFull(){
return (numbersOfItems == QUEUE_SIZE);
}
public boolean isEmpty(){
return (numbersOfItems == 0);
}
public String toString(){
String queueString = "";
for(int i = front; i< front + numbersOfItems; i++){
queueString += queue[i%QUEUE_SIZE].toString()+"\n";
}
return queueString;
}
public Player dequeue() throws DataStructureException{
if(!isEmpty()){
front = (front+1)%QUEUE_SIZE;
numbersOfItems--;
return queue[(QUEUE_SIZE+front-1)%QUEUE_SIZE];
}
else
throw new DataStructureException("Queue stack: cannot dequeue");
}
public Player enqueue(Player p) throws DataStructureException{
if(!isFull()){
queue[(back+1)%QUEUE_SIZE]=p;
back = (back+1)%QUEUE_SIZE;
numbersOfItems--;
return queue[(QUEUE_SIZE+front-1)%QUEUE_SIZE];
}
else
throw new DataStructureException("Queue stack: cannot enqueue");
}
}