Skip to content

Fix maven link and correct Stack implementation #1240

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
repositories {
mavenLocal()
maven {
url = 'http://repo.maven.apache.org/maven2'
url = 'https://repo.maven.apache.org/maven2'
}
}

Expand Down
47 changes: 18 additions & 29 deletions src/main/java/com/dataStructures/Stack.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,42 +19,38 @@ public class Stack<E> implements Serializable {
/**
* Position of tail in stack
*/

private int tail = -1;

/**
* Size of stack at any given time
*/

private int size;

/**
* Uninitialized array to hold stack elements.
* WIll be initialized with initial capacity once the object is created
* Will be initialized with initial capacity once the object is created
*/
private Object[] elements;
private E[] elements;

/**
* No argument to create stack object with initial capacity
*/
@SuppressWarnings("unchecked")
public Stack() {
elements = new Object[INITIAL_CAPACITY];
elements = (E[]) new Object[INITIAL_CAPACITY];
}

/**
* Method to check if the given stack is empty or not
*/

public boolean empty() {
return elements == null || size == 0;
}


/**
* Method to check the element on head without removing it
*/

public Object peek() {
public E peek() {
if (empty()) {
throw new EmptyStackException();
}
Expand All @@ -65,13 +61,12 @@ public Object peek() {
/**
* Method to remove the top element from stack
*/

public Object pop() {
public E pop() {
if (empty()) {
throw new EmptyStackException();
}

Object removedElement = elements[tail];
E removedElement = elements[tail];
tail--;
size--;
return removedElement;
Expand All @@ -80,29 +75,23 @@ public Object pop() {
/**
* Method to add element to stack
*/
public Object push(Object e) {

boolean isSuccess = false;
if (tail < (INITIAL_CAPACITY - 1)) {
tail++;
elements[tail] = e;
} else {
Object[] extendedElements = new Object[INITIAL_CAPACITY + EXTENDED_CAPACITY];
System.arraycopy(elements, 0, extendedElements, 0, (tail + 1));
@SuppressWarnings("unchecked")
public void push(E e) {

tail = tail + 1;
if (tail >= INITIAL_CAPACITY) {
E[] extendedElements = (E[]) new Object[INITIAL_CAPACITY + EXTENDED_CAPACITY];
System.arraycopy(elements, 0, extendedElements, 0, tail);
elements = extendedElements;
tail++;
elements[tail] = e;
}
size++;
return e;

elements[tail] = e;
size = size + 1;
}

/**
* Method to search for an element in stack
*/

public int search(Object o) {
public int search(E o) {

int index = -1;
boolean found = false;
Expand All @@ -111,7 +100,7 @@ public int search(Object o) {
}

for (int i = 0; i < size(); i++) {
if (elements[i] == o) {
if (elements[i].equals(o)) {
index = i;
found = true;
break;
Expand Down