Sorting Stack by implementing as Arrays - arrays

I saw a problem on the web,i.e, I am trying to sort a stack using another reference stack in C. I tried to do it by implementing it as an array but it is not working.
(condition is that you have to use only arrays)
#include <stdio.h>
int MAXSIZE = 5;
int stack[5];
int tmpstack[5];
int tmp;
int top = -1;
int top2=-1;
int isempty(int A[],int B) {
if(B == -1)
return 1;
else
return 0;
}
int isfull(int A[],int B) {
if(B == MAXSIZE)
return 1;
else
return 0;
}
int pop(int A[],int B) {
int data;
if(!isempty(A,B)) {
data = A[B];
B = B - 1;
return data;
}
else {
printf("Could not retrieve data, Stack is empty.\n");
}
}
int push(int data,int A[],int B) {
if(!isfull(A,B)) {
B = B + 1;
A[B] = data;
}
else {
printf("Could not insert data, Stack is full.\n");
}
}
int sortStack(int stack[])
{
int tmpStack[MAXSIZE];
while (!isempty(stack,top))
{
int tmp = stack[top];
pop(stack,top);
while (!isempty(tmpstack,top2) && tmpstack[top2] > tmp)
{
push(pop(tmpstack,top2),stack,top);
}
push(tmp,tmpstack,top2);
}
return 0;
}
int main() {
int n ,a;
n=5;
for(int i = 0;i < n ; ++i){
scanf("%d",&a);
push(a,stack,top);
}
sortStack(stack);
while (!isempty(tmpstack,top2))
{
printf("%d ",tmpstack[top2]);
pop(tmpstack,top2);
}
return 0;
}
What I did here is that I made similar functions in arrays which we used in stacks only and we are supposed to use them only for implementing our stacks.

Related

Facing problems in binary search in c

#include <stdio.h>
int bsearch(int ar[],int n,int key)
{
int s=0;
int e=n-1;
while(s<=e){
int mid=(e+s)/2;
if(mid==key){
return mid;
}
else if(key<mid){
e=mid-1;
}
else if(key>mid){
s=mid+1;
}
}
return -1;
}
I made the function for the binary search
int main()
{
int n,key;
int ar[n];
scanf("%d",&n);
for(int i=0;i<n;i++){
printf("ar[%d]= ",i);
scanf("%d",&ar[i]);
}
printf("Enter key>> \n");
scanf("%d",&key);
printf("%d is the index",bsearch(ar,n,key));
return 0;
}
Then I inputted an sorted array but with repetitions. Shown in the following image.
The output is coming as 3 is the index.
But it should as come as 6 is the index.
mid is an index of element, not a value. So, I have corrected your function:
#include <stdio.h>
int bsearch(int ar[], int n, int key)
{
int s=0;
int e=n-1;
while(s <= e){
int mid = (e + s) / 2;
if(key == ar[mid]) {
return mid;
}
else if(key < ar[mid]) {
e = mid-1;
}
else if(key > ar[mid]) {
s = mid+1;
}
}
return -1;
}

Function to verify if an element present in an array

