In: Computer Science
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:
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));
}
}
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;
}