Skip to content

Commit 49face4

Browse files
author
krypton36
committed
Data structures in java.
0 parents  commit 49face4

File tree

4 files changed

+352
-0
lines changed

4 files changed

+352
-0
lines changed

LinkedListDS.java

+174
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
/* Joshua Villasenor
2+
masc0431
3+
*/
4+
package data_structures;
5+
import java.util.*;
6+
7+
public class LinkedListDS<E> implements ListADT<E> {
8+
private int size;
9+
private Node <E> head, tail;
10+
private Node <E> pElement,cElement;
11+
12+
public LinkedListDS(){
13+
size = 0;
14+
head= tail = null;
15+
}
16+
17+
public void addFirst(E obj) {
18+
Node <E> newNode = new Node <E>(obj);
19+
20+
if(isEmpty()){
21+
head = tail = newNode;
22+
}
23+
else{
24+
newNode.next = head;
25+
head = newNode;
26+
}
27+
size++;
28+
}
29+
30+
public void addLast(E o) {
31+
32+
if(isEmpty()){
33+
addFirst(o);
34+
}
35+
else{
36+
Node <E> newNode = new Node <E>(o);
37+
tail.next = newNode;
38+
tail = newNode;
39+
size++;
40+
}
41+
}
42+
43+
public E removeFirst() {
44+
if(head == null) return null;
45+
E tmp = head.data;
46+
head = head.next;
47+
size--;
48+
return tmp;
49+
}
50+
51+
public E removeLast() {
52+
if(head == null) return null;
53+
E temp = tail.data;
54+
remove(tail.data);
55+
size--;
56+
return temp;
57+
}
58+
59+
public E peekFirst() {
60+
if(head == null ) return null;
61+
return head.data;
62+
}
63+
64+
public E peekLast() {
65+
if(tail == null) return null;
66+
return tail.data;
67+
}
68+
69+
70+
public E find(E obj) {
71+
if(contains(obj))
72+
return cElement.data;
73+
return null;
74+
}
75+
76+
public boolean remove(E obj) {
77+
boolean rTF = contains(obj);
78+
79+
if(cElement == null) return false;
80+
81+
if(head == tail){
82+
if(rTF)
83+
makeEmpty();
84+
return rTF;
85+
}
86+
87+
if(cElement == head){
88+
head = head.next;
89+
}
90+
91+
if(cElement == tail){
92+
pElement.next = null;
93+
tail = pElement;
94+
}
95+
else{
96+
pElement.next = cElement.next;
97+
}
98+
size--;
99+
return rTF;
100+
}
101+
102+
public void makeEmpty() {
103+
head = null;
104+
tail = null;
105+
size = 0;
106+
}
107+
108+
@SuppressWarnings("unchecked")
109+
public boolean contains(E obj) {
110+
cElement = head;
111+
pElement = head;
112+
113+
114+
while(cElement != null && ((Comparable<E>)obj).compareTo(cElement.data)!=0){
115+
pElement = cElement;
116+
cElement = cElement.next;
117+
}
118+
if(cElement == null) return false;
119+
return cElement != null;
120+
}
121+
122+
public boolean isEmpty() {
123+
return head==null;
124+
}
125+
126+
public boolean isFull() {
127+
return false;
128+
}
129+
130+
public int size() {
131+
return size;
132+
}
133+
134+
public Iterator<E> iterator() {
135+
return new IteratorHelper();
136+
}
137+
138+
@SuppressWarnings("hiding")
139+
private class Node<E>{
140+
E data;
141+
Node<E> next;
142+
143+
public Node(E dat){
144+
data = dat;
145+
next = null;
146+
}
147+
}
148+
149+
class IteratorHelper implements Iterator<E>{
150+
protected Node <E> iterIndex;
151+
152+
public IteratorHelper(){
153+
iterIndex = head;
154+
}
155+
156+
public boolean hasNext(){
157+
return iterIndex != null;
158+
}
159+
160+
public E next(){
161+
if(!hasNext()) throw new NoSuchElementException();
162+
163+
if(iterIndex == null) return iterIndex.data;
164+
165+
E tmp = iterIndex.data;
166+
iterIndex = iterIndex.next;
167+
168+
return tmp;
169+
}
170+
public void remove() {
171+
throw new UnsupportedOperationException();
172+
}
173+
}
174+
}

