In: Computer Science
Implement stack in C
Struct:
struct patients{
int id;
int severity;
char *firstName;
char *lastName;
char *state;
int time_spent;
};
Given Array: struct patients* patientsArray[4] = {&p1, &p2, &p3, &p4};
Create two functions one for pushing this array to the stack and one for popping the variables such as p1 p2 p3 p4 by its ID
#include <stdio.h>
#include <stdlib.h>
struct patients
{
int id;
int severity;
char *firstName;
char *lastName;
char *state;
int time_spent;
};
struct Stack
{
int top;
unsigned capacity;
struct patients* patients;
};
struct Stack* createStack(unsigned capacity)
{
struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack));
stack->capacity = capacity;
stack->top = -1;
stack->patients = (struct patients*)malloc(stack->capacity * sizeof(struct patients));
return stack;
}
int isFull(struct Stack* stack)
{
return stack->top == stack->capacity - 1;
}
int isEmpty(struct Stack* stack)
{
return stack->top == -1;
}
void push(struct Stack* stack, struct patients pat)
{
if (isFull(stack))
return;
stack->patients[++stack->top] = pat;
printf("%s %s pushed to stack\n", pat.firstName, pat.lastName);
}
int pop(struct Stack* stack)
{
int id;
if (isEmpty(stack))
id = -1;
else
id = stack->patients[stack->top--].id;
return id;
}
int main(void)
{
struct patients p1;
p1.id = 178;
p1.severity = 4;
p1.firstName = "John";
p1.lastName = "Smith";
p1.state = "California";
p1.time_spent = 45;
struct patients p2;
p2.id = 139;
p2.severity = 7;
p2.firstName = "Mary";
p2.lastName = "Jane";
p2.state = "Texas";
p2.time_spent = 23;
struct patients p3;
p3.id = 199;
p3.severity = 2;
p3.firstName = "Ann";
p3.lastName = "Doe";
p3.state = "Florida";
p3.time_spent = 33;
struct patients p4;
p4.id = 167;
p4.severity = 5;
p4.firstName = "Ralph";
p4.lastName = "Bush";
p4.state = "Hawaii";
p4.time_spent = 55;
struct Stack* stack = createStack(10);
push(stack, p1);
push(stack, p2);
push(stack, p3);
push(stack, p4);
printf("Patient id %d popped from stack\n", pop(stack));
return 0;
}
**************************************************************** SCREENSHOT ***********************************************************