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 completed #2056

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
32 changes: 28 additions & 4 deletions Exercise_1.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,56 @@ class Stack {
static final int MAX = 1000;
int top;
int a[] = new int[MAX]; // Maximum size of Stack

// Time Complexity :O(1)
// Space Complexity :O(1)
boolean isEmpty()
{
//Write your code here
if(top==-1)
return true;

return false;
}

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

// Time Complexity :O(1)
// Space Complexity :O(N)
boolean push(int x)
{
//Check for stack Overflow
if(top==MAX-1)
return false;
//Write your code here
top++;
a[top] = x;
return true;
}

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

// Time Complexity :O(N)
// Space Complexity :O(1)
int peek()
{
//Write your code here
if(top==-1)
return -1;

return a[top];
}
}

Expand Down
29 changes: 24 additions & 5 deletions Exercise_3.java → LinkedList.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,49 @@ static class Node {
Node(int d)
{
//Write your code here
data = d;
this.next = null;
}
}

// Time Complexity :O(1)
// Space Complexity :O(N)
// Method to insert a new node
public static LinkedList insert(LinkedList list, int data)
{
// Create a new node with given data

Node temp = new Node(data);
// If the Linked List is empty,
// then make the new node as head

if(list.head==null)
{
list.head = temp;
}
// Else traverse till the last node
else{
Node travel = list.head;
while(travel.next!=null){
travel = travel.next;
}
travel.next = temp;
}
// and insert the new_node there

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

return list;
}

// Method to print the LinkedList.
// Time Complexity :O(N)
// Space Complexity :O(N)
public static void printList(LinkedList list)
{
// Traverse through the LinkedList

Node temp = list.head;
while(temp!=null){
System.out.println(temp.data);
temp = temp.next;
}
// Print the data at current node

// Go to next node
Expand Down
36 changes: 32 additions & 4 deletions Exercise_2.java → StackAsLinkedList.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,58 @@ static class StackNode {
StackNode(int data)
{
//Constructor here
this.data = data;
this.next = null;
}
}


// Time Complexity :O(1)
// Space Complexity :O(1)
public boolean isEmpty()
{
//Write your code here for the condition if stack is empty.
if(root==null)
return true;

return false;
}

// Time Complexity :O(1)
// Space Complexity :O(1)
public void push(int data)
{
//Write code to push data to the stack.
StackNode temp = new StackNode(data);
temp.next = root;
root = temp;
}

// Time Complexity :O(1)
// Space Complexity :O(1)
public int pop()
{
//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.
int val = root.data;
root = root.next;
return val;

//Also return the popped element
}

// Time Complexity :O(1)
// Space Complexity :O(1)
public int peek()
{
//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 Down