diff --git a/Java/Data-Structures/STACKS/ONE-WAY/ArrayStack.java b/Java/Data-Structures/STACKS/FIXED-ARRAY-STACK/ArrayStack.java similarity index 100% rename from Java/Data-Structures/STACKS/ONE-WAY/ArrayStack.java rename to Java/Data-Structures/STACKS/FIXED-ARRAY-STACK/ArrayStack.java diff --git a/Java/Data-Structures/STACKS/ONE-WAY/MainAR.java b/Java/Data-Structures/STACKS/FIXED-ARRAY-STACK/MainAR.java similarity index 100% rename from Java/Data-Structures/STACKS/ONE-WAY/MainAR.java rename to Java/Data-Structures/STACKS/FIXED-ARRAY-STACK/MainAR.java diff --git a/Java/Data-Structures/STACKS/LINKED-STACK/LinkedStack.java b/Java/Data-Structures/STACKS/LINKED-STACK/LinkedStack.java new file mode 100644 index 0000000..a6a7225 --- /dev/null +++ b/Java/Data-Structures/STACKS/LINKED-STACK/LinkedStack.java @@ -0,0 +1,70 @@ +import java.util.*; +public class LinkedStack +{ + private int length; //indicates the size of the linked list + private StackNode top; //acting like a head of linked list + + public LinkedStack() + { + length=0; + top=null; + } + + //adds a specified data to the top of this stack + public void push(int data) + { + StackNode temp = new StackNode(data); + temp.setNext(top); + top=temp; + length++; + } + + //removes the data at the top of this stack and returns a reference to it.throws an empty stack exception if the stack is empty + public int pop() throws EmptyStackException + { + if(isEmpty()) + { + throw new EmptyStackException(); + } + int result = top.getData(); + top = top.getNext(); + length--; + return result; + } + + //returns a reference to the data at the top of this stack. + //the data is not removed from the stack throws an EmptyStackException if the stack is empty + public int peek() throws EmptyStackException + { + if(isEmpty()) + { + throw new EmptyStackException(); + } + return top.getData(); + } + + //returns true if this stack is empty and false otherwise + public boolean isEmpty() + { + return length==0; + } + + //returns the number of eleements in the stack + public int size() + { + return length; + } + + //returns the string representation of the stack + public String toString() + { + String result=""; + StackNode current=top; + while(current!=null) + { + result=result+current.toString()+"\n"; + current=current.getNext(); + } + return result; + } +} \ No newline at end of file diff --git a/Java/Data-Structures/STACKS/LINKED-STACK/MainLS.java b/Java/Data-Structures/STACKS/LINKED-STACK/MainLS.java new file mode 100644 index 0000000..23536e8 --- /dev/null +++ b/Java/Data-Structures/STACKS/LINKED-STACK/MainLS.java @@ -0,0 +1,22 @@ +public class MainLS +{ + public static void main(String[] args) + { + try + { + LinkedStack ls = new LinkedStack(); + ls.push(1); + ls.push(2); + ls.push(3); + ls.push(4); + ls.pop(); + System.out.println(ls); + System.out.println(ls.isEmpty()); + System.out.println(ls.size()); + } + catch(Exception e) + { + System.out.println(e); + } + } +} \ No newline at end of file diff --git a/Java/Data-Structures/STACKS/LINKED-STACK/StackNode.java b/Java/Data-Structures/STACKS/LINKED-STACK/StackNode.java new file mode 100644 index 0000000..c03af02 --- /dev/null +++ b/Java/Data-Structures/STACKS/LINKED-STACK/StackNode.java @@ -0,0 +1,33 @@ +public class StackNode +{ + private int data; + private StackNode next; + + //CONSTRUCTOR + public StackNode(int value) + { + this.data = value; + } + //GETTERS + public int getData() + { + return data; + } + public StackNode getNext() + { + return next; + } + //SETTERS + public void setNext(StackNode next) + { + this.next = next; + } + public void setData(int data) + { + this.data = data; + } + public String toString() + { + return data+""; + } +} \ No newline at end of file diff --git a/Java/Data-Structures/STACKS/ONE-WAY/.gitignore b/Java/Data-Structures/STACKS/ONE-WAY/.gitignore deleted file mode 100644 index ae231e3..0000000 --- a/Java/Data-Structures/STACKS/ONE-WAY/.gitignore +++ /dev/null @@ -1,4 +0,0 @@ -# IGNORE FILES FOR JAVA - -*.class -*.jar \ No newline at end of file diff --git a/Java/README.md b/Java/README.md index 9dd20ba..8cecb99 100644 --- a/Java/README.md +++ b/Java/README.md @@ -29,10 +29,13 @@ #### STACKS -* ONE WAY - * [ArrayStack](Data-Structures/STACKS/ONE-WAY/ArrayStack.java) -* TWO WAY +* FIXED ARRAY STACK + * [FixedArray Stack](Data-Structures/STACKS/FIXED-ARRAY-STACK/ArrayStack.java) +* DYNAMIC ARRAY STACK +* LINKED STACK + * [Linked Stack](Data-Structures/STACKS/LINKED-STACK/LinkedStack.java) * INBUILT STACKS +* MISC STACKS ### :rocket: DYNAMIC PROGRAMMING diff --git a/datastructures.md b/datastructures.md index 6235bda..efb3e5b 100644 --- a/datastructures.md +++ b/datastructures.md @@ -106,37 +106,38 @@ Indexer for Data Structures Lover ### :octocat: STACKS -#### ONE WAY STACK - -##### FIXED ARRAY STACK +#### FIXED ARRAY STACK * blog * docs * implementation * [C++](C++/Data-Structures/STACKS/Stack.hpp) - * [JAVA](Java/Data-Structures/STACKS/ONE-WAY/ArrayStack.java) + * [JAVA](Java/Data-Structures/STACKS/FIXED-ARRAY-STACK/ArrayStack.java) -##### DYNAMIC ARRAY STACK +#### DYNAMIC ARRAY STACK -##### LINKED STACK +#### LINKED STACK -#### TWO WAY STACK +* blog +* docs +* implementation + * [JAVA](Java/Data-Structures/STACKS/LINKED-STACK/LinkedStack.java) -##### FIXED-ARRAY-STACK +#### INBUILT STACKS * blog * docs * implementation - * [C++](C++/Data-Structures/STACKS/Tstack.hpp) + * [C++](C++/Data-Structures/STACKS/stack.cpp) -##### DYNAMIC-ARRAY-STACK +#### MISC STACKS -#### INBUILT STACKS +##### TWO WAY STACK * blog * docs * implementation - * [C++](C++/Data-Structures/STACKS/stack.cpp) + * [C++](C++/Data-Structures/STACKS/Tstack.hpp) ### :octocat: QUEUES diff --git a/docs/complexity.md b/docs/complexity.md index 7571c76..04efa8a 100644 --- a/docs/complexity.md +++ b/docs/complexity.md @@ -76,16 +76,13 @@ This page contains the complexities of different algorithms in this repository. * [ARRAYLISTS](#arraylists-(JAVA)) * [VECTORS](#vectors-(C++JAVA)) * [STACKS](#stacks) - * [ONE WAY](#one-way) - * [FIXED ARRAY STACK](#fixed-array-stack) - * [DYNAMIC ARRAY STACK](#dynamic-array-stack) - * [LINKED STACK](#linked-stack) - * [TWO WAY](#two-way) - * ARRAY-STACK - * LINKED-STACK + * [FIXED ARRAY STACK](#fixed-array-stack) + * [DYNAMIC ARRAY STACK](#dynamic-array-stack) + * [LINKED STACK](#linked-stack) * [INBUILT STACK](#inbuilt-stack) * JAVA * C++ + * [MISC STACKS](#misc-stacks) * QUEUES * HASHTABLE * TREES @@ -347,9 +344,7 @@ SNo. | Operations | Order and Type of Time Complexity O(n) | Order and Type of S ### STACKS -#### ONE WAY - -##### FIXED ARRAY STACK +#### FIXED ARRAY STACK SNo. | Operations | Order and Type of Time Complexity O(n) | Order and Type of Space Complexity ---- | ---------- | -------------------------------------- | ---------------------------------- @@ -359,7 +354,7 @@ SNo. | Operations | Order and Type of Time Complexity O(n) | Order and Type of S 4 | Is empty or not | O(1) -- Constant | O(1) -- Constant 5 | Printing the stack | O(n) -- Linear | O(1) -- Constant -##### DYNAMIC ARRAY STACK +#### DYNAMIC ARRAY STACK SNo. | Operations | Order and Type of Time Complexity O(n) | Order and Type of Space Complexity ---- | ---------- | -------------------------------------- | ---------------------------------- @@ -369,7 +364,7 @@ SNo. | Operations | Order and Type of Time Complexity O(n) | Order and Type of S 4 | Is empty or not | O(1) -- Constant | O(1) -- Constant 5 | Printing the stack | O(n) -- Linear | O(1) -- Constant -##### LINKED STACK +#### LINKED STACK SNo. | Operations | Order and Type of Time Complexity O(n) | Order and Type of Space Complexity ---- | ---------- | -------------------------------------- | ---------------------------------- @@ -379,23 +374,19 @@ SNo. | Operations | Order and Type of Time Complexity O(n) | Order and Type of S 4 | Is empty or not | O(1) -- Constant | O(1) -- Constant 5 | Printing the stack/deletestack | O(n) -- Linear | O(1) -- Constant -#### TWO WAY - -##### ARRAY-STACK - -##### LINKED-STACK - #### INBUILT STACK -#### QUEUES +#### MISC STACKS + +### QUEUES -#### HASHTABLE +### HASHTABLE -#### TREES +### TREES -#### HEAPS +### HEAPS -##### DYNAMIC 1D ARRAYS (C,C++) +#### DYNAMIC 1D ARRAYS (C,C++) SNo. | Operations | Order of complexity O(n) | Type of Complexity ---- | ---------- | ------------------------ | ------------------ @@ -408,7 +399,7 @@ SNo. | Operations | Order and Type of Time Complexity O(n) | Order and Type of S 7 | Update value without having index | O(n) | Linear 8 | Update value having index | O(1) | Constant -##### DYNAMIC 2D ARRAYS (C,C++) +#### DYNAMIC 2D ARRAYS (C,C++) SNo. | Operations | Order of complexity O(n) | Type of Complexity ---- | ---------- | ------------------------ | ------------------ @@ -421,7 +412,7 @@ SNo. | Operations | Order and Type of Time Complexity O(n) | Order and Type of S 7 | Update value without having index | O(n^2) | Quadratic 8 | Update value having index | O(1) | Constant -##### DYNAMIC 3D ARRAYS (C,C++) +#### DYNAMIC 3D ARRAYS (C,C++) SNo. | Operations | Order of complexity O(n) | Type of Complexity ---- | ---------- | ------------------------ | ------------------ @@ -434,6 +425,6 @@ SNo. | Operations | Order and Type of Time Complexity O(n) | Order and Type of S 7 | Update value without having index | O(n^3) | Cubic 8 | Update value having index | O(1) | Constant -#### GRAPHS +### GRAPHS -#### BLOCKCHAIN \ No newline at end of file +### BLOCKCHAIN \ No newline at end of file