From 538102d93205ca1952008d36a03fa378fd8c6b9c Mon Sep 17 00:00:00 2001 From: SupulCFG Date: Sun, 17 Oct 2021 14:25:10 +0530 Subject: [PATCH 1/3] Added C - Queue --- Data Structures/Queue/queue.c | 118 ++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 Data Structures/Queue/queue.c diff --git a/Data Structures/Queue/queue.c b/Data Structures/Queue/queue.c new file mode 100644 index 0000000..762c302 --- /dev/null +++ b/Data Structures/Queue/queue.c @@ -0,0 +1,118 @@ +#include +#include + +//self referencing structure with one data element +struct Node { + int data; + struct Node *next; +}; + +// Aliasing for easier referencing +typedef struct Node Node; + +/*-------------------------------------- Queue Functions -------------------------------------- */ +/** Ideally these should go in a seperate Header file (ie. queue.h) along with the self referencing structure + * + * Note : Queue is a ADT which is also a derived DS of LinkedList + * + * Queue Functions + * Create Node : create_node + * Insert : enqueue (append to the end of Queue) + * Delete : dequeue (remove the first node of the Queue) + * Peek : peek (views the first element in the queue) + * Display List : print_queue + * + */ + +// Creates a Node in Heap and returns a pointer +Node * create_node(int data) { + Node *new_node = malloc(1 * sizeof(Node)); + new_node->data = data; + new_node->next = NULL; + + return new_node; +} + +// appends a node to the end of the Queue +void enqueue(Node **front, int data) { + if(*front == NULL) { + *front = create_node(data); + }else { + Node *current = *front; + + while(current->next != NULL) { + current = current->next; + } + + current->next = create_node(data); + } +} + +// removes the first element in the queue +void dequeue(Node **front) { // Can be modified to return the removed Node | int according to the usage + if(*front == NULL) { + printf("Dequeue Failed : Empty Queue!!\n"); + return; + } + + Node *current = *front; + + *front = current->next; + + int deleted = current->data; + + free(current); + + printf("Dequeued : %d\n", deleted); + +} + +//Takes a peek at the first elemen in the queue +void peek(Node *head) { + if(head == NULL) { + printf("Empty Queue!!\n"); + }else { + printf("First Element of the Queue : %d\n", head->data); + } +} + +// Prints all the elements in the Queue +void print_queue(Node *head) { + if(head == NULL) { + printf("Empty Queue!!\n"); + return; + } + + printf("["); + + while(head != NULL) { + printf(" %d", head->data); + + if(head->next != NULL) printf(","); + + head = head->next; + } + + printf(" ]\n"); +} +/* --------------------------------- End of Linked List Functions ----------------------------------- */ + +int main() { + Node *queue = NULL; // Pointer to the First Node of the Queue + + enqueue(&queue, 10); + enqueue(&queue, 30); + enqueue(&queue, 50); + enqueue(&queue, 60); + + print_queue(queue); + + + dequeue(&queue); + dequeue(&queue); + peek(queue); + + print_queue(queue); + + return 0; +} \ No newline at end of file From bfb4b7651de9842e7025cc9e1689a3a57a388939 Mon Sep 17 00:00:00 2001 From: SupulCFG Date: Sun, 17 Oct 2021 14:35:43 +0530 Subject: [PATCH 2/3] Added C - Queue - (Quickfix) --- Data Structures/Queue/queue.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Data Structures/Queue/queue.c b/Data Structures/Queue/queue.c index 762c302..3f044a7 100644 --- a/Data Structures/Queue/queue.c +++ b/Data Structures/Queue/queue.c @@ -13,7 +13,7 @@ typedef struct Node Node; /*-------------------------------------- Queue Functions -------------------------------------- */ /** Ideally these should go in a seperate Header file (ie. queue.h) along with the self referencing structure * - * Note : Queue is a ADT which is also a derived DS of LinkedList + * Note : Queue is a ADT which is also a derived DS from LinkedList * * Queue Functions * Create Node : create_node @@ -95,7 +95,7 @@ void print_queue(Node *head) { printf(" ]\n"); } -/* --------------------------------- End of Linked List Functions ----------------------------------- */ +/* --------------------------------- End of Queue Functions ----------------------------------- */ int main() { Node *queue = NULL; // Pointer to the First Node of the Queue From 0665a3b543343fc208f87c60164c313d15298c1b Mon Sep 17 00:00:00 2001 From: SupulCFG Date: Mon, 18 Oct 2021 10:31:38 +0530 Subject: [PATCH 3/3] Added C - Stack --- Data Structures/Stack/stack.c | 113 ++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 Data Structures/Stack/stack.c diff --git a/Data Structures/Stack/stack.c b/Data Structures/Stack/stack.c new file mode 100644 index 0000000..eea97ef --- /dev/null +++ b/Data Structures/Stack/stack.c @@ -0,0 +1,113 @@ +#include +#include + +//self referencing structure with one data element +struct Node { + int data; + struct Node *next; +}; + +// Aliasing for easier referencing +typedef struct Node Node; + +/*-------------------------------------- Stack Functions -------------------------------------- */ +/** Ideally these should go in a seperate Header file (ie. stack.h) along with the self referencing structure + * + * Stack Functions + * Create Node : create_node + * Push : push (adds an item to the top of the stack) + * Pop : pop (removes the topmost item from the stack) + * Peek : peek (take a look at the topmost item without popping) + * Display Stack : print_stack + * + */ + +Node * create_node(int data) { + Node *new_node = malloc(1 * sizeof(Node)); + + new_node->data = data; + new_node->next = NULL; + + return new_node; +} + + +// adds a Node to the top of the stack +void push(Node **top, int data) { + Node *new_node = create_node(data); + + if(*top != NULL) { + new_node->next = *top; + } + + *top = new_node; + +} + +// Pops the Top Node from the Stack +int pop(Node **top) { + if(*top == NULL) { + printf("Popping Failed: Empty Stack !!\n"); + return -1; + } + + Node *popped = *top; + + *top = popped->next; + + int popped_int = popped->data; + + free(popped); + + printf("Successfully Popped : %d\n", popped_int); + + return popped_int; +} + +// Prints all the elements in the Stack +void print_stack(Node *top) { + if(top == NULL) { + printf("Empty Stack!!\n"); + return; + } + + printf("["); + + while(top != NULL) { + printf(" %d", top->data); + + if(top->next != NULL) printf(","); + + top = top->next; + } + + printf(" ]\n"); +} + +//Prints the topmost item in the Stack +void peek(Node *top) { + if(top == NULL) { + printf("Empty Stack!!\n"); + }else { + printf("First item of the Stack : %d\n", top->data); + } +} + +/* --------------------------------- End of Stack Functions ----------------------------------- */ + +int main() { + Node *stack = NULL; + + push(&stack,10); + push(&stack,20); + push(&stack,50); + + pop(&stack); + peek(stack); + + push(&stack,40); + push(&stack,80); + + print_stack(stack); + +} \ No newline at end of file