Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/OpenGenus/cosmos into aut…
Browse files Browse the repository at this point in the history
…oformat-js
  • Loading branch information
arnavb committed Feb 26, 2019
2 parents 181cf42 + 19633a2 commit b8e07cc
Show file tree
Hide file tree
Showing 20 changed files with 389 additions and 74 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Each type has several hundreds of problems with solutions in several languages s

# Maintainers

This is a massive collaboration and to keep the quality intact and drive the vision in the proper direction, we have maintainers.
This is a massive collaboration. Hence, to keep the quality intact and drive the vision in the proper direction, we have maintainers.

> Maintainers are your friends forever. They are vastly different from moderators.
Expand Down
71 changes: 71 additions & 0 deletions code/data_structures/src/linked_list/linked_list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
class LinkedList {
constructor(value) {
this.head = null;
this.length = 0;
this.addToHead(value);
}

addToHead(value) {
const newNode = { value };
newNode.next = this.head;
this.head = newNode;
this.length++;
return this;
}

removeFromHead() {
if (this.length === 0) {
return undefined;
}

const value = this.head.value;
this.head = this.head.next;
this.length--;

return value;
}

find(val) {
let thisNode = this.head;

while(thisNode) {
if(thisNode.value === val) {
return thisNode;
}

thisNode = thisNode.next;
}

return thisNode;
}

remove(val) {
if(this.length === 0) {
return undefined;
}

if (this.head.value === val) {
return this.removeFromHead();
}

let previousNode = this.head;
let thisNode = previousNode.next;

while(thisNode) {
if(thisNode.value === val) {
break;
}

previousNode = thisNode;
thisNode = thisNode.next;
}

if (thisNode === null) {
return undefined;
}

previousNode.next = thisNode.next;
this.length--;
return this;
}
}
Original file line number Diff line number Diff line change
@@ -1,155 +1,109 @@
import java.util.Scanner;

// Part of Cosmos by OpenGenus Foundation
public class CircularLinkedList {

public class circular_linked_list {
Node head, tail;

// Node Class

class Node {

int value;
Node next;

public Node(int value, Node next) {

this.value = value;
this.next = next;

}

}

// Manipulation Methods

public void Add(int value) {

Node n = new Node(value, head);
if (head == null) {

head = n;
tail = n;
n.next = n;

}
else {

tail.next = n;
tail = n;

}

}

public void Remove(int value) {

Node aux = head;
Node prev = tail;
boolean found = false;

// Find the node with the value

if (aux == null) return;
if (aux == head && aux.value == value) { found = true; }
else {

prev = aux;
aux = aux.next;
while (aux != head) {

if (aux.value == value) {
found = true;
break;
}

prev = aux;
aux = aux.next;
}

}

// If found, remove it

if (!found) return;
if (aux == head && aux == tail) { head = tail = null; }
else {
if (aux == head) head = aux.next;
if (aux == tail) tail = prev;
prev.next = aux.next;
}

}

public void Print() {

if (head == null) return;

System.out.print(head.value + " ");

Node aux = head.next;
while (aux != head) { System.out.print(aux.value + " "); aux = aux.next; }
System.out.println();
System.out.println("HEAD is " + head.value);
System.out.println("TAIL is " + tail.value);

}

//

Node head, tail;

// Example Usage

public static void main(String[] args) {

int selection = 0;
Scanner input = new Scanner(System.in);
circular_linked_list list = new circular_linked_list();
CircularLinkedList list = new CircularLinkedList();

do {

System.out.print("1. Insert\n2. Remove\n3. Print\n> ");
selection = input.nextInt();

if (selection == 1) {

System.out.print("Enter Value to Insert: ");
int value = input.nextInt();

list.Add(value);
list.Print();

System.out.print("\n");

}
else if (selection == 2) {

System.out.print("Enter Value to Remove: ");
int value = input.nextInt();

list.Remove(value);
list.Print();

System.out.println("\n");

}
else if (selection == 3) {

list.Print();
System.out.println("\n");

}
else {

System.out.println("Invalid Selection. Exiting");

}

}
while (selection > 0 && selection < 4);

input.close();

}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ class DoublyLinkedList {
private Node last;
private int n = 0; //size


/*-----Insert Method-----*/

public void insert(int pos, int data){
if (pos > n + 1){
pos = n + 1;
Expand Down Expand Up @@ -75,7 +73,6 @@ public void insert(int pos, int data){
}

/*-----Delete Method-----*/

public void delete(int pos){
if (n == 0) {
System.out.println("List is Empty!");
Expand All @@ -86,6 +83,12 @@ public void delete(int pos){
pos = n;
}

if (pos == 1) {
first = first.next;
first.prev = null;
return;
}

int c = 1;
Node cur = first;

Expand All @@ -94,16 +97,16 @@ public void delete(int pos){
cur = cur.next;
}


cur.prev.next = cur.next;
cur.next.prev = cur.prev;

if (cur.prev != null) {
cur.prev.next = cur.next;
}
if (cur.next != null) {
cur.next.prev = cur.prev;
}
n--;

}

/*-----Print in forward direction-----*/

public void display() {
Node cur = first;

Expand All @@ -116,7 +119,6 @@ public void display() {
}

/*-----Print in reverse direction-----*/

public void reverseDisplay() {
Node cur = last;

Expand Down Expand Up @@ -145,11 +147,14 @@ public static void main(String[] args) {

switch(c) {
case 1:
System.out.println("Enter insertion position");
int pos = sc.nextInt();
System.out.println("Enter value");
int val = sc.nextInt();
l.insert(pos, val);
break;
case 2:
System.out.println("Enter deletion position");
pos = sc.nextInt();
l.delete(pos);
break;
Expand Down
5 changes: 5 additions & 0 deletions code/data_structures/src/queue/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,8 @@ Functions
- size()
--Returns the size of the queue

## Time Complexity
- Enqueue - O(1)
- Dequeue - O(1)
- size - O(1)
- front - O(n)
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#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 -> data );
}

int
empty(stack* top) {
return (top == NULL);
}

void
pop(stack** top) {
stack* temp = *top;
*top = (*top)->next;
free(temp);
temp = NULL;
}

int
checkBalanced(char *s) {
int len = strlen(s);
if (len % 2 != 0) return 0;
stack *head = NULL;
for (int i = 0;i < len ;i++) {
if (s[i] == '{' || s[i] == '[' || s[i] == '(') {
push(&head, s[i]);
}
else {
char temp = peek(head);
if (s[i] == '}' && temp == '{') pop(&head);
else if (s[i] == ']' && temp == '[') pop(&head);
else if (s[i] == ')' && temp == '(') pop(&head);
else return 0;
}
}
return (empty(head));
}

int
main()
{
char *s = (char*) malloc(sizeof(char) * 100);
printf("Enter the expression\n");
scanf("%[^\n]s" , s);
int res = checkBalanced(s);
if (res) printf("Expression is valid\n");
else printf("Expression is not valid\n");
return 0;
}
Loading

0 comments on commit b8e07cc

Please sign in to comment.