forked from OpenGenus/cosmos
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/OpenGenus/cosmos into aut…
…oformat-js
- Loading branch information
Showing
20 changed files
with
389 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
54 changes: 4 additions & 50 deletions
54
...lar_linked_list/circular_linked_list.java → ...cular_linked_list/CircularLinkedList.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
code/data_structures/src/stack/balanced_expression/balanced_expression.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.