Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack2.c
More file actions
Latest commit
60 lines (54 loc) · 1.38 KB
/
Copy pathstack2.c
File metadata and controls
60 lines (54 loc) · 1.38 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
/* Name: Vishwa Venkateshwaran */
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"lab4.h"
voidchangeArrSize(void*pointer, intsize) {
inti=0;
int*p=malloc(size*sizeof(int));
Stack2*stack2= (Stack2*) pointer;
for(i=0; i<stack2->length; i++) {
*(p+i) =*((stack2->entries+i));
}
free(stack2->entries);
stack2->entries=p;
}
Stack*newStack2() {
Stack*Stack=malloc(sizeof(Stack));
Stack2*stack2=malloc(sizeof(Stack2));
if (Stack) {
Stack->Stack=stack2;
Stack->length=lengthS2;
Stack->push=pushS2;
Stack->pop=popS2;
stack2->arrSize=MIN_SIZE;
stack2->length=0;
stack2->entries=malloc(stack2->arrSize*sizeof(int));
}
returnStack;
}
voidpushS2(void*pointer, intdata) {
Stack2*stack2= (Stack2*) pointer;
*(stack2->entries+stack2->length) =data;
stack2->length++;
if (stack2->length==stack2->arrSize) {
stack2->arrSize=2*stack2->arrSize;
changeArrSize(stack2, stack2->arrSize);
}
}
intpopS2(void*pointer) {
Stack2*stack2= (Stack2*) pointer;
intdata=*(stack2->entries+stack2->length-1);
stack2->length--;
if (stack2->length>MIN_SIZE) {
if (stack2->length==1/4*(stack2->arrSize)) {
stack2->arrSize=1/2*stack2->arrSize;
changeArrSize(stack2, stack2->arrSize);
}
}
returndata;
}
intlengthS2(void*pointer) {
Stack2*stack2= (Stack2*) pointer;
returnstack2->length;
}