Question

In: Computer Science

Download everything.c. You can compile and run it with the usual commands. You know that putting...

Download everything.c. You can compile and run it with the usual commands.

You know that putting all your code into a single file is bad form. Therefore, your goal is to refactor everything.c so that the code is in multiple source (.c and .h) files, as well as write a Makefile to compile and run the program. Here are your constraints:

  • There should be no code duplication
  • Each .c and .h file must only #include header files that it actually needs to
  • Stack and Queue need to be in their own source file
  • The main function should be in its own source file
  • The Makefile must have at least the following targets (of course you need more)
    • runOn4: run the application on the command line argument 4
    • runOn20: run the application on the command line argument 20
    • clean: remove all generated files, leaving only the source files and the Makefile
    • symbols: print the symbols of all .o files and .exe file
    • assemblies: creates one .s file for each .c file

everything.c code:

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

#define SHOULD_PRINT 1

struct Node {
int data;
struct Node *next;
};

struct Stack {
struct Node *head;
};

struct Queue {
struct Node *head;
struct Node *tail;
};

struct Node* new_node(int d) {
struct Node *n = malloc(1 * sizeof(struct Node));
n->data = d;
n->next = NULL;
return n;
}

void push(struct Stack *s, int e) {
struct Node* newFirst = new_node(e);
newFirst->next = s->head;
s->head = newFirst;
}

int pop(struct Stack *s) {
int result = s->head->data;
s->head = s->head->next;
return result;
}

void enqueue(struct Queue *q, int e) {
struct Node* newFirst = new_node(e);
if (q->head == NULL) {
q->head = newFirst;
q->tail = newFirst;
} else {
q->tail->next = newFirst;
q->tail = newFirst;
}
}

int dequeue(struct Queue *q) {
int result = q->head->data;
q->head = q->head->next;
if (q->head == NULL) {
q->tail = NULL;
}
return result;
}
  
int main(int argc, char **argv) {
if (argc < 2) {
printf("requires one argument\n");
exit(1);
}
int n = atoi(argv[1]);
struct Stack *s = malloc(sizeof(struct Stack));
struct Queue *q = malloc(sizeof(struct Queue));
for (int i=0; i<n; i++) {
if (SHOULD_PRINT) printf("put %d on the stack\n", i);
push(s, i);
}
for (int i=0; i<n; i++) {
enqueue(q, pop(s));
}
for (int i=0; i<n; i++) {
if (SHOULD_PRINT) printf("get %d off of the queue\n", dequeue(q));
}
}

Solutions

Expert Solution

Here is the above program splitted in multiple files

Main.c

#include<stdio.h>
#include<stdlib.h>
#include"Node.c"

int main(int argc, char **argv) {
if (argc < 2) {
printf("requires one argument\n");
exit(1);
}
int n = atoi(argv[1]);
struct Stack *s = malloc(sizeof(struct Stack));
struct Queue *q = malloc(sizeof(struct Queue));
int i;
for (i=0; i<n; i++) {
if (SHOULD_PRINT) printf("put %d on the stack\n", i);
push(s, i);
}
for (i=0; i<n; i++) {
enqueue(q, pop(s));
}
for (i=0; i<n; i++) {
if (SHOULD_PRINT) printf("get %d off of the queue\n", dequeue(q));
}
}

Node.h

#include <stdio.h>
#include <stdlib.h>
#define SHOULD_PRINT 1

struct Node {
int data;
struct Node *next;
};

struct Stack {
struct Node *head;
};

struct Queue {
struct Node *head;
struct Node *tail;
};

Node.c

#include <stdio.h>
#include <stdlib.h>
#include "Node.h"
#define SHOULD_PRINT 1


struct Node* new_node(int d) {
struct Node *n = malloc(1 * sizeof(struct Node));
n->data = d;
n->next = NULL;
return n;
}

void push(struct Stack *s, int e) {
struct Node* newFirst = new_node(e);
newFirst->next = s->head;
s->head = newFirst;
}

int pop(struct Stack *s) {
int result = s->head->data;
s->head = s->head->next;
return result;
}

void enqueue(struct Queue *q, int e) {
struct Node* newFirst = new_node(e);
if (q->head == NULL) {
q->head = newFirst;
q->tail = newFirst;
} else {
q->tail->next = newFirst;
q->tail = newFirst;
}
}

int dequeue(struct Queue *q) {
int result = q->head->data;
q->head = q->head->next;
if (q->head == NULL) {
q->tail = NULL;
}
return result;
}
  


Related Solutions

