-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBucketstack.c
More file actions
174 lines (167 loc) · 4.67 KB
/
Copy pathBucketstack.c
File metadata and controls
174 lines (167 loc) · 4.67 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
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
174
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <assert.h>
#include "bucketstack.h"
//struct NodeBucket {
// char** val;
// struct NodeBucket* next;
//};
//typedef struct {
// struct NodeBucket* firstBucket;
// int topElt;
// int bucketSize;
//} Stack;
void initStack (int bucketSize, Stack **stack){
(*stack)=malloc(sizeof(Stack));
(*stack)->firstBucket=NULL;
(*stack)->topElt=-1;
(*stack)->bucketSize=bucketSize;
}
bool isEmpty (const Stack *stack){
if(stack->firstBucket==NULL)
return true;
return false;
}
void push (char* val, Stack *stack){
if(stack->firstBucket==NULL){
struct NodeBucket *newbucket;
newbucket=malloc(sizeof(struct NodeBucket));
newbucket->next=NULL;
newbucket->val=malloc(sizeof(char*)*stack->bucketSize);
newbucket->val[0]=malloc(strlen(val)+1);
strcpy(newbucket->val[0],val);
//printf("%s",newbucket->val[0]);
//newbucket->next=NULL;
stack->topElt=0;
stack->firstBucket=newbucket;
}
else if(stack->topElt==(stack->bucketSize)-1){
struct NodeBucket *newbucket;
newbucket=malloc(sizeof(struct NodeBucket));
newbucket->val=malloc(sizeof(char*)*stack->bucketSize);
newbucket->val[0]=malloc(strlen(val)+1);
strcpy(newbucket->val[0],val);
newbucket->next=stack->firstBucket;
stack->firstBucket=newbucket;
stack->topElt=0;
}
else{
stack->topElt=stack->topElt+1;
stack->firstBucket->val[stack->topElt]=malloc(strlen(val)+1);
strcpy(stack->firstBucket->val[stack->topElt],val);
}
}
void pop(Stack *stack){
assert(stack->firstBucket!=NULL);
if(stack->topElt==0){
struct NodeBucket *temp;
free(stack->firstBucket->val[0]);
free(stack->firstBucket->val);
temp=stack->firstBucket;
//free(stack->firstBucket);
stack->firstBucket=temp->next;
free(temp);
stack->topElt=stack->bucketSize-1;
}
else{
free(stack->firstBucket->val[stack->topElt]);
stack->topElt=stack->topElt-1;
}
}
int size (const Stack *stack){
int s_size=0;
int b_size;
b_size=stack->bucketSize;
struct NodeBucket *cursor;
if(stack->firstBucket==NULL)
return 0;
else
cursor=stack->firstBucket->next;
for(;cursor!=NULL;cursor=cursor->next){
s_size=s_size+b_size;
}
return (s_size+(stack->topElt+1));
}
char* top (const Stack *stack){
assert(stack->firstBucket!=NULL);
struct NodeBucket *cursor;
cursor=stack->firstBucket;
return(cursor->val[stack->topElt]);
}
void swap (Stack *stack){
assert(size(stack)>=2 && (stack->firstBucket!=NULL));
if(stack->topElt==0){
char *temp1;
char *temp2;
char *temp_alloc;
temp1=stack->firstBucket->val[0];
temp2=stack->firstBucket->next->val[stack->bucketSize-1];
// stack->firstBucket->val[0]=realloc(stack->firstBucket->val[0],strlen(temp2)+1);
temp_alloc=malloc(strlen(temp1)+1);
strcpy(temp_alloc,temp1);
// strcpy(stack->firstBucket->val[0],temp2);
// stack->firstBucket->val[0]=cursor->val[stack->bucketSize-1];
// stack->firstBucket->next->val[stack->bucketSize-1]=realloc(stack->firstBucket->next->val[stack->bucketSize-1],strlen(temp_alloc)+1);
// strcpy(stack->firstBucket->next->val[stack->bucketSize-1],temp_alloc);
free(stack->firstBucket->val[0]);
stack->firstBucket->val[0] = temp2;
stack->firstBucket->next->val[stack->bucketSize-1]=temp_alloc;
// free(temp_alloc);
}
else{
char *temp1;
char *temp2;
temp1=stack->firstBucket->val[stack->topElt];
char *temp_alloc=malloc(strlen(temp1)+1);
temp2=stack->firstBucket->val[(stack->topElt)-1];
// stack->firstBucket->val[stack->topElt]=realloc(stack->firstBucket->val[stack->topElt],strlen(temp2)+1);
strcpy(temp_alloc,temp1);
// strcpy(stack->firstBucket->val[stack->topElt],temp2);
// stack->firstBucket->val[(stack->topElt)-1]=realloc(stack->firstBucket->val[(stack->topElt)-1],strlen(temp_alloc)+1);
// strcpy(stack->firstBucket->val[(stack->topElt)-1],temp_alloc);
free(stack->firstBucket->val[stack->topElt]);
stack->firstBucket->val[stack->topElt]=temp2;
stack->firstBucket->val[stack->topElt-1]=temp_alloc;
// free(temp_alloc);
}
}
void print (const Stack *stack){
struct NodeBucket *cursor;
cursor=stack->firstBucket;
int top=stack->topElt;
int i=0;
printf("stack is:\n");
for(;cursor!=NULL;cursor=cursor->next,i++){
if(i>0)
top=stack->bucketSize-1;
for(;top>=0;top=top-1){
printf(" %s\n",cursor->val[top]);
}
}
}
void clear(Stack *stack){
struct NodeBucket *cursor;
cursor=stack->firstBucket;
int top=stack->topElt;
struct NodeBucket *temp;
int i=0;
for(;cursor!=NULL;cursor=temp,i++){
temp=cursor->next;
if(i>0)
top=stack->bucketSize-1;
for(;top>=0;top=top-1){
free(cursor->val[top]);
}
free(cursor->val);
free(cursor);
}
stack->firstBucket=NULL;
stack->topElt=-1;
}
void destroyStack(Stack **stack){
clear(*stack);
free(*stack);
(*stack)=NULL;
}