filename
stringlengths
7
140
content
stringlengths
0
76.7M
code/data_structures/src/list/singly_linked_list/operations/push/push.cpp
#include <iostream> using namespace std; // Everytime you work with Append or Push function, there is a corner case. // When a NULL Node is passed you have to return a Node that points to the given value. // There are two ways to achieve this: // 1. Receive the double pointer as an argument. // 2. Return the new Node....
code/data_structures/src/list/singly_linked_list/operations/reverse/reverse.c
#include <stdio.h> #include <stdlib.h> #include <unistd.h> typedef struct node { int value; struct node* next; }node; node* head = NULL; node* create_node(int val) { node *tmp = (node *) malloc(sizeof(node)); tmp->value = val; tmp->next = NULL; return tmp; } void create_list(int val) { s...
code/data_structures/src/list/singly_linked_list/operations/reverse/reverse.cpp
/* * Part of Cosmos by OpenGenus Foundation */ #include <iostream> using namespace std; //Definition for singly-linked list. struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) { } }; // Reverse function for linkedlist ListNode* reverseList(ListNode* A) { ListNod...
code/data_structures/src/list/singly_linked_list/operations/reverse/reverse_iteration.cpp
#include <iostream> using namespace std; struct node { int data; struct node * next; }*head; void reverse() { node *prev = NULL; node *cur = head; node *next; while (cur != NULL) { next = cur->next; cur->next = prev; prev = cur; cur = next; } head...
code/data_structures/src/list/singly_linked_list/operations/reverse/reverse_recursion.cpp
#include <iostream> // Part of Cosmos by OpenGenus Foundation using namespace std; class node { public: int data; node* next; public: node(int d) { data = d; next = nullptr; } }; void print(node*head) { while (head != nullptr) { cout << head->data << "-->"; ...
code/data_structures/src/list/singly_linked_list/operations/reverse/reverse_recursion2.cpp
#include <iostream> using namespace std; struct Node { int data; struct Node* next; }; void printReverse(struct Node* head) { if (head == NULL) return; printReverse(head->next); cout << head->data << " "; } void push(struct Node** head_ref, char new_data) { struct Node* new_node = new...
code/data_structures/src/list/singly_linked_list/operations/rotate/rotate.cpp
/* * Part of Cosmos by OpenGenus Foundation */ #include <iostream> //Definition for singly-linked list. struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) { } }; ListNode* rotate(ListNode* A, int B) { int t = 0; ListNode* temp = A; ListNode* prev2 = NULL...
code/data_structures/src/list/singly_linked_list/operations/rotate_a_linked_list_by_k_nodes/rotate_a_linked_list_by_k_nodes.cpp
// C++ program to rotate a linked list counter clock wise by k Nodes // where k can be greater than length of linked list #include <iostream> class Node { public: int data; Node *next; Node(int d): next(nullptr),data(d) { } }; Node *insert() { // no. of values to insert std::cout << "Enter n...
code/data_structures/src/list/singly_linked_list/operations/sort/bubble_sort.cpp
///Bubble sort a linked list #include <iostream> #include <stack> using namespace std; class node { public: int data; node* next; public: node(int d) { data = d; next = NULL; } }; void print(node*head) { while (head != NULL) { cout << head->data << "-->"; h...
code/data_structures/src/list/singly_linked_list/operations/unclassified/linked_list.java
// Part of Cosmos by OpenGenus Foundation public class LinkedList { private class Node { int data; Node next; public Node() { } } private Node head; private Node tail; private int size; public int size() { return this.size; } public boolean isEmpty() { return this.size == 0; } public void ...
code/data_structures/src/list/singly_linked_list/operations/unclassified/linked_list_example.java
import java.io.InputStreamReader; import java.util.LinkedList; import java.util.Scanner; public class LinkedListeg { class Node { int value; Node next = null; Node(int value) { this.value = value; } } protected Node head = null; protected Node tail = null; public void addToFront(int value) { Node...
code/data_structures/src/list/singly_linked_list/operations/unclassified/linked_list_operations.cpp
#include <iostream> using namespace std; //built node .... node = (data and pointer) struct node { int data; //data item node* next; //pointer to next node }; //built linked list class linkedlist { private: node* head; //pointer to the first node public: linkedlist() //constructor ...
code/data_structures/src/list/singly_linked_list/operations/unclassified/union_intersection_in_list.c
// C/C++ program to find union and intersection of two unsorted // linked lists // Part of Cosmos by OpenGenus Foundation #include<stdio.h> #include<stdlib.h> #include<conio.h> /* Link list node */ struct Node { int data; struct Node* next; }; /* A utility function to insert a node at the beginning of a ...
code/data_structures/src/list/singly_linked_list/singly_linked_list.c
#ifndef _LINKED_LIST_C_ #define _LINKED_LIST_C_ #include <stdio.h> #include <stdlib.h> #include <stdbool.h> /* C99 required */ typedef bool (*compare_func)(void* p_data1, void* p_data2); typedef void (*traverse_func)(void *p_data); typedef void (*destroy_func)(void* p_data); typedef struct linked_list_node { str...
code/data_structures/src/list/singly_linked_list/singly_linked_list.cpp
#include "singly_linked_list.h" int main() { Linkedlist<int> link; for (int i = 10; i > 0; --i) link.rearAdd(i); link.print(); std::cout << link.size() << std::endl; Linkedlist<int> link1(link); link1 = link1; link1.print(); link1.deletePos(100); link1.modify(5, 100); li...
code/data_structures/src/list/singly_linked_list/singly_linked_list.cs
namespace LinkedList { class Node<T> { // properties private T value; private Node<T> nextNode; // constructors public Node(T value, Node<T> nextNode) { this.value = value; this.nextNode = nextNode; } public Node(T value)...
code/data_structures/src/list/singly_linked_list/singly_linked_list.go
package main import "fmt" type node struct { next *node label string } func (list *node) insert(new_label string) *node { if list == nil { new_node := &node { next: nil, label: new_label, } return new_node } else if list.next == nil { new_node :...
code/data_structures/src/list/singly_linked_list/singly_linked_list.h
#pragma once #include <iostream> template <typename T> struct Node { T date; Node* pNext; }; template <typename T> class Linkedlist { public: Linkedlist(); Linkedlist(const Linkedlist<T> &list); Linkedlist<T>& operator= (const Linkedlist<T> &rhs); ~Linkedlist(); void headAdd(const T& date...
code/data_structures/src/list/singly_linked_list/singly_linked_list.java
/* Part of Cosmos by OpenGenus Foundation */ class SinglyLinkedList<T> { private Node head; public SinglyLinkedList() { head = null; } public void insertHead(T data) { Node<T> newNode = new Node<>(data); //Create a new link with a value attached to it newNode.next = head; //Set the new link to point to th...
code/data_structures/src/list/singly_linked_list/singly_linked_list.js
/* Part of Cosmos by OpenGenus Foundation */ /* SinglyLinkedList!! * A linked list is implar to an array, it hold values. * However, links in a linked list do not have indexes. With * a linked list you do not need to predetermine it's size as * it grows and shrinks as it is edited. This is an example of * a singl...
code/data_structures/src/list/singly_linked_list/singly_linked_list.py
# Part of Cosmos by OpenGenus Foundation class Node: def __init__(self, data): self.data = data self.next = None def get_data(self): return self.data def get_next(self): return self.next def set_next(self, new_next): self.next = new_next class LinkedList: ...
code/data_structures/src/list/singly_linked_list/singly_linked_list.rb
# Part of Cosmos by OpenGenus Foundation class Node attr_accessor :data attr_accessor :next def initialize(data) @data = data @next = nil end end class LinkedList attr_reader :size def initialize @head = nil @size = 0 end # Add a new node at end of linked list def add!(data) if...
code/data_structures/src/list/singly_linked_list/singly_linked_list.swift
// Part of Cosmos by OpenGenus Foundation import Foundation public struct Node<T> { private var data: T? private var next: [Node<T>?] = [] init() { self.data = nil } init(data: T) { self.data = data } func getData() -> T? { return self.data } ...
code/data_structures/src/list/singly_linked_list/singly_linked_list_menu_driven.c
// use -1 option to exit // to learn about singly link list visit https://blog.bigoitsolution.com/learn/data-structures/922/ #include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node* next; }; int main() { int opt, value, flag, loc; struct Node *head, *new_node, *temp; head = (struct Node*)ma...
code/data_structures/src/list/singly_linked_list/singly_linked_list_with_3_nodes.java
package linkedlist; public class LinkedList { Node head; static class Node { int data; Node next; Node(int data){ this.data = data; next = null; } } public void printList() { Node n = head; while(n!= ...
code/data_structures/src/list/singly_linked_list/singly_linked_list_with_classes.cpp
#include <iostream> class Node { private: int _data; Node *_next; public: Node() { } void setData(int Data) { _data = Data; } void setNext(Node *Next) { if (Next == NULL) _next = NULL; else _next = Next; } int Data() { ...
code/data_structures/src/list/skip_list/README.md
# cosmos Your personal library of every algorithm and data structure code that you will ever encounter
code/data_structures/src/list/skip_list/skip_list.c
#include <stdlib.h> #include <stdio.h> #include <limits.h> // Part of Cosmos by OpenGenus Foundation #define SKIPLIST_MAX_LEVEL 6 typedef struct snode { int key; int value; struct snode **forward; } snode; typedef struct skiplist { int level; int size; struct snode *header; } skiplist; ski...
code/data_structures/src/list/skip_list/skip_list.cpp
/** * skip list C++ implementation * * Average Worst-case * Space O(n) O(n log n) * Search O(log n) 0(n) * Insert O(log n) 0(n) * Delete O(log n) 0(n) * Part of Cosmos by OpenGenus Foundation */ #include <algorithm> // std::less, std::max #include <cassert> // ass...
code/data_structures/src/list/skip_list/skip_list.java
// Part of Cosmos by OpenGenus Foundation import java.util.*; public class SkipList<E> { /** Initialize the random variable */ static private Random value = new Random(); // Hold the Random class object /** Create a random number function from the standard Java Random class. Turn it into a unifor...
code/data_structures/src/list/skip_list/skip_list.rb
class Node attr_accessor :key attr_accessor :value attr_accessor :forward def initialize(k, v = nil) @key = k @value = v.nil? ? k : v @forward = [] end end class SkipList attr_accessor :level attr_accessor :header def initialize @header = Node.new(1) @level = 0 @max_level...
code/data_structures/src/list/skip_list/skip_list.scala
import java.util.Random import scala.collection.Searching._ import scala.reflect.ClassTag object SkipList { val MAX_LEVELS = 32; val random = new Random() } class SkipList[T](private var nextLists: Array[SkipList[T]], private var data: T) { def this(firstElement: T) = this(Array.ofDim(SkipList.MAX_LEVELS...
code/data_structures/src/list/skip_list/skip_list.swift
import Foundation public struct Stack<T> { fileprivate var array: [T] = [] public var isEmpty: Bool { return array.isEmpty } public var count: Int { return array.count } public mutating func push(_ element: T) { array.append(element) } public mutating func pop() -> T? { return array...
code/data_structures/src/list/stack_using_linked_list/stack_using_linked_list.c
#include<stdio.h> struct node { int data; struct node *next; }; struct node * head=0; //inserting an element void push(int x) { struct node * temp=0; temp=(struct node *)malloc(sizeof(struct node)); if(temp==0) { printf("\nOverflow"); return; } temp->data=x; temp...
code/data_structures/src/list/xor_linked_list/README.md
# cosmos Your personal library of every algorithm and data structure code that you will ever encounter
code/data_structures/src/list/xor_linked_list/xor_linked_list.cpp
/* * Part of Cosmos by OpenGenus Foundation * * xor linked list synopsis * *** incomplete *** *** ***Begin *** Iterator invalidation rules are NOT applicable. *** ***[x] Insertion: all iterators and references unaffected. ***[x] Erasure: only the iterators and references to the erased element is invalidated. ...
code/data_structures/src/maxHeap/maxHeap.cpp
#include "maxHeap.h" using namespace std; void maxHeap::heapifyAdd(int idx) { int root = (idx-1)/2; if(heap[idx]>heap[root]) { int temp = heap[root]; heap[root] = heap[idx]; heap[idx] = temp; heapifyAdd(root); } } void maxHeap::heapifyRemove(int idx) { int max = id...
code/data_structures/src/maxHeap/maxHeap.h
#ifndef COSMOS_MAXHEAP_H #define COSMOS_MAXHEAP_H #include <iostream> class maxHeap { private : int* heap; int capacity; int currSize; void heapifyAdd(int idx); void heapifyRemove(int idx); public: maxHeap(int capacity); void insert(int itm); void remove(); void print(); }; ...
code/data_structures/src/maxHeap/maxHeap.java
public class MaxHeap { private int[] heap; private int currSize; private int capacity; public MaxHeap(int capacity) { this.heap = new int[capacity]; this.currSize = 0; this.capacity = capacity; } private void heapifyAdd(int idx) { int root = (idx - 1) / 2; ...
code/data_structures/src/maxHeap/maxheap.py
# Python3 implementation of Max Heap import sys class MaxHeap: def __init__(self, maxsize): self.maxsize = maxsize self.size = 0 self.Heap = [0] * (self.maxsize + 1) self.Heap[0] = sys.maxsize self.FRONT = 1 # Function to return the position of # paren...
code/data_structures/src/maxSubArray(KadaneAlgorithm)/KadaneAlgorithm.cpp
#include <iostream> #include <vector> using namespace std; int Kadane(vector<int>& arr) { int max_so_far = INT_MIN; int max_ending_here = 0; for (int i = 0; i < arr.size(); i++) { max_ending_here = max_ending_here + arr[i]; if (max_ending_here < 0) { max_ending_here = 0; ...
code/data_structures/src/maxSubArray(KadaneAlgorithm)/KadaneAlgorithm.java
import java.util.*; class KadaneAlgorithm { static int maxSubarraySum(int a[], int n) { long max = a[0]; long sum = 0; for (int i = 0; i < n; i++) { sum += a[i]; if (sum > max) max = sum; if (sum < 0) sum = 0; } return max; } public static void main(String[]...
code/data_structures/src/maxSubArray(KadaneAlgorithm)/Kadane_algorithm.py
def maxSubArraySum(a,size): max_so_far = a[0] - 1 max_ending_here = 0 for i in range(0, size): max_ending_here = max_ending_here + a[i] if (max_so_far < max_ending_here): max_so_far = max_ending_here if max_ending_here < 0: max_ending_here = 0...
code/data_structures/src/minHeap/minHeap.py
import sys class MinHeap: def __init__(self, maxsize): self.maxsize = maxsize self.size = 0 self.Heap = [0] * (self.maxsize + 1) self.Heap[0] = -1 * sys.maxsize self.FRONT = 1 def parent(self, pos): return pos // 2 def leftChild(self, pos): return ...
code/data_structures/src/minqueue/minqueue.cpp
#include <deque> #include <iostream> using namespace std; //this is so the minqueue can be used with different types //check out the templeting documentation for more info about this //This version of minqueue is built upon the deque data structure template <typename T> class MinQueue { public: MinQueue() {} ...
code/data_structures/src/other/README.md
# cosmos Your personal library of every algorithm and data structure code that you will ever encounter
code/data_structures/src/other/ways_to_swap.cpp
//DIFFERENT WAYS TO SWAP 2 NUMBERS:- #include<bits/stdc++.h> //it includes most of the headers files needed for basic programming using namespace std; int main() { int a = 10, b = 5, temp; // 1. USING THIRD VARIABLE temp = a; a = b; b = temp; // 2. USING ADDITION AN...
code/data_structures/src/prefix_sum_array/prefix_sum_array.py
def maximum_sum_subarray(arr, n): min_sum = 0 res = 0 prefixSum = [] prefixSum.append(arr[0]) for i in range(1, n): prefixSum.append(prefixSum[i-1] + arr[i]) for i in range(n): res = max(res, prefixSum[i] - min_sum) min_sum = min(min_sum, prefixSum[i]) return res n =...
code/data_structures/src/prefix_sum_array/prefix_sum_subarray.cpp
// Part of Cosmos by OpenGenus //Max Sum Subarray in O(n) #include <bits/stdc++.h> using namespace std; int maximumSumSubarray(int *arr, int n){ int minPrefixSum = 0; int res = numeric_limits<int>::min(); int prefixSum[n]; prefixSum[0] = arr[0]; for (int i = 1; i < n; i++) { prefixSum[i] = prefixSu...
code/data_structures/src/queue/README.md
# Queue Description --- A queue or FIFO (first in, first out) is an abstract data type that serves as a collection of elements, with two principal operations: enqueue, the process of adding an element to the collection.(The element is added from the rear side) and dequeue, the process of removing the first element tha...
code/data_structures/src/queue/circular_buffer/circular_buffer.cpp
#include <iostream> #include <vector> template <typename T, long SZ> class circular_buffer { private: T* m_buffer; long m_index; public: circular_buffer() : m_buffer {new T[SZ]()} , m_index {0} { } ~circular_buffer() { delete m_buffer; } std::vector<T> get...
code/data_structures/src/queue/circular_buffer/circular_buffer.py
class CircularBuffer: __array = [] __size = 0 __idx = 0 def __init__(self, size): self.__array = [[] for _ in range(0, size)] self.__size = size def push(self, x): self.__array[self.__idx] = x self.__idx = (self.__idx + 1) % self.__size def get_ordered(self): ...
code/data_structures/src/queue/double_ended_queue/deque_library_function.cpp
#include <iostream> #include <deque> using namespace std; void showdq (deque <int> g) { deque <int> :: iterator it; // Iterator to iterate over the deque . for (it = g.begin(); it != g.end(); ++it) cout << '\t' << *it; cout << "\n"; } int main () { deque <int...
code/data_structures/src/queue/double_ended_queue/double_ended_queue.c
#include <stdio.h> #include <conio.h> #define MAX 10 int deque[MAX]; int left =-1 ; int right = -1 ; void inputdeque(void); void outputdeque(void); void insertleft(void); void insertright(void); void deleteleft(void); void deleteright(void); void display(void); int main( ) { int option; printf("\n ...
code/data_structures/src/queue/double_ended_queue/double_ended_queue.cpp
#include <iostream> using namespace std; #define MAX 10 int deque[MAX]; int leftt = -1; int rightt = -1; void inputdeque(void); void outputdeque(void); void insertleft(void); void insertright(void); void deleteleft(void); void deleteright(void); void display(void); int main( ) { int option; cout << "\n *****...
code/data_structures/src/queue/double_ended_queue/double_ended_queue.py
""" python programme for showing dequeue data sturcture in python """ """ class for queue type data structure having following methods""" class Queue: def __init__(self): self.array = [] # ''' initialise empty array ''' def front_enqueue(self, data): # ''' adding element from front ''' s...
code/data_structures/src/queue/queue/README.md
# Cosmos Collaborative effort by [OpenGenus](https://github.com/OpenGenus/cosmos)
code/data_structures/src/queue/queue/queue.c
/* * C Program to Implement a Queue using an Array * Part of Cosmos by OpenGenus Foundation */ #include <stdio.h> #include <stdlib.h> #define MAX 50 int queue_array[MAX]; int rear = -1; int front = -1; void insert() { int add_item; if (rear == MAX - 1) { printf("Queue Overflow \n"); } else { ...
code/data_structures/src/queue/queue/queue.cpp
/* Part of Cosmos by OpenGenus Foundation */ #include <iostream> #include <cassert> template <typename T> class Queue { private: int m_f; int m_r; std::size_t m_size; T * m_data; public: /*! \brief Constructs queue object with size. * \param sz Size of the queue. */ Queue( std::size_t sz = 10...
code/data_structures/src/queue/queue/queue.cs
/** * Queue implementation using a singly-linked link. * Part of the OpenGenus/cosmos project. (https://github.com/OpenGenus/cosmos) * * A queue is a first-in first-out (FIFO) data structure. * Elements are manipulated by adding elements to the back of the queue and removing them from * the front. */ using Sys...
code/data_structures/src/queue/queue/queue.exs
defmodule Queue do def init, do: loop([]) def loop(queue) do receive do {:enqueue, {sender_pid, item}} -> queue = enqueue(queue, item) send sender_pid, {:ok, item} loop(queue) {:queue_next, {sender_pid}} -> {queue, next} = queue_next(queue) send sender_pid, {:...
code/data_structures/src/queue/queue/queue.go
// Part of Cosmos by OpenGenus Foundation package main import "fmt" type Queue []int func (s *Queue) Push(v int) Queue { return append(*s, v) } func (s *Queue) Pop() Queue { length := len(*s) if length != 0 { return (*s)[1:] } return *s } func (s *Queue) Top() int { return (*s)[0] } func (s *Queue) Empty(...
code/data_structures/src/queue/queue/queue.java
/* Part of Cosmos by OpenGenus Foundation */ import java.util.ArrayList; class Queue<T> { private int maxSize; private ArrayList<T> queueArray; private int front; private int rear; private int nItems; public Queue(int size) { maxSize = size; queueArray = new ArrayList<>(); front = 0; rear = -1; nItem...
code/data_structures/src/queue/queue/queue.js
/* * Part of Cosmos by OpenGenus Foundation * */ function Queue(size_max) { data = new Array(size_max + 1); max = (size_max + 1); start = 0; end = 0; this.empty = () => { return start == end; } this.full = () => { return start == (end + 1) % max; } this.enqueue = (x) => { if (this.f...
code/data_structures/src/queue/queue/queue.py
# Part of Cosmos by OpenGenus Foundation class Queue: def __init__(self, size_max): self.data = [None] * (size_max + 1) self.max = (size_max + 1) self.head = 0 self.tail = 0 def empty(self): return self.head == self.tail def full(self): return self.head == (...
code/data_structures/src/queue/queue/queue.rb
# Part of Cosmos by OpenGenus Foundation class Queue attr_accessor :items def initialize @items = [] end def enqueue(element) @items.push(element) end def dequeue @items.delete_at(0) end def empty? @items.empty? end def front @items.first end def back @items.last ...
code/data_structures/src/queue/queue/queue.swift
/* Part of Cosmos by OpenGenus Foundation */ public struct Queue<T> { private var elements: Array<T>; init() { self.elements = []; } init(arrayLiteral elements: T...) { self.elements = elements } public var front: T? { return elements.first } ...
code/data_structures/src/queue/queue/queue_vector.cpp
#include <iostream> #include <vector> using namespace std; // Part of Cosmos by OpenGenus Foundation class Queue { private: // Vector for storing all of the values vector<int> _vec; public: void enqueue(int & p_put_obj); // Puts object at the end of the queue void dequeue(int & p_ret_obj); // Returns obje...
code/data_structures/src/queue/queue_stream/queue_stream.cs
using System; namespace Cosmos_Data_Structures { /// <summary> /// QueueStream provides an alternative Queue implementation to High speed /// IO/Stream operations that works on multitheaded environment. /// Internally use 'circular array' in the implementation. /// </summary> [Serializable] p...
code/data_structures/src/queue/queue_using_linked_list/README.md
# Cosmos Collaborative effort by [OpenGenus](https://github.com/OpenGenus/cosmos)
code/data_structures/src/queue/queue_using_linked_list/queue_using_linked_list.c
#include <stdio.h> #include <stdlib.h> /* @author AaronDills An implementation of a queue data structure using C and Linked Lists*/ /* Structure for a Node containing an integer value and reference to another Node*/ struct Node { int value; struct Node *next; }; /* Initalize global Nodes for the head and ...
code/data_structures/src/queue/queue_using_linked_list/queue_using_linked_list.cpp
#include <cassert> #include <iostream> // Part of Cosmos by OpenGenus Foundation using namespace std; template <class T> class Queue { private: class Node { public: Node(const T &data, Node *next) : data(data), next(next) { } T data; Node *next; }; ...
code/data_structures/src/queue/queue_using_linked_list/queue_using_linked_list.java
class Node<T> //NODE CLASS { T data; Node<T> next; } // Part of Cosmos by OpenGenus Foundation //Queue Using linked list public class QueueUsingLL<T> { private Node<T> front; // Front Node private Node<T> rear; // Rear Node private int size; public int size(){ return size; } //Returns TRUE if qu...
code/data_structures/src/queue/queue_using_linked_list/queue_using_linked_list.py
# Part of Cosmos by OpenGenus Foundation Queue = {"front": None, "back": None} class Node: def __init__(self, data, next): self.data = data self.next = next def Enqueue(Queue, element): N = Node(element, None) if Queue["back"] == None: Queue["front"] = N Queue["back"] = ...
code/data_structures/src/queue/queue_using_linked_list/queue_using_linked_list.rb
# Part of Cosmos by OpenGenus Foundation class LinkedQueue attr_reader :size def initialize @head = nil @tail = nil @size = 0 end def enqueue(element) new_node = Node.new(element) @size += 1 if @head @tail.next = new_node @tail = new_node else @head = new_node ...
code/data_structures/src/queue/queue_using_stack/queue_using_stack.c
#include<stdio.h> #include<stdlib.h> #include<assert.h> typedef struct stack { int top; unsigned capacity; int* A; } stack; typedef struct queue { struct stack* S1; struct stack* S2; } queue; /*STACK*/ stack* createStack(unsigned capacity) { stack* newStack = (stack*)malloc(sizeof(stack))...
code/data_structures/src/queue/queue_using_stack/queue_using_stack.cpp
#include <iostream> #include <stack> using namespace std; // Part of Cosmos by OpenGenus Foundation // queue data structure using two stacks class queue { private: stack<int> s1, s2; public: void enqueue(int element); int dequeue(); void displayQueue(); }; // enqueue an element to the queue void queue ...
code/data_structures/src/queue/queue_using_stack/queue_using_stack.java
import java.util.NoSuchElementException; import java.util.Stack; /** * Implements https://docs.oracle.com/javase/8/docs/api/java/util/Queue.html * Big O time complexity: O(n) * Part of Cosmos by OpenGenus Foundation */ class Queue<T> { Stack<T> temp = new Stack<>(); Stack<T> value = new Stack<>(); pub...
code/data_structures/src/queue/queue_using_stack/queue_using_stack.py
# Part of Cosmos by OpenGenus Foundation class Stack: def __init__(self): self.items = [] def push(self, x): self.items.append(x) def pop(self): return self.items.pop() def size(self): return len(self.items) def is_empty(self): return self.size() == 0 ...
code/data_structures/src/queue/queue_using_stack/queue_using_stack.sh
#!/bin/bash #Function to start the Program function start(){ echo -e "\t\t1.[E]nter in Queue" echo -e "\t\t2. [D]elete from Queue" echo -e "\t\t3. [T]erminate the Program" read OP } # Function to Insert Values Into Queue function insert(){ read -p "How many Elements would you like to Enter " NM for ((a=0;$a<$NM...
code/data_structures/src/queue/reverse_queue/reverse_queue.cpp
#include <iostream> #include <stack> #include <queue> using namespace std; // Part of Cosmos by OpenGenus Foundation void print(queue<int> q) { while (!q.empty()) { cout << q.front() << " "; q.pop(); } cout << endl; } int main() { queue<int> q; stack<int> s; //build the qu...
code/data_structures/src/queue/reverse_queue/reverse_queue.go
// Part of Cosmos by OpenGenus Foundation package main /* Exptected output: Current Queue is [], Is Queue empty ? true Try to push 30 into Queue Try to push 12 into Queue Try to push 34 into Queue Try to push 2 into Queue Try to push 17 into Queue Try to push 88 into Queue Reverse Queue 88 17 2 34 12 30 */ import "f...
code/data_structures/src/queue/reverse_queue/reverse_queue.java
import java.util.*; public class reverse_queue { // Part of Cosmos by OpenGenus Foundation //method to print contents of queue public static void print(Queue<Integer> queue) { for(int x : queue) { System.out.print(x + " "); } System.out.print("\n"); } public static void main (Str...
code/data_structures/src/queue/reverse_queue/reverse_queue.py
# Part of Cosmos by OpenGenus Foundation. # Make a new Queue class with list. class Queue: def __init__(self, size_max): self.__max = size_max self.__data = [] # Adds an item to the end of the queue. def enqueue(self, x): if len(self.__data) == self.__max: print("Queue ...
code/data_structures/src/queue/reverse_queue/reverse_queue.swift
/* Part of Cosmos by OpenGenus Foundation */ // Queue implementation public struct Queue<T> { private var elements: Array<T>; init() { self.elements = []; } init(arrayLiteral elements: T...) { self.elements = elements } public var front: T? { return e...
code/data_structures/src/sparse_table/sparse_table.cpp
/* Part of Cosmos by OpenGenus Foundation */ #include <iostream> #include <cassert> #include <functional> #include <iomanip> #include <cmath> template<typename T, std::size_t S> class SparseTable { public: /*! * \brief Builds a sparse table from a set of static data. It is the user responsibility to delete a...
code/data_structures/src/stack/Quick_sort_usingSack/quick_sort.cpp
#include <iostream> #include <stack> using namespace std; int arr[] = {4, 1, 5, 3, 50, 30, 70, 80, 28, 22}; stack<int> higherStack, lowerStack; void displayArray(); void quick_sort(int lower, int higher); void partition(int lower, int higher); int main() { lowerStack.push(0); higherStack.push(9); quick_s...
code/data_structures/src/stack/README.md
# Stack Description --- Stacks are an abstract data type (ADT) that function to store and manipulate data. Their order is last-in-first-out (LIFO). To implement a stack, an array or [linked list](../linked_list) can be used. A stack is similar to a [Tower of Hanoi](https://en.wikipedia.org/wiki/Tower_of_Hanoi) in that ...
code/data_structures/src/stack/abstract_stack/README.md
# Stack Description --- Stacks are an abstract data type (ADT) that function to store and manipulate data. Their order is last-in-first-out (LIFO). To implement a stack, an array or [linked list](../linked_list) can be used. A stack is similar to a [Tower of Hanoi](https://en.wikipedia.org/wiki/Tower_of_Hanoi) in that ...
code/data_structures/src/stack/abstract_stack/cpp/array_stack.java
import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ class Stack<E> { private E[] arr = null; private int CAP; private int top = -1; private int size = 0; @SuppressWarnings("unchecked") public Stack(int cap) { this.CAP = cap; this....
code/data_structures/src/stack/abstract_stack/cpp/array_stack/array_stack.cpp
#include <iostream> #include "../istack.h" #include "arraystack.h" int main() { int s; IStack<int> *stack = new ArrayStack<int>(); try { stack->peek(); } catch (char const *e) { std::cout << e << std::endl << std::endl; } stack->push(20); std::cout << "Added 20" << s...
code/data_structures/src/stack/abstract_stack/cpp/array_stack/array_stack.h
#ifndef ARRAYSTACH_H #define ARRAYSTACK_H #include <string> #include <sstream> #include "../istack.h" template<typename T> class ArrayStack : public IStack<T> { private: int capacity; int top; T **elements; void expandArray(); public: ArrayStack(int capacity = 10); ...
code/data_structures/src/stack/abstract_stack/cpp/is_stack.h
#ifndef ISTACK_H #define ISTACK_H #include <string> #include <sstream> template <typename T> class IStack { public: virtual ~IStack() {}; /** * Add an element to stack */ virtual void push(const T& element) = 0; /** * Remove an element from stack ...
code/data_structures/src/stack/abstract_stack/is_stack.h
#ifndef ISTACK_H #define ISTACK_H #include <string> #include <sstream> template <typename T> class IStack { public: virtual ~IStack() {}; /** * Add an element to stack */ virtual void push(const T& element) = 0; /** * Remove an element from stack ...
code/data_structures/src/stack/balanced_expression/balanced_expression.c
#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct stack { char data; struct stack* next; } stack; void push(stack** top, char data) { stack* new = (stack*) malloc(sizeof(stack)); new->data = data; new->next = *top; *top = new; } char peek(stack* top) { return ( top -> da...
code/data_structures/src/stack/balanced_expression/balanced_expression.cpp
// Stack | Balance paraenthesis | C++ // Part of Cosmos by OpenGenus Foundation #include <iostream> #include <stack> bool checkBalanced(string s) { // if not in pairs, then not balanced if (s.length() % 2 != 0) return false; std::stack <char> st; for (const char: s) { //adding open...
code/data_structures/src/stack/balanced_expression/balanced_expression.java
import java.util.*; class BalancedExpression { public static void main(String args[]) { System.out.println("Balanced Expression"); Scanner in = new Scanner(System.in); String input = in.next(); if (isExpressionBalanced(input)) { System.out.println("The expression is balanced"); } else { System.out.p...
code/data_structures/src/stack/balanced_expression/balanced_expression.py
OPENING = '(' def is_balanced(parentheses): stack = [] for paren in parentheses: if paren == OPENING: stack.append(paren) else: try: stack.pop() except IndexError: # too many closing parens return False return len(stack) =...
code/data_structures/src/stack/infix_to_postfix/README.md
# Stack Description --- Stacks are an abstract data type (ADT) that function to store and manipulate data. Their order is last-in-first-out (LIFO). To implement a stack, an array or [linked list](../linked_list) can be used. A stack is similar to a [Tower of Hanoi](https://en.wikipedia.org/wiki/Tower_of_Hanoi) in that ...
code/data_structures/src/stack/infix_to_postfix/infix_to_postfix.c
#include <stdio.h> #include <string.h> #include <stdlib.h> // Part of Cosmos by OpenGenus Foundation // Stack type struct Stack { int top; unsigned capacity; int* array; }; // Stack Operations struct Stack* createStack( unsigned capacity ) { struct Stack* stack = (struct Stack*) malloc(sizeof(struct S...