I am working in ADT of an Array and when doing a function to verify if an element is present in the array, it didn't work, actually, it just said the element 1 is present in the array, but it doesn't work with other integers elements of the array. The function is
int checkElement(Array *a, int elem){
for (int i = 0; i < a->size; i++){
if (a->array[i] == elem){
return 1;
}
else return 0;
}
}
Compilable code
#include <stdio.h>
#include <stdlib.h>
typedef struct array{
int size;
int *array;
}Array;
Array* allocateMemory(int size){
Array *a;
a = (Array*) malloc(sizeof(Array));
if (a == NULL)
{
return NULL;
}
a->array = (int*) calloc(size, sizeof(int));
if (a->array == NULL)
{
return NULL;
}
a->size = size;
return a;
}
int add(Array *a, int elem, int c){
if ((c > a->size) || (c < 0)){
return 0;
}
a->array[c] = elem;
return 1;
}
int print(Array *a, int c){
return a->array[c];
}
int checkElement(Array *a, int elem){
for (int i = 0; i < a->size; i++){
if (a->array[i] == elem){
return 1;
}
else return 0;
}
}
int main(void)
{
Array *a1;
a1 = allocateMemory(10);
int elem = 1;
for (int i = 0; i < 10; i++)
{
if (add(a1, elem, i)== 0)
{
printf("Something went wrong\n");
}
elem += 1;
}
printf("Array 1: \n");
for (int i = 0; i < 10; i++)
{
printf("\a[%d] = %d\n", i, print(a1, i));
}
int element = 6;
int flag = checkElement(a1, element);
if (flag == 1) printf("\nElement %d is present in the array", element);
else printf("\nElement %d is not present in the array", element);
return 0;
}
Output:
Array 1:
[0] = 1
[1] = 2
.
.
.
[9] = 10
Element 2 is not present in the array
int flag = checkElement(a1, element);
int checkElement(Array *a, int elem){
for (int i = 0; i < a->size; i++){
if (a->array[i] == elem){
return 1;
}
}
return 0;
}

Problems with implementing a stack in C

I tried to implement to stack in C. Here is my code, I ran the code, the code ended with:
Thread 1: EXC_BAD_ACCESS error
I am so confused, don't know what went wrong, can anyone debug y code? Thanks!
And I have one more question, why command+k key didn't work for me? I have to indent line by line.
#include <stdio.h>
#include <stdlib.h>
#define init_size 10
#define increment 1
typedef struct sqStack
{
int* top;
int* base;
int stack_size;
} sqStack;
int init_stack(sqStack* sq)
{
if(sq->base==NULL)
{
sq->base = (int*)malloc(init_size*sizeof(int));//Thread 1: EXC_BAD_ACCESS
}
if(sq->base==NULL) exit(-1);
sq->stack_size=init_size;
sq->top=sq->base;
return 1;
}
int push(sqStack* sq, int e)
{
if(sq==NULL) exit(-1);
if(sq->top-sq->base==sq->stack_size)
{
int* q = (int*)realloc(sq->base,
(init_size+increment)*sizeof(int));
if(q==NULL) exit(-1);
else
{
sq->base=q;
sq->stack_size += increment;
sq->top=sq->base+sq->stack_size;
}
*sq->top++=e;
}
return 1;
}
int pop(sqStack* sq,int*e)
{
if(sq==NULL) exit(-1);
if(sq->base==sq->top) exit(-1);
sq->top-=1;
*e=*sq->top;
return 1;
}
int empty(sqStack* sq)
{
if(sq->base==sq->top) return 1;
else return 0;
}
int main()
{
sqStack* sq=NULL;
init_stack(sq);
push(sq,1);
int *e=(int*)malloc(sizeof(int));
pop(sq,e);
printf("%d\n",*e);
return 0;
}
In function int init_stack(sqStack* sq)
the
if(sq->base==NULL)
{
sq->base = (int*)malloc(init_size*sizeof(int));//Thread 1: EXC_BAD_ACCESS
}
should be:
sq->base = (int*)malloc(init_size*sizeof(int));//Thread 1: EXC_BAD_ACCESS
In function int push(sqStack* sq, int e)
the
}
*sq->top++=e;
}
return 1;
}
should be
}
}
*sq->top++=e;
return 1;
}
In function int main()
the is not need use int *e=(int*)malloc(sizeof(int)); just use int e;
the
sqStack* sq=NULL;
init_stack(sq);
push(sq,1);
int *e=(int*)malloc(sizeof(int));
pop(sq,e);
printf("%d\n",*e);
should be
sqStack sq;
init_stack(&sq);
push(&sq,1);
int e;
pop(sq,&e);
printf("%d\n",e);
The following code could work:
#include <stdio.h>
#include <stdlib.h>
#define init_size 10
#define increment 1
typedef struct sqStack {
int* top;
int* base;
int stack_size;
} sqStack;
int init_stack(sqStack* sq) {
sq->base = (int*)malloc(init_size * sizeof(int));
if (sq->base == NULL) exit(-1);
sq->stack_size = init_size;
sq->top = sq->base;
return 1;
}
int push(sqStack* sq, int e) {
if (sq == NULL) exit(-1);
if (sq->top - sq->base == sq->stack_size) {
int* q = (int*)realloc(sq->base, (init_size + increment) * sizeof(int));
if (q == NULL)
exit(-1);
else {
sq->base = q;
sq->stack_size += increment;
sq->top = sq->base + sq->stack_size;
}
}
*sq->top++ = e;
return 1;
}
int pop(sqStack* sq, int* e) {
if (sq == NULL) exit(-1);
if (sq->base == sq->top) exit(-1);
sq->top -= 1;
*e = *sq->top;
return 1;
}
int empty(sqStack* sq) {
if (sq->base == sq->top)
return 1;
else
return 0;
}
int main() {
sqStack sq;
init_stack(&sq);
push(&sq, 1);
int e;
pop(&sq, &e);
printf("%d\n", e);
return 0;
}

