Skip to content
This repository has been archived by the owner on Oct 22, 2021. It is now read-only.

Added C - Queue #70

Merged
merged 3 commits into from
Oct 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 118 additions & 0 deletions Data Structures/Queue/queue.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
#include <stdio.h>
#include <stdlib.h>

//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 from 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 Queue 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;
}
113 changes: 113 additions & 0 deletions Data Structures/Stack/stack.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#include <stdio.h>
#include <stdlib.h>

//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);

}