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:...
Construct two small C programs, compile and set up two executable files for the two commands...
Construct two small C programs, compile and set up two executable files for the two commands world and mars. Both commands simply print out messages on the screen: world n - print the message hello world on the screen n times mars n - print the message HELLO MARS on the screen n times Thanks a lot for your help!!!
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...
Download the world.SQL and run it in MySQL. Now based on this database, write query that...
Download the world.SQL and run it in MySQL. Now based on this database, write query that shows a) the most populated city in each country. b) the second most populated city in each country. c) the most populated city in each continent. d) the most populated country in each continent. e) the most populated continent. f) the number of people speaking each language. g) the most spoken language in each continent. h) number of languages that they are official language...
You will be creating a JUnit Test Class for Gradebook.java, (listing 1.1) and you can download...
You will be creating a JUnit Test Class for Gradebook.java, (listing 1.1) and you can download it from blackboard. Gradebook has two attributes: an array of int called scores to hold scores and scoreSize that indicates how many scores are currently held in the array. This field is initially set to 0. Task #1: Add a getScoreSize() method to the Gradebook class which returns scoresSize; Add a toString() method to the Gradebook class that returns a string with each score...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT