-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedNoteList.cpp
78 lines (55 loc) · 1.88 KB
/
LinkedNoteList.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include "LinkedNoteList.h"
inline LinkedNoteList* NoteStack_findElem(LinkedNoteList *pStack, byte uNote)
{
LinkedNoteList* pElem = pStack;
while (pElem) {
if (pElem->uNote == uNote) break;
pElem = pElem->pNext;
}
return pElem;
} // NoteStack_findElem
void NoteStack_erase(LinkedNoteList **ppStack, byte uNote)
{
LinkedNoteList *pElem = NoteStack_findElem(*ppStack, uNote);
if (!pElem) return;
if (pElem->pPrev) pElem->pPrev->pNext = pElem->pNext;
if (pElem->pNext) pElem->pNext->pPrev = pElem->pPrev;
// Change root pointer if necessary
if (pElem == *ppStack) *ppStack = pElem->pNext;
free(pElem);
} // NoteStack_removeNote
inline LinkedNoteList* NoteStack_lastElem(LinkedNoteList *pStack)
{
if (!pStack) return NULL;
LinkedNoteList* pLast = pStack;
while (pLast->pNext) pLast = pLast->pNext;
return pLast;
} // NoteStack_lastElem
void NoteStack_push(LinkedNoteList **ppStack, byte uNote, byte uVelocity)
{
if (!uNote) return;
LinkedNoteList* pElemNew = (LinkedNoteList*) malloc(sizeof(LinkedNoteList));
pElemNew->uNote = uNote;
pElemNew->uVelocity = uVelocity;
pElemNew->pPrev = NoteStack_lastElem(*ppStack);
if (pElemNew->pPrev) pElemNew->pPrev->pNext = pElemNew;
pElemNew->pNext = NULL;
// If list was empty, let root pointer point to new element
if (!*ppStack) *ppStack = pElemNew;
} // NoteStack_addNote
byte NoteStack_last(LinkedNoteList *pStack)
{
return NoteStack_lastElem(pStack)->uNote;
} // NoteStack_last
void NoteStack_popLast(LinkedNoteList **ppStack, byte &uNote, byte &uVelocity)
{
uNote = uVelocity = 0;
if (!*ppStack) return;
LinkedNoteList* pLast = NoteStack_lastElem(*ppStack);
uNote = pLast->uNote;
uVelocity = pLast->uVelocity;
// Forget and delete element
if (pLast->pPrev) pLast->pPrev->pNext = NULL;
if (*ppStack == pLast) *ppStack = NULL;
free(pLast);
} // NoteStack_popLast