Skip to content

Commit 084a020

Browse files
committed
some new sorting algorithms added, datastructures stack and queue added
1 parent 16fe1cd commit 084a020

21 files changed

Lines changed: 788 additions & 248 deletions

.idea/vcs.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package main.datastructures;
2+
3+
public interface IQueue<T> {
4+
void add(T element);
5+
T get();
6+
void removeFirst();
7+
boolean isEmpty();
8+
void enqueue(T element);
9+
T dequeue();
10+
T peek();
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package main.datastructures;
2+
3+
public interface IStack<T> {
4+
void push(T element);
5+
void add(T element);
6+
T get();
7+
T pop();
8+
void clear();
9+
void removeLast();
10+
boolean isEmpty();
11+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package main.datastructures;
2+
3+
/******************************************************************************
4+
* Instances of class IntStack are ...
5+
*
6+
*
7+
* @author Krystof Saml
8+
* @version 1.00.0000
9+
*/
10+
11+
public class IntStack {
12+
private Link top;
13+
14+
private static class Link {
15+
public int value;
16+
public Link next;
17+
18+
public Link(int value) {
19+
this.value = value;
20+
}
21+
}
22+
23+
public boolean isEmpty() {
24+
return top == null;
25+
}
26+
27+
public void push(int value) {
28+
Link newLink = new Link(value);
29+
newLink.next = top;
30+
top = newLink;
31+
}
32+
33+
public int pop() {
34+
if (isEmpty()) throw new NullPointerException("Stack is empty");
35+
Link result = top;
36+
top = top.next;
37+
return result.value;
38+
}
39+
40+
public int get() {
41+
if (isEmpty()) throw new NullPointerException("Stack is empty");
42+
return top.value;
43+
}
44+
45+
public void clear() {
46+
top = null;
47+
}
48+
49+
public void removeLast() {
50+
if (isEmpty()) throw new NullPointerException("Stack is empty");
51+
top = top.next;
52+
}
53+
//== CONSTANT CLASS ATTRIBUTES =============================================
54+
//== VARIABLE CLASS ATTRIBUTES =============================================
55+
//== STATIC INITIALIZER BLOCK ==============================================
56+
//== CONSTANT INSTANCE ATTRIBUTES ==========================================
57+
//== VARIABLE INSTANCE ATTRIBUTES ==========================================
58+
//==========================================================================
59+
//== CONSTRUCTORS AND FACTORY METHODS ======================================
60+
//==========================================================================
61+
//== PUBLIC CLASS METHODS ==================================================
62+
//== PRIVATE CLASS METHODS =================================================
63+
//== ACCESS METHODS OF INSTANCES ===========================================
64+
//== PUBLIC METHODS OF INSTANCES ===========================================
65+
//== PRIVATE METHODS OF INSTANCES ==========================================
66+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package main.datastructures;
2+
3+
/******************************************************************************
4+
* Instances of class QueueArray are ...
5+
*
6+
*
7+
* @author Krystof Saml
8+
* @version 1.00.0000
9+
*/
10+
11+
public class QueueArray<T> implements IQueue<T> {
12+
private Object[] array;
13+
private int first;
14+
private int count;
15+
16+
public QueueArray() {
17+
this(10);
18+
}
19+
20+
public QueueArray(int initialCapacity) {
21+
array = new Object[initialCapacity];
22+
first = 0;
23+
count = 0;
24+
}
25+
@Override
26+
public void add(T element) {
27+
if (count == array.length) expandArray();
28+
array[(first + count) % array.length] = element;
29+
count++;
30+
}
31+
32+
private void expandArray() {
33+
Object[] newArray = new Object[array.length * 2];
34+
for (int i = 0; i < array.length; i++) {
35+
newArray[i] = array[(first + i) % array.length];
36+
}
37+
array = newArray;
38+
first = 0;
39+
}
40+
41+
@Override
42+
public T get() {
43+
if (isEmpty()) throw new NullPointerException("Queue is empty");
44+
45+
return (T)array[first];
46+
}
47+
48+
@Override
49+
public void removeFirst() {
50+
if (isEmpty()) throw new NullPointerException("Queue is empty");
51+
array[first] = null;
52+
first = (first + 1) % array.length;
53+
count--;
54+
}
55+
56+
@Override
57+
public boolean isEmpty() {
58+
return count == 0;
59+
}
60+
61+
@Override
62+
public void enqueue(T element) {
63+
this.add(element);
64+
}
65+
66+
@Override
67+
public T dequeue() {
68+
T result = this.get();
69+
removeFirst();
70+
return result;
71+
}
72+
73+
@Override
74+
public T peek() {
75+
return this.get();
76+
}
77+
78+
79+
//== CONSTANT CLASS ATTRIBUTES =============================================
80+
//== VARIABLE CLASS ATTRIBUTES =============================================
81+
//== STATIC INITIALIZER BLOCK ==============================================
82+
//== CONSTANT INSTANCE ATTRIBUTES ==========================================
83+
//== VARIABLE INSTANCE ATTRIBUTES ==========================================
84+
//==========================================================================
85+
//== CONSTRUCTORS AND FACTORY METHODS ======================================
86+
//==========================================================================
87+
//== PUBLIC CLASS METHODS ==================================================
88+
//== PRIVATE CLASS METHODS =================================================
89+
//== ACCESS METHODS OF INSTANCES ===========================================
90+
//== PUBLIC METHODS OF INSTANCES ===========================================
91+
//== PRIVATE METHODS OF INSTANCES ==========================================
92+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package main.datastructures;
2+
3+
/******************************************************************************
4+
* Instances of class QueueLinkedList are ...
5+
*
6+
*
7+
* @author Krystof Saml
8+
* @version 1.00.0000
9+
*/
10+
11+
public class QueueLinkedList<T> implements IQueue<T> {
12+
private Link<T> first;
13+
private Link<T> last;
14+
15+
private static class Link<T> {
16+
T value;
17+
Link<T> next;
18+
19+
public Link(T value) {
20+
this.value = value;
21+
}
22+
23+
}
24+
@Override
25+
public void add(T element) {
26+
Link<T> newLink = new Link<>(element);
27+
if (isEmpty()) {
28+
first = newLink;
29+
} else {
30+
last.next = newLink;
31+
}
32+
last = newLink;
33+
}
34+
35+
@Override
36+
public T get() {
37+
if (isEmpty()) throw new NullPointerException("Queue is empty");
38+
return first.value;
39+
}
40+
41+
@Override
42+
public void removeFirst() {
43+
if (isEmpty()) throw new NullPointerException("Queue is empty");
44+
first = first.next;
45+
}
46+
47+
@Override
48+
public boolean isEmpty() {
49+
return first == null;
50+
}
51+
52+
@Override
53+
public void enqueue(T element) {
54+
this.add(element);
55+
}
56+
57+
@Override
58+
public T dequeue() {
59+
T result = get();
60+
removeFirst();
61+
return result;
62+
}
63+
64+
@Override
65+
public T peek() {
66+
return this.get();
67+
}
68+
//== CONSTANT CLASS ATTRIBUTES =============================================
69+
//== VARIABLE CLASS ATTRIBUTES =============================================
70+
//== STATIC INITIALIZER BLOCK ==============================================
71+
//== CONSTANT INSTANCE ATTRIBUTES ==========================================
72+
//== VARIABLE INSTANCE ATTRIBUTES ==========================================
73+
//==========================================================================
74+
//== CONSTRUCTORS AND FACTORY METHODS ======================================
75+
//==========================================================================
76+
//== PUBLIC CLASS METHODS ==================================================
77+
//== PRIVATE CLASS METHODS =================================================
78+
//== ACCESS METHODS OF INSTANCES ===========================================
79+
//== PUBLIC METHODS OF INSTANCES ===========================================
80+
//== PRIVATE METHODS OF INSTANCES ==========================================
81+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package main.datastructures;
2+
3+
/******************************************************************************
4+
* Instances of class StackArray are ...
5+
*
6+
*
7+
* @author Krystof Saml
8+
* @version 1.00.0000
9+
*/
10+
11+
public class StackArray<T> implements IStack<T> {
12+
private int freeIndex;
13+
private Object[] array;
14+
15+
public StackArray() {
16+
this(10);
17+
}
18+
19+
public StackArray(int initialCapacity) {
20+
if (initialCapacity <= 0) {
21+
throw new IllegalArgumentException("Capacity must be positive number");
22+
}
23+
freeIndex = 0;
24+
array = new Object[initialCapacity];
25+
}
26+
@Override
27+
public void push(T element) {
28+
if (freeIndex == array.length) expandArray();
29+
array[freeIndex++] = element;
30+
}
31+
32+
@Override
33+
public void add(T element) {
34+
this.push(element);
35+
}
36+
37+
private void expandArray() {
38+
Object[] newArray = new Object[array.length * 2];
39+
for (int i = 0; i < array.length; i++) {
40+
newArray[i] = array[i];
41+
}
42+
array = newArray;
43+
}
44+
45+
@Override
46+
public T get() {
47+
if (isEmpty()) throw new NullPointerException("Stack is empty");
48+
return (T) array[freeIndex - 1];
49+
}
50+
51+
@Override
52+
public T pop() {
53+
if (isEmpty()) throw new NullPointerException("Stack is empty");
54+
if (freeIndex < array.length / 2) reduceArray();
55+
T result = (T) array[--freeIndex];
56+
array[freeIndex] = null;
57+
return result;
58+
}
59+
60+
@Override
61+
public void clear() {
62+
array = new Object[10];
63+
}
64+
65+
@Override
66+
public void removeLast() {
67+
if (isEmpty()) throw new NullPointerException("Stack is empty");
68+
if (freeIndex < array.length / 2) reduceArray();
69+
array[--freeIndex] = null;
70+
}
71+
72+
@Override
73+
public boolean isEmpty() {
74+
return freeIndex == 0;
75+
}
76+
77+
public T elementAt(int index) {
78+
if (index < 0 || index >= array.length)
79+
throw new IllegalArgumentException("Index must be between 0 and " + (array.length - 1));
80+
return (T) array[index];
81+
}
82+
83+
private void reduceArray() {
84+
Object[] newArray = new Object[array.length / 2];
85+
for (int i = 0; i < newArray.length; i++) {
86+
newArray[i] = array[i];
87+
}
88+
array = newArray;
89+
}
90+
//== CONSTANT CLASS ATTRIBUTES =============================================
91+
//== VARIABLE CLASS ATTRIBUTES =============================================
92+
//== STATIC INITIALIZER BLOCK ==============================================
93+
//== CONSTANT INSTANCE ATTRIBUTES ==========================================
94+
//== VARIABLE INSTANCE ATTRIBUTES ==========================================
95+
//==========================================================================
96+
//== CONSTRUCTORS AND FACTORY METHODS ======================================
97+
//==========================================================================
98+
//== PUBLIC CLASS METHODS ==================================================
99+
//== PRIVATE CLASS METHODS =================================================
100+
//== ACCESS METHODS OF INSTANCES ===========================================
101+
//== PUBLIC METHODS OF INSTANCES ===========================================
102+
//== PRIVATE METHODS OF INSTANCES ==========================================
103+
}

0 commit comments

Comments
 (0)