Is this program to get the mininmum value in a stack of O(1) complexity?

I have implemented push pop and get minimum in O(1) complexity. I have seen many solutions in C++. This is an implementation in C itself. Is the following program correct?
#include <stdio.h>
#include <stdlib.h>
int stack[15],aux[15];
int top=-1,count=-1,aux_count=-1,temp_aux=-1;
void push_auxilary(int ele)
{
aux[++aux_count] = ele;
}
void push_stack(int ele)
{
stack[++top]=ele;
}
void push(int ele)
{
if(top < 0 && aux_count < 0)
{
push_auxilary(ele);
push_stack(ele);
}
else
{
if(ele > aux[aux_count])
{
push_auxilary(aux[aux_count]);
push_stack(ele);
}
else
{
push_stack(ele);
push_auxilary(ele);
}
}
}
int pop_stack()
{
return stack[top--];
}
int pop_auxilary()
{
return aux[aux_count--];
}
int pop()
{
int a = pop_stack();
pop_auxilary();
return a;
}
void display()
{
for (int i = top; i >= 0; i--)
{
printf("%d\n",stack[i]);
/* code */
}
}
int get_min()
{
return aux[aux_count];
}
int main(int argc, char const *argv[])
{
int i=0;
push(5);
push(9);
push(1);
push(6);
push(1);
push(54);
push(34);
push(9);
push(3);
push(4);
push(7);
push(12);
push(02);
printf("the %d\n",get_min() );
for (i = aux_count; i >= 0; i--)
{
printf("%d\n",aux[i]);
}
return 0;
}
Looks like it gets the job done in O(1) indeed. Algorithmic-ally correct, but terrible from code reusage point of view.

How to remove item from array in C?

