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
28 changes: 28 additions & 0 deletions Exercise_1.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,56 @@ class Stack {
boolean isEmpty()
{
//Write your code here
return (top == 0);
}

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

boolean push(int x)
{
//Check for stack Overflow
//Write your code here
if(top == MAX)
{
System.out.println("Stack Overflow");
return false;
}

top += 1;
a[top] = x;
return true;
}

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

int ele = a[top];
top -= 1;
return ele;

}

int peek()
{
//Write your code here
if(top == 0)
{
System.out.println("Stack Underflow");
return 0;
}

return a[top];
}
}

Expand Down
37 changes: 36 additions & 1 deletion Exercise_2.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,65 @@ static class StackNode {
StackNode(int data)
{
//Constructor here
this.data = data;
this.next = null;
}
}

public StackAsLinkedList()
{
this.root = new StackNode(Integer.MIN_VALUE);
}


public boolean isEmpty()
{
//Write your code here for the condition if stack is empty.
//Write your code here for the conditi on if stack is empty.
if(root.data == Integer.MIN_VALUE)
{
return true;
}

return false;
}

public void push(int data)
{
//Write code to push data to the stack.
StackNode temp = new StackNode(data);
temp.next = root;
root = temp;
}

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

StackNode temp = root;
int ele = temp.data;
root = root.next;
temp.next = null;
return ele;

}

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

return root.data;
}

//Driver code
Expand Down
52 changes: 44 additions & 8 deletions Exercise_3.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,43 @@ static class Node {
Node(int d)
{
//Write your code here
this.data = d;
this.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,
// then make the new node as head
if(list.head == null)
{
list.head = newNode;
}

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

// Insert the new_node at last node
// Else traverse till the last node
// and insert the new_node there
// Insert the new_node at last node
// Return the list by head
else
{
Node temp = list.head;

while(temp.next != null)
{
temp = temp.next;
}

temp.next = newNode;

}

return list;


}

Expand All @@ -42,9 +63,24 @@ public static void printList(LinkedList list)
{
// Traverse through the LinkedList

// Print the data at current node

// Go to next node
// Print the data at current node

// Go to next node

if(list == null || list.head == null)
{
System.out.println("The linked list is empty");
return;
}

Node temp = list.head;

while(temp!=null)
{
System.out.print(temp.data + " ");
temp = temp.next;
}

}

// Driver code
Expand Down