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

Done PreCourse-1 #1991

Open
wants to merge 2 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
32 changes: 31 additions & 1 deletion Exercise_1.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Time complexity O(1) for each operation
// Problems faced: Unable to calculate the time and spce complexity properly

class Stack {
//Please read sample.java file before starting.
//Kindly include Time and Space complexity at top of each file
Expand All @@ -7,29 +10,55 @@ class Stack {

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

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

boolean push(int x)
{
//Check for stack Overflow
if(top>MAX-1)
return false;
else
{
top=top+1;
a[top]=x;
return true;
}
//Write your code here
}

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

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

Expand All @@ -42,5 +71,6 @@ public static void main(String args[])
s.push(20);
s.push(30);
System.out.println(s.pop() + " Popped from stack");

}
}
27 changes: 25 additions & 2 deletions Exercise_3.java → LinkedList.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,34 @@ static class Node {
Node(int d)
{
//Write your code here
this.data =d;


}
}

// Method to insert a new node
public static LinkedList insert(LinkedList list, int data)
{
// Create a new node with given data
Node newNode =new Node(data);

// If the Linked List is empty,
// then make the new node as head
// then make the new node as head
if(list.head==null)
{
list.head=newNode;
}
else
{
Node temp =list.head;
while(temp.next!= null)
{
temp=temp.next;
}
temp.next=newNode;
}
return list;

// Else traverse till the last node
// and insert the new_node there
Expand All @@ -41,7 +59,12 @@ public static LinkedList insert(LinkedList list, int data)
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
40 changes: 39 additions & 1 deletion Exercise_2.java → StackAsLinkedList.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ static class StackNode {

StackNode(int data)
{
this.data=data;
this.next=null;
//Constructor here
}
}
Expand All @@ -16,23 +18,59 @@ static class StackNode {
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.
//Write code to push data to the stack.
StackNode newNode=new StackNode(data) ;
if(root==null)
{
root=newNode;
newNode.next=null;
}
else{
newNode.next=root;
root=newNode;
}
}

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

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

//Driver code
Expand Down