Tpe 233
Tpe 233
I – MEMBERS
- Tsem Idriss Terrance: 22S74689
II – SOURCE CODE
#include <stdio.h>
#include <stdlib.h>
/* since Boolean not a native type c, lets create a custom type (an enum) to handle Boolean
*/
// Stack tructure
typedef struct {
int items[MAX];
int top;
} Stack;
// Structure for Queue
typedef struct {
int items[MAX];
} Queue;
/*
*/
s->top = -1;
}
void push(Stack *s, int value) {
if (isStackFull(s) == true) {
printf("Stack is full!\n");
return;
s->items[++s->top] = value;
if (isStackEmpty(s) == true) {
printf("Stack is empty!\n");
return -1;
return s->items[s->top--];
/*
q->front = -1;
q->rear = -1;
if (isQueueFull(q) == true) {
printf("Queue is full!\n");
return;
q->items[++q->rear] = value;
if (isQueueEmpty(q) == true) {
printf("Queue is empty!\n");
return -1;
return q->items[q->front++];
// Menu Function:
/*
*/
void menu() {
Stack stack;
Queue queue;
initStack(&stack);
initQueue(&queue);
/*
- here the while loop always evaluate to true making the program to keep running
- the program only stops only when explicitly demanded by the user (by pressing option 3)
*/
while (1) {
printf("\nChoose a structure:\n");
printf("1. Stack\n");
printf("2. Queue\n");
printf("3. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("\nStack Operations:\n");
printf("1. Push\n");
printf("2. Pop\n");
scanf("%d", &choice);
if (choice == 1) {
scanf("%d", &value);
push(&stack, value);
} else if (choice == 2) {
value = pop(&stack);
} else {
break;
break;
case 2:
printf("\nQueue Operations:\n");
printf("1. Enqueue\n");
printf("2. Dequeue\n");
scanf("%d", &choice);
if (choice == 1) {
scanf("%d", &value);
enqueue(&queue, value);
} else if (choice == 2) {
value = dequeue(&queue);
} else {
break;
break;
case 3:
exit(0);
default:
printf("Invalid choice.\n");
int main() {
menu();
return 0;