I'm trying to use a Queue as a buffer for failed messages via LTE-M. So that if there is a connection again i can send all the failed messages out of the buffer.
For that i created two files.
queue_array.h
#ifndef INC_QUEUE_ARRAY_H_
#define INC_QUEUE_ARRAY_H_
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
#define QUEUE_EMPTY 0
/*Queue has five properties. capacity stands for the maximum number of elements Queue can hold.
Size stands for the current size of the Queue and elements is the array of elements. front is the
index of first element (the index at which we remove the element) and rear is the index of last element
(the index at which we insert the element) */
typedef struct
{
int head, tail, num_entries, size;
char **values;
}queue;
void init_queue(queue *q, int max_size);
bool queue_empty(queue *q);
bool queue_full(queue *q);
void queue_destroy(queue *q);
bool enqueue(queue *q , char *element);
int dequeue(queue *q);
int count_queue(queue *q);
char* front(queue *q);
#endif /* INC_QUEUE_ARRAY_H_ */
Queue_array.c
#include "queue_array.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
void init_queue(queue *q, int max_size)
{
q->size=max_size;
q->values=(char**)malloc(sizeof(char*)*q->size);
q->num_entries =0; //empty
q->head=0;
q->tail=0;
}
bool queue_empty(queue*q)
{
return(q->num_entries ==0);
}
bool queue_full(queue*q)
{
return(q->num_entries == q->size);
}
void queue_destroy(queue*q)
{
free(q->values);
}
int dequeue(queue *q)
{
if(queue_empty(q))
{
return QUEUE_EMPTY;
}
q->head = (q->head +1) % q->size;
q->num_entries--;
return 1;
}
char* front(queue *q)
{
if(q->size!=0)
{
/* Return the element which is at the front*/
return q->values[q->head];
}
return NULL;
}
int count_queue(queue *q)
{
return q->num_entries;
}
bool enqueue(queue *q , char *element)
{
if(queue_full(q))
{
return false;
}
else
{
q->num_entries++;
q->tail=(q->tail +1) % q->size;
q->values[q->tail] = (char *) malloc((sizeof(element)+1)* sizeof(char));
strcpy(q->values[q->tail], element);
}
return true;
}
Now when i run this code i only have these chars in the front element: "MESSAGE". Because there is something of the message there i think the mistake is in the enqueue function where the storage of the q->value is allocated.. But i dont know where. Does someone see the mistake?
Run Code:
char *temp;
init_queue(&msg_queue, 5);
if(enqueue(&msg_queue,"FIRSTFAILEDTEXTMESSAGE")!=0)
{
//nothing
}
if(enqueue(&msg_queue,"SECONDFAILEDTEXTMESSAGE")!=0)
{
//nothing
}
temp= front(&msg_queue);
temp = (char *)malloc(22 * sizeof(char )); //allocate the first message with 22 Chars
Related
If I input 'E 5' I need to enqueue 5 onto the queue with no output. If I input 'D' I need to print the dequeued element.
Below is my code:
#include<stdio.h>
#include <stdlib.h>
typedef struct queue{
int start;//first element index
int end;//last element index
int size;//length of queue
int *ptr;//actual queue
}queue;
queue* createQueue(int x){
queue* Q=(queue*)malloc(sizeof(queue));
Q->start=0;
Q->end=0;
Q->size=1;
Q->ptr[0]=x;
return Q;
}
void enqueue(int x, queue*qptr, int i){
qptr->size++;//increase size
qptr->end++;//increase index of last element
qptr->ptr[i]=x;
return;
}
void dequeue(queue*qptr){
if(qptr->size==0){
printf("Empty\n");
return;
}
printf("%d\n",qptr->ptr[qptr->start]);
qptr->start++;
qptr->size--;
}
int main(){
char a; int i=0;int j;queue* qptr;
while(scanf("%c",&a)!=-1){
if(a=='E'){
if(i==0){
scanf("%d",&j);
qptr =createQueue(j);
i++;
}
else{
scanf("%d",&j);
enqueue(j, qptr, i);
i++;
}
}
else if(a=='D'){
if(i==0){printf("Empty\n");}
else {dequeue(qptr);i++;}
}
}
return 0;
}
I don't understand what are my errors because it seems correct to me but the code doesn't compile. Please help me.
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;
A Program to implement Queue ADT using C
Declaring the headers in .c file or header file doesn't change anything in the errors
1) Header File (queue.h)
#ifndef QUEUE
#define QUEUE
#include <stdio.h>
#include <stdlib.h>
typedef struct QNodeType
{
char ch;
struct QNodeType *next;
}QNode;
typedef struct QueueType
{
QNode *head,*tail;
}Queue;
Queue **Create_Queue(); // Initialize the queue
void ClearQueue(); // Remove all items from the queue
int Enqueue(Queue **q,char ch); // Enter an item in the queue
char Dequeue(Queue **q); // Remove an item from the queue
int isEmpty(Queue **q); // Return true if queue is empty
int isFull(Queue **q); // Return true if queue is full
// Define TRUE and FALSE if they have not already been defined
#ifndef FALSE
#define FALSE (0)
#endif
#ifndef TRUE
#define TRUE (!FALSE)
#endif
#endif
2) .c File for defining the queue functions
#include "queue.h"
Queue** Create_Queue()
{
Queue **q;
(*q)->head = (*q)->tail = NULL;
return q;
}
void ClearQueue(Queue **q)
{
QNode *temp;
while((*q)->head != NULL)
{
temp = (*q)->head;
(*q)->head = (*q)->head->next;
free(temp);
}
(*q)->head = (*q)->tail = NULL; // Reset indices to start over
}
int Enqueue(Queue **q,char ch)
{
QNode *temp;
if(isFull(q)) return FALSE;
temp = (QNode *)malloc(sizeof(QNode));
temp->ch = ch;
temp->next = NULL;
if((*q)->head == NULL)
(*q)->head = (*q)->tail = temp;
else
{
(*q)->tail->next = temp; // Insert into the queue
(*q)->tail = temp; // Set tail to new last node
}
return TRUE;
}
char Dequeue(Queue **q)
{
char ch;
QNode *temp;
if(isEmpty(q)) return '\0';
else
{
ch = (*q)->head->ch;
temp = (*q)->head;
(*q)->head = (*q)->head->next;
free(temp);
if(isEmpty(q))
{
(*q)->head = (*q)->tail = NULL;
}
}
return ch;
}
int isEmpty(Queue **q)
{
return ((*q)->head == NULL);
}
int isFull(Queue **q)
{
return FALSE;
}
3) Driver program or the main code
#include "queue.h"
int main()
{
char testString[27];
int i;
char ch;
Queue **q=Create_Queue();
Enqueue(q,'A');
printf("Enqueued: %c\n", Dequeue(q));
strcpy(testString, "abcdefghijklmnopqrasuvwxyz");
i = 0;
printf("Testing enqueuing of string: %s\n", testString);
while(testString[i] != '\0')
{
if(!Enqueue(q,testString[i]))
{
printf("Queue is full. Unable to enqueue %c\n", testString[i]);
}
i++;
}
printf("Dequeued letters are...\n");
while((ch = Dequeue(q)) != '\0') // Dequeue returns null terminator
printf("%c", ch); // when queue is empty
printf("\nEnd of queue encountered...\n");
return 0;
}
The above program shows segmentation fault when run on Linux and an arbitrary return value when run on dev c++.
But when I run the code without the QUEUE Structure(basically this means declaring a *head and *tail pointer of static type in the queue.c file which allows for only one queue to be created at a time. And thus the CreateQueue function will be of void type and other functions doesn't require an argument of type Queue**)) it ran without any bugs.
I think that the problem is in create_Queue() you are not allocating any memory in it, after that in void ClearQueue() you are using free().
My code is basically functions used for making/using a stack. I've tried almost everything, but I don't know why my program is displaying this error:
Error: Syntax error before 'struct'
#include "stack.h"
#include <stdio.h>
#include <stdlib.h>
#define CAPACITY 128
struct stack_struct {
ElemType items[CAPACITY];
int top;
};
StackPtr stk_create(){
StackPtr s = malloc(sizeof(struct stack_struct));
s->top = -1; // stack initially empty
return s;
}
// TODO
StackPtr stk_clone(StackPtr s) {
return NULL; // temporary placeholder
}
void stk_free(StackPtr s) {
free(s);
}
int stk_push(StackPtr s, ElemType val){
if(s->top == CAPACITY - 1)
struct stack_struct * temp;
temp = (struct stack_struct*)malloc(sizeof(struct stack_struct));
s->top++;
s->items[s->top] = val;
return 1;
}
ElemType stk_pop(StackPtr s){
if(s->top == -1)
abort(); // library function which terminates program!!!
s->top--;
return s->items[s->top+1];
}
int stk_is_full(StackPtr s){
return s->top == CAPACITY-1;
}
int stk_is_empty(StackPtr s){
return s->top == -1;
}
int stk_size(StackPtr s) {
return s->top+1;
}
void stk_clear(StackPtr s){
s->top = -1;
}
void stk_print(StackPtr s) {
int i;
printf("\n----TOP-----\n");
for(i=s->top; i>=0; i--) {
printf(FORMAT_STRING, s->items[i]);
}
printf("---BOTTOM---\n");
}
int main() {
StackPtr sptr;
sptr = stk_create();
stk_push(sptr, 1.7);
stk_push(sptr, 3.14);
stk_print(sptr);
stk_pop(sptr);
stk_print(sptr);
stk_free(sptr);
}
As I could see, function stack_push should look like this
int stk_push(StackPtr s, ElemType val){
if(stk_is_full(s))
return -1; // stack already full, we couldn't push new elem
s->top++;
s->items[s->top] = val;
return 1;
}
I think error in this line (line 35 in your source code):
struct stack_struct * temp;
. Let's try
typedef struct stack_struct * temp;
or change declare struct
struct stack_struct {
ElemType items[CAPACITY];
int top;} stack;
and then call
stack* temp;
in line 35.
I am trying to implement a circular queue that takes a struct
⟶ checks if the queue is full if not enqueues ⟶ else return 0
⟶ checks if the queue is empty if not dequeues ⟶ else return 0
The following is my code :
#ifndef_QUEUE_H_
#define QUEUE_H_
#define QUEUE_SIZE 32
typedef struct queue
{
int head;
int tail;
int buffer[QUEUE_SIZE];
} queue_t;
void init_queue(queue_t *);
int enqueue(queue_t *, int);
int isfull(queue_t *);
int dequeue(queue_t *);
int queue_empty(queue_t *);
#endif
/* queue.h ends here */
/* *************************queue.c starts here************************ */
#include<queue.h>
#include<stm32f30x.h>
#define QUEUE_SIZE 32
int head;
int tail;
void init_queue(queue_t *q)
{
q->head = 0;
q->tail = 0;
}
int enqueue(queue_t *q, int a )
{
if ((((q->head)+1)%QUEUE_SIZE)!=q->tail)
{
q->buffer[q->head]=a;
q->head=(((q->head)+1)%QUEUE_SIZE);
return 1 ;
}
else
{
return 0 ;
}
}
int dequeue(queue_t *q)
{
int temp ;
if (queue_empty(q))
{
return 0;
}
else
{
temp = q->buffer[q->tail];
q->tail=((q->tail)+1)%QUEUE_SIZE;
return temp;
}
}
int queue_empty(queue_t *q)
{
if (q->head == q->tail)
{
return 1;
}
return 0;
}
/* **************queue.c ends here********************** */
When I try to test the queue: example enter "hello" and try to print the content inside it it prints "ello hlo elo.." and such incorrect replies.
I am using the enqueue and dequeue function in my getchar and putchar functions.
Below are my getchar and putchar functions:
queue_t rxbuf;
queue_t txbuf;
int putchar(int c)
{
while(!(enqueue(&txbuf, c)));
}
int nonblockgetchar(void)
{
dequeue(&rxbuf);
}
int getchar(void)
{
int ch;
while (!(ch=dequeue(&rxbuf)));
return (ch);
}
Appreciate your time and help.