Hey guys i have been trying to create circular queue library for my project purpose.But while developing a SIGEGV error from a if statement under empty function whenever i am using an condition at that particular if condition i am encountering this error.
#include <stdio.h>
#define SIZE 5
typedef struct queue
{
int values[SIZE];
int head;
int tail;
int size;
int count;
int full_flag;
}queue_t;
int init(queue_t* q)
{
//q->values;
q->head=-1;
q->tail=-1;
q->size=SIZE;
q->count=1;
q->full_flag=0;
}
int empty(queue_t* q) //This if statement where i am getting problem at
{
if(q->count==0)
{
return 1;
}
else
{
return 0;
}
}
int enqueue(queue_t* q,int input)
{
if(empty(q)==1)
{
printf("Queue is full\n");
}
else
{
if(q->head=-1)
{
q->head=0;
}
q->tail=(q->tail+1)%q->size;
q->values[q->tail]=input;
q->count++;
}
}
int deque(queue_t* q)
{
int result;
q->head=q->head%q->size;
result=q->values[q->head];
q->head++;
printf("Removing from %d\n",result);
q->count--;
}
int main(void) {
queue_t* q;
//q->head=5;
// q->values[2]=3;
init(q);
enqueue(q,2);
enqueue(q,3);
enqueue(q,4);
enqueue(q,5);
enqueue(q,6);
//printf("%d\n",q->values[q->tail]);
//printf("%d",q->head);
// your code goes here
return 0;
}
when i executing without that if loop everything works fine.BUt with it everything becoming a chaos.Please guys help me out thanks in advance
you have defined a pointer without allocating memory to it in your main function
queue_t* q;
this is a wild(dangling) pointer because it doesn't point to a specific piece of memory. you must allocate memory to it. like this:
queue_t* q = malloc(sizeof(queue_t));
or
queue_t q;
queue_t* pq = &q;
Related
I am trying to implement a queue in C, and I get a prototype declaration in my remove(q) method. I'm wondering what I'm doing wrong here, I am using an older version of queue implementation as an exercise, but cannot put this together.
#include <stdio.h>
#define MAXQUEUE 100
#define TRUE (1==1)
#define FALSE (1==0)
struct queue {
int items[MAXQUEUE];
int front, rear;
};
empty(pq)
struct queue *pq;
{
return ((pq->front == pq->rear) ? TRUE : FALSE);
}
remove(pq)
struct queue *pq;
{
if (empty(pq)) {
printf("queue underflow\n");
exit(1);
}
if (pq->front == MAXQUEUE - 1)
pq->front = 0;
else
(pq->front)++;
return (pq->items[pq->front]);
}
int main() {
struct queue q;
q.front = q.rear = MAXQUEUE - 1;
if (empty(q)) {
printf("hello");
} else
printf("\n sod off\n");
return 0;
}
Your functions aren't functions. You need to adjust them to take a queue as an arguments and return your values like so:
int empty(struct queue *pq)
{
return ((pq->front == pq ->rear) ? TRUE : FALSE);
}
int remove(struct queue *pq)
{
if(empty(pq)){
printf("queue underflow\n");
exit(1);
}
if(pq->front == MAXQUEUE-1)
pq->front = 0;
else
(pq->front)++;
return (pq->items[pq->front]);
}
Making these changes along with one change in your main (if (empty(q) -> if (empty(&q)) compiles and outputs hello.
what if you declare your functions like this:
void
empty(struct queue *pq)
{
...
}
and
void
remove(struct queue *pq)
{
...
}
There are lots of C examples by using an array to represent the tree.
However, I'm trying to use a tree(Nodes) for the Binary heap.
I was trying to add the child left and then right. In order to insert the elements in Width-first-Search, I've used a Queue. Is this necessary?
My Queue is using a Node* not a int type. Because, if I put a int in the Queue I must search the position of the Node(more time needed). Is this an appropriate way to built a Binary heap?
During the insertion, there are bubbling up and downs. However, since i have a binary heap and also a Queue. Do I have to match the orders in Queue also?
4.Do I need to search for inserting position like a BST? or do I approach to the address directly by using Queue?
I've learned the theory. However, the codes and structures are unfamiliar.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int left(int a,int b)
{
return (int)(a-floor((float)a/(float)b)*b);
}
typedef struct Node
{
int data;
struct Node* leftNext;
struct Node* rightNext;
}Node;
struct queue
{
Node** data;
int top;
int bottom;
int size;
}typedef Queue;
int push(Queue* Q,Node* input)
{
printf("PUSH : %d\n",input);
//한칸은 비울 것임.
if(left((*Q).top-(*Q).bottom,(*Q).size)==(*Q).size-1)
{
printf("큐가 다 찼습니다.\n");
return 0;
}
else
{
//큐의 크기를 넘으면 아래로 이동
if((*Q).top==(*Q).size)
(*Q).top=0;
printf("TOP : %d\n",(*Q).top);
(*Q).data[(*Q).top++]=input;
return 1;
}
}
Node* pop(Queue* Q)
{
if(left((*Q).top-(*Q).bottom,(*Q).size)==0)
printf("큐가 비었습니다.\n");
else
{
if((*Q).bottom==(*Q).size)
(*Q).bottom=0;
printf("BOTTOM : %d\n",(*Q).bottom);
return (*Q).data[(*Q).bottom];
}
}
typedef struct Heap
{
Node* head;
}Heap;
int insert(Node** head,int data,Queue* Q)
{
if((*head)!=NULL)
{
if((*head)->leftNext==NULL)
{
(*head)->leftNext=(Node*)malloc(sizeof(Node));
(*head)->leftNext->data=data;
(*head)->leftNext->leftNext=NULL;
(*head)->leftNext->rightNext=NULL;
push(Q,&(*head)->leftNext);
}
else if((*head)->rightNext==NULL)
{
(*head)->rightNext=(Node*)malloc(sizeof(Node));
(*head)->rightNext->data=data;
(*head)->rightNext->leftNext=NULL;
(*head)->rightNext->rightNext=NULL;
push(Q,&(*head)->rightNext);
(*Q).bottom++;
}
else
{
if(insert((pop(Q)),data,Q)==1)
return 1;
else
return 0;
}
}
else
{
printf("헤드가 비었습니다.");
return 0;
}
return 1;
}
int main()
{
//입력 15 6 12 7 10 17
Heap h;
//큐 초기화
Queue Q;
//큐 크기
int temp=100;
Q.data=(Node**)malloc(sizeof(Node*)*temp);
Q.size=temp;
Q.top=0;
Q.bottom=0;
h.head=(Node*)malloc(sizeof(Node));
h.head->data=15;
h.head->leftNext=NULL;
h.head->rightNext=NULL;
insert(&(h.head),6,&Q);
insert(&(h.head),12,&Q);
insert(&(h.head),7,&Q);
insert(&(h.head),10,&Q);
insert(&(h.head),17,&Q);
return 0;
}
I'm trying to implement a queue in c. I have gotten as far as implementing an enqueue function in my code. However, when I am testing it I don't get the desired output. Can someone please tell me what I am doing wrong?
struct queue{
int array[30];
int *front; //pointer to front of queue
int *rear; //pointer to rear of queue
int count; //counts number of elements in queue
};
//initialising a queue
struct queue * new_Queue()
{
struct queue *q;
q->count=0;
q->front=&q->array[-1];
q->rear=&q->array[-1];
return q;
};
int queueCount(struct queue *q)
{
return q->count;
}
int isFull(struct queue *q)
{
if(q->count==30){
printf("%s","Buffer is full!");
return 1;
}
return 0;
}
int isEmpty(struct queue *q)
{
if(q->count==0){
printf("%s","Queue is empty!");
return 1;
}
return 0;
}
int enqueue(struct queue * q,int i)
{
if(isFull(q)){
return 0;
}
if(isEmpty(q)){
q->front+1;
}
int k=*(q->rear+1);
q->array[k]=i;
printf("enque success!");
return 1;
}
int main(int argc, char**argv)
{
int i=10;
struct queue *newQueue;
enqueue(newQueue,i);
int j= queueCount(newQueue);
printf("%d",j);
}
You need memory for your queue. At the moment, you have an uninitialised pointer that points to a random location in memory. Dereferencing that pointer is undefined behaviour and will very likely give you a seg fault.
You have to decide how you want to store your queue. You can either allocate it on the heap with malloc. This is what your function new_Queue should do:
struct queue *new_Queue()
{
struct queue *q = malloc(sizeof(*q)); // TO DO: Error checking
q->count = 0;
q->front = q->array;
q->rear = q->array;
return q;
}
You client code then looks like this:
struct *q = new_Queue();
enqueue(q, x);
// Do more stuff ...
free(q); // Release resources
The queue structure isn't big. You can also allocate it on the stack. In that casen you need an initialisation function :
void queue_init(struct queue *q)
{
q->count = 0;
q->front = q->array;
q->rear = q->array;
}
and call it like:
struct queue *q;
queue_init(&q);
enqueue(&q, 12);
Note the addres-of operator &. You don't have to (and cannot) free the queue here.
You can't access the array at index -1. You could make the front the next element to dequeue and the rear point to the space where the next element is enqueued. In a circular buffer, that will make the cases of empty and full list indistiguishable, but you can use the count to distinguish between them.
// Whenever i am increasing function call times of push more than 8, it get crashed before that everything is working good. your help is required. In the below code i have created a program for stack using Dynamic array keeping in mind there should not be any stack overflow by using realloc function to doubling the values whenever the stack gets filled.
#include<stdio.h>
#include<stdlib.h>
#include<memory.h>
typedef struct ArrayStack
{
int top;
int capacity;
int *arr;
}*stack;
stack Creation()
{
stack S;
S=(stack)malloc(sizeof(struct ArrayStack));
if(!S)return NULL;
S->top=-1;
S->capacity=1;
S->arr=(int*)malloc(S->capacity*sizeof(int));
if(!S->arr)return NULL;
return S;
}
int is_Full(stack S)
{
return S->top==S->capacity-1;
}
int is_Empty(stack S)
{
return S->top==-1;
}
void Doubling(stack S)
{
S->capacity*=2;
S->arr=realloc(S->arr,S->capacity);
}
void push(stack S,int data)
{
if(is_Full(S))
Doubling(S);
S->arr[++S->top]=data;
}
int pop(stack S)
{
if(is_Empty(S))
printf("\nStack underflow");
else
return S->arr[S->top--];
}
int main()
{
stack S;
int i=0,size=9;
S=Creation();
**for(i=0;i<size;i++)
push(S,19+1);** // As in this case
return 0;
}
S->arr = malloc(S->capacity * sizeof(int));
S->arr = realloc(S->arr, S->capacity);
You only reallocate enough space for S->capacity / sizeof(int) items.
typedef struct queue {
int q[max];
int qhead;
int qrear;
} queue;
void init_queue(queue *QUEUE)
{
QUEUE.qhead = 0;
QUEUE.qrear = -1;
}
void enqueue(queue *QUEUE,int data)
{
QUEUE.qrear++;
QUEUE.q[QUEUE.qrear] = data;
}
int process_queue(queue *QUEUE)
{
if(QUEUE.qhead > QUEUE.qrear)
return -1;
else
return QUEUE.q[QUEUE.qhead++];
}
I am implementing queues using arrays just to keep it simple. Wats the error with the above code?
First of all, the . operator is used to access members of a struct. You need -> to access members of a pointer to a struct:
void init_queue(queue *QUEUE)
{
QUEUE->qhead = 0;
QUEUE->qrear = -1;
}
Just as a tid-bit, a->b is equivalent to (*a).b - that is, first dereferencing the pointer a, and then accessing a member of that struct.
Besides the compilation error from using . instead of ->, you also have potential buffer overflow in enqueue; you'll overflow your buffer after max calls. You have a couple of choice depending on what you want.
1) You can turn this into a circular queue:
void enqueue(queue *QUEUE,int data)
{
QUEUE->qrear = (QUEUE->qrear + 1) % max;
QUEUE->q[QUEUE->qrear] = data;
}
2) You can stop adding once you hit max:
void enqueue(queue *QUEUE,int data)
{
if (QUEUE->qrear < (max - 1))
{
QUEUE->qrear++;
QUEUE->q[QUEUE->qrear] = data;
}
}
You should use -> instead of .:
Accessing structs' values using pointers is done using QUEUE-> or (*QUEUE). and not QUEUE.. You first need to dereference the pointer and only then access the value.
typedef struct queue {
int q[max];
int qhead;
int qrear;
} queue;
void init_queue(queue *QUEUE)
{
QUEUE->qhead = 0;
QUEUE->qrear = -1;
}
void enqueue(queue *QUEUE,int data)
{
QUEUE->qrear++;
QUEUE->q[QUEUE->qrear] = data;
}
int process_queue(queue *QUEUE)
{
if(QUEUE->qhead > QUEUE->qrear)
return -1;
else
return QUEUE->q[QUEUE->qhead++];
}