Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void push(Stack *s, int element)
- {
- //cheak if array is overflowed or not
- //if aary is overflowed we need to double the size of the array
- if(s->top == s->size -1)// is overflowed
- {
- //double the size of the array
- int *temp = (int*)malloc(sizeof(int)* s->size * 2);
- if (temp == '\0') // we can not alocate no more space there
- {
- printf("Error -- Unalble to allocate memory... ");
- return;
- }
- //memory allocated succesfully
- int i = 0;
- for(i = 0; i <= s->top; ++i)// copying existing data to the data array on the new allocated array
- {
- temp[i] = s->data[i];
- }
- free(s->data);
- s->data = temp;
- s->size *= 2;
- }
- // if not ovcerflowed
- //top willl be increment by 1 fist and than that top will be index
- s->data[++s->top] = element; //this is the push operation incres the top by 1 and than put the value at the top index of the array
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement