11.07.2014 Views

Flute acoustics: measurement, modelling and design - School of ...

Flute acoustics: measurement, modelling and design - School of ...

Flute acoustics: measurement, modelling and design - School of ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

APPENDIX B. PROGRAM LISTINGS 305<br />

/*<br />

Vector.c<br />

By Andrew Botros, 2001-2003<br />

Listing B.28: Vector.c<br />

Vector.c is a Vector ADT.<br />

Refer to Vector.h for interface details.<br />

*/<br />

#include <br />

#include <br />

#include "Vector.h"<br />

Vector createVector(void) {<br />

/* allocate memory for *Vector */<br />

Vector v = (Vector)malloc(size<strong>of</strong>(*v));<br />

/* initialise num to 0, pointers to NULL */<br />

v->num = 0;<br />

v->head = NULL;<br />

v->tail = NULL;<br />

return v;<br />

}<br />

void addElement(Vector v, void* object) {<br />

/* if the Vector is empty, head <strong>and</strong> tail point to new object */<br />

if(v->tail == NULL) {<br />

v->tail = (Node)malloc(size<strong>of</strong>(*(v->tail)));<br />

v->tail->object = object;<br />

v->tail->next = NULL;<br />

v->head = v->tail;<br />

}<br />

/* else, add to end <strong>of</strong> Vector <strong>and</strong> update tail */<br />

else {<br />

v->tail->next = (Node)malloc(size<strong>of</strong>(*(v->tail->next)));<br />

v->tail->next->object = object;<br />

v->tail->next->next = NULL;<br />

v->tail = v->tail->next;<br />

}<br />

/* increment size */<br />

v->num++;<br />

return;<br />

}<br />

void insertAt(Vector v, void* object, int index) {<br />

int i;<br />

Node cur;<br />

Node prev;<br />

/* if Vector is empty <strong>and</strong> insert at 0, head <strong>and</strong> tail point to new<br />

object */<br />

if((v->num == 0) && (index == 0)) {<br />

v->tail = (Node)malloc(size<strong>of</strong>(*(v->tail)));<br />

v->tail->object = object;<br />

v->tail->next = NULL;<br />

v->head = v->tail;<br />

}<br />

else {

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!