Skip to content

Commit

Permalink
added linked stack in java
Browse files Browse the repository at this point in the history
  • Loading branch information
GauravWalia19 committed Dec 16, 2018
1 parent e0d1a0a commit 47a6793
Show file tree
Hide file tree
Showing 9 changed files with 162 additions and 46 deletions.
70 changes: 70 additions & 0 deletions Java/Data-Structures/STACKS/LINKED-STACK/LinkedStack.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
22 changes: 22 additions & 0 deletions Java/Data-Structures/STACKS/LINKED-STACK/MainLS.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
33 changes: 33 additions & 0 deletions Java/Data-Structures/STACKS/LINKED-STACK/StackNode.java
Original file line number Diff line number Diff line change
@@ -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+"";
}
}
4 changes: 0 additions & 4 deletions Java/Data-Structures/STACKS/ONE-WAY/.gitignore

This file was deleted.

9 changes: 6 additions & 3 deletions Java/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
25 changes: 13 additions & 12 deletions datastructures.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
45 changes: 18 additions & 27 deletions docs/complexity.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
---- | ---------- | -------------------------------------- | ----------------------------------
Expand All @@ -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
---- | ---------- | -------------------------------------- | ----------------------------------
Expand All @@ -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
---- | ---------- | -------------------------------------- | ----------------------------------
Expand All @@ -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
---- | ---------- | ------------------------ | ------------------
Expand All @@ -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
---- | ---------- | ------------------------ | ------------------
Expand All @@ -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
---- | ---------- | ------------------------ | ------------------
Expand All @@ -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
### BLOCKCHAIN

0 comments on commit 47a6793

Please sign in to comment.