Question

In: Computer Science

[50%] Code Snippet Given snippet code below that you are required to complete. You are not...

  1. [50%] Code Snippet

Given snippet code below that you are required to complete. You are not allowed to make a new function or change any given code. Please complete each section that are marked with the notation “INSERT YOUR CODE HERE”.

Once you complete the snippet below, your output should have the same result with the given output below.

Descriptions:

  1. [15%] isValid()

This function is for checking that there is no duplicated employee data in the linked list. This function will check the name, jobPos, grade and age. It will return to 1 if no duplicated data found at those properties and return to 0 vice versa.

  1. [10%] push()

This function is built for inserting a new data in the linked list. You are required to check whether the inputted is novel data. If the inputted data is not valid, the program will print “ The inputted data is duplicated!”.

Note : please analyze how to push a data by given sequence of inserting in the main function and please compare it to the given output!

  1. [15%] pop()

this function is built for deleting a data. If you are trying to delete a data when the list is empty, then the program will prompt you “the list is empty!”.

Note : please analyze how to pop a data by given sequence of deleting in the main function and please compare it to the given output!

  1. [10%] printAll()

this function is built for printing all the data in the list along with the total employee number. If you are trying to delete a data when the list is empty, then the program will prompt you “the list is empty!”.

//

// Created by Hanry Ham on 2020-05-24.

