last pop in stack not working yet

This commit is contained in:
Emanuel Rodriguez 2023-06-30 07:12:43 -07:00
parent 0763ca0cd4
commit cfaf438fd4
3 changed files with 163 additions and 3 deletions

BIN
c/a.out

Binary file not shown.

View File

@ -9,15 +9,52 @@ typedef struct Node {
struct Node* next;
} Node;
typedef struct {
typedef struct Queue {
Node* head;
Node* tail;
int length;
} Queue;
// void enqueue(Queue q) {}
typedef struct ArrayQueue {
Node* head;
Node* tail;
int length;
Node* data[MAX_LEN];
} ArrayQueue;
// void enqueue(Queue q) {}
ArrayQueue* new_array_queue() {
ArrayQueue* a_q = malloc(sizeof(ArrayQueue));
a_q->head = NULL;
a_q->tail = NULL;
for (int i = 0; i < MAX_LEN; i++) {
a_q->data[i] = NULL;
}
a_q->length = 0;
return (a_q);
}
void arr_enqueue(ArrayQueue* a, Node* n) {
if (a->length == 0) {
a->head = n;
a->tail = n;
a->data[a->length] = n;
a->length++;
} else {
a->tail = n;
a->data[a->length] = n;
a->length++;
}
}
// not a very good idea, this will require a O(n) walk through the array to
// shift everything to the left
bool arr_dequeue(ArrayQueue* a) {
if (a->length == 0) {
return false;
} else {
}
}
void enqueue(Queue* q, Node* n) {
if (q->length == 0) {
@ -54,6 +91,12 @@ Node* new_node(int value) {
return n;
}
void free_node(Node* n) {
if (n != NULL) {
free(n);
}
}
Node* peek(Queue* q) { return q->head; }
int main() {

117
c/stack.c Normal file
View File

@ -0,0 +1,117 @@
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
/*
how a stack grows
len = 0 ______
push: len = 1 [A]
push: len = 2 [A] <-- [B] new elements point to the previous top
push: len = 3 [A] <-- [B] <-- [C]
pop: len = 2 [A] <-- [B] <-/- [C] need to free C here and set new head of Stack to be B
pop: len = 1 [A] <-/- [B]
pop: len = 0 /[A]/
*/
typedef struct Node {
int value;
struct Node* next;
} Node;
typedef struct Stack {
int length;
Node* head;
} Stack;
Node* new_node(int value) {
Node* n = malloc(sizeof(Node));
n->value = value;
return (n);
}
Stack* new_stack() {
Stack* s = malloc(sizeof(Stack));
s->length = 0;
s->head = NULL;
return (s);
}
bool pop(Stack* s) {
if (s->length == 0) {
return false;
} else {
Node* n = s->head;
if (s->length == 1) {
s->head = NULL;
} else {
s->head = n->next;
}
free(n);
s->length--;
return true;
}
}
void delete_stack(Stack* s) {
while (s->length > 0) {
pop(s);
}
}
void push(Stack* s, Node* n) {
if (s->length == 0) {
s->head = n;
s->length++;
} else {
// make the old head point to what will be the new head
n->next = s->head;
// make the head point to n now
s->head = n;
s->length++;
}
}
int main() {
Stack* s = new_stack();
Node* a = new_node(10);
Node* b = new_node(11);
Node* c = new_node(12);
Node* d = new_node(13);
push(s, a);
printf("the value at the top of the stack is: %d\n", s->head->value);
printf("the length of the stack is: %d\n", s->length);
push(s, b);
printf("the value at the top of the stack is: %d\n", s->head->value);
printf("the length of the stack is: %d\n", s->length);
push(s, c);
printf("the value at the top of the stack is: %d\n", s->head->value);
printf("the length of the stack is: %d\n", s->length);
push(s, d);
printf("the value at the top of the stack is: %d\n", s->head->value);
printf("the length of the stack is: %d\n", s->length);
bool res = pop(s);
printf("the value at the top of the stack is: %d\n", s->head->value);
printf("the length of the stack is: %d\n", s->length);
res = pop(s);
printf("the value at the top of the stack is: %d\n", s->head->value);
printf("the length of the stack is: %d\n", s->length);
res = pop(s);
printf("the value at the top of the stack is: %d\n", s->head->value);
printf("the length of the stack is: %d\n", s->length);
res = pop(s);
printf("the value at the top of the stack is: %d\n", s->head->value);
printf("the length of the stack is: %d\n", s->length);
// delete_stack(s);
return (0);
}