4
4
5
5
#ifndef EXPRESSION_EVAL_STACK_H
6
6
#define EXPRESSION_EVAL_STACK_H
7
- #include <stdlib.h>
7
+ #include <ctype.h>
8
+ #include <memory.h>
9
+
8
10
/*
9
11
10
12
DS : Stack
@@ -14,7 +16,8 @@ operations : push , pop , search , size
14
16
15
17
typedef struct LinkedListInternal
16
18
{
17
- char value ;
19
+ void * value ;
20
+ char dataType ;
18
21
struct LinkedListInternal * next ;
19
22
20
23
}LinkedList ;
@@ -42,13 +45,13 @@ typedef struct stackInternal
42
45
43
46
//stackoperations : push , pop
44
47
typedef void (* StackOperation_1_wptr )(Stack );
45
- typedef void (* StackOperation_1 )(Stack * );
46
- typedef void (* StackOperation_2 )(Stack * ,char );
48
+ typedef LinkedList * (* StackOperation_1 )(Stack * );
49
+ typedef void (* StackOperation_3 )(Stack * , void * ,char );
47
50
48
51
49
52
typedef struct StackOperationsInternal
50
53
{
51
- StackOperation_2 push ;
54
+ StackOperation_3 push ;
52
55
StackOperation_1 pop ;
53
56
StackOperation_1_wptr traverse ;
54
57
@@ -59,37 +62,44 @@ LinkedList operations
59
62
starts:
60
63
*/
61
64
62
- void LinkedListOperation_2_add (LinkedList * * root ,char value )
65
+ void LinkedListOperation_3_add (LinkedList * * root ,void * value , char dataType )
63
66
{
64
- if (* root == NULL )
65
- {
66
- * root = (LinkedList * )malloc (sizeof (LinkedList ));
67
- (* root )-> value = value ;
68
- (* root )-> next = NULL ;
69
- }
70
- else
71
- {
72
- LinkedList * node ;
67
+ LinkedList * node ;
73
68
node = (LinkedList * )malloc (sizeof (LinkedList ));
74
- node -> value = value ;
69
+ if (dataType == 'c' )
70
+ {
71
+ node -> value = (char * ) malloc (sizeof (char ));
72
+ node -> dataType = 'c' ;
73
+ /* using memcpy to copy string: */
74
+ memcpy (node -> value , value ,1 );
75
+ }
76
+ else if (dataType == 'd' )
77
+ {
78
+ node -> value = (int * )malloc (sizeof (int ));
79
+ node -> dataType = 'd' ;
80
+ /* using memcpy to copy string: */
81
+ memcpy (node -> value , value ,1 );
82
+ }
75
83
node -> next = * root ;
76
84
* root = node ;
77
- }
78
85
79
86
}
80
87
81
- void LinkedListOperation_1_delete_last_added (LinkedList * * root )
88
+ LinkedList * LinkedListOperation_1_delete_last_added (LinkedList * * root )
82
89
{
90
+ void * ptr ;
83
91
if (* root == NULL )
84
92
{
85
- free (* root );
93
+ //free(*root);
94
+ return * root ;
86
95
}
87
96
else
88
97
{
89
98
LinkedList * tmp ;
90
99
tmp = * root ;
91
100
* root = (* root )-> next ;
92
- free (tmp );
101
+ //free(tmp);
102
+ return tmp ;
93
103
}
94
104
95
105
}
@@ -103,27 +113,37 @@ LinkedList operations
103
113
* Stack operations
104
114
*
105
115
*/
106
- void StackOperation_2_push (Stack * S ,char value )
116
+
117
+ void StackOperation_3_push (Stack * S ,void * value , char dataType )
107
118
{
108
- LinkedListOperation_2_add (& (S -> root ),value );
119
+ LinkedListOperation_3_add (& (S -> root ),value , dataType );
109
120
(S -> size )++ ;
110
121
}
111
122
112
- void StackOperation_1_pop (Stack * S )
123
+ LinkedList * StackOperation_1_pop (Stack * S )
113
124
{
114
- LinkedListOperation_1_delete_last_added (& (S -> root ));
115
125
(S -> size )-- ;
126
+ return LinkedListOperation_1_delete_last_added (& (S -> root ));
116
127
117
128
}
118
129
119
130
void StackOperation_1_traverse (Stack S )
120
131
{
121
132
LinkedList * current ;
133
+ char * val ;
122
134
int count = S .size ;
123
135
current = S .root ;
136
+
124
137
while (count )
125
138
{
126
- printf ("value=%c\n" ,current -> value );
139
+ val = current -> value ;
140
+ if (current -> dataType == 'd' ) {
141
+ printf ("value=%d\n" ,* val );
142
+ }
143
+ else if (current -> dataType == 'c' ) {
144
+ printf ("value=%c\n" ,* val );
145
+ }
146
+
127
147
current = current -> next ;
128
148
count -- ;
129
149
}
0 commit comments