From 2551bedae36d8cb7cf8362b8c52a06137a246f5a Mon Sep 17 00:00:00 2001 From: ZoranPandovski Date: Mon, 27 Sep 2021 21:24:15 +0200 Subject: [PATCH] Cleanup directories --- .../LinkedList/Javascript/linked_list.css | 26 ++++ .../LinkedList/Javascript/linked_list.html | 9 ++ .../LinkedList/Javascript/linked_list.js | 130 ++++++++++++++++++ .../stack/C/Array_implementation_stack.c | 55 ++++++++ data_structures/Stack/stack/C/LINKED_STACK.c | 46 +++++++ data_structures/Stack/stack/JAVA/STACK.java | 92 +++++++++++++ data_structures/Stack/stack/Python/stack.py | 32 +++++ .../Stack/stack/Python/stackClass.py | 13 ++ data_structures/Stack/stack/README.md | 1 + data_structures/Stack/stack/SpecialStack.java | 66 +++++++++ .../Stack/stack/c++/balanced_paranthesis.cpp | 42 ++++++ .../Stack/stack/c++/mehmetSahin/Stack.cpp | 102 ++++++++++++++ .../Stack/stack/c++/mehmetSahin/Stack.hpp | 74 ++++++++++ .../Stack/stack/c++/mehmetSahin/testStack.cpp | 28 ++++ .../Stack/stack/c++/parenthesisChecker.cpp | 57 ++++++++ .../stack/c/Array_implementation_stack.c | 55 ++++++++ .../renatoSantosReverseArray/Reverse.js | 13 ++ 17 files changed, 841 insertions(+) create mode 100644 data_structures/Linked_list/LinkedList/Javascript/linked_list.css create mode 100644 data_structures/Linked_list/LinkedList/Javascript/linked_list.html create mode 100644 data_structures/Linked_list/LinkedList/Javascript/linked_list.js create mode 100644 data_structures/Stack/stack/C/Array_implementation_stack.c create mode 100644 data_structures/Stack/stack/C/LINKED_STACK.c create mode 100644 data_structures/Stack/stack/JAVA/STACK.java create mode 100644 data_structures/Stack/stack/Python/stack.py create mode 100644 data_structures/Stack/stack/Python/stackClass.py create mode 100644 data_structures/Stack/stack/README.md create mode 100644 data_structures/Stack/stack/SpecialStack.java create mode 100644 data_structures/Stack/stack/c++/balanced_paranthesis.cpp create mode 100644 data_structures/Stack/stack/c++/mehmetSahin/Stack.cpp create mode 100644 data_structures/Stack/stack/c++/mehmetSahin/Stack.hpp create mode 100644 data_structures/Stack/stack/c++/mehmetSahin/testStack.cpp create mode 100644 data_structures/Stack/stack/c++/parenthesisChecker.cpp create mode 100644 data_structures/Stack/stack/c/Array_implementation_stack.c create mode 100644 data_structures/Stack/stack/javaScript/renatoSantosReverseArray/Reverse.js diff --git a/data_structures/Linked_list/LinkedList/Javascript/linked_list.css b/data_structures/Linked_list/LinkedList/Javascript/linked_list.css new file mode 100644 index 0000000000..47d8af7a64 --- /dev/null +++ b/data_structures/Linked_list/LinkedList/Javascript/linked_list.css @@ -0,0 +1,26 @@ +.node-list{ + margin-top:10px; +} +.node{ + display:inline-block; +} +.node span{ + display:inline-block; + border:1px solid #ccc; + border-radius:100%; + padding:8px; + transition:1s all; +} +.node:before{ + content:''; + display:inline-block; + width:50px; + height:0px; + margin-bottom:4px; + line-height:1px; + border:1px solid #333; + border-width:1px 0 0; +} +.zoom{ + transform:scale(2) +} diff --git a/data_structures/Linked_list/LinkedList/Javascript/linked_list.html b/data_structures/Linked_list/LinkedList/Javascript/linked_list.html new file mode 100644 index 0000000000..3eafed23fd --- /dev/null +++ b/data_structures/Linked_list/LinkedList/Javascript/linked_list.html @@ -0,0 +1,9 @@ +
+ + + + +
+ + +
diff --git a/data_structures/Linked_list/LinkedList/Javascript/linked_list.js b/data_structures/Linked_list/LinkedList/Javascript/linked_list.js new file mode 100644 index 0000000000..90deab7646 --- /dev/null +++ b/data_structures/Linked_list/LinkedList/Javascript/linked_list.js @@ -0,0 +1,130 @@ +var Node = function(data){ + this.data=data; + this.next=null; +} + +var LinkedList = function(){ + this.next=null +} + +LinkedList.prototype.addNode = function(value){ + var ref= this; + while(ref.next != null){ + ref = ref.next + }; + ref.next = new Node(value) +} + +LinkedList.prototype.findNode = function(value){ + var ref= this.next; + while(ref != null){ + if(ref.data == value){ + return ref + } + ref = ref.next + }; + + return null +} + +LinkedList.prototype.removeNode = function(value){ + var ref= this.next, + prev = this, + next = null; + while(ref != null){ + if(ref.data == value){ + next = ref.next; + prev.next = next; + return true; + } + prev = ref; + ref = ref.next; + }; + + return false +} + +var ll =new LinkedList(); + +//ll.addNode(new Node(10)); + +var submit =document.getElementsByClassName('js-submit')[0]; + +var find =document.getElementsByClassName('js-find')[0]; + +var remove =document.getElementsByClassName('js-remove')[0]; + +var nodeList = document.getElementsByClassName('node-list')[0]; + +submit.addEventListener('click',function(e){ + ll.addNode(document.getElementsByClassName('value')[0].value); + renderView(); +}) + +remove.addEventListener('click',function(e){ + ll.removeNode(document.getElementsByClassName('value')[0].value); + renderView(); +}) + +find.addEventListener('click',function(e){ + var value = document.getElementsByClassName('value')[0].value; + if(ll.findNode(value) != null){ + _zoom(document.getElementsByClassName(value),true); + setTimeout(function(){ + _zoom(document.getElementsByClassName(value),false); + },1500) + } + +}) + +function _zoom(ref,flag){ + if(flag) { + console.log(ref) + for (i in ref){ + if(typeof ref[i] == 'object'){ + ref[i].classList.add('zoom') + } + } + } else { + for (i in ref){ + if(typeof ref[i] == 'object'){ + ref[i].classList.remove('zoom') + } + }0 + } +} +function renderView(){ + var str = '', + ref = ll, + counter=0; + nodeList.innerHTML = ''; + while(ref.next !== null){ + nodeList.appendChild(createNode(ref.next.data,counter)); + counter++; + ref = ref.next; + } + + function createNode(value,counter){ + var container = document.createElement('div'), + dataElement = document.createElement('span'); + + container.className="node"; + dataElement.innerHTML = value; + + dataElement.style.background = getColor(value,counter) + container.appendChild(dataElement); + dataElement.className=value; + return container + } + function getColor(value,counter){ + value = 255 - Number(value)%255; + switch (value%3){ + case 0: + return 'rgb('+value+','+value+',0)'; + case 1: + return 'rgb('+value+',0,'+value+')'; + default: + return 'rgb(0,'+value+','+value+')'; + } + } +} diff --git a/data_structures/Stack/stack/C/Array_implementation_stack.c b/data_structures/Stack/stack/C/Array_implementation_stack.c new file mode 100644 index 0000000000..7ad1d32554 --- /dev/null +++ b/data_structures/Stack/stack/C/Array_implementation_stack.c @@ -0,0 +1,55 @@ +/* c program for the array implementation of stack */ + +#include +#include + +#define size 100 +int stack[size], top = -1; + +void push(int x){ + +if(top > size-1){ + +printf("StackOverflow"); + +}else{ + stack[++top] = x; + printf("Element inserted:%d \n",x); +} + +} +void pop(){ + if(top < 0){ + printf("StackUnderFlow"); + } + else{ + printf("Element deleted %d", stack[top]); + top--; + } + +} +void prints(){ + int i; + printf("Element of stack are:"); + for(i=0; i<=top;i++){ + printf("%d ",stack[i]); + + } + +} + +int main(void){ + + + push(1); + push(2); + push(3); + push(4); + push(5); + push(6); + push(7); + pop(); + prints(); + + return 0; +} diff --git a/data_structures/Stack/stack/C/LINKED_STACK.c b/data_structures/Stack/stack/C/LINKED_STACK.c new file mode 100644 index 0000000000..899cb52841 --- /dev/null +++ b/data_structures/Stack/stack/C/LINKED_STACK.c @@ -0,0 +1,46 @@ +#include +#include + +// LINKED_STACK + +typedef struct node{ + int info; + struct node *next; +}NODE; + +typedef NODE * LINKED_STACK; + +void create_stack(LINKED_STACK *p){ + *p = NULL; +} + +int isEmpty(LINKED_STACK p){ + return (p == NULL); +} + +void insert(LINKED_STACK *p, int info){ + NODE *new_node; + new_node = (NODE *) malloc (sizeof(NODE)); + if(!new_node){ + puts("Memory FULL\n"); + exit(1); + } + new_node->info = info; + new_node->next = *p; + *p = new_node; +} + +void removeSTACK (LINKED_STACK *p){ + NODE *aux = *p; + + if(isEmpty(*p)){ + puts("Stack is Empty\n"); + exit(2); + } + (*p) = (*p)->next; + free(aux); +} + + + +int main(){} diff --git a/data_structures/Stack/stack/JAVA/STACK.java b/data_structures/Stack/stack/JAVA/STACK.java new file mode 100644 index 0000000000..fd2560b1ae --- /dev/null +++ b/data_structures/Stack/stack/JAVA/STACK.java @@ -0,0 +1,92 @@ +import java.util.ArrayList; + +/** + * @author avcbcoder last modified @29-Oct-2018 @4:39:00 PM Code Library - TODO + */ + +/** + * <========================================= About Genric stack class =============================================> + * + * DESCRIPTION of all the methods in stack class -------------------------------------------------------------------- + * 1.push(x) -> push x to the stack --------------------------------------------------------------------------------- + * 2.pop(x) -> pop x from the stack --------------------------------------------------------------------------------- + * 3.top(x) -> peeks the top of stack but doesn't removes ----------------------------------------------------------- + * 4.display() -> prints all the elements in stack from top to bottom ----------------------------------------------- + * 5.isEmpty() -> returns true if stack is empty -------------------------------------------------------------------- + */ + +/** + */ + +public class STACK { + + /** + * For Debugging + */ + public static void main(String[] args) throws Exception { + STACK st = new STACK(); + st.push(1); + st.push(10); + st.push(5); + st.push(8); + st.display(); + System.out.println("Removed " + st.pop()); + st.display(); + } + + protected ArrayList data = new ArrayList<>(); + protected int tos = -1; + protected T defaultValue = null; + + public STACK() { + this(10); + } + + public STACK(int initialCapacity) { + this.data = new ArrayList<>(); + } + + public void push(T value) throws Exception { + this.tos++; + if (this.tos < data.size()) + data.set(this.tos, value); + else + data.add(value); + } + + public T top() throws Exception { + if (tos == -1) + throw new Exception("stack is empty"); + return data.get(this.tos); + } + + public T pop() throws Exception { + if (tos == -1) + throw new Exception("stack is empty"); + else { + T rv = this.data.get(this.tos); + this.data.set(this.tos, defaultValue); + this.tos--; + return (rv); + } + } + + public int size() { + return this.tos + 1; + } + + public boolean isEmpty() { + if (this.tos == -1) + return true; + return false; + } + + public void display() { + System.out.println("---------------------------"); + System.out.print("top-> "); + for (int i = this.tos; i >= 0; i--) + System.out.print(this.data.get(i) + " "); + System.out.print("<-bottom"); + System.out.println("\n---------------------------"); + } +} diff --git a/data_structures/Stack/stack/Python/stack.py b/data_structures/Stack/stack/Python/stack.py new file mode 100644 index 0000000000..24a9de62a3 --- /dev/null +++ b/data_structures/Stack/stack/Python/stack.py @@ -0,0 +1,32 @@ +'''This code is written MTH Junaidi github: Miranjunaidi on 26th Oct 2019 at 7:15 PM IST''' + +#POP function removes the last element of the list +def pop(stack): + del stack[-1] + return stack +#push function adds a new number num to the list at the end. +def push(stack,num): + stack.append(num) + return stack +#print function prints all the element of the stack sequestially +def prints(stack): + for i in stack: + print(i,end = " ") + print("") +#This is where the main funtions start + +print("\n welcome, a new empty stack is created. \n press index numbers to do the opertations \n") +stack = [] +a=0 +while a!=4: + print(" 1.push \n 2.pop \n 3.see the stack \n 4.quit") + a = int(input()) + if a == 1: + num = int(input("Enter the number you want to push")) + push(stack, num) + elif a == 2: + pop(stack) + elif a==3: + prints(stack) + elif a>4 or a<0: + print("enter a valid operation") \ No newline at end of file diff --git a/data_structures/Stack/stack/Python/stackClass.py b/data_structures/Stack/stack/Python/stackClass.py new file mode 100644 index 0000000000..d9d0b3e9e0 --- /dev/null +++ b/data_structures/Stack/stack/Python/stackClass.py @@ -0,0 +1,13 @@ +class Stack: + def __init__(self, data=[]): + self.Stack = data + def __repr__(self): + return self.Stack + def push(self, item): + self.stack.append(item) + def pop(self): + self.stack.pop(-1) + def peek(self): + return self.stack[-1] + def isEmpty(self): + return len(self.Stack) > 0 diff --git a/data_structures/Stack/stack/README.md b/data_structures/Stack/stack/README.md new file mode 100644 index 0000000000..3b99029395 --- /dev/null +++ b/data_structures/Stack/stack/README.md @@ -0,0 +1 @@ +Stack is a linear data structure which follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). \ No newline at end of file diff --git a/data_structures/Stack/stack/SpecialStack.java b/data_structures/Stack/stack/SpecialStack.java new file mode 100644 index 0000000000..bfd05feb99 --- /dev/null +++ b/data_structures/Stack/stack/SpecialStack.java @@ -0,0 +1,66 @@ +//Java implementation of SpecialStack +// Note : here we use Stack class for Stack implementation + +import java.util.Stack; + +/* A class that supports all the stack operations and one additional +operation getMin() that returns the minimum element from stack at +any time. This class inherits from the stack class and uses an +auxiliarry stack that holds minimum elements */ + +class SpecialStack extends Stack +{ + Stack min = new Stack<>(); + + /* SpecialStack's member method to insert an element to it. This method + makes sure that the min stack is also updated with appropriate minimum + values */ + void push(int x) + { + if(isEmpty() == true) + { + super.push(x); + min.push(x); + } + else + { + super.push(x); + int y = min.pop(); + min.push(y); + if(x < y) + min.push(x); + else + min.push(y); + } + } + + /* SpecialStack's member method to insert an element to it. This method + makes sure that the min stack is also updated with appropriate minimum + values */ + public Integer pop() + { + int x = super.pop(); + min.pop(); + return x; + } + + /* SpecialStack's member method to get minimum element from it. */ + int getMin() + { + int x = min.pop(); + min.push(x); + return x; + } + + /* Driver program to test SpecialStack methods */ + public static void main(String[] args) + { + SpecialStack s = new SpecialStack(); + s.push(10); + s.push(20); + s.push(30); + System.out.println(s.getMin()); + s.push(5); + System.out.println(s.getMin()); + } +} diff --git a/data_structures/Stack/stack/c++/balanced_paranthesis.cpp b/data_structures/Stack/stack/c++/balanced_paranthesis.cpp new file mode 100644 index 0000000000..e6364c0122 --- /dev/null +++ b/data_structures/Stack/stack/c++/balanced_paranthesis.cpp @@ -0,0 +1,42 @@ +#include +#include +#include +using namespace std; + +bool balancing(char a,char b) +{ + return ((a=='(' && b==')')||(a=='{' && b=='}') || (a=='[' && b==']')); +} +bool isbalanced(string str) +{ + stack s; + int i; + for(i=0;i>str; + cout< +Stack:: Stack() +{ + head = nullptr; +} + +template +Stack:: Stack(const Stack &other) +{ + copy(other); +} + +template +Stack:: ~Stack() +{ + MakeEmpty(); +} + +template +void Stack:: operator=(const Stack &other) +{ + if (head) { + MakeEmpty(); + } + copy(other); +} + +template +void Stack:: copy(const Stack &other) +{ + if (!other.IsEmpty()) { + head = new Node(head->info); + Node* pother = other.head->next; + Node* pthis = head; + + while (pother) { + pthis->next = new Node(pother->info); + pthis = pthis->next; + pother = pother->next; + } + } else { + throw std::string("Stack is empty"); + } +} + +template +bool Stack:: IsEmpty() +{ + return (head == nullptr); +} + +template +void Stack:: MakeEmpty() +{ + while (!IsEmpty()) { + Node* p = head; + head = head->next; + delete p; + } +} + +template +T Stack:: Top() +{ + if (!IsEmpty()) { + return head->info; + } else { + throw -1; + } +} + + +template +void Stack:: Pop() +{ + if (!IsEmpty()) { + Node* p = head; // 1 + head = head->next; + delete p; + } else { + throw std::string("Stack is empty"); + } +} + + +template +void Stack:: Push(T item) +{ + Node* temp = new Node(item); + temp->next = head; + head = temp; +} + diff --git a/data_structures/Stack/stack/c++/mehmetSahin/Stack.hpp b/data_structures/Stack/stack/c++/mehmetSahin/Stack.hpp new file mode 100644 index 0000000000..f0bc4405df --- /dev/null +++ b/data_structures/Stack/stack/c++/mehmetSahin/Stack.hpp @@ -0,0 +1,74 @@ +// +// Stack.hpp +// This program creates the stack data structure implementation using pointers. +// +// Created by Mehmet Sahin on 10/2/18. +// Copyright © 2018 Mehmet Sahin. All rights reserved. +// + +#ifndef Stack_hpp +#define Stack_hpp + +#include + +template +struct Node { + T info; + Node* next; + Node(T i): info(i), next(nullptr) {}; +}; + +template +class Stack { + +private: + Node* head; // a pointer to top node + +public: + Stack(); // default constructor + Stack(const Stack &other); // copy constructor + ~Stack(); // destructor + + /** + * Overloaded assignment operator to copy other object to this object + * @param other is a reference to Stack object + */ + void operator=(const Stack &other); + + /** + * Copy function to help copy constructor and (=) operator + * @param other is a reference to Stack object + */ + void copy(const Stack &other); + + /** + * Is the stack empty? + * @return a bool + */ + bool IsEmpty(); + + /** + * Makes the stack empty + */ + void MakeEmpty(); + + /** + * Get the item on top of the stack + * @return a T type item + */ + T Top(); + + /** + * Deletes the top item in the stack + */ + void Pop(); + + /** + * Push given item to top of the stack + * @param item is a T type. + */ + void Push(T item); + +}; + +#endif /* Stack_hpp */ diff --git a/data_structures/Stack/stack/c++/mehmetSahin/testStack.cpp b/data_structures/Stack/stack/c++/mehmetSahin/testStack.cpp new file mode 100644 index 0000000000..29ea12248f --- /dev/null +++ b/data_structures/Stack/stack/c++/mehmetSahin/testStack.cpp @@ -0,0 +1,28 @@ +// +// main.cpp +// +// Created by Mehmet Sahin on 10/2/18. +// Copyright © 2018 Mehmet Sahin. All rights reserved. +// + +#include +#include "Stack.cpp" +using std::cout; +using std::endl; + +int main(int argc, const char * argv[]) { + Stack s1; + + s1.Push(10); + s1.Push(15); + s1.Push(20); + cout << s1.Top() << endl; + s1.Pop(); + s1.Pop(); + cout << s1.Top() << endl; + + cout << s1.IsEmpty() << endl; + s1.MakeEmpty(); + cout << s1.IsEmpty() << endl; + return 0; +} diff --git a/data_structures/Stack/stack/c++/parenthesisChecker.cpp b/data_structures/Stack/stack/c++/parenthesisChecker.cpp new file mode 100644 index 0000000000..acad4bb26f --- /dev/null +++ b/data_structures/Stack/stack/c++/parenthesisChecker.cpp @@ -0,0 +1,57 @@ +/* +by @ritul120 + +Given an expression string exp. Examine whether the pairs and the orders of “{“,”}”,”(“,”)”,”[“,”]” are correct in exp. +For example, the program should print 'balanced' for exp = “[()]{}{[()()]()}” and 'not balanced' for exp = “[(])” + +*/ + +#include +#include +using namespace std; + +bool match(char a,char b) +{ + if(a=='{' && b=='}') + return true; + if(a=='(' && b==')') + return true; + if(a=='[' && b==']') + return true; + return false; +} +bool balanced(string str) +{ int i,n= str.length(); + stack st; + + for(i=0;i>str; + if(balanced(str)) + cout<<"balanced"< +#include + +#define size 100 +int stack[size], top = -1; + +void push(int x){ + +if(top > size-1){ + +printf("StackOverflow"); + +}else{ + stack[++top] = x; + printf("Element inserted:%d \n",x); +} + +} +void pop(){ + if(top < 0){ + printf("StackUnderFlow"); + } + else{ + printf("Element deleted %d", stack[top]); + top--; + } + +} +void prints(){ + int i; + printf("Element of stack are:"); + for(i=0; i<=top;i++){ + printf("%d ",stack[i]); + + } + +} + +int main(void){ + + + push(1); + push(2); + push(3); + push(4); + push(5); + push(6); + push(7); + pop(); + prints(); + + return 0; +} diff --git a/data_structures/Stack/stack/javaScript/renatoSantosReverseArray/Reverse.js b/data_structures/Stack/stack/javaScript/renatoSantosReverseArray/Reverse.js new file mode 100644 index 0000000000..1f838c014e --- /dev/null +++ b/data_structures/Stack/stack/javaScript/renatoSantosReverseArray/Reverse.js @@ -0,0 +1,13 @@ +const ReverseJS = (oldArray) => { + let initPosition = 0 + let lastPosition = oldArray.length - 1 + let newArray = [] + while (lastPosition >= 0) { + newArray[initPosition] = oldArray[lastPosition] + initPosition++ + lastPosition-- + } + return newArray +} + +module.exports.ReverseJS = ReverseJS; \ No newline at end of file