Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
kjc015
CSCI206-S21-62
Commits
fd3f11dd
Commit
fd3f11dd
authored
Mar 30, 2021
by
kjc015
Browse files
Lab08, Exercise 4, complete
parent
e1862e8d
Changes
4
Hide whitespace changes
Inline
Side-by-side
Labs/Lab08/Makefile
View file @
fd3f11dd
CC
=
gcc
CFLAGS
=
-Wall
-Werror
all
:
strnode_test
all
:
strnode_test
node_test
strnode_test
:
strnode.o strnode_test.o strnode.h
clean
:
rm
-f
strnode_test
*
.o
node_test
:
node.o node_test.o node.h
clean
:
rm
-f
node_test
*
.o
Labs/Lab08/node.c
0 → 100644
View file @
fd3f11dd
#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
);
}
Labs/Lab08/node.h
0 → 100644
View file @
fd3f11dd
#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
Labs/Lab08/node_test.c
0 → 100644
View file @
fd3f11dd
#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
;
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment