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
27 changes: 24 additions & 3 deletions Exercise_1.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,59 @@ class Stack {
//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
int a[] = new int[MAX]; // Maximum size of Stack
int numElements=0;

boolean isEmpty()
{
//Write your code here
//Write your code here
return numElements==0;
}

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

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

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

}

int peek()
{
//Write your code here
return this.a[this.top];
}
}

// Driver code
class Main {
class Exercise_1 {
public static void main(String args[])
{
Stack s = new Stack();
Expand Down
52 changes: 35 additions & 17 deletions Exercise_2.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
public class StackAsLinkedList {
// StackAsLinkedList
public class Exercise_2 {
//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());
}
}
class StackAsLinkedList {

StackNode root;

Expand All @@ -9,44 +26,45 @@ static class StackNode {
StackNode(int data)
{
//Constructor here
this.data=data;
}
}


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

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

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

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

//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());
}
}
156 changes: 89 additions & 67 deletions Exercise_3.java
Original file line number Diff line number Diff line change
@@ -1,70 +1,92 @@
import java.io.*;
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

// 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);
}
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
this.data = d;
}
}

// 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 {
// Else traverse till the last node
// and insert the new_node there

// Insert the new_node at last node
Node current = list.head;
while (current.next != null) {
current = current.next;
}
current.next = newNode;
}


// Return the list by head
return list;
}

// 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
Node current = list.head;

while (current != null) {
// Print the data at current node
System.out.print(current.data + " ");

// Go to next node
current = current.next;
}
System.out.println();
}

}

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

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

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

// Print the LinkedList
list.printList(list);
}
}