Skip to content
Open
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
29 changes: 29 additions & 0 deletions Exercise_1.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
class Stack {
//Please read sample.java file before starting.
//Time Complexity: 0(1)
//Space Complexity: 0(n)
//Kindly include Time and Space complexity at top of each file
static final int MAX = 1000;
int top;
Expand All @@ -8,28 +10,55 @@ class Stack {
boolean isEmpty()
{
//Write your code here
if(top<0){
return true;
else{
return false;
}
}
}

Stack()
{
//Initialize your constructor
top=-1;
}

boolean push(int x)
{
//Check for stack Overflow
if(top>=Max-1)
{
System.out.println("stack Overflow");
return false;
}
//Write your code here
top = top+1;
a[top]=x;
return true;
}

int pop()
{
//If empty return 0 and print " Stack Underflow"
if(isEmpty()){
System.out.println("Stack Underflow")
return 0;
}
//Write your code here
int value=a[top];
top=top-1;
return value;
}

int peek()
{
//Write your code here
if(isEmpty()){
System.out.println("Stack is Empty");
return 0;
}
return a[top];
}
}

Expand Down
25 changes: 25 additions & 0 deletions Exercise_2.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//Time Complexity: 0(1)
//Space Complexity: 0(n)
public class StackAsLinkedList {

StackNode root;
Expand All @@ -9,30 +11,53 @@ static class StackNode {
StackNode(int data)
{
//Constructor here
this.data=data;
this.next=null;
}
}


public boolean isEmpty()
{
//Write your code here for the condition if stack is empty.
if(root==null){
return true;
}else{
return false;
}
}

public void push(int data)
{
//Write code to push data to the stack.
StackNode newNode =new StackNode(data);
newNode.next=root;
root=newNode;
System.out.println(data+"pushed to stack");
}

public int pop()
{
//If Stack Empty Return 0 and print "Stack Underflow"
if(isEmpty()){
System.out.println("Stack Underflow");
return 0;
}
//Write code to pop the topmost element of stack.
int poppedValue=root.data;
root=root.next;
return poppedValue;
//Also return the popped element
}

public int peek()
{
//Write code to just return the topmost element without removing it.
if(isEmpty()){
System.out.println("Stack is Empty");
return 0;
}
return root.data;
}

//Driver code
Expand Down
22 changes: 20 additions & 2 deletions Exercise_3.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,51 @@ static class Node {
Node(int d)
{
//Write your code here
data=d;
next=null;
}
}

// Method to insert a new node
public static LinkedList insert(LinkedList list, int data)
{
// Create a new node with given data
Node newNode=new Node(data);

// If the Linked List is empty,
if(list.head==null){
list.head=newNode;
}
// then make the new node as head

// Else traverse till the last node
// and insert the new_node there

else{
Node last=list.head;
while (last.next!=null){
last=last.next;
}

// Insert the new_node at last node
last.next=new_node;
}
// Return the list by head
return list;

}

// Method to print the LinkedList.
public static void printList(LinkedList list)
{
// Traverse through the LinkedList
Node currentNode = list.head;
while(currentNode!=null){
System.out.print(currentNode.data+" ");
}

// Print the data at current node

// Go to next node
currentNode=currentNode.next;
}

// Driver code
Expand Down