Commit fd3f11dd authored by kjc015's avatar kjc015
Browse files

Lab08, Exercise 4, complete

parent e1862e8d
CC=gcc CC=gcc
CFLAGS=-Wall -Werror CFLAGS=-Wall -Werror
all: strnode_test all: strnode_test node_test
strnode_test: strnode.o strnode_test.o strnode.h strnode_test: strnode.o strnode_test.o strnode.h
clean: clean:
rm -f strnode_test *.o rm -f strnode_test *.o
node_test: node.o node_test.o node.h
clean:
rm -f node_test *.o
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "node.h"
struct node *node_create(void* data, int size){
struct node* newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = (char*)malloc(size);
strcpy(newNode->data, data);
newNode->next = NULL;
return newNode;
}
void node_destroy(struct node *n){
free(n->data);
free(n);
}
#ifndef NODE_H_
#define NODE_H_
struct node{
void* data;
struct node* next;
};
struct node *node_create(void* data, int size);
void node_destroy(struct node *n);
#endif
#include <stdio.h>
#include <string.h>
#include "node.h"
// global variables (data segment)
struct node *n1, *n2, *n3, *p;
//------------ MY MAIN FUNCTION --------------------
int main(int argc, char *argv[]) {
// create strnodes
n1 = node_create("hello", 6);
n2 = node_create("there", 6);
n3 = node_create("prof", 5);
printf("node_test running...\n");
// manually "link" the nodes together.
n1->next = n2;
n2->next = n3;
n3->next = NULL;
// p points to node n1 initially
p = n1;
while (p != NULL) {
// Complete this line to print the current node's string and
// the stored length (do not use strlen!)
printf("str: %s - length: %zu\n", (char*)p->data, strlen(p->data));
// TODO: add code to move p to point to next node
// until you add this line, this program will have an infinite loop.
node_destroy(p);
p = (*p).next;
}
p = NULL;
return 0;
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment