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...
*Answer in C program* #include <stdio.h> int main() {      FILE *fp1;      char c;     ...
*Answer in C program* #include <stdio.h> int main() {      FILE *fp1;      char c;      fp1= fopen ("C:\\myfiles\\newfile.txt", "r");      while(1)      {         c = fgetc(fp1);         if(c==EOF)             break;         else             printf("%c", c);      }      fclose(fp1);      return 0; } In the program above which statement is functioning for opening a file Write the syntax for opening a file What mode that being used in the program. Give the example from the program Referring to...
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...
In simple Java language algorithm: Implement a static stack class of char. Your class should include...
In simple Java language algorithm: Implement a static stack class of char. Your class should include two constructors. One (no parameters) sets the size of the stack to 10. The other constructor accepts a single parameter specifying the desired size of the stack a push and pop operator an isEmpty and isFull method . Both return Booleans indicating the status of the stack Using the stack class you created in problem 1), write a static method called parse that parses...
#include <iostream> #include <stack> #include <queue> using namespace std; void printFromStack(string expr){ stack<char> myStack; for(int i=0;...
#include <iostream> #include <stack> #include <queue> using namespace std; void printFromStack(string expr){ stack<char> myStack; for(int i=0; i<expr.length(); i++){ //Insert code here to push each character onto the stack } cout << "My stack is popped in this order" << endl; while(!myStack.empty()){ //Insert code here to cout the top of the stack one by one //Pop each one after it’s printed out } cout << endl; } void printFromQueue(string expr){ queue<char> myQueue; //Insert code here to push each character onto the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT