0% found this document useful (0 votes)
11 views8 pages

Tpe 233

The document contains a C program that implements stack and queue data structures with their respective operations. It defines functions for initializing, checking the status, pushing, popping for stacks, and enqueueing, dequeueing for queues. A menu-driven interface allows users to interact with these data structures and perform operations until they choose to exit.

Uploaded by

michaeltsemzang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views8 pages

Tpe 233

The document contains a C program that implements stack and queue data structures with their respective operations. It defines functions for initializing, checking the status, pushing, popping for stacks, and enqueueing, dequeueing for queues. A menu-driven interface allows users to interact with these data structures and perform operations until they choose to exit.

Uploaded by

michaeltsemzang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

TPE 233

I – MEMBERS
- Tsem Idriss Terrance: 22S74689

II – SOURCE CODE
#include <stdio.h>

#include <stdlib.h>

#define MAX 100 // Maximum size of stack and queue

/* since Boolean not a native type c, lets create a custom type (an enum) to handle Boolean

Here we define the structure the stack

*/

typedef enum{true, false} Boolean;

// Stack tructure

typedef struct {

int items[MAX];

int top;

} Stack;
// Structure for Queue

typedef struct {

int items[MAX];

int front, rear;

} Queue;

/*

FUNCTIONS TO MANIPULATE THE STACK

* initStack: use to initialize the stack structure

* isStackFull: checks if the stack is full or not

* isStackEmpty: checks if the stack is empty

* push: to add elements in the stack

* pop: to remove elements from the stack

*/

void initStack(Stack *s) { //takes a pointer to a stack data structure

s->top = -1;

Boolean isStackFull(Stack *s) {

return s->top == MAX - 1 ? true : false;

Boolean isStackEmpty(Stack *s) {

return s->top == -1 ? true: false;

}
void push(Stack *s, int value) {

if (isStackFull(s) == true) {

printf("Stack is full!\n");

return;

s->items[++s->top] = value;

printf("%d has been pushed onto the stack.\n", value);

int pop(Stack *s) {

if (isStackEmpty(s) == true) {

printf("Stack is empty!\n");

return -1;

return s->items[s->top--];

/*

FUNCTIONS TO MANIPULATE THE STACK

* initQueue: use to initialize the queue structure

* isQueueFull: checks if the queue is full or not

* isQueueEmpty: checks if the queue is empty

* enqueue: to add elements in the queue

* dequeue: to remove elements from the queue


*/

void initQueue(Queue *q) {

q->front = -1;

q->rear = -1;

int isQueueFull(Queue *q) {

return q->rear == MAX - 1 ? true : false;

int isQueueEmpty(Queue *q) {

return q->front == -1 || q->front > q->rear ? true : false;

void enqueue(Queue *q, int value) {

if (isQueueFull(q) == true) {

printf("Queue is full!\n");

return;

if (q->front == -1) q->front = 0;

q->items[++q->rear] = value;

printf("%d has been added to the queue.\n", value);

int dequeue(Queue *q) {

if (isQueueEmpty(q) == true) {
printf("Queue is empty!\n");

return -1;

return q->items[q->front++];

// Menu Function:

/*

Here, we permit the user to choose the

structure he wants to play around with.

Depending on the structure the user chooses

he is allowed to equally choose an operation he wants to

use on this data structure.

*/

void menu() {

Stack stack;

Queue queue;

initStack(&stack);

initQueue(&queue);

int choice, value;

/*

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

printf("Enter your choice: ");

scanf("%d", &choice);

//use switch case to track the users choise

switch (choice) {

case 1:

printf("\nStack Operations:\n");

printf("1. Push\n");

printf("2. Pop\n");

printf("3. Return to main menu\n");

printf("Enter your choice: ");

scanf("%d", &choice);

if (choice == 1) {

printf("Enter the value to push: ");

scanf("%d", &value);

push(&stack, value);

} else if (choice == 2) {

value = pop(&stack);

if (value != -1) printf("Popped value: %d\n", value);

} else {
break;

break;

case 2:

printf("\nQueue Operations:\n");

printf("1. Enqueue\n");

printf("2. Dequeue\n");

printf("3. Return to main menu\n");

printf("Enter your choice: ");

scanf("%d", &choice);

if (choice == 1) {

printf("Enter the value to enqueue: ");

scanf("%d", &value);

enqueue(&queue, value);

} else if (choice == 2) {

value = dequeue(&queue);

if (value != -1) printf("Dequeued value: %d\n", value);

} else {

break;

break;

case 3:

//exit the loop if user chooses to.


printf("Goodbye!\n");

exit(0);

default:

printf("Invalid choice.\n");

// the main function to handle all the operations

int main() {

//start by calling the menu function to initialize users choises

menu();

return 0;

You might also like