-
Notifications
You must be signed in to change notification settings - Fork 0
/
stocks.c
100 lines (93 loc) · 2.04 KB
/
stocks.c
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include "stocks.h"
/*Add to stock*/
void addStockA(char * command, char ** stockA, List * stockB, int * index)
/*This function add the cmd to stockA, if stockA is full we push cmd to stockB*/
{
int i = 0;
if (stockA[N - 1] == NULL)
//stock A not full
{
while (stockA[i] != NULL)
i++;
stockA[i] = command;
}
else
//Stock A full
{
addStockB(stockA[0], stockB, index);
pullStockA(stockA);
stockA[N - 1] = command;
}
(*index)++;
}
void addStockB(char* command, List * stockB, int * index)
/*This function add a command to END of stock B*/
{
stockB->TotalIndex++;
ListNode* lNode = (ListNode *)malloc(sizeof(ListNode));
lNode->commands = command;
lNode->index = *index - N;
lNode->next = NULL;
if (stockB->head == NULL)
stockB->head = stockB->tail = lNode;
else
{
stockB->tail->next = lNode;
stockB->tail = lNode;
}
}
/*Print stocks*/
void printStockA(char ** short_term_history, int * index)
/*This function print stockA*/
{
int i;
//what index we want to print
int num =*index;
if (num < N)
num = 0;
else
num = *index - 7;
for (i = 0; i < N; i++)
printf("%d: %s\n", num+i,short_term_history[i]);
}
void printstockB(List * stockB)
/*This function print stockB*/
{
ListNode* lnode = stockB->head;
while (lnode)
{
printf("%d: %s\n", lnode->index, lnode->commands);
lnode = lnode->next;
}
}
/*List functions*/
void createNode(char * command, int index, List * lst)
/*This function create list node from stock b*/
{
ListNode * temp = (ListNode *)malloc(sizeof(ListNode));
temp->commands = command;
temp->next = NULL;
if (lst->head == NULL)
{
lst->head = lst->tail = temp;
}
else
{
lst->tail->next = temp;
lst->tail = temp;
}
}
void emptyList(List * stockB)
/*This function empty a list*/
{
stockB->head = stockB->tail = NULL;
stockB->TotalIndex = 0;
}
void pullStockA(char ** stock)
/*This function pull stockA one place*/
{
int i;
for (i = 0; i < N - 1; i++)
stock[i] = stock[i + 1];
return;
}