Euler’s Method and Introduction to MATLAB • Start MATLAB • Inline Commands: You can type commands...
Euler’s Method and Introduction to MATLAB • Start MATLAB • Inline Commands: You can type commands directly into the command window: Type and expression and then hit enter to evaluate the expression. For example: >> 2+2 If you want to suppress the output of a command follow it with ‘;”.  For example >>2+2; Practice evaluating a few expressions in the command window. (In MATLAB multiplication is represented by * so 3*2=6). • Variables and Vectors: You can define variables in the...
Compile and run the following code then answer the following questions: a.) Run this command from...
Compile and run the following code then answer the following questions: a.) Run this command from the shell prompt: ./a.out ls -F Explain, in your own words, why you see the screen output you do and how that output relates to both the content of the program AND the nature of the shell command used to invoke it. Be sure to comment on the pointer arithmetic done inside the line: execvp(*(argv+1), argv+1); b.) Run this command from the shell prompt:...
Task #1 void Methods Copy the file geometry.cpp. This program will compile, but when you run...
Task #1 void Methods Copy the file geometry.cpp. This program will compile, but when you run it , it doesn’t appear to do anything except wait. That is because it is waiting for user input, but the user doesn’t have the menu to choose from yet. We will need to create this. Above the main, write the prototype for a function called PrintMenu that has no parameter list and does not return a value.    Below the main, write the function...
Use the TestCorrectness.java/TestCorrectness.cpp to compile, fix, and run your code. QueueUsingStack.java is also provided, but you...
Use the TestCorrectness.java/TestCorrectness.cpp to compile, fix, and run your code. QueueUsingStack.java is also provided, but you will need to complete some codes. public class TestCorrectness {    public static void main(String[] args) throws Exception {        int queueSize = 7;        QueueUsingStack qViaStack = new QueueUsingStack(queueSize);        Queue queue = new Queue(queueSize);        System.out.println("**** Enqueue Test ****");        System.out.println();        for (int i = 1; i <= 4; i++) {            int...
Part Two: Download VendingChange.java (above). Run it and become familiar with the output. Essentially, you need...
Part Two: Download VendingChange.java (above). Run it and become familiar with the output. Essentially, you need to create a user friendly GUI app using the code parameters of VendingChange. Turn VendingChange.java into a GUI app. 1) Your program must display the input dialog. 2) You must check that the data entered by the user follows the required input. If not, your program must display an error dialog and exit. 3) If the input is valid, then your program must display...
Lab 11 C++ Download the attached program and run it - as is. It will prompt...
Lab 11 C++ Download the attached program and run it - as is. It will prompt for a Fibonacci number then calculate the result using a recursive algorithm. Enter a number less then 40 unless you want to wait for a very long time. Find a number that takes between 10 and 20 seconds. (10,000 - 20,000 milliseconds). Modify the program to use the static array of values to "remember" previous results. Then when the function is called again with...
Create a project for this assignment. You can name it assignment02 if you wish. Download the...
Create a project for this assignment. You can name it assignment02 if you wish. Download the database file pizza-190807A.sqlite into the top level of the project. Create the pizza_services.py module first and put in the code given in the assignment. Using this code ensures that you can use the services in a similar way to the example. The assignment suggests adding a method customer to the class. This will return a list of rows from the customer table in the...
in C++ Compile and run the program for the following values: r = 2 Cm, h...
in C++ Compile and run the program for the following values: r = 2 Cm, h = 10 Cm. The answer should be: The cross section area of the cylinder is 3.8955634 c The side area of the cylinder is 19.474819 inch-sqr Did you get the same answer? Explain the reason for such an error and fix the problem. Modify the previous program to include a new function called total_area, that computes the total suface area of a cylinder. The...
6. Write a program in C programming (compile and run), a pseudocode, and draw a flowchart...
6. Write a program in C programming (compile and run), a pseudocode, and draw a flowchart for each of the following problems: a) Obtain three numbers from the keyboard, compute their product and display the result. b) Obtain two numbers from the keyboard, and determine and display which (if either) is the smaller of the two numbers. c) Obtain a series of positive numbers from the keyboard, and determine and display their average (with 4 decimal points). Assume that the...
Objectives To learn to code, compile, and run a program using file input and an output...
Objectives To learn to code, compile, and run a program using file input and an output file. Assignment Plan and code a program utilizing one file for input and one file for output to solve the following problem: Write a program to determine the highest number, the lowest number, their total, and the average of each line of numbers in a file. A file contains 7 numbers per line. How many lines a file contains is unknown. Note Label all...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT