diff --git a/data_structures/queues/C/queue_using_array.c b/data_structures/queues/C/queue_using_array.c new file mode 100644 index 0000000..f2a49c6 --- /dev/null +++ b/data_structures/queues/C/queue_using_array.c @@ -0,0 +1,76 @@ +#include +#include +#define MAX 10 + +int A[MAX]; +int front = -1; +int rear = -1; + +void enqueue() +{ + if ((rear+1)%MAX == front) + { + printf("Can't Insert! FULL\n"); + return; + } + + if (front == -1) + { + front++; + } + printf("Enter data: "); + scanf("%d", &A[(rear+1)%MAX]); +} + +void dequeue() +{ + if (front == -1) + { + printf("Empty!\n"); + return; + } + if (front == rear) + { + printf("data: %d\n", A[front]); + front = -1; + rear = -1; + return; + } + printf("data: %d\n", A[(rear+1)%MAX]); +} + +void atFront() +{ + if (front == -1) + { + printf("Empty!\n"); + return; + } + printf("At the top: %d\n", A[front]); +} + +int main() +{ + int choice; + + while(1) + { + printf("1. Enqueue\n2. Dequeue\n3. At front\n4. Exit\n\nEnter Choice: "); + scanf("%d", &choice); + switch(choice) + { + case 1: + enqueue(); + break; + case 2: + dequeue(); + break; + case 3: + atFront(); + break; + default: + exit(0); + } + } + +} \ No newline at end of file diff --git a/data_structures/queues/C/queue_using_linkedlist.c b/data_structures/queues/C/queue_using_linkedlist.c new file mode 100644 index 0000000..6b47d56 --- /dev/null +++ b/data_structures/queues/C/queue_using_linkedlist.c @@ -0,0 +1,78 @@ +#include +#include + +struct node { + int data; + struct node *next; +}; + +struct node* front = NULL, *rear = NULL; + +void enqueue() +{ + struct node* temp = (struct node* )malloc(sizeof(struct node)); + printf("Enter data: "); + scanf("%d", &temp->data); + temp->next = NULL; + + if (front == NULL) + { + front = rear = temp; + return; + } + + rear->next = temp; + rear = temp; +} + +void dequeue() +{ + if (front == NULL) + { + printf("Empty!\n"); + return; + } + struct node* temp = front; + printf("data: %d\n", front->data); + if (front == rear) + front = rear = NULL; + else + front = front->next; + free(temp); +} + +void atFront() +{ + if (front == NULL) + { + printf("Empty!\n"); + return; + } + printf("At the top: %d\n", front->data); +} + +int main() +{ + int choice; + + while(1) + { + printf("1. Enqueue\n2. Dequeue\n3. At front\n4. Exit\n\nEnter Choice: "); + scanf("%d", &choice); + switch(choice) + { + case 1: + enqueue(); + break; + case 2: + dequeue(); + break; + case 3: + atFront(); + break; + default: + exit(0); + } + } + +} \ No newline at end of file diff --git a/data_structures/stacks/stack.c b/data_structures/stacks/C/stack.c similarity index 100% rename from data_structures/stacks/stack.c rename to data_structures/stacks/C/stack.c diff --git a/data_structures/stacks/C/stack_using_linked_list.c b/data_structures/stacks/C/stack_using_linked_list.c new file mode 100644 index 0000000..6b47d56 --- /dev/null +++ b/data_structures/stacks/C/stack_using_linked_list.c @@ -0,0 +1,78 @@ +#include +#include + +struct node { + int data; + struct node *next; +}; + +struct node* front = NULL, *rear = NULL; + +void enqueue() +{ + struct node* temp = (struct node* )malloc(sizeof(struct node)); + printf("Enter data: "); + scanf("%d", &temp->data); + temp->next = NULL; + + if (front == NULL) + { + front = rear = temp; + return; + } + + rear->next = temp; + rear = temp; +} + +void dequeue() +{ + if (front == NULL) + { + printf("Empty!\n"); + return; + } + struct node* temp = front; + printf("data: %d\n", front->data); + if (front == rear) + front = rear = NULL; + else + front = front->next; + free(temp); +} + +void atFront() +{ + if (front == NULL) + { + printf("Empty!\n"); + return; + } + printf("At the top: %d\n", front->data); +} + +int main() +{ + int choice; + + while(1) + { + printf("1. Enqueue\n2. Dequeue\n3. At front\n4. Exit\n\nEnter Choice: "); + scanf("%d", &choice); + switch(choice) + { + case 1: + enqueue(); + break; + case 2: + dequeue(); + break; + case 3: + atFront(); + break; + default: + exit(0); + } + } + +} \ No newline at end of file