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
50 changes: 43 additions & 7 deletions Exercise_1.java
Original file line number Diff line number Diff line change
@@ -1,44 +1,80 @@
// Time Complexity:
//Space Complexity:


class Stack {
//Please read sample.java file before starting.
//Kindly include Time and Space complexity at top of each file
//Kindly include Time and Space complexity at top of each file
static final int MAX = 1000;
int top;
int a[] = new int[MAX]; // Maximum size of Stack

boolean isEmpty()
{
//Write your code here
//Check top
if (top==-1)
{
return true;
}
else
return false;
}

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

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

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

return a[top];
}
}

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

// Driver code
class Main {
public class Main {
public static void main(String args[])
{
Stack s = new Stack();
s.push(10);
s.push(10);
// System.out.println(s.a);
s.push(20);
s.push(30);
System.out.println(s.pop() + " Popped from stack");
Expand Down
136 changes: 84 additions & 52 deletions Exercise_2.java
Original file line number Diff line number Diff line change
@@ -1,52 +1,84 @@
public class StackAsLinkedList {

StackNode root;

static class StackNode {
int data;
StackNode next;

StackNode(int data)
{
//Constructor here
}
}


public boolean isEmpty()
{
//Write your code here for the condition if stack is empty.
}

public void push(int data)
{
//Write code to push data to the stack.
}

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
}

public int peek()
{
//Write code to just return the topmost element without removing it.
}

//Driver code
public static void main(String[] args)
{

StackAsLinkedList sll = new StackAsLinkedList();

sll.push(10);
sll.push(20);
sll.push(30);

System.out.println(sll.pop() + " popped from stack");

System.out.println("Top element is " + sll.peek());
}
}
public class StackAsLinkedList {

StackNode root;

static class StackNode {
int data;
StackNode next;
StackNode previous;
}

void StackAsLinkedList() {
this.root = null;
}

public boolean isEmpty() {
if (root == null) {
return true;
} else
return false;
}

public void push(int data) {
StackNode temp = new StackNode();
if (temp == null) {
System.out.print("\nHeap Overflow");
}
if (root==null){
// System.out.println("In if");
// root=new StackNode();
temp.data=data;
temp.next=null;
temp.previous=null;
root=temp;
}
else{
// System.out.println("In else");
temp.data = data;
temp.previous=root;
root.next = temp;
temp.next=null;
root = temp;
}

}

public int pop() {
int pop_data;
// 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 == null) {
System.out.print("\nStack Underflow");
return 0;
}
pop_data = root.data;
// update the top pointer to point to the next node
root = (root).previous;
return pop_data;
}

public int peek() {
if (!isEmpty()) {
return root.data;
} else {
System.out.println("Stack is empty");
return -1;
}
}

// Driver code
public static void main(String[] args) {

StackAsLinkedList sll = new StackAsLinkedList();

sll.push(10);
sll.push(20);
sll.push(30);

System.out.println(sll.pop() + " popped from stack");

System.out.println("Top element is " + sll.peek());
}

}
139 changes: 70 additions & 69 deletions Exercise_3.java
Original file line number Diff line number Diff line change
@@ -1,70 +1,71 @@
import java.io.*;

// Java program to implement
// a Singly Linked List
public class LinkedList {

Node head; // head of list

// Linked list Node.
// This inner class is made static
// so that main() can access it
static class Node {

int data;
Node next;

// Constructor
Node(int d)
{
//Write your code here
}
}

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

// If the Linked List is empty,
// then make the new node as head

// Else traverse till the last node
// and insert the new_node there
public class StackAsSinglyLinkedList {

// Insert the new_node at last node
// Return the list by head

}

// Method to print the LinkedList.
public static void printList(LinkedList list)
{
// Traverse through the LinkedList

// Print the data at current node

// Go to next node
}

// Driver code
public static void main(String[] args)
{
/* Start with the empty list. */
LinkedList list = new LinkedList();

//
// ******INSERTION******
//

// Insert the values
list = insert(list, 1);
list = insert(list, 2);
list = insert(list, 3);
list = insert(list, 4);
list = insert(list, 5);

// Print the LinkedList
printList(list);
}
}
StackNode root;

static class StackNode {
int data;
StackNode next;
}

void StackAsLinkedList() {
this.root = null;
}

public boolean isEmpty() {
if (root == null) {
return true;
} else
return false;
}

public void push(int data) {
StackNode temp = new StackNode();
if (temp == null) {
System.out.print("\nHeap Overflow");
}

temp.data = data;
temp.next = root;
root = temp;

}

public int pop() {
int pop_data;
// 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 == null) {
System.out.print("\nStack Underflow");
return 0;
}
pop_data = root.data;
// update the top pointer to point to the next node
root = (root).next;
return pop_data;
}

public int peek() {
if (!isEmpty()) {
return root.data;
} else {
System.out.println("Stack is empty");
return -1;
}
}

// Driver code
public static void main(String[] args) {

StackAsLinkedList sll = new StackAsLinkedList();

sll.push(10);
sll.push(20);
sll.push(30);

System.out.println(sll.pop() + " popped from stack");

System.out.println("Top element is " + sll.peek());
}

}