i need help guys
trying to implement dynamic queue in C using an array. Nothing seems to be working (just started learning C)
I need help guys
here is the code
#include
#include
int pos = 0;
void popValue (int *dynamic_arr){
if (pos == 0){
printf("\n stack is empty! Nothing left to pop.");
}else {
//just testing random shit at this point...
//free(dynamic_arr);
pos--;
dynamic_arr = realloc(dynamic_arr, pos * sizeof(int));
printf("\n success !");
}
}
void pushValue (int *dynamic_arr){
int value;
printf("\n enter value to be pushed:");
scanf("%d", &value);
dynamic_arr[pos] = value;
pos++;
dynamic_arr = realloc(dynamic_arr, pos * sizeof(int));
printf("\n %d %d", dynamic_arr, dynamic_arr[pos]);
}
void showStack(int *dynamic_arr){
int i;
for (i = 0; i < 15 ; i++){
printf("\n value: %d is located at: %d", dynamic_arr[i], &dynamic_arr[i]);
}
}
main(){
int operation;
int *dynamic_arr = calloc(1, sizeof(int));
do{
printf("\n press 1 for push, 2 for pop, 3 to stop:");
scanf("%d", &operation);
switch(operation){
case 1:
pushValue(dynamic_arr);
break;
case 2:
popValue(dynamic_arr);
break;
}
showStack(dynamic_arr);
}while(operation != 3);
return 0;
}
>int switch with only two cases and no default
jesus christ
post your homework somewhere else kid
specify "queue", "dynamic" and "array"
1) not homework
2) why is this bad ? what should i use ?
i just want to learn and you are not of any help. why did you even reply anyway ?
i meant stack sorry
the issue is that realloc's return value is local and caller doesn't know the new pointer
you can either use double pointer or struct with pointer in it and pass pointer to this struct
struct queue {
long size;
long size;
int *data;
};
void queue_init(struct queue *q)
{
q->size = 0;
q->capacity = 32;
q->data = xmalloc(sizeof(int) * q->capacity);
}
void queue_push(struct queue *q, int val)
{
if (q->size == q->capacity) {
q->capacity *= 2;
q->data = xrealloc(q->data, sizeof(int) * q->capacity);
}
q->data[q->size] = val;
q->size++;
}
int queue_pop(struct queue *q, int *out)
{
if (q->size == 0) {
return -1;
}
*out = q->size;
q->size--;
return 0;
}
*xalloc and xrealloc are wrappers that print errno error string and exit process using err()
*initial state can be done differently starting on 0 capacity and specially handing it on first push
you should send the argument as int **dynamic_arr. otherwise you can't change its value and realloc fucks you.
if you could implement it in my code it would be much appreciated
Cniles are unironically just pretentious douchebags who don't even code rofl that's why he didn't help you: he can't.
i'll let you deal with deleting elements
#include
typedef struct
{
size_t size;
int *element;
} array_s;
static int array_init(array_s *const array, size_t const size);
static int array_insert(array_s *const array, int const data, size_t const index);
static int array_fetch(array_s *const array, size_t const index);
static int array_free(array_s *const array);
int main(void)
{
array_s array;
if (0 != array_init(&array, 10u))
{
return 1;
}
for(int k = 0; 25 > k; ++k)
{
if (0 != array_insert(&array, k, (size_t)k))
{
return 1;
}
}
if (0 != array_free(&array))
{
return 1;
}
return 0;
}
static int array_init(array_s *const array, size_t const size)
{
array->size = 0u;
array->element = (int *)calloc(size, sizeof(int));
if (NULL == array->element)
{
return 1;
}
return 0;
}
static int array_insert(array_s *const array, int const data, size_t const index)
{
if (index >= array->size)
{
array->size = index + 1;
array->element = (int *) realloc(array->element, array->size * sizeof(int));
if (NULL == array->element)
{
return 1;
}
}
array->element[index] = data;
return 0;
}
static int array_fetch(array_s *const array, size_t const index)
{
if (index size)
{
return array->element[index];
}
else
{
return 0;
}
}
static int array_free(array_s *const array)
{
free(array->element);
array->element = NULL;
array->size = 0u;
return 0;
}
thank you for helping out
but is it possible to do this without using struct ?
you already got the answer 3 times
Yes if you want to code like a objectfag
just for the sake of argument
Maybe a stupid question but here goes anyway.
Why use an array, wouldn't that mean you have to shift everything when you dequeue an item?
values get pushed and poped at the end of the array (allocating/de-allocating memory). If that makes any sense
C passes by value when a function is called. If you want a function to change what a pointer points to then you need to pass it a double pointer. int **dynamic_arr as the function parameter and &dynamic_arr as the value you pass to it.
#include
#include
int pos = 0;
void popValue (int **dynamic_arr){
if (pos == 0){
printf("\n stack is empty! Nothing left to pop.");
}else {
printf("popping %d \n", (*dynamic_arr)[pos]);
pos--;
(*dynamic_arr) = realloc((*dynamic_arr), sizeof(int)*pos);
}
}
void pushValue (int **dynamic_arr){
int value;
printf("\n enter value to be pushed:");
scanf("%d", &value);
printf("pushing %d \n", value);
pos++;
(*dynamic_arr) = realloc((*dynamic_arr), sizeof(int)*pos);
(*dynamic_arr)[pos] = value;
}
void showStack(int *dynamic_arr){
for (int i = 1; i
but if you do removal and insertion at on end, isn't that a stack, not a queue?
i meant to say stack, i said that earlier
i see. thank you kind sir. *tips fedora*
oh ok, didn't see that part sry
C is driving me crazy srsly