Skip to content

update StackOfLinkedList #1081

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 1 commit into from
Oct 18, 2019
Merged
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
118 changes: 74 additions & 44 deletions DataStructures/Stacks/StackOfLinkedList.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ public static void main(String[] args) {
stack.push(4);
stack.push(5);

stack.printStack();
System.out.println(stack);

System.out.println("Size of stack currently is: " + stack.getSize());

stack.pop();
stack.pop();
assert stack.pop() == 5;
assert stack.pop() == 4;

System.out.println("Top element of stack currently is: " + stack.peek());
}
Expand All @@ -48,65 +48,95 @@ public Node(int data) {

class LinkedListStack {

Node head = null;
/**
* Top of stack
*/
Node head;

/**
* Size of stack
*/
private int size;

/**
* Init properties
*/
public LinkedListStack() {
head = null;
size = 0;
}

public void push(int x) {
Node n = new Node(x);
if (head == null) {
head = n;
} else {
Node temp = head;
n.next = temp;
head = n;
}
/**
* Add element at top
*
* @param x to be added
* @return <tt>true</tt> if add successfully
*/
public boolean push(int x) {
Node newNode = new Node(x);
newNode.next = head;
head = newNode;
size++;
return true;
}

public void pop() {
if (head == null) {
System.out.println("Empty stack. Nothing to pop");
/**
* Pop element at top of stack
*
* @return element at top of stack
* @throws NoSuchElementException if stack is empty
*/
public int pop() {
if (size == 0) {
throw new NoSuchElementException("Empty stack. Nothing to pop");
}

Node temp = head;
Node destroy = head;
head = head.next;
System.out.println("Popped element is: " + temp.data);
int retValue = destroy.data;
destroy = null; // clear to let GC do it's work
size--;
return retValue;
}

/**
* Peek element at top of stack
*
* @return element at top of stack
* @throws NoSuchElementException if stack is empty
*/
public int peek() {
if (head == null) {
return -1;
if (size == 0) {
throw new NoSuchElementException("Empty stack. Nothing to pop");
}
return head.data;
}

public void printStack() {
Node temp = head;
System.out.println("Stack is printed as below: ");
while (temp != null) {
if (temp.next == null) {
System.out.print(temp.data);
} else {
System.out.print(temp.data + " -> ");
}
temp = temp.next;
@Override
public String toString() {
Node cur = head;
StringBuilder builder = new StringBuilder();
while (cur != null) {
builder.append(cur.data).append("->");
cur = cur.next;
}
System.out.println();
return builder.replace(builder.length() - 2, builder.length(), "").toString();
}

/**
* Check if stack is empty
*
* @return <tt>true</tt> if stack is empty, otherwise <tt>false</tt>
*/
public boolean isEmpty() {
return head == null;
return size == 0;
}

/**
* Return size of stack
*
* @return size of stack
*/
public int getSize() {
if (head == null)
return 0;
else {
int size = 1;
Node temp = head;
while (temp.next != null) {
temp = temp.next;
size++;
}
return size;
}
return size;
}
}