Skip to content

Commit a57f6f1

Browse files
committed
stack: adding generic stack support
1 parent 2815892 commit a57f6f1

File tree

1 file changed

+45
-25
lines changed

1 file changed

+45
-25
lines changed

expression_eval/stack.h

Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
#ifndef EXPRESSION_EVAL_STACK_H
66
#define EXPRESSION_EVAL_STACK_H
7-
#include <stdlib.h>
7+
#include <ctype.h>
8+
#include <memory.h>
9+
810
/*
911
1012
DS : Stack
@@ -14,7 +16,8 @@ operations : push , pop , search , size
1416

1517
typedef struct LinkedListInternal
1618
{
17-
char value;
19+
void *value;
20+
char dataType;
1821
struct LinkedListInternal *next;
1922

2023
}LinkedList;
@@ -42,13 +45,13 @@ typedef struct stackInternal
4245

4346
//stackoperations : push , pop
4447
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);
4750

4851

4952
typedef struct StackOperationsInternal
5053
{
51-
StackOperation_2 push;
54+
StackOperation_3 push;
5255
StackOperation_1 pop ;
5356
StackOperation_1_wptr traverse;
5457

@@ -59,37 +62,44 @@ LinkedList operations
5962
starts:
6063
*/
6164

62-
void LinkedListOperation_2_add(LinkedList **root,char value)
65+
void LinkedListOperation_3_add(LinkedList **root,void *value,char dataType)
6366
{
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;
7368
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+
}
7583
node->next = *root;
7684
*root = node;
77-
}
7885

7986
}
8087

81-
void LinkedListOperation_1_delete_last_added(LinkedList **root)
88+
LinkedList *LinkedListOperation_1_delete_last_added(LinkedList **root)
8289
{
90+
void *ptr;
8391
if (*root == NULL)
8492
{
85-
free(*root);
93+
//free(*root);
94+
return *root;
8695
}
8796
else
8897
{
8998
LinkedList *tmp;
9099
tmp = *root;
91100
*root = (*root)->next;
92-
free(tmp);
101+
//free(tmp);
102+
return tmp;
93103
}
94104

95105
}
@@ -103,27 +113,37 @@ LinkedList operations
103113
* Stack operations
104114
*
105115
*/
106-
void StackOperation_2_push(Stack *S,char value)
116+
117+
void StackOperation_3_push(Stack *S,void *value, char dataType)
107118
{
108-
LinkedListOperation_2_add(&(S->root),value);
119+
LinkedListOperation_3_add(&(S->root),value,dataType);
109120
(S->size)++;
110121
}
111122

112-
void StackOperation_1_pop(Stack *S)
123+
LinkedList *StackOperation_1_pop(Stack *S)
113124
{
114-
LinkedListOperation_1_delete_last_added(&(S->root));
115125
(S->size)--;
126+
return LinkedListOperation_1_delete_last_added(&(S->root));
116127

117128
}
118129

119130
void StackOperation_1_traverse(Stack S)
120131
{
121132
LinkedList *current;
133+
char *val;
122134
int count = S.size;
123135
current = S.root;
136+
124137
while (count)
125138
{
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+
127147
current = current->next;
128148
count--;
129149
}

0 commit comments

Comments
 (0)