From 3fcd82cbe8578d1f48ef52820e891de241c5a853 Mon Sep 17 00:00:00 2001 From: divija rao Date: Mon, 22 Dec 2025 18:24:14 -0800 Subject: [PATCH 1/7] Done precourse1 --- .DS_Store | Bin 0 -> 6148 bytes Exercise_1.cpp | 54 -------------------------------- Exercise_1.java | 46 ---------------------------- Exercise_1.js | 35 --------------------- Exercise_1.py | 26 +++++++++++----- Exercise_2.cpp | 52 ------------------------------- Exercise_2.java | 52 ------------------------------- Exercise_2.js | 36 ---------------------- Exercise_2.py | 14 +++++++-- Exercise_3.cpp | 80 ------------------------------------------------ Exercise_3.java | 70 ------------------------------------------ Exercise_3.js | 49 ----------------------------- Exercise_3.py | 49 ++++++++++++++++++++--------- 13 files changed, 64 insertions(+), 499 deletions(-) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..dae3e91a92d5c6d08c0f98bcc54c75542c6fff31 GIT binary patch literal 6148 zcmeHKJxT*n5S~f4SO`MObUrU2O%Y3BI}hL?auh3XAf)pge)D74 zbxC1k5oTcKdvAW;+x?229U|h{^KwcwCZZB5NUvi0B5~244BS%ySxv@J|w7Mc5Dpw(*dI+08oD8Rj}4s0+_@B z%#MvAJP@{0pp~+Z7;NS6C(p}{jiHqj>&-aFZ)TrRSZ{|vX*zK>3>ypt16>A&wjE3V zzlENd|GP!83kHILvtodYYF^FoO5R(0FDJb=LGPhIf?Q39(N+x9R*a3b;z=tn$r|zO W*cfU#{8|o-9|7SdBpCP)2EGALswmh1 literal 0 HcmV?d00001 diff --git a/Exercise_1.cpp b/Exercise_1.cpp index 381e274d5..e69de29bb 100644 --- a/Exercise_1.cpp +++ b/Exercise_1.cpp @@ -1,54 +0,0 @@ -#include - -using namespace std; - -#define MAX 1000 - -class Stack { - //Please read sample.java file before starting. - //Kindly include Time and Space complexity at top of each file - int top; - -public: - int a[MAX]; // Maximum size of Stack - - Stack() { //Constructor here } - bool push(int x); - int pop(); - int peek(); - bool isEmpty(); -}; - -bool Stack::push(int x) -{ - //Your code here - //Check Stack overflow as well -} - -int Stack::pop() -{ - //Your code here - //Check Stack Underflow as well -} -int Stack::peek() -{ - //Your code here - //Check empty condition too -} - -bool Stack::isEmpty() -{ - //Your code here -} - -// Driver program to test above functions -int main() -{ - class Stack s; - s.push(10); - s.push(20); - s.push(30); - cout << s.pop() << " Popped from stack\n"; - - return 0; -} diff --git a/Exercise_1.java b/Exercise_1.java index 314a3cb45..e69de29bb 100644 --- a/Exercise_1.java +++ b/Exercise_1.java @@ -1,46 +0,0 @@ -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 a[] = new int[MAX]; // Maximum size of Stack - - boolean isEmpty() - { - //Write your code here - } - - Stack() - { - //Initialize your constructor - } - - boolean push(int x) - { - //Check for stack Overflow - //Write your code here - } - - int pop() - { - //If empty return 0 and print " Stack Underflow" - //Write your code here - } - - int peek() - { - //Write your code here - } -} - -// 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"); - } -} diff --git a/Exercise_1.js b/Exercise_1.js index 207189ea0..e69de29bb 100644 --- a/Exercise_1.js +++ b/Exercise_1.js @@ -1,35 +0,0 @@ -class Stack { - //Please read sample.java file before starting. - //Kindly include Time and Space complexity at top of each file -​ - constructor() { - //Initialize your constructor - this.MAX = 1000; - this.top = -1; - this.a = new Array(this.MAX); - } -​ - function isEmpty() { - //Write your code here - } -​ - function push(x) { - //Check for stack Overflow - //Write your code here - } -​ - function pop() { - //If empty return 0 and print " Stack Underflow" - //Write your code here - } -​ - function peek() { - //Write your code here - } -} -​ -let s = new Stack(); -s.push(10); -s.push(20); -s.push(30); -console.log(s.pop() + " Popped from stack"); diff --git a/Exercise_1.py b/Exercise_1.py index 532833f5d..be1c7436d 100644 --- a/Exercise_1.py +++ b/Exercise_1.py @@ -1,21 +1,31 @@ class myStack: #Please read sample.java file before starting. #Kindly include Time and Space complexity at top of each file - def __init__(self): - + time :0(1) + space:0(n) + def __init__(self): + self.stack = [] def isEmpty(self): - + return len(self.stack) == 0 def push(self, item): - + self.stack.append(item) def pop(self): - + if self.isEmpty(): + print("Stack Underflow") + return None + return self.stack.pop() def peek(self): - + if self.isEmpty(): + print("Stack is Empty") + return None + return self.stack[-1] def size(self): - + return len(self.stack) def show(self): - + return self.stack + + s = myStack() s.push('1') diff --git a/Exercise_2.cpp b/Exercise_2.cpp index 1eb3de9b9..e69de29bb 100644 --- a/Exercise_2.cpp +++ b/Exercise_2.cpp @@ -1,52 +0,0 @@ -#include -using namespace std; - -// A structure to represent a stack -class StackNode { -public: - int data; - StackNode* next; -}; - -StackNode* newNode(int data) -{ - StackNode* stackNode = new StackNode(); - stackNode->data = data; - stackNode->next = NULL; - return stackNode; -} - -int isEmpty(StackNode* root) -{ - //Your code here -} - -void push(StackNode** root, int data) -{ - //Your code here -} - -int pop(StackNode** root) -{ - //Your code here -} - -int peek(StackNode* root) -{ - //Your code here -} - -int main() -{ - StackNode* root = NULL; - - push(&root, 10); - push(&root, 20); - push(&root, 30); - - cout << pop(&root) << " popped from stack\n"; - - cout << "Top element is " << peek(root) << endl; - - return 0; -} \ No newline at end of file diff --git a/Exercise_2.java b/Exercise_2.java index 5a9c4868c..e69de29bb 100644 --- a/Exercise_2.java +++ b/Exercise_2.java @@ -1,52 +0,0 @@ -public class StackAsLinkedList { - - StackNode root; - - static class StackNode { - int data; - StackNode next; - - StackNode(int data) - { - //Constructor here - } - } - - - public boolean isEmpty() - { - //Write your code here for the condition if stack is empty. - } - - public void push(int data) - { - //Write code to push data to the stack. - } - - 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 - } - - public int peek() - { - //Write code to just return the topmost element without removing it. - } - - //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()); - } -} diff --git a/Exercise_2.js b/Exercise_2.js index 2e3216f94..e69de29bb 100644 --- a/Exercise_2.js +++ b/Exercise_2.js @@ -1,36 +0,0 @@ -class StackAsLinkedList { -​ - static stackNode = class { -​ - constructor(d) { - //Constructor here - this.data = d; - this.next = null; - } - } -​ - function isEmpty() { - //Write your code here for the condition if stack is empty. - } -​ - function push(data) { - //Write code to push data to the stack. - } -​ - function pop() { - //If Stack Empty Return 0 and print "Stack Underflow" - //Write code to pop the topmost element of stack. - //Also return the popped element - } -​ - function peek() { - //Write code to just return the topmost element without removing it. - } -} -//Driver code -const sll = new StackAsLinkedList(); -sll.push(10); -sll.push(20); -sll.push(30); -console.log(sll.pop() + " popped from stack"); -console.log("Top element is " + sll.peek()); diff --git a/Exercise_2.py b/Exercise_2.py index b11492215..0617ff42b 100644 --- a/Exercise_2.py +++ b/Exercise_2.py @@ -1,4 +1,5 @@ - +/time 0(1) +/space 0(n) class Node: def __init__(self, data): self.data = data @@ -6,10 +7,17 @@ def __init__(self, data): class Stack: def __init__(self): - + self.top = None def push(self, data): - + new_node = Node(data) + new_node.next = self.top + self.top = new_node def pop(self): + if self.top is None: + return None + popped_value = self.top.data + self.top = self.top.next + return popped_val a_stack = Stack() while True: diff --git a/Exercise_3.cpp b/Exercise_3.cpp index f34d89ac1..e69de29bb 100644 --- a/Exercise_3.cpp +++ b/Exercise_3.cpp @@ -1,80 +0,0 @@ -#include -using namespace std; - -// A linked list node (changes) -class Node -{ - public: - int data; - Node *next; -}; - -/* Given a reference (pointer to pointer) -to the head of a list and an int, inserts -a new node on the front of the list. */ -void push(Node** head_ref, int new_data) -{ - /* 1. allocate node */ - - /* 2. put in the data */ - - /* 3. Make next of new node as head */ - - /* 4. move the head to point to the new node */ -} - -/* Given a node prev_node, insert a new node after the given -prev_node */ -void insertAfter(Node* prev_node, int new_data) -{ - /*1. check if the given prev_node is NULL */ - - /* 2. allocate new node */ - - /* 3. put in the data */ - - /* 4. Make next of new node as next of prev_node */ - - /* 5. move the next of prev_node as new_node */ -} - -/* Given a reference (pointer to pointer) to the head -of a list and an int, appends a new node at the end */ -void append(Node** head_ref, int new_data) -{ - /* 1. allocate node */ - - /* 2. put in the data */ - - /* 3. This new node is going to be - the last node, so make next of - it as NULL*/ - - /* 4. If the Linked List is empty, - then make the new node as head */ - - /* 5. Else traverse till the last node */ - - /* 6. Change the next of last node */ -} - -// This function prints contents of -// linked list starting from head -void printList(Node *node) -{ - //Your code here -} - -/* Driver code*/ -int main() -{ - Node* head = NULL; - append(&head, 6); - push(&head, 7); - push(&head, 1); - append(&head, 4); - insertAfter(head->next, 8); - cout<<"Created Linked list is: "; - printList(head); - return 0; -} \ No newline at end of file diff --git a/Exercise_3.java b/Exercise_3.java index fb66d329d..e69de29bb 100644 --- a/Exercise_3.java +++ b/Exercise_3.java @@ -1,70 +0,0 @@ -import java.io.*; - -// 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 - } - } - - // Method to insert a new node - public static LinkedList insert(LinkedList list, int data) - { - // Create a new node with given data - - // If the Linked List is empty, - // then make the new node as head - - // Else traverse till the last node - // and insert the new_node there - - // Insert the new_node at last node - // Return the list by head - - } - - // Method to print the LinkedList. - public static void printList(LinkedList list) - { - // Traverse through the LinkedList - - // Print the data at current node - - // Go to next node - } - - // 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); - } -} \ No newline at end of file diff --git a/Exercise_3.js b/Exercise_3.js index d1511f80e..e69de29bb 100644 --- a/Exercise_3.js +++ b/Exercise_3.js @@ -1,49 +0,0 @@ -// Java program to implement -// a Singly Linked List -class LinkedList { - constructor() { - this.head = null; - } - // Linked list Node. - static Node = class { - constructor(d) { - this.data = d; - this.next = null; - } - } -​ - // Method to insert a new node - function insert(list, data) { - // Create a new node with given data -​ - // If the Linked List is empty, - // then make the new node as head -​ - // Else traverse till the last node - // and insert the new_node there -​ - // Insert the new_node at last node - // Return the list by head - } -​ - // Method to print the LinkedList. - function printList(list) { - // Traverse through the LinkedList -​ - // Print the data at current node -​ - // Go to next node - } -} - // Driver code - /* Start with the empty list. */ - let list = new LinkedList(); -​ - // ******INSERTION****** - // Insert the values - list.insert(list, 1); - list.insert(list, 2); - list.insert(list, 3); - list.insert(list, 4); - // Print the LinkedList - list.printList(list); diff --git a/Exercise_3.py b/Exercise_3.py index a5d466b59..0ccfe6162 100644 --- a/Exercise_3.py +++ b/Exercise_3.py @@ -3,7 +3,8 @@ class ListNode: A node in a singly-linked list. """ def __init__(self, data=None, next=None): - + self.data = data + self.next = next class SinglyLinkedList: def __init__(self): """ @@ -13,20 +14,40 @@ def __init__(self): self.head = None def append(self, data): - """ - Insert a new element at the end of the list. - Takes O(n) time. - """ + + new_node = ListNode(data) + + if self.head is None: + self.head = new_node + return + + current = self.head + while current.next: + current = curren + def find(self, key): - """ - Search for the first element with `data` matching - `key`. Return the element or `None` if not found. - Takes O(n) time. - """ + + current = self.head + while current: + if current.data == key: + return current + current = current.next + return None + def remove(self, key): - """ - Remove the first occurrence of `key` in the list. - Takes O(n) time. - """ + + current = self.head + prev = None + + while current: + if current.data == key: + if prev: + prev.next = current.next + else: + self.head = current.next + return + prev = current + current = current.next + From e48177f62f77e48b354cb57113b0b764cf324aa4 Mon Sep 17 00:00:00 2001 From: divija rao Date: Mon, 22 Dec 2025 18:55:31 -0800 Subject: [PATCH 2/7] Done precourse1 --- .DS_Store | Bin 6148 -> 6148 bytes 1 file changed, 0 insertions(+), 0 deletions(-) diff --git a/.DS_Store b/.DS_Store index dae3e91a92d5c6d08c0f98bcc54c75542c6fff31..52ec3e6e867a052397ca4b9896d9cb81671cbb25 100644 GIT binary patch delta 113 zcmZoMXfc?uES9f`fq{XAL60GwA(NpbH{Zo2DJMS(D8`{7Xp}Bhd)yIKJ_WCQL55*) ca(-?BP!9tG(}&H1jNjNM7VvFm=lIJH0PXb~kpKVy delta 113 zcmZoMXfc?uESBXN0|NsKgC0XVLncE>ZoZ34QcivnP>kcwSw(r?+T)I>@+oln*va>o jbEC-@WEch~=jRpx^)N6n6>Jt{{Khu1fNwK9$6tN`ua+Jf From 98cc962fcc0b7706061ecd9903595ba54750c00b Mon Sep 17 00:00:00 2001 From: divija rao Date: Mon, 22 Dec 2025 21:50:16 -0800 Subject: [PATCH 3/7] Done precourse1 --- .DS_Store | Bin 6148 -> 6148 bytes 1 file changed, 0 insertions(+), 0 deletions(-) diff --git a/.DS_Store b/.DS_Store index 52ec3e6e867a052397ca4b9896d9cb81671cbb25..397881aaabf35bce35a4e907793c904c713ccb32 100644 GIT binary patch delta 113 zcmZoMXfc?uELP|o0|NsKgC0XVLncE>ZoZ34QcivnP>kdE?7xC?wZ|P% Date: Mon, 22 Dec 2025 21:51:28 -0800 Subject: [PATCH 4/7] Done precourse1 --- .DS_Store | Bin 6148 -> 6148 bytes 1 file changed, 0 insertions(+), 0 deletions(-) diff --git a/.DS_Store b/.DS_Store index 397881aaabf35bce35a4e907793c904c713ccb32..fa7c2a2f8a57f88a12ada4d553b2fa4d004fd4b4 100644 GIT binary patch delta 113 zcmZoMXfc?uELL2bfq{XAL60GwA(NpbH{Zo2DJMS(D8|8K<=JCYd)yIKJ_WCQL55*) ca(-?BP!9tGbHHXn#&2vB3-~s(bNuB80QwUfb^rhX delta 113 zcmZoMXfc?uELP|o0|NsKgC0XVLncE>ZoZ34QcivnP>kdE?7xC?wZ|P% Date: Mon, 22 Dec 2025 21:54:43 -0800 Subject: [PATCH 5/7] pythoncode --- .DS_Store | Bin 6148 -> 6148 bytes 1 file changed, 0 insertions(+), 0 deletions(-) diff --git a/.DS_Store b/.DS_Store index fa7c2a2f8a57f88a12ada4d553b2fa4d004fd4b4..c2e0bf3db858aaec22f481cfa82479c0decf9115 100644 GIT binary patch delta 113 zcmZoMXfc?uELK*Lfq{XAL60GwA(NpbH{Zo2DJMS(D8|8kVB2S-+T)I>@+o-b3o;CY clk;;6fO;4hm=|moWc Date: Mon, 22 Dec 2025 22:15:28 -0800 Subject: [PATCH 6/7] Done precourse1 --- .DS_Store | Bin 6148 -> 6148 bytes 1 file changed, 0 insertions(+), 0 deletions(-) diff --git a/.DS_Store b/.DS_Store index c2e0bf3db858aaec22f481cfa82479c0decf9115..b73327228c1e6451df6c623350def218c8e1cd4a 100644 GIT binary patch delta 113 zcmZoMXfc?uELL$T0|NsKgC0XVLncE>ZoZ34QcivnP>dslMJ(62_P8Ued@+o-b3o;CY clk;;6fO;4hm=|moWc Date: Mon, 22 Dec 2025 22:40:00 -0800 Subject: [PATCH 7/7] Done Precourse1 --- .DS_Store | Bin 6148 -> 6148 bytes 1 file changed, 0 insertions(+), 0 deletions(-) diff --git a/.DS_Store b/.DS_Store index b73327228c1e6451df6c623350def218c8e1cd4a..4caa84dd2ab1fb633129926d0e6f225114d50889 100644 GIT binary patch delta 112 zcmZoMXfc?uEJlr&fq{XAL60GwA(NpbH{Zo2DJMS(D8_L^Go!(@_P8Uedj~u!H delta 113 zcmZoMXfc?uELL$T0|NsKgC0XVLncE>ZoZ34QcivnP>dslMJ(62_P8Ued