-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patha14f2.c
More file actions
93 lines (76 loc) · 2.16 KB
/
Copy patha14f2.c
File metadata and controls
93 lines (76 loc) · 2.16 KB
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
#include <stdio.h>
#include <stdlib.h>
#define StackLimit 50
typedef int StackElementType;
typedef struct {
int Top;
StackElementType Element[StackLimit];
} StackType;
typedef enum {
FALSE, TRUE
} boolean;
void CreateStack(StackType *Stack);
boolean EmptyStack(StackType Stack);
boolean FullStack(StackType Stack);
void Push(StackType *Stack, StackElementType Item);
void Pop(StackType *Stack, StackElementType *Item);
int main() {
int maxMemory, currentMemory;
StackType aStack;
StackElementType a;
printf("Please enter maximum memory address: ");
scanf("%d", &maxMemory);
CreateStack(&aStack);
while(a!= 0) {
printf("Please enter the next relative memory address: ");
scanf("%d", &a);
if(a!= 0) {
Push(&aStack, a);
}
}
printf("Please enter the current memory address: ");
scanf("%d", ¤tMemory);
while(!EmptyStack(aStack) || currentMemory < 0 || currentMemory > maxMemory) {
a = aStack.Element[aStack.Top];
currentMemory = currentMemory + a;
Pop(&aStack, &a);
if(currentMemory < 0 || currentMemory > maxMemory) {
printf("Access Violation Exception at address %d" ,currentMemory);
break;
}
printf("Executing instruction: %d\n", currentMemory);
}
}
void CreateStack(StackType *Stack)
{
Stack -> Top = -1;
}
boolean EmptyStack(StackType Stack)
{
return (Stack.Top == -1);
}
boolean FullStack(StackType Stack)
{
return (Stack.Top == (StackLimit - 1));
}
void Push(StackType *Stack, StackElementType Item)
{
if (!FullStack(*Stack)) {
Stack -> Top++;
Stack -> Element[Stack -> Top] = Item;
} else
printf("Full Stack...");
}
void Pop(StackType *Stack, StackElementType *Item)
/* ÄÝ÷åôáé: Ìéá óôïßâá Stack.
Ëåéôïõñãßá: ÄéáãñÜöåé ôï óôïé÷åßï Item áðü ôçí êïñõöÞ ôçò Óôïßâáò áí ç Óôïßâá äåí åßíáé êåíÞ.
ÅðéóôñÝöåé: Ôï óôïé÷åßï Item êáé ôçí ôñïðïðïéçìÝíç Stack.
¸îïäïò: ÌÞíõìá êåíÞò óôïßâáò áí ç Stack åßíáé êåíÞ
*/
{
if (!EmptyStack(*Stack)) {
*Item = Stack -> Element[Stack -> Top];
Stack -> Top--;
} else
printf("Empty Stack...");
}