Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Completed PreCourse - 1 #1989

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
51 changes: 41 additions & 10 deletions Exercise_1.java
Original file line number Diff line number Diff line change
@@ -1,36 +1,67 @@
class Stack {
class Stack {
// Time Complexity :
// Space Complexity :
//Push - TC - O(1) SC - O(1)
//Peek - TC - O(1) SC - O(1)
//Pop - TC - O(1) SC - O(1)
// Did this code successfully run on Leetcode : not found on leetcode
// Any problem you faced while coding this : I set the top incorrectly first, but fixed it later

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

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

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

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

}

int pop()
{
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 tmp = a[top-1];
return tmp;
}

int peek()
{
{
if(top == 0){
System.out.println("Stack underflow");
return 0;
}
int tmp = a[top-1];
return tmp;
//Write your code here
}
}

}

// Driver code
Expand Down
43 changes: 35 additions & 8 deletions Exercise_2.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
public class StackAsLinkedList {
// Time Complexity :
// Space Complexity :
//Push - TC - O(1) SC - O(1)
//Peek - TC - O(1) SC - O(1)
//Pop - TC - O(1) SC - O(1)
// Did this code successfully run on Leetcode : not found on leetcode
// Any problem you faced while coding this : Yes, figuring out the part on how to handle the root scenario. Also - adding to the start of the LL not the end


class StackAsLinkedList {

StackNode root;

Expand All @@ -7,31 +16,49 @@ static class StackNode {
StackNode next;

StackNode(int data)
{
{
this.data = data;
this.next = null;
//Constructor here
}
}
}


public boolean isEmpty()
{
//Write your code here for the condition if stack is empty.
//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.
{
//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"
//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 tmp = root.data;
root = root.next;
return tmp;
}

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

Expand Down
41 changes: 34 additions & 7 deletions Exercise_3.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import java.io.*;
// Time Complexity :
// Space Complexity :
//N is the length of the linkedList at the point
//Insert - TC - O(N) SC - O(N)
//Print - TC - O(N) SC - O(N)
// Did this code successfully run on Leetcode : didnt find the problem on leetcode
// Any problem you faced while coding this : -


import java.io.*;

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

Node head; // head of list
static Node head; // head of list

// Linked list Node.
// This inner class is made static
Expand All @@ -16,14 +25,16 @@ static class Node {

// Constructor
Node(int d)
{
{
this.data = d;
this.next = null;
//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,
Expand All @@ -33,8 +44,19 @@ public static LinkedList insert(LinkedList list, int data)
// and insert the new_node there

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

Node newNode = new Node(data);
if(list.head == null){
list.head = newNode;
}else{
Node curr = list.head;
while(curr.next !=null ){
curr = curr.next;
}
curr.next = newNode;
}
return list;
}

// Method to print the LinkedList.
Expand All @@ -44,7 +66,12 @@ public static void printList(LinkedList list)

// Print the data at current node

// Go to next node
// Go to next node
Node curr = head;
while(curr != null){
System.out.print(curr.data+" ");
curr = curr.next;
}
}

// Driver code
Expand Down