Skip to content
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 .DS_Store
Binary file not shown.
6 changes: 6 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="src" path=""/>
<classpathentry kind="output" path=""/>
</classpath>
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/LinkedList$Node.class
/LinkedList.class
/Main.class
/Stack.class
/StackAsLinkedList$StackNode.class
/StackAsLinkedList.class
17 changes: 17 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>PreCourse1</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
53 changes: 44 additions & 9 deletions Exercise_1.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,69 @@ 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;
int a[] = new int[MAX]; // Maximum size of Stack

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

// Time Complexity: O(1)
// Space Complexity: O(1)
public boolean isFull() {
if(top== MAX-1) {
return true;
}
return false;
}

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


// Time Complexity: O(1)
// Space Complexity: O(1)
boolean push(int x)
{
//Check for stack Overflow
//Write your code here
if(isFull()) {
System.out.println("Stack Overflow");
return false;
}
a[++top]= x;
System.out.println("Pushed");
return true;

}

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

return a[top--];
}


// Time Complexity: O(1)
// Space Complexity: O(1)
int peek()
{
//Write your code here
if(isEmpty()) {
return 0;
}
return a[top];
}
}

Expand Down
52 changes: 0 additions & 52 deletions Exercise_2.java

This file was deleted.

49 changes: 34 additions & 15 deletions Exercise_3.java → LinkedList.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,41 @@
// a Singly Linked List
public class LinkedList {

Node head; // head of list
Node head; // head of list

// Linked list Node.
// This inner class is made static
// so that main() can access it
static class Node {
class Node {

int data;
Node next;

// Constructor
Node(int d)
{
//Write your code here
this.data=d;
}
}

// Method to insert a new node
public static LinkedList insert(LinkedList list, int data)
//Time Complexity- O(n)
public 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 {
Node curr=list.head;
while(curr.next!=null) {
curr=curr.next;
}
curr.next= newNode;
}
return list;


// Else traverse till the last node
// and insert the new_node there
Expand All @@ -37,9 +48,17 @@ public static LinkedList insert(LinkedList list, int data)

}

// Method to print the LinkedList.
public static void printList(LinkedList list)
// Method to print the LinkedList.
//Time Complexity- O(n)
public void printList(LinkedList list)
{
Node curr=list.head;
while(curr.next!=null) {
System.out.println(curr.data + ",");
curr=curr.next;
}
System.out.println(curr.data);

// Traverse through the LinkedList

// Print the data at current node
Expand All @@ -58,13 +77,13 @@ public static void main(String[] args)
//

// Insert the values
list = insert(list, 1);
list = insert(list, 2);
list = insert(list, 3);
list = insert(list, 4);
list = insert(list, 5);
list = list.insert(list, 1);
list = list.insert(list, 2);
list = list.insert(list, 3);
list = list.insert(list, 4);
list = list.insert(list, 5);

// Print the LinkedList
printList(list);
list.printList(list);
}
}
82 changes: 82 additions & 0 deletions StackAsLinkedList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
public class StackAsLinkedList {

StackNode root;

static class StackNode {
int data;
StackNode next;

StackNode(int data)
{
this.data= data;
}
}


// Time Complexity: O(1)
// Space Complexity: O(1)
public boolean isEmpty()
{
if(root== null) {
return true;

}
return false;
}


// Time Complexity: O(1)
// Space Complexity: O(1)
public void push(int data)
{
StackNode newNode= new StackNode(data);
newNode.next= root;
root= newNode;
System.out.println("Pushed");



}

// Time Complexity: O(1)
// Space Complexity: O(1)
public int pop()
{
if(isEmpty()) {
System.out.println("Stack Underflow");
return 0;
}
int x= root.data;
root= root.next;
return x;


//Also return the popped element
}

// Time Complexity: O(1)
// Space Complexity: O(1)
public int peek()
{
if(isEmpty()) {
System.out.println("Stack Underflow");
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());
}
}