-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e0d1a0a
commit 47a6793
Showing
9 changed files
with
162 additions
and
46 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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+""; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters