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

Precourse 1 file changes in Java #2011

Open
wants to merge 3 commits 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
Binary file added Exercise_1.class
Binary file not shown.
38 changes: 33 additions & 5 deletions Exercise_1.java
Original file line number Diff line number Diff line change
@@ -1,46 +1,74 @@
//Space complexity O(N)
//Time complexity O(1)
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 top =-1;
int a[] = new int[MAX]; // Maximum size of Stack

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

Stack()
{
//Initialize your constructor
}

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

}

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

}

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

}




// Driver code
class Main {
class Exercise_1 {
public static void main(String args[])
{
Stack s = new Stack();
s.push(10);
s.push(20);
s.push(30);
s.push(30);
System.out.println(s.pop() + " Popped from stack");
}
}
40 changes: 34 additions & 6 deletions Exercise_2.java
Original file line number Diff line number Diff line change
@@ -1,50 +1,78 @@
public class StackAsLinkedList {
//TimeCOmplexity -> 0(1)
//Space Complexity -> O(1)
public class Exercise_2 {

StackNode root;

StackNode root = null;

static class StackNode {
int data;
StackNode next;

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


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

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

}

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

}

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


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

StackAsLinkedList sll = new StackAsLinkedList();
Exercise_2 sll = new Exercise_2();

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());
Expand Down
51 changes: 37 additions & 14 deletions Exercise_3.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import java.io.*;

//Time Complexity - > O(N)
//Space Complexity - > O(N)
// Java program to implement
// a Singly Linked List
public class LinkedList {
public class Exercise_3 {

Node head; // head of list

Expand All @@ -18,40 +19,62 @@ static class Node {
Node(int d)
{
//Write your code here
data = d;
next = null;
}
}

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

// Create a new node with given data

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

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





// 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)
public static void printList(Exercise_3 list)
{
Node temp = list.head;
// Traverse through the LinkedList

while (temp.next != null) {
// Print the data at current node

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

}

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

//
// ******INSERTION******
Expand Down