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
46 changes: 0 additions & 46 deletions Exercise_1.java

This file was deleted.

52 changes: 0 additions & 52 deletions Exercise_2.java

This file was deleted.

70 changes: 0 additions & 70 deletions Exercise_3.java

This file was deleted.

73 changes: 73 additions & 0 deletions LinkedList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// 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) {
this.data = d;
this.next = null;
}
}

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

if (list.head == null) {
list.head = new_node;
} else {
Node last = list.head;
while (last.next != null) {
last = last.next;
}
last.next = new_node;
}

return list;

}

// Method to print the LinkedList.
public static void printList(LinkedList list) {

Node curr = list.head;

while(curr.next != null){
System.out.print(curr.data + " ");
curr = curr.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);
}
}
52 changes: 52 additions & 0 deletions Stack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Time Complexity: O(1)
// Space Complexity: O(n)

public class Stack {

static final int MAX = 1000;
int top;
int a[] = new int[MAX]; // Maximum size of Stack

boolean isEmpty() {
return (top < 0);
}

Stack() {
top = -1;
}

boolean push(int x) {
if(top>=(MAX-1)){
System.out.println("Stack Overflow");
return false;
}
a[++top] = x;
return true;
}

int pop() {
if(top<0){
System.out.println("Stack Underflow");
return 0;
}
return a[top--];
}

int peek() {
if(top<0){
return 0;
}
return a[top];
}
}

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

public class StackAsLinkedList {

StackNode root;

static class StackNode {
int data;
StackNode next;

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

public boolean isEmpty() {
return (root == null);
}

public void push(int data) {
StackNode newNode = new StackNode(data);
newNode.next = root;
root = newNode;
}

public int pop() {
if (root == null) {
System.out.println("Stack Underflow");
return 0;
}

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

public int peek() {
return root.data;
}

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

Excercise_2 sll = new Excercise_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());
}
}