Académique Documents
Professionnel Documents
Culture Documents
A
all to pop would return the top-most value on the sta
k, whi
h is 6, and the sta
k would
now look like
2
5
Assignment 3 27/03/03
employ a linked-list. A linked-list is somewhat of a dynami
array that grows and shrinks
as values are added to it. Rather than being stored in a
ontinuous blo
k of memory, the
values in the dynami
array are linked together with pointers. Ea
h element of a linked list
is a stru
ture that
ontains a value and a link to its neighbor. The link is basi
ally a pointer
to another stru
ture that
ontains a value and another pointer to another stru
ture, and so
on. As an exmple,
onsider a linked list representation of the three values on the sta
k in
the previous example. In a linked-list, this would look like
Stack
myStack
Heap
head
value
value
value
next
next
next
The rst box on the left is a pointer of type sta
kT * that is lo
ated in stati
memory whi
h
points to the sta
kT stru
ture in dynami
memory. This stru
ture
ontains a pointer to the
rst element of the sta
k, or the head. The type for this stru
ture is given by the sta
kT
type,
typedef stru
t {
nodeT *head;
} sta
kT;
this
ontains a pointer
alled head to a stru
ture of type nodeT, whi
h is just a node in the
linked list, and this type is given by
typedef stru
t _nodeT {
valueT value;
stru
t _nodeT *next;
} nodeT;
where the valueT is just a type of your
hoosing. In this assignment we will have employ
linked lists of
hara
ter values, so that the type denition for valueT is given by
typedef
har valueT;
The box in the last node with the diagonal line indi
ates that this pointer is the NULL pointer,
in that it does not point to anything.
Assignment 3 27/03/03
Assignment
Your job for this assignment is to use the above data stru
tures and
reate the following
publi
fun
tions to manipulate the sta
k (whi
h are de
lared in sta
k.h):
sta
kT *NewSta
k(void);
void Push(sta
kT *sta
k, valueT value);
valueT Pop(sta
kT *sta
k);
void EmptySta
k(sta
kT *sta
k);
void FreeSta
k(sta
kT *sta
k);
bool IsEmpty(sta
kT *sta
k);
if there is no head).
Set the head eld of the sta
k to point to the new node.
3. Pop This fun
tion should take as its argument a pointer to the sta
k and should return
the value from the top of the sta
k. It should then:
Return 0 and print out an error if the sta
k is empty.
Set the head eld to point to the next node.
Free the spa
e asso
iated with the old node.
4. EmptySta
k This fun
tion should take as its argument a pointer to a sta
kT and it
should free up the spa
e asso
iated with every node in the linked list in the sta
k, but
it should not free up the spa
e asso
iated with the sta
kT stru
ture in memory, but
should set the head pointer to NULL.
5. FreeSta
k This fun
tion should take as its argument a pointer to a sta
kT and should
free up the spa
e asso
iated with the sta
kT stru
t allo
ated in dynami
memory. This
fun
tion should only free up memory asso
iated with an empty sta
k, and should print
an error to the s
reen when the user attempts to free a non-empty sta
k.
Assignment 3 27/03/03
6. IsEmpty This fun
tion takes as its argument a pointer to a sta
kT in memory and
returns true if it is empty, and false otherwise. Your sta
k is dened as empty when
its head pointer is NULL. The fun
tion returns a boolean enumerated type, whi
h is
dened as
typedef enum {
false, true
} bool;
7. NewNode This fun
tion should return a pointer to a nodeT type in dynami
memory
and return NULL and print out an error if there is not enough memory to do so. Sin
e
it is a private fun
tion and it is only needed by the Push fun
tion, then it should be
de
lared as a stati
fun
tion so that it is private to sta
k.
.
Getting started
You should
opy the following les to your dire
tory:
/home/
os315/assignments/assignment3/main.
/home/
os315/assignments/assignment3/sta
k.h
/home/
os315/assignments/assignment3/Makefile
Your job is to
reate the le sta
k.
whi
h
ontains denitions of the fun
tions dened in
sta
k.h as well as the stati
ally dened fun
tion NewNode. You should in
lude the sta
k.h
header le as well as stdio.h in your version of sta
k.
. The fun
tions you
reate will be
used in the main.
program whi
h performs a few tests with your sta
k fun
tions. You should
not alter the main.
le, sin
e this is the le that will be used to grade your assignment.
When the make program starts, it looks in the Makefile for the target you request. Sin
e
you request test, the make program looks in the Makefile where it says test:, and the
lines are given by
test:
main.o
$(CC) -o test main.o $(SOLUTION)
Assignment 3 27/03/03
where the tabs (not spa
es) before dependen
ies and
ommand are very important! Sin
e
you requested the test target, the make program looks at this line, and sees that the le
that is required to
reate it is main.o. The make program then looks to see if main.o is up
to date by
he
king the entry for it in the Makefile, whi
h is given by
main.o: main.
sta
k.h
$(CC) -
main.
This line says that the main.o obje
t le depends on its sour
e main.
and the header le
sta
k.h. If main.
and sta
k.h have been
hanged sin
e the last
ompile, then main.o is
reated with the
ommand listed, whi
h is given by
g
-
main.
where the $(CC)$ variable has been lled in with g
, and we see that this line simply
reates the obje
t le main.o. On
e main.o is
reated (and hen
e up to date), the
ommand
beneath the test statement is exe
uted. In this
ase, the
ommand is
$(CC) -o test main.o $(SOLUTION)
So when you type make test, the program test is
reated by linking the obje
t les main.o
and /home/
os315/assignments/assignment3/sta
k.o. The sta
k.o le is a pre
ompiled
header le whi
h
ontains the solution. Your job is to write your own sta
k.
fun
tion that
ompiles to
reate your own sta
k.o. To see how the solution is, run the test program
after you
ompile it with
$ ./test
A series of tests will run and the s
ore you re
ieve for your run is also displayed along with
the test. When you would like to test your own version of sta
k.
, you
an
ompile it and
link it with the
ommand make without any arguments. Without any arguments, make looks
at the line that says all. This target requires the main exe
utable to be up to date (main
is what the $(EXEC) variable is). This exe
utable is
reated by
ompiling and linking your
version of sta
k.
with main.
. You
an then test your own implementation by running
the
ommand
$ ./main
You
an also
lean up your dire
tory by typing make
lean, and this will exe
ute the
ommand
rm -f *.o *~ \#*# a.out test $(EXEC)
whi h will remove exe utables, obje t les, and ema s ba kup and autosave les.
Assignment 3 27/03/03