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.
Given: struct Person { int id;     int stats[3] }; Which is the correct way to...
Given: struct Person { int id;     int stats[3] }; Which is the correct way to initialise an array of Persons? 1. struct Person persons[2] = {7, "8,9,3", 8, "2,5,9"}; 2. struct Person persons[2] = "7, {8,9,3}, 8, {2,5,9}"; 3. struct Person persons[2] = "7, "8,9,3", 8, "2,5,9"; 4. struct Person persons[2] = {7, {8,9,3}, 8, {2,5,9}}; Which of the following is not a primitive type in the C language? 1. string 2. int 3. long 4. char Given: struct...
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...
(C++) You are given a file consisting of students’ names in the following form: lastName, firstName...
(C++) You are given a file consisting of students’ names in the following form: lastName, firstName middleName. (Note that a student may not have a middle name.) Write a program that converts each name to the following form: firstName middleName lastName. Your program must read each student’s entire name in a variable and must consist of a function that takes as input a string, consists of a student’s name, and returns the string consisting of the altered name. Use the...
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"; }
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...
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
#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...
Using C++ Design a class named PersonData with the following member variables: lastName firstName address city...
Using C++ Design a class named PersonData with the following member variables: lastName firstName address city state zip phone and at a minimum the following member functions: A class constructor - which initializes all the member variables above a display() function - which ONLY displays all the person's data to the console Also write the appropriate accessor/mutator functions for the member variables Write a NewPersonData function. This is a stand-alone function that you can use in your main to generates...
#include <stdio.h> #include <string.h> #include<stdlib.h> #include<conio.h> struct Bank_Account_Holder { int account_no; char name[80]; int balance; };...
#include <stdio.h> #include <string.h> #include<stdlib.h> #include<conio.h> struct Bank_Account_Holder { int account_no; char name[80]; int balance; }; int n; void accept(struct Bank_Account_Holder[], int); void display(struct Bank_Account_Holder[], int); void save(struct Bank_Account_Holder[], int); void load(struct Bank_Account_Holder[], int); int search(struct Bank_Account_Holder[], int, int); void deposit(struct Bank_Account_Holder[], int, int, int); void withdraw(struct Bank_Account_Holder[], int, int, int); int lowBalenquiry(int,int); void main(void) { clrscr(); struct Bank_Account_Holder data[20]; int choice, account_no, amount, index; printf("NHU Banking System\n\n"); printf("Enter the count of records: "); scanf("%d", &n); accept(data, n); do {...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT