Question

In: Computer Science

Implement stack in C Struct: struct patients{ int id; int severity; char *firstName; char *lastName; char...

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

Solutions

Expert Solution

#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 ***********************************************************


Related Solutions

A person has a firstname, lastname, ID, and email. A phone number is of the form...
A person has a firstname, lastname, ID, and email. A phone number is of the form countrycode, number. A person may have several related telephone numbers, and a telephone number may be associated with multiple people. The possible relationships are: home, work, and mobile. A person may have at most one phone number for each type of relationship. Draw schema diagram and define and create the tables that implement the model and enforce the given constraints.
IN C++ Given a struct Node { int value; Node *left, *right;}; , implement the functions...
IN C++ Given a struct Node { int value; Node *left, *right;}; , implement the functions below. a) int getSmallest(Node * r); // return smallest value in the BST with root r. Assume r not null. b) int getSecondSmallest(Node * r); // return 2nd smallest value in BST with root r. Assume r not null and r has a nonnull left or right child. c) void removeSecondSmallest(Node * r); // remove 2nd smallest value in BST with root r. Assume...
Translate the following C++ code to Pseudocode: int main() { stack<char> stk,stk2; int length =0,ele; while(cin>>ele)...
Translate the following C++ code to Pseudocode: int main() { stack<char> stk,stk2; int length =0,ele; while(cin>>ele) { stk.push(ele); length++; } if(length%2) stk.pop(); for (int i=0;i<length/2;i++) { ele=stk.top(); stk.pop(); stk2.push(ele); } int flag=1; for(int i=0;i<length/2;i++) { if(stk.top()==stk2.top()) { stk.pop();stk2.pop(); } else { flag=1; break; } } if(flag==1) cout<<"NOT palindrome"; else cout<<"palindrome"; }
Design a database/mysql that has 3 tables: TABLE 1 student: StudID (int), LastName (varchar), FirstName (varchar),...
Design a database/mysql that has 3 tables: TABLE 1 student: StudID (int), LastName (varchar), FirstName (varchar), MiddleName(varchar) TABLE 2 Course: StudID, CourseID TABLE 3 study course: CourseID(int), CourseTitle(varchar), Units(int), PreqCourse(varchar) ** Insert at least 5 records. ** Use the "select" command to view the content of the tables. ** POST THE SCREENSHOT
How would I setup this dictionary for Python 3? class Student(object): def __init__(self, id, firstName, lastName,...
How would I setup this dictionary for Python 3? class Student(object): def __init__(self, id, firstName, lastName, courses = None): The “id”, “firstName” and “lastName” parameters are to be directly assigned to member variables (ie: self.id = id) The “courses” parameter is handled differently. If it is None, assign dict() to self.courses, otherwise assign courses directly to the member variable. Note: The “courses” dictionary contains key/value pairs where the key is a string that is the course number (like “course1”) and...
#include #include #include int reverse(int); // Stack ADT Type Defintions typedef struct node { void* dataPtr;...
#include #include #include int reverse(int); // Stack ADT Type Defintions typedef struct node { void* dataPtr; struct node* link; } STACK_NODE; typedef struct { int count; STACK_NODE* top; } STACK; /* =============== createStack ============== This algorithm creates an empty stack. Pre Nothing Post Returns pointer to a null stack -or- NULL if overflow */ STACK* createStack(void) { // Local Definitions STACK* stack; // Statements stack = (STACK*)malloc(sizeof(STACK)); if (stack) { stack->count = 0; stack->top = NULL; } // if return...
Write the MIPS assembly version of this C code: int weird(char[] s, int x) { int...
Write the MIPS assembly version of this C code: int weird(char[] s, int x) { int i; for (i = 0; i < x; i++) { if (s[i] == ‘A’) { return power(10, i); } } return -1; } int power(int base, int i) { int j = 0; while (j < base) { base = base * base; j++; } return base; }
In c++: Code Challenge Consider the following code: struct ListNode { int value; struct ListNode *next;...
In c++: Code Challenge Consider the following code: struct ListNode { int value; struct ListNode *next; }; ListNode *head; // List head pointer Assume that a linked list has been created and head points to the first node. Write code that traverses the list displaying the contents of each node’s value member Now write the code that destroys the linked list
C Implement the function Append (char** s1, char** s2) that appends the second character array to...
C Implement the function Append (char** s1, char** s2) that appends the second character array to the first, replaces the first array with the result and replaces the second character array with the original first array. For example, if the first character array is "hello" and the second is "world" at the end of the function the new value of the first character array should be"helloworld" and the second array should be "hello". If the input is invalid the function...
c++ Exercise 1: Make a struct “Fate” that contains the following variables: int month, int year,...
c++ Exercise 1: Make a struct “Fate” that contains the following variables: int month, int year, int day. Make another struct Product that would contain the following variables: 1- Product name. 2- An array for the product ingredients. Set its max size to 3. 3- Product date of expiration. Use the date struct you made. Use those structs in entering the data for two products of your choice. Make a function printProduct(Product product) that prints out the contents of the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT