Skip to content

Instantly share code, notes, and snippets.

@retraigo
Created August 9, 2022 10:00
Show Gist options
  • Save retraigo/355bf3cbc812482c3f01968ccf398450 to your computer and use it in GitHub Desktop.
Save retraigo/355bf3cbc812482c3f01968ccf398450 to your computer and use it in GitHub Desktop.
#include <stdio.h>
char queue[256];
int last = -1;
int limit = 256;
void enqueue(char itemToInsert) {
if (last == limit - 1) {
printf("Queue Overflow");
return;
}
last = last + 1;
queue[last] = itemToInsert;
}
void dequeue() {
if (last == -1) {
printf("Queue Underflow");
return;
}
for(int i = 0; i < last; ++i) {
queue[i] = queue[i + 1];
}
last -= 1;
}
void main() {
enqueue('a');
enqueue('b');
enqueue('c');
enqueue('d');
enqueue('e');
enqueue('f');
printf("\n\nADDED ITEMS");
for(int i = 0; i <= last; ++i) {
printf("\n%c", queue[i]);
}
dequeue();
printf("\n\nREMOVED FIRST ITEM");
for(int i = 0; i <= last; ++i) {
printf("\n%c", queue[i]);
}
}
#include <stdio.h>
char stack[256];
int last = -1;
int limit = 256;
void push(char itemToInsert) {
if (last == limit - 1) {
printf("Stack Overflow");
return;
}
last = last + 1;
stack[last] = itemToInsert;
}
void pop() {
if (last == -1) {
printf("Stack Underflow");
return;
}
stack[last] = NULL;
last = last - 1;
}
void main() {
push('a');
push('b');
push('c');
push('d');
push('e');
push('f');
printf("\n\nADDED ITEMS");
for(int i = 0; i <= last; ++i) {
printf("\n%c", queue[i]);
}
push();
printf("\n\nREMOVED LAST ITEM");
for(int i = 0; i <= last; ++i) {
printf("\n%c", queue[i]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment