-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackImplementation.c
More file actions
52 lines (42 loc) · 885 Bytes
/
StackImplementation.c
File metadata and controls
52 lines (42 loc) · 885 Bytes
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
/* This is the file StackImplementation.c */
#include <stdio.h>
#include <stdlib.h>
#include "StackInterface.h"
void InitializeStack(Stack *S)
{
S->ItemList=NULL;
}
int Empty(Stack *S)
{
return (S->ItemList==NULL);
}
int Full(Stack *S)
{
return 0;
}
/* We assume an already constructed stack is not full since it can potentially */
/* grow as a linked structure */
void Push(ItemType X, Stack *S)
{
StackNode *Temp;
Temp=(StackNode *) malloc(sizeof(StackNode));
if (Temp==NULL){
printf("system storage is exhausted");
} else {
Temp->Link=S->ItemList;
Temp->Item=X;
S->ItemList=Temp;
}
}
void Pop(Stack *S, ItemType *X)
{
StackNode *Temp;
if (S->ItemList==NULL){
printf("attempt to pop the empty stack");
} else {
Temp=S->ItemList;
*X=Temp->Item;
S->ItemList=Temp->Link;
free(Temp);
}
}