common.h
#ifndef COMMON_H #define COMMON_H #include <stdbool.h> #define MAX_LEN 20 #define MAX_ITEMS 5 struct Stack { char items[MAX_ITEMS][MAX_LEN]; int top; }; void stackInit(struct Stack *stack); int stackPush(struct Stack *stack, char *string); char* stackPop(struct Stack *stack); void reverseStack(struct Stack *stack); int searchStack(struct Stack *stack, char *target); bool isStackEmpty(struct Stack *stack); struct Queue { char items[MAX_ITEMS][MAX_LEN]; int front; int rear; }; void queueInit(struct Queue *queue); int enqueue(struct Queue *queue, char *string); char* dequeue(struct Queue *queue); void reverseQueue(struct Queue *queue); int searchQueue(struct Queue *queue, char *target); bool isQueueEmpty(struct Queue *queue); struct Node { char data[MAX_LEN]; struct Node *next; }; struct Node* createNode(char *data); void freeLinkedList(struct Node *head); struct Node* appendNode(struct Node *head, char *data); void bubbleSort(int array[], int size); void selectionSort(int array[], int size); int linearSearch(int array[], int size, int target); int binarySearch(int array[], int size, int target); #endif
stack.c
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h> #include "common.h" void stackInit(struct Stack *stack) { stack->top = 0; } int stackPush(struct Stack *stack, char *string) { if (stack->top >= MAX_ITEMS) return -1; if (strlen(string) > MAX_LEN) return -1; strcpy(stack->items[stack->top], string); stack->top++; return 0; } char* stackPop(struct Stack *stack) { if (stack->top == 0) return NULL; stack->top--; char *temp = stack->items[stack->top]; memset(stack->items[stack->top], 0, MAX_LEN); return temp; } void reverseStack(struct Stack *stack) { if (stack->top == 0) return; int left = 0; int right = (stack->top - 1); while (left < right) { char temp[MAX_LEN] = {0}; strcpy(temp, stack->items[left]); strcpy(stack->items[left], stack->items[right]); strcpy(stack->items[right], temp); left++; right--; } } int searchStack(struct Stack *stack, char *target) { if (strlen(target) > MAX_LEN) return -1; for (int i = 0; i < stack->top; i++) { if (strcmp(stack->items[i], target) == 0) return i; } return -1; } bool isStackEmpty(struct Stack *stack) { if (stack->top == 0) { return true; } else { return false; } }
A stack is an algorithm that enables computer scientists to manage data following a simple rule--Last In First Out.
Let's say there are pancakes towered--stacked over each other--and we want to do something with this tower of pancakes.
In reality, we could eat them, we could take off any pancakes the way we want but computers cannot do these things, rather,
we have to program them.
So first we stackInit() the stack--this sets useful variables that the computer keeps track off.
Now the next step is that we can stackPush() new pancakes to the stack--this appends new pancakes.
Remember there is "no limit" to how many pancakes this to-be-stack can possess. Our limit would be the computer's memory.
Note: Pop in this scenario is meaning: to remove new pancakes at the end.
If we feel like pancakes are in lot, we can stackPop() the stack--this removes the last new pancake.
Remember, the last in is the first out. The last pancake in, is the first pancake out or popped.
We could change the rule of the stack algorithm and say if First in then first out--this is another algorithm on its own
called a queue. Using the reverseStack() flips the stack, top to bottom and bottom to top.
We could search--using searchStack()--the stack of pancakes based on something, but in this case their names are used.
We could check if the stack is empty using isStackEmpty(). we could do this to prevent popping new pancakes when the stack is empty.
queue.c
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h> #include "common.h" void queueInit(struct Queue *queue) { queue->front = 0; queue->rear = 0; } int enqueue(struct Queue *queue, char *string) { if (queue->rear >= MAX_ITEMS) return -1; if (strlen(string) > MAX_LEN) return -1; strcpy(queue->items[queue->rear], string); queue->rear++; return 0; } char* dequeue(struct Queue *queue) { if (queue->front == queue->rear) return NULL; char *temp = queue->items[queue->front]; memset(queue->items[queue->front], 0, MAX_LEN); queue->front++; return temp; } void reverseQueue(struct Queue *queue) { if (queue->front == queue->rear) return; int right = (queue->rear - 1); int left = queue->front; while (left < right) { char temp[MAX_LEN] = {0}; strcpy(temp, queue->items[left]); strcpy(queue->items[left], queue->items[right]); strcpy(queue->items[right], temp); left++; right--; } } int searchQueue(struct Queue *queue, char *target) { if (strlen(target) > MAX_LEN) return -1; for (int i = 0; i < queue->rear; i++) { if (strcmp(queue->items[i], target) == 0) return i; } return -1; } bool isQueueEmpty(struct Queue *queue) { if (queue->rear == queue->front) { return true; } else { return false; } }
A queue is an algorithm that computer scientists use in their work. It follows a rule--First In First Out.
Imagine there were three customers at a supermarket who wanted to buy chocolate icecream. The first customer being A,
the second being B, the third customer being C. A vendor will expect orders to come from first person to come into the line or queue
and then the second person till the last person (third person). This illustrates the rule--First In line is First Out to
be attended to (serviced).
If vendor wants to start the queue, the vendor calls the queueInit()
function.
If the vendor wants to call someone into queue or allow someone to join the queue. The vendor can call the
enqueue()function.
If vendor wants to service (attend to) a customer, just by calling the dequeue()
function metaphorically means that the customer has been serviced (attend to) and removed (dequeued) of the queue.
reverseQueue() will reverse the queue to be last in first out hence a stack-like algorithm.
searchQueue() this will search the queue for a target and once found, it returns the index.
isQueueEmpty this will return true or false depending on whether the queue is empty or not empty.
quiz.c
#include <stdio.h> #include <stdbool.h> #include <stdlib.h> #include <string.h> #include <ctype.h> int main(void) { char questions[][100] = {"Who was the first president of USA", "What was the year Apollo", "When does February have 29 days", "Why is Africa the hottest continent"}; char options[4][2][100] = { {"A. Abraham Lincoln", "B. Goerge Washington"}, {"A. 1970", "B. 1969"}, {"A. Leap Year", "B. Ordinary Year"}, {"A. Equatorial Climate", "B. Solar Power"} }; char answers[] = {'B', 'B', 'A', 'A'}; char guess; int numberOfQuestions = sizeof(questions)/sizeof(questions[0]); int score = 0; printf("\n......Quiz Game......\n\n"); for (int i = 0; i < numberOfQuestions; i++) { for (int j = 0; j < 1; j++) { printf("%s\n",questions[i]); printf("%s\n%s\n", options[i][j],options[i][j+1]); } printf("Enter a choice; "); scanf(" %c", &guess); guess = toupper(guess); if (guess == answers[i]) { printf("Correct\n...........\n"); score++; } else{ printf("Incorrect\n.........\n"); } } printf("\nScore %.1f\n..............\n", (((float)score / (float)numberOfQuestions) * 100.00)); printf("\n"); return 0; }