//

  

  #include<stdio.h>

  #include<string.h>

  #include<stdlib.h>

  

  struct Employee{

    char name[20];

    char jobPos[15];

    int grade;

    int age;

    Employee *next;

}*head = NULL, *tail = NULL;

  

  bool isValid(char *name, char *jobPos, int grade, int age){

    // [15%] (1) INSERT YOUR CODE HERE

    

}

  

  void push(char *name, char *jobPos, int grade, int age){

    struct Employee *curr = (struct Employee *) malloc(sizeof(Employee));
    // [10%] (2) INSERT YOUR CODE HERE

    if( ){

        

        

    }else{

        
    }

}

  

  void pop(){

    struct Employee * curr = head;

    // [15%] (3) INSERT YOUR CODE HERE

      

}

  

  void printAll(){

    printf("\n\n");

    struct Employee * curr = head;

    int empCtr = 0;

    if(!curr){

        printf("the list is empty!");

    }else{

        // [10%] (4) INSERT YOUR CODE HERE

        

}

  

  int main(){
    pop();

      printAll();

    push("Hanry", "Supervisor", 12, 27);

    push("Yen", "Manager", 13, 40);

    pop();

    push("Derwin", "Manager", 15, 31);

    push("Andry", "Manager", 15, 30);

    pop();

    push("Saka", "Manager", 15, 32);

    pop();

    push("Afan", "Manager", 16, 35);

    push("Fredy", "Senior Manager", 18, 45);

    pop();

    printAll();

    return 0;

}

Output:

the list is empty

the list is empty!

=======================================================

|               Name|       Job Position| Grade|   Age|

=======================================================

|              Fredy|     Senior Manager|    18|    45|

|               Afan|            Manager|    16|    35|

|               Saka|            Manager|    15|    32|

=======================================================

Total Employee : 3

=======================================================

Solutions

Expert Solution

NOTE: After reading the given code, I assumed that insertion occurs at the head and deletion occurs at the tail.

1. The required source code is given below:-

//-------------------------------------------------------------------------------------------

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

struct Employee{

char name[20];

char jobPos[15];

int grade;

int age;

Employee *next;

}*head = NULL, *tail = NULL;

  

bool isValid(char *name, char *jobPos, int grade, int age)
{
// [15%] (1) INSERT YOUR CODE HERE
   struct Employee *emp=head;
while(emp!=NULL)
   {
       if((strcmp(name,emp->name)==0)&&(strcmp(jobPos,emp->jobPos)==0)&&(grade==emp->grade)&&(age==emp->age))
       {
           return 0;
       }
       emp = emp->next;
   }
   return 1;
}

  

void push(char *name, char *jobPos, int grade, int age)
{
struct Employee *curr = (struct Employee *) malloc(sizeof(Employee));
  
   // [10%] (2) INSERT YOUR CODE HERE
if(isValid(name,jobPos,grade,age))
   {  
       strcpy(curr->name,name);
       strcpy(curr->jobPos,jobPos);
       curr->grade = grade;
       curr->age = age;
      
       if(head==NULL || tail==NULL)
       {  
           curr->next = NULL;
           head=tail=curr;
       }
       else
       {
           curr->next = head;
           head = curr;
       }
}
   else
   {
       free(curr);
printf("The inputted data is duplicated!\n");
}
}

  

void pop()
{
struct Employee * curr = tail;

// [15%] (3) INSERT YOUR CODE HERE
   if(head==NULL || tail==NULL)
   {  
       printf("the list is empty!\n");
   }
   else
   {
       if(head==tail)
       {
           head=tail=NULL;
       }
       else
       {
           struct Employee * tmp = head;
           while(tmp->next!=tail)
           {
               tmp=tmp->next;
           }
           tail = tmp;
           tail->next=NULL;
       }
      
       free(curr);
   }
}

  

void printAll()
{

printf("\n\n");
  
struct Employee * curr = head;
  
int empCtr = 0;

if(!curr)
   {
       printf("the list is empty!");
}
   else
   {
// [10%] (4) INSERT YOUR CODE HERE
       printf("=======================================================\n");
       printf("| Name| Job Position| Grade| Age|\n");
       printf("=======================================================\n");
       while(curr!=NULL)
       {
           printf("| %s| %s| %d| %d|\n",curr->name,curr->jobPos,curr->grade,curr->age);
           empCtr++;
           curr = curr->next;
       }
       printf("=======================================================\n");
       printf("Total Employee : %d\n",empCtr);
       printf("=======================================================\n");
   }
}
  

int main(){
pop();

printAll();

push("Hanry", "Supervisor", 12, 27);

push("Yen", "Manager", 13, 40);

pop();

push("Derwin", "Manager", 15, 31);

push("Andry", "Manager", 15, 30);

pop();

push("Saka", "Manager", 15, 32);

pop();

push("Afan", "Manager", 16, 35);

push("Fredy", "Senior Manager", 18, 45);

pop();

printAll();

return 0;

}

//-------------------------------------------------------------------------------------------

2. This is a screenshot of the output:-


Related Solutions

Given the code snippet below, complete this program by: Displaying the data (temperatures) Displaying the minimum...
Given the code snippet below, complete this program by: Displaying the data (temperatures) Displaying the minimum temperature value Displaying the maximum temperature value Displaying the mean temperature SUBMIT THE PYTHON FILE (.PY) NOT SCREENSHOT, WORD DOCUMENT ETC. IF YOUR SUBMISSION IS NOT A .PY FILE, AUTOMATIC ZERO. #Code snippet below. Copy this into your favorite IDE and complete the tasks import numpy as np import pandas as pd temps = np.random.randint(60, 101, 6) temperatures = pd.Series(temps) 2. Write a python...
Given the below Java code snippet, assuming the code is correct, and the names are meaningful,...
Given the below Java code snippet, assuming the code is correct, and the names are meaningful, select the best answer for the below questions: public static void main(String[] args) { int numStations = 10; int enter = 0; int exit = 0; Train train = new Train(numStations); for (int station = 0; station < numStations; station++) {      enter = enter + train.enterPassengers(station);      exit = exit + train.exitPassengers(station); } System.out.println("Number of passengers entered the train: " + enter); System.out.println("Number...
Question 31 Given the code snippet below, what prints? void fun(int *p) { int q =...
Question 31 Given the code snippet below, what prints? void fun(int *p) { int q = 10; p = &q; } int main() { int r = 20; int *p = &r; fun(p); cout << *p; return 0; } Question 31 options: 10 20 compiler error Runtime error Question 32 A union’s members are exactly like the members of a Structure. Question 32 options: True False Question 33 Given the code below, what are the errors. #include <iostream> using namespace...
a) Find a bug in the code snippet below. Assume that a, b, c and d...
a) Find a bug in the code snippet below. Assume that a, b, c and d are correctly declared and initialized integer variables. a = b+c if (a=d) then print *,”A equals to D” else print *,”A does not equal D” end if b) Find a bug in the code snippet below. Assume that a,b,and c are correctly declared and initialized integer variables. if (a+b) > c) then print *,”Sum of A+B is greater than C” end if
1. Give the O-runtime depending on n for the code snippet below (n > 0). for(...
1. Give the O-runtime depending on n for the code snippet below (n > 0). for( j = n; j <= 0; j = j - 1)                                     for( k = 1; k <= n; k = k + 1)                                                 print(" "); 2. Give the O-runtime depending on n for the code snippet below (n > 0).            for( j = 1; j <= n; j = j + 1)                                     for( k = 1; k <= n; k =...
Complete the below code so that your program generates a random walk on the given graph....
Complete the below code so that your program generates a random walk on the given graph. The length of your walk should also be random. /******************************************************************/ #include <stdio.h> #include <stdlib.h> #include <time.h> typedef struct NODE_s *NODE; typedef struct NODE_s{ char name; NODE link[10]; }NODE_t[1]; #define nodeA 0 #define nodeB 1 #define nodeC 2 #define nodeD 3 #define nodeE 4 #define nodeF 5 int main() { srandom(time(NULL)); //printf("%d\n", random()); NODE_t node[6]; node[nodeA]->name = 'A'; node[nodeB]->name = 'B'; node[nodeC]->name = 'C'; node[nodeD]->name...
Write a code snippet for the following:   You need to control the maximum number of people...
Write a code snippet for the following:   You need to control the maximum number of people who can be in a   restaurant at any given time. A group cannot enter the restaurant if they   would make the number of people exceed 100 occupants. Use random numbers   between 1 and 20 to simulate groups arriving to the restaurant. After each   random number, display the size of the group trying to enter and the number   of current occupants. As soon as the...
Viewing the code snippet below answer the following questions: int num=10; do {      cout << “Hello”...
Viewing the code snippet below answer the following questions: int num=10; do {      cout << “Hello” << endl;      num--; } while(num > 1); After the above code is executed: “Hello” printed how many times: ___________ What is value ofnum:  ____________ language is c++
The following code snippet is free of errors (issues that will prevent the code from compiling)...
The following code snippet is free of errors (issues that will prevent the code from compiling) but it does contain a major bug that will cause the program to hang (not finish running). Identify the problem and create a line of code to fix it. #include <stdio.h> char get_input(); int main() { printf("The user's inputted letter is %c\n", get_input()); return 0; } char get_input() { printf("Please enter a letter: \n"); char input = getchar(); while (!(input >= 'a' && input...
Complete this question by entering your answers in the tabs below. Required 1 Required 2 Complete...
Complete this question by entering your answers in the tabs below. Required 1 Required 2 Complete the above schedule of the company’s total costs and costs per unit. (Round the per unit variable cost and fixed cost to 2 decimal places.) Units Produced and Sold 69,000 89,000 109,000 Total costs: Variable costs $213,900 Fixed costs 390,000 Total costs $603,900 $0 $0 Cost per unit: Variable cost Fixed cost Total cost per unit $0.00 $0.00 $0.00
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT