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

Pre Course 1 Completed. #2090

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
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"java.debug.settings.onBuildFailureProceed": true
}
42 changes: 39 additions & 3 deletions Exercise_1.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,71 @@
// Time Complexity : all the operations on the stack are O(1)
// Space Complexity : O(n)
// Did this code successfully run on Leetcode : didn't provide the links
// Any problem you faced while coding this : nope


// Your code here along with comments explaining your approach
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;
static final int MAX = 10;
int top;
int a[] = new int[MAX]; // Maximum size of Stack

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

Stack()
{
//Initialize your constructor
//Initialize your constructor
int 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
{
a[++top] = x;
return true;
}


}

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

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

Expand All @@ -41,6 +77,6 @@ public static void main(String args[])
s.push(10);
s.push(20);
s.push(30);
System.out.println(s.pop() + " Popped from stack");
System.out.println(s.pop() +" popped element");
}
}
60 changes: 53 additions & 7 deletions Exercise_2.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
public class StackAsLinkedList {
// Time Complexity : push takes O(1), pop takes O(n), peek takes O(n).
// Space Complexity : O(n)
// Did this code successfully run on Leetcode : didn't provide the links
// Any problem you faced while coding this : nope


// Your code here along with comments explaining your approach
class StackAsLinkedList {

StackNode root;
StackNode root;
StackNode tail;

static class StackNode {
int data;
Expand All @@ -9,30 +17,68 @@ static class StackNode {
StackNode(int data)
{
//Constructor here
this.data = data;
this.next = null;
}
}


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

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

public int pop()
{
//If Stack Empty Return 0 and print "Stack Underflow"
{ //If Stack Empty Return 0 and print "Stack Underflow"
if(root == null)
{
System.out.println("Stack Underflow");
return 0;
}
//Write code to pop the topmost element of stack.
StackNode temp = root;
int x = tail.data;
if(tail == root)
root = tail = null;
else
{
while(temp.next != tail)
temp = temp.next;
tail = temp;
}
return x;
//Also return the popped element
}

public int peek()
{
//Write code to just return the topmost element without removing it.
if(tail == null)
return 0;
return tail.data;
}

//Driver code
Expand Down
162 changes: 93 additions & 69 deletions Exercise_3.java → LinkedList.java
Original file line number Diff line number Diff line change
@@ -1,70 +1,94 @@
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);
}
import java.io.*;
// Time Complexity : adding a new node to the linked list takes O(n) TC, but if we maintain tail pointer then we can achienve it in O(1).
// Space Complexity : O(n)
// Did this code successfully run on Leetcode : didn't provide the links
// Any problem you faced while coding this : nope


// Your code here along with comments explaining your approach
// 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
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 node = new Node(data);

// If the Linked List is empty,
// then make the new node as head
if(list.head == null)
{
list.head = node;
return list;
}
// Else traverse till the last node
// and insert the new_node there
else{
Node temp = list.head;
while(temp.next != null)
temp = temp.next;
temp.next = node;
}
// Insert the new_node at last node
// Return the list by head
return list;
}

// Method to print the LinkedList.
public static void printList(LinkedList list)
{
// Traverse through the LinkedList
Node node = list.head;
while(node!= null)
{
System.out.println(node.data);
node = node.next;
}

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