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 Complete #2057

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
29 changes: 28 additions & 1 deletion Exercise_1.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Time Complexity: O(1) for all operations
// Space Complexity: O(n) //n=size of Stack
class Stack {
//Please read sample.java file before starting.
//Kindly include Time and Space complexity at top of each file
Expand All @@ -8,28 +10,53 @@ class Stack {
boolean isEmpty()
{
//Write your code here
if(top==-1)
{ return true;}

return false;

}

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

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

Expand Down
91 changes: 91 additions & 0 deletions LinkedList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import java.io.*;
//Time Complexity : O(n)
//Space Complexity : O(n)

// 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 newNode=new Node(data);


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

// Return the list by head
return list;
}

// Method to print the LinkedList.
public static void printList(LinkedList list)
{
// Traverse through the LinkedList
Node cNode=list.head;
while(cNode!=null){
// Print the data at current node
System.out.print(cNode.data + " ");
// Go to next node
cNode=cNode.next;
}




}

// 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);
}
}
86 changes: 86 additions & 0 deletions StackAsLinkedList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
//Time Complexity: O(1)
//Space Complexity: O(n)

public class StackAsLinkedList {

StackNode root;

static class StackNode {
int data;
StackNode next;

StackNode(int data)
{
//Constructor here
this.data=data;
this.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 newnode=new StackNode(data);
if(root == null){
root=newnode;
}else{
StackNode temp=root;
root=newnode;
newnode.next=temp;

}

}

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
if(isEmpty()){
System.out.print("Stack Underflow");
return 0;

}
int popped=root.data;
root=root.next;
return popped;
}

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

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

StackAsLinkedList sll = new StackAsLinkedList();

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