-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlists.c
173 lines (117 loc) · 3.61 KB
/
lists.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include <stdio.h>
#define MaxStack 50 /* MaxStack stands for the size of all stack arrays */
typedef struct {
int topPosition;
char * stackArray [MaxStack];
} stringStack; /* type for a stack of strings */
int full (stringStack stack);
int push (stringStack *stack, char * item);
void initializeStack (stringStack * stack);
int empty (stringStack stack);
char * pop (stringStack *stack);
char * top (stringStack stack);
int size (stringStack stack);
char * get_nth (stringStack stack, int nth);
void print (stringStack stack);
/*I am adding these comments to see if this is working... awesome! P.S. Hi from Nedi*/
int main (void)
{
stringStack bills, magazines, notes;
initializeStack (&bills);
initializeStack (&magazines);
initializeStack (¬es);
push (&bills, "Puppies");
push (&bills, "Rainbows");
push (&bills, "Cotton Candy");
printf ("Bills : %d", empty (bills));
printf ("\nMagazines : %d\n", empty (magazines));
push (&magazines, "Heartwarming Stories - March 2009");
push (&magazines, "Cooking with Blueberries - Spring 2009");
printf ("Bills:\n");
print (bills);
printf ("Magazines:\n");
print (magazines);
printf ("Notes:\n");
print (notes);
printf ("\n%s\n", pop (&bills));
printf ("%s", pop (&bills));
printf ("\n\n%s\n", pop (&magazines));
printf ("%s", pop (&magazines));
printf ("\n\nBills : %d", empty (bills));
printf ("\nMagazines : %d", empty (magazines));
push (&bills, "Puppies");
push (&bills, "Cotton Candy);
printf ("\n\nBills : %d", size (bills));
printf ("\n\nBills : %s", get_nth (bills, 1));
printf ("\n\nBills : %s", get_nth (bills, 2));
return 0;
}
int full (stringStack stack)
{
/* determine if there are more positions in a stackArray */
return (stack.topPosition == (MaxStack-1));
}
int push (stringStack *stack, char * item)
{
/* return -1 if stack full */
if (full (*stack)) {
printf ("attempt to push item onto an already full stack\n");
return -1;
}
/* add item to stack */
(stack->topPosition) ++;
stack->stackArray[stack->topPosition] = item;
}
void initializeStack (stringStack * stack) /* (sets topPosition of stack to -1) */
{
stack->topPosition = -1;
}
int empty (stringStack stack)
{
return (stack.topPosition < 0);
}
char * pop (stringStack *stack)
/* (returns 0 if stack is empty, otherwise removes the top string from the stack and returns it) */
{
char * top;
if (empty (* stack))
{
return 0;
}
else
top = stack->stackArray[stack->topPosition];
(stack->topPosition) --;
return top;
}
char * top (stringStack stack)
/* (returns 0 if stack is empty, otherwise returns the top string on the stack) */
{
if (empty (stack))
{
return 0;
}
return stack.stackArray[stack.topPosition];
}
int size (stringStack stack)
/* returns the number of items currently on the stack */
{
return (stack.topPosition + 1);
}
void print (stringStack stack)
/* prints all of the current elements on the stack */
{
while ((top (stack)) != NULL)
{
printf ("\t%s\n", pop (&stack));
}
}
char * get_nth (stringStack stack, int nth)
/* returns the string at the nth position from the top of the stack */
{
int max;
max = stack.topPosition;
if (nth < MaxStack && nth >= 0)
{
return stack.stackArray[max - nth];
}
}