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
11 changes: 11 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>PreCourse-1</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
</buildSpec>
<natures>
</natures>
</projectDescription>
49 changes: 43 additions & 6 deletions Exercise_1.java
Original file line number Diff line number Diff line change
@@ -1,36 +1,71 @@
class Stack {
//push operation : time complexity to check : O(1) and space : O(1)
//pop operation : time complexity to check : O(1) and space : O(1)
//empty check operation : time complexity to check : O(1) and space : O(1)
//peek operation : time complexity to check : O(1) and space : O(1)
public class Stack {
//Please read sample.java file before starting.
//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


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

}

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

}

// check for stack overflow , if not assign top increment 1 and add data (x) to it
boolean push(int x)
{
//Check for stack Overflow
//Write your code here
if(top==MAX-1){
System.out.println("stack overflow");
return false;
}
else{
a[++top] = x;
System.out.println("Pushed: " + x);
}
return true;
}

// check if top is -1, else decrement top to reduce the pointer and return the value at that array location
int pop()
{
//If empty return 0 and print " Stack Underflow"
//Write your code here
}

if(top==-1){
System.out.print("stack empty");
return -1;

}
return a[top--];


}
// check if top is -1 as there will be no element if -1,else return the stack top thru using top pointer
int peek()
{
//Write your code here
if(top==-1) {
return -1;
}
return a[top];
}

}

// Driver code
Expand All @@ -42,5 +77,7 @@ public static void main(String args[])
s.push(20);
s.push(30);
System.out.println(s.pop() + " Popped from stack");
System.out.println(s.peek() + " Peek from stack");

}
}
52 changes: 45 additions & 7 deletions Exercise_2.java
Original file line number Diff line number Diff line change
@@ -1,38 +1,72 @@


//time complexity push operation: O(1) / space O(1)
//time complexity pop operation: O(1) / space O(1)
//time complexity peek operation: O(1) / space O(1)
//time complexity isEmpty operation: O(1) / space O(1)
public class StackAsLinkedList {

StackNode root;

static class StackNode {
int data;
StackNode next;

//constructor initalized with data
StackNode(int data)
{
//Constructor here
this.data=data;
this.next=null;

}
}


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

//at the root, adding new node pointing newNode.next as root element
public void push(int data)
{
//Write code to push data to the stack.
StackNode newNode = new StackNode(data);
newNode.next=root; // use root as pointer to stack top
root = newNode;

System.out.println(data + " pushed to stack");
}

//check if stack empty and then return 0 else get root.data and update root -> root.next and return it
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(root == null) {
System.out.println("Stack Underflow");
return 0;
}
int popEl = root.data;
root=root.next; // update the top pointer
return popEl;

}

//check stack empty using root null chek else return root.data
public int peek()
//Write code to just return the topmost element without removing it.
{
//Write code to just return the topmost element without removing it.
if(root == null) {
System.out.println("Stack Underflow");
return 0;
}
return root.data;

}

//Driver code
Expand All @@ -46,7 +80,11 @@ public static void main(String[] args)
sll.push(30);

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

System.out.println("Top element is " + sll.peek());
//added line for stack empty check
System.out.println(" stack empty " + sll.isEmpty());
}
}

165 changes: 98 additions & 67 deletions Exercise_3.java
Original file line number Diff line number Diff line change
@@ -1,70 +1,101 @@
import java.io.*;
// Java program to implement
// a Singly Linked List
import java.io.*;

//time complexity insert operation: O(n) / space O(1)

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

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 data)
{
//Write your code here
this.data=data;
this.next=null;
}
}

// new node added if list not empty, O(n) operation
//space O(1) as no new list is created and its the same list
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

Node newNode = new Node(data);

if(list.head==null) {
list.head=newNode;
return list;

}

Node curr = list.head;
while(curr.next!=null) {
curr= curr.next;

}

curr.next = newNode;

return list;

}

// O(n) operation in worst case to traverse linkedlist
public static void printList(LinkedList list)
{
// Traverse through the LinkedList

// Print the data at current node

// Go to next node

Node curr=list.head;
while(curr!=null) {
System.out.println(curr.data + " ");
curr = curr.next;
}
System.out.println();


}

// 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);

// 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);
}
// Print the LinkedList
printList(list);
}
}
20 changes: 20 additions & 0 deletions Sample.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,23 @@


// Your code here along with comments explaining your approach
//Exercise_1 : Implement Stack using Array.
//approach -



//Exercise 2 : implment stack using linkedlist
//approach - use a singly linkedlist
// each node has data and next pointer , ex 1->2->3->4->5->6
// will use a pointer to point the head that is stack top
// to push : each new node created with value x
// set the newnode.next=top (as top points head or stack top)
// update top to newnode
//push,pop,peek all at O(1) time complexity and space is O(1)

//Exercise 3:
//linkedlist - traversal
// maintain curr pointer and add new node if current not null at the end of list
// time complexity-
// insert at end position O(n) - as need to traverse the list and add at end
//space O(1)