I want to remove element from b->array as shown in below code:removeItem function
I have tried to remove 12.12 from the b->array = {11.11,12.12,13.13}but it leads to segmentation fault instead of printing {11.11,13.13}. can anybody help to get rid of it?
typedef struct
{
float val;
}data;
typedef struct
{
data **array;
int size;
}bag;
int main(int argc,char* argv[])
{
bag *str = createBag();
#ifdef DEBUG
printf("Inital values: %p %d\n",str->array,str->size);
#endif
data *iptr = createData(10.10);
data *iptr1 = createData(11.11);
data *iptr2 = createData(12.12);
data *iptr3 = createData(13.13);
data *iptr4 = createData(14.14);
#ifdef DEBUG
printf("%f\n",iptr->val);
#endif
addData(str,iptr);
addData(str,iptr1);
addData(str,iptr2);
addData(str,iptr3);
addData(str,iptr4);
printBag(str);
data *ptr = getData(str,4);
printf("Data item 4 is:%f\n",ptr->val);
int b = getBagSize(str);
printf("Size of bag: %d\n",b);
removeBack(str);
printBag(str);
removeFront(str);
printBag(str);
//cleanBag(str);
int s = searchBag(str,10.10);
printf("%d\n",s);
removeItem(str,12.12);
printBag(str);
return 0;
}
bag* createBag()
{
bag *str = (bag*)malloc(sizeof(bag));
str->array = NULL;
str->size = 0;
return str;
}
data* createData(float v)
{
data *iptr = (data*)malloc(sizeof(data));
iptr->val = v;
return iptr;
}
void addData(bag* b, data* d)
{
b->size++;
data** array1 = (data**)malloc(sizeof(data*)* b->size);
if(b->array!= NULL)
{
int i;
for(i = 0; i<b->size;i++)
{
array1[i] = b->array[i];
}
}
free(b->array);
array1[b->size-1] = d;
b->array = array1;
}
void printBag(bag *b)
{
int i;
for(i=0; i<b->size;i++)
{
printf("%f\n",b->array[i]->val);
}
}
data* getData(bag *b, int pos)
{
if(pos>5)
{
printf("change array position\n");
}
return b->array[pos];
}
int getBagSize(bag *b)
{
return b->size;
}
void removeBack(bag *b)
{
b->size--;
free(b->array[b->size]);
data **array2 = (data**)malloc(sizeof(data*)* b->size);
if(b->array!= NULL)
{
int i;
for(i = 0; i<b->size;i++)
{
array2[i] = b->array[i];
#ifdef DEBUG
printf("%f\n",array2[i]->val);
#endif
}
free(b->array);
b->array = array2;
}
}
void removeFront(bag *b)
{
b->size--;
free(b->array[0]);
data **array2 = (data**)malloc(sizeof(data*)* b->size);
if(b->array!= NULL)
{
int i;
for(i = 0; i<b->size;i++)
{
array2[i] = b->array[i+1];
#ifdef DEBUG
printf("%f\n",array2[i]->val);
#endif
}
free(b->array);
b->array = array2;
}
}
/*void cleanBag(bag *b)
{
int i;
for(i=0;i<b->size;i++)
{
free(b->array[i]);
}
free(b->array);
free(b);
}*/
int searchBag(bag *b,float v)
{
int i;
for(i=0;i<b->size;i++)
{
if(b->array[i]->val==v)
{
return (i+1);
}
}
return -1;
}
void removeItem(bag *b, float v)
{
int flag = 0,i = 0;
flag = searchBag(b,v);
if(flag != -1)
{
data **array2 = (data**)malloc(sizeof(data*)*(b->size-1));
for(i = 0; i < (b->size); i++)
{
if(i == (flag-1))
{
i = i + 1;
continue;
}
array2[i] = b->array[i];
}
free(b->array);
b->size--;
b->array = array2;
}
else
{
printf("Element is not found\n");
}
}
There are two mistakes in your code.
searchBag returns 0, if the float value is not found in the bag.
However, the function removeItem test for if(flag != -1). It should test for if(flag != 0) instead.
The reason for the seg. fault you encountered also lies in the removeItem function.
Note how the memory allocated for array2 is for b->size-1 elements only. Valid array indices for this array are in the range of 0 ... b->size-2.
Now, look at the for loop, and you will note that the index variable i will run from 0 to b->size-1. Exactly in the last iteration i will become b->size-1, so the code tries to write at the location array2[b->size-1] which is beyond the allocated memory of array2.
A possible fix of the removeItem function could look like this:
void removeItem(bag *b, float v)
{
int flag = 0;
int oldArrayIndex = 0;
int newArrayIndex = 0;
flag = searchBag(b,v);
if(flag != 0)
{
data **array2 = (data**) malloc(sizeof(data*)*(b->size-1));
for(oldArrayIndex = 0; oldArrayIndex < (b->size); oldArrayIndex++)
{
if(oldArrayIndex != (flag-1))
{
array2[newArrayIndex] = b->array[oldArrayIndex];
newArrayIndex++;
}
}
free(b->array);
b->size--;
b->array = array2;
}
else
{
printf("Element is not found\n");
}
}

Resources