ListADT.java

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/* Joshua Villasenor
2+
masc0431
3+
*/
4+
package data_structures;
5+
import java.util.Iterator;
6+
7+
8+
public interface ListADT<E> extends Iterable<E> {
9+
10+
// Adds the Object obj to the beginning of the list
11+
public void addFirst(E obj);
12+
13+
// Adds the Object obj to the end of the list
14+
public void addLast(E o);
15+
16+
// Removes the first Object in the list and returns it.
17+
// Returns null if the list is empty.
18+
public E removeFirst();
19+
20+
// Removes the last Object in the list and returns it.
21+
// Returns null if the list is empty.
22+
public E removeLast();
23+
24+
// Returns the first Object in the list, but does not remove it.
25+
// Returns null if the list is empty.
26+
public E peekFirst();
27+
28+
// Returns the last Object in the list, but does not remove it.
29+
// Returns null if the list is empty.
30+
public E peekLast();
31+
32+
// Finds and returns the Object obj if it is in the list, otherwise
33+
// returns null. Does not modify the list in any way
34+
public E find(E obj);
35+
36+
// Removes the first instance of thespecific Object obj from the list, if it exists.
37+
// Returns true if the Object obj was found and removed, otherwise false
38+
public boolean remove(E obj);
39+
40+
// The list is returned to an empty state.
41+
public void makeEmpty();
42+
43+
// Returns true if the list contains the Object obj, otherwise false
44+
public boolean contains(E obj);
45+
46+
// Returns true if the list is empty, otherwise false
47+
public boolean isEmpty();
48+
49+
// Returns true if the list is full, otherwise false
50+
public boolean isFull();
51+
52+
// Returns the number of Objects currently in the list.
53+
public int size();
54+
55+
// Returns an Iterator of the values in the list, presented in
56+
// the same order as the list.
57+
public Iterator<E> iterator();
58+
59+
}

Queue.java

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/* Joshua Villasenor
2+
masc0431
3+
*/
4+
package data_structures;
5+
import java.util.*;
6+
7+
public class Queue<E> implements Iterable<E> {
8+
ListADT <E> list;
9+
10+
public Queue(){
11+
list = new LinkedListDS <E>();
12+
}
13+
// inserts the object obj into the queue
14+
public void enqueue(E obj) {
15+
list.addLast(obj);
16+
}
17+
// removes and returns the object at the front of the queue
18+
public E dequeue() {
19+
return list.removeFirst();
20+
}
21+
22+
// returns the number of objects currently in the queue
23+
public int size() {
24+
return list.size();
25+
}
26+
27+
// returns true if the queue is empty, otherwise false
28+
public boolean isEmpty() {
29+
return list.isEmpty();
30+
}
31+
32+
// returns but does not remove the object at the front of the queue
33+
public E peek() {
34+
return list.peekFirst();
35+
}
36+
37+
// returns true if the Object obj is in the queue
38+
public boolean contains(E obj) {
39+
return list.contains(obj);
40+
}
41+
42+
// returns the queue to an empty state
43+
public void makeEmpty() {
44+
list.makeEmpty();
45+
}
46+
47+
// removes the Object obj if it is in the queue and
48+
// returns true, otherwise returns false.
49+
public boolean remove(E obj){
50+
return list.remove(obj);
51+
}
52+
53+
// returns an iterator of the elements in the queue. The elements
54+
// must be in the same sequence as dequeue would return them.
55+
public Iterator<E> iterator() {
56+
return list.iterator();
57+
}
58+
59+
}

Stack.java

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/* Joshua Villasenor
2+
masc0431
3+
*/
4+
package data_structures;
5+
import java.util.*;
6+
7+
public class Stack <E> implements Iterable<E> {
8+
ListADT <E> list;
9+
public Stack(){
10+
list = new LinkedListDS<E>();
11+
}
12+
// inserts the object obj into the stack
13+
public void push(E obj) {
14+
list.addFirst(obj);
15+
}
16+
17+
// pops and returns the element on the top of the stack
18+
public E pop() {
19+
return list.removeFirst();
20+
}
21+
22+
// returns the number of elements currently in the stack
23+
public int size() {
24+
return list.size();
25+
}
26+
27+
// return true if the stack is empty, otherwise false
28+
public boolean isEmpty() {
29+
return list.isEmpty();
30+
}
31+
32+
// returns but does not remove the element on the top of the stack
33+
public E peek() {
34+
return list.peekFirst();
35+
}
36+
37+
// returns true if the object obj is in the stack,
38+
// otherwise false
39+
public boolean contains(E obj) {
40+
return list.contains(obj);
41+
}
42+
43+
// returns the stack to an empty state
44+
public void makeEmpty() {
45+
list.makeEmpty();
46+
}
47+
48+
// removes the Object obj if it is in the stack and
49+
// returns true, otherwise returns false.
50+
public boolean remove(E obj) {
51+
return list.remove(obj);
52+
}
53+
54+
// returns a iterator of the elements in the stack. The elements
55+
// must be in the same sequence as pop() would return them.
56+
public Iterator<E> iterator() {
57+
return list.iterator();
58+
}
59+
60+
}

0 commit comments

Comments
 (0)