From 831693380dee66899d74589182e8d1a62eaaec2a Mon Sep 17 00:00:00 2001 From: pankhuri0209 Date: Tue, 22 Oct 2024 13:04:15 -0400 Subject: [PATCH 1/5] test push --- Exercise_1.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Exercise_1.java b/Exercise_1.java index 314a3cb45..3460c44c3 100644 --- a/Exercise_1.java +++ b/Exercise_1.java @@ -7,7 +7,10 @@ class Stack { boolean isEmpty() { - //Write your code here + //Write your code here + + + return true; } Stack() @@ -19,17 +22,20 @@ boolean push(int x) { //Check for stack Overflow //Write your code here + return true; } int pop() { //If empty return 0 and print " Stack Underflow" //Write your code here + return 1; } int peek() { //Write your code here + return 1; } } From 7f56134e45f4c37d1c50acecc7c2026a8ff45385 Mon Sep 17 00:00:00 2001 From: pankhuri0209 Date: Tue, 22 Oct 2024 13:32:01 -0400 Subject: [PATCH 2/5] git commit exercise_1 code --- Exercise_1.java | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/Exercise_1.java b/Exercise_1.java index 3460c44c3..d92d1ae4e 100644 --- a/Exercise_1.java +++ b/Exercise_1.java @@ -8,34 +8,53 @@ class Stack { boolean isEmpty() { //Write your code here - - - return true; + if(a.length==0) + { + return true; + } + return false; } Stack() { - //Initialize your constructor + //Initialize your constructor + top = -1; } boolean push(int x) { //Check for stack Overflow //Write your code here + if(isEmpty()) + { + top++; + a[top] = x; + } + a[++top]=x; return true; } int pop() - { + { + if(isEmpty()) + { + System.out.println("Stack Underflow"); + return 0; + } + return a[top--]; //If empty return 0 and print " Stack Underflow" //Write your code here - return 1; + // return 1; } int peek() { //Write your code here - return 1; + if(isEmpty()) + { + return -1; + } + return a[top]; } } From d6543b441101fa380ed783c6ba7ac6ac9cf10373 Mon Sep 17 00:00:00 2001 From: pankhuri0209 Date: Tue, 22 Oct 2024 14:01:00 -0400 Subject: [PATCH 3/5] git commit exercise_2 code --- Exercise_2.java | 127 ++++++++++++++++++++++++++++-------------------- 1 file changed, 75 insertions(+), 52 deletions(-) diff --git a/Exercise_2.java b/Exercise_2.java index 5a9c4868c..d1fccc616 100644 --- a/Exercise_2.java +++ b/Exercise_2.java @@ -1,52 +1,75 @@ -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()); - } -} +class Exercise_2 { + public static class StackAsLinkedList { + + StackNode root; + + static class StackNode { + int data; + StackNode next; + + StackNode(int data) { + //Constructor here + this.data = data; + } + } + + + 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 (isEmpty()) { +// root = newNode; +// } + newNode.next = root; + root = newNode; + // root.next = newNode; + + } + + 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.println("Stack Underflow"); + return 0; + } + int data = root.data; + root = root.next; + return data; + } + + public int peek() { + //Write code to just return the topmost element without removing it. + if (isEmpty()) { + System.out.println("Stack Underflow"); + return 0; + } + int data = root.data; + + return data; + } + } + //Driver code + public static void main(String[] args) { + + StackAsLinkedList sll = new StackAsLinkedList(); + + sll.push(10); + sll.push(20); + sll.push(30); + // sll.pop(); + System.out.println(sll.pop() + " popped from stack"); + + System.out.println("Top element is " + sll.peek()); + } + +} From 1be2ad67ab1e510704a319062dab56694f168df7 Mon Sep 17 00:00:00 2001 From: pankhuri0209 Date: Tue, 22 Oct 2024 14:42:48 -0400 Subject: [PATCH 4/5] git commit exercise_2 code --- Exercise_1.cpp | 54 ----------------------- Exercise_1.js | 35 --------------- Exercise_1.py | 24 ---------- Exercise_2.cpp | 52 ---------------------- Exercise_2.java | 25 ++++++----- Exercise_2.js | 36 --------------- Exercise_2.py | 32 -------------- Exercise_3.cpp | 80 --------------------------------- Exercise_3.java | 115 ++++++++++++++++++++++++++++-------------------- Exercise_3.js | 49 --------------------- Exercise_3.py | 32 -------------- 11 files changed, 80 insertions(+), 454 deletions(-) delete mode 100644 Exercise_1.cpp delete mode 100644 Exercise_1.js delete mode 100644 Exercise_1.py delete mode 100644 Exercise_2.cpp delete mode 100644 Exercise_2.js delete mode 100644 Exercise_2.py delete mode 100644 Exercise_3.cpp delete mode 100644 Exercise_3.js delete mode 100644 Exercise_3.py diff --git a/Exercise_1.cpp b/Exercise_1.cpp deleted file mode 100644 index 381e274d5..000000000 --- a/Exercise_1.cpp +++ /dev/null @@ -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.js b/Exercise_1.js deleted file mode 100644 index 207189ea0..000000000 --- a/Exercise_1.js +++ /dev/null @@ -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 deleted file mode 100644 index 532833f5d..000000000 --- a/Exercise_1.py +++ /dev/null @@ -1,24 +0,0 @@ -class myStack: - #Please read sample.java file before starting. - #Kindly include Time and Space complexity at top of each file - def __init__(self): - - def isEmpty(self): - - def push(self, item): - - def pop(self): - - - def peek(self): - - def size(self): - - def show(self): - - -s = myStack() -s.push('1') -s.push('2') -print(s.pop()) -print(s.show()) diff --git a/Exercise_2.cpp b/Exercise_2.cpp deleted file mode 100644 index 1eb3de9b9..000000000 --- a/Exercise_2.cpp +++ /dev/null @@ -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 d1fccc616..fd0f870c3 100644 --- a/Exercise_2.java +++ b/Exercise_2.java @@ -1,5 +1,4 @@ -class Exercise_2 { - public static class StackAsLinkedList { +class StackAsLinkedList { StackNode root; @@ -58,18 +57,20 @@ public int peek() { return data; } } - //Driver code - public static void main(String[] args) { +class Exercise_2 { - StackAsLinkedList sll = new StackAsLinkedList(); + //Driver code + public static void main(String[] args) { - sll.push(10); - sll.push(20); - sll.push(30); - // sll.pop(); - System.out.println(sll.pop() + " popped from stack"); + StackAsLinkedList sll = new StackAsLinkedList(); - System.out.println("Top element is " + sll.peek()); - } + sll.push(10); + sll.push(20); + sll.push(30); + // sll.pop(); + 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 deleted file mode 100644 index 2e3216f94..000000000 --- a/Exercise_2.js +++ /dev/null @@ -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 deleted file mode 100644 index b11492215..000000000 --- a/Exercise_2.py +++ /dev/null @@ -1,32 +0,0 @@ - -class Node: - def __init__(self, data): - self.data = data - self.next = None - -class Stack: - def __init__(self): - - def push(self, data): - - def pop(self): - -a_stack = Stack() -while True: - #Give input as string if getting an EOF error. Give input like "push 10" or "pop" - print('push ') - print('pop') - print('quit') - do = input('What would you like to do? ').split() - #Give input as string if getting an EOF error. Give input like "push 10" or "pop" - operation = do[0].strip().lower() - if operation == 'push': - a_stack.push(int(do[1])) - elif operation == 'pop': - popped = a_stack.pop() - if popped is None: - print('Stack is empty.') - else: - print('Popped value: ', int(popped)) - elif operation == 'quit': - break diff --git a/Exercise_3.cpp b/Exercise_3.cpp deleted file mode 100644 index f34d89ac1..000000000 --- a/Exercise_3.cpp +++ /dev/null @@ -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..3318c3e81 100644 --- a/Exercise_3.java +++ b/Exercise_3.java @@ -1,70 +1,89 @@ import java.io.*; // Java program to implement -// a Singly Linked List -public class LinkedList { - +// a Singly Linked List + +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; - + static class Node { + + int data; + Node next; + // Constructor - Node(int d) - { - //Write your code here - } - } - + 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) - { + 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 - - } - + + // Else traverse till the last node + // and insert the new_node there + + // Insert the new_node at last node + // Return the list by head + Node newNode = new Node(data); + if (list.head == null) { + list.head = newNode; + } else { + Node curr = list.head; + while (curr.next != null) { + curr = curr.next; + + } + curr.next = newNode; + } + return list; + + } + // Method to print the LinkedList. - public static void printList(LinkedList list) - { + public static void printList(LinkedList list) { // Traverse through the LinkedList - - // Print the data at current node - - // Go to next node - } - + + // Print the data at current node + + // Go to next node + Node curr = list.head; + System.out.print("LinkedaList:"); + while (curr != null) { + System.out.print(curr.data + " "); + curr = curr.next; + } + System.out.println(); + } +} +class Exercise_3 { // Driver code - public static void main(String[] args) - { + public static void main(String[] args) { /* Start with the empty list. */ - LinkedList list = new LinkedList(); - + 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); - + list = LinkedList.insert(list, 1); + list = LinkedList.insert(list, 2); + list = LinkedList.insert(list, 3); + list = LinkedList.insert(list, 4); + list = LinkedList.insert(list, 5); + // Print the LinkedList - printList(list); - } + LinkedList.printList(list); + } } \ No newline at end of file diff --git a/Exercise_3.js b/Exercise_3.js deleted file mode 100644 index d1511f80e..000000000 --- a/Exercise_3.js +++ /dev/null @@ -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 deleted file mode 100644 index a5d466b59..000000000 --- a/Exercise_3.py +++ /dev/null @@ -1,32 +0,0 @@ -class ListNode: - """ - A node in a singly-linked list. - """ - def __init__(self, data=None, next=None): - -class SinglyLinkedList: - def __init__(self): - """ - Create a new singly-linked list. - Takes O(1) time. - """ - self.head = None - - def append(self, data): - """ - Insert a new element at the end of the list. - Takes O(n) time. - """ - - 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. - """ - - def remove(self, key): - """ - Remove the first occurrence of `key` in the list. - Takes O(n) time. - """ From 58dcf89ffc3c91df2804b41559fc598a1b5db9af Mon Sep 17 00:00:00 2001 From: pankhuri0209 Date: Tue, 22 Oct 2024 14:45:36 -0400 Subject: [PATCH 5/5] git commit exercise_2 code --- Exercise_3.java | 1 + 1 file changed, 1 insertion(+) diff --git a/Exercise_3.java b/Exercise_3.java index 3318c3e81..2ad3f37a6 100644 --- a/Exercise_3.java +++ b/Exercise_3.java @@ -85,5 +85,6 @@ public static void main(String[] args) { // Print the LinkedList LinkedList.printList(list); + //Completed Precourse 1 } } \ No newline at end of file