I am trying to enqueue data in a circular queue created using
struct Arrayqueue
{
int front,rear;
int capacity;
int *array;
}*queue;
struct Arrayqueue* create()
{
int data;
struct Arrayqueue* queue=malloc(sizeof(struct Arrayqueue));
printf("\nWrite the capacity of queue: ");
scanf("%d",&data);
queue->capacity=data;
queue->front=queue->rear=-1;
queue->array=malloc( queue->capacity * sizeof(int));
return(queue);
}
I am able to process the create function. Here is my enqueue function
int isFull(){
if((queue->rear+1)%queue->capacity==queue->front)
return 1;
else
return 0;
}
void enqueue()
{
int data;
printf("Enter the data: ");
scanf("%d",&data);
if(!isFull()){
queue->rear=(queue->rear+1)%queue->capacity;
queue->array[queue->rear]=data;
if(queue->front==-1)
queue->front=queue->rear;
}
else
printf("\nQueue is full\n");
}
Whenever I try to put some data, my program crashes. Where in the program is the problem?
Related
I have initialised two stacks using a structure with which I am creating a queue. But the stack is not able to store the values which is why enqueue or dequeue operations are not working properly.
Here is the code:
#include<stdio.h>
#include<stdlib.h>
struct stack{
int top;
int size;
int *s;
};
int isfull(struct stack *st){
if(st->top==st->size-1){
return 1;
}
return 0;
}
int isempty(struct stack *st){
if(st->top==-1){
return 1;
}
return 0;
}
void push(struct stack *st,int x){
if(isfull(st)){
printf("FULL!!\n");
}
else{
st->top++;
st->s[st->top]=x;
}
}
int pop(struct stack *st){
int x=-1;
if(isempty(st)){
printf("EMPTY!!\n");
}
else{
x=st->s[st->top];
st->top--;
}
return x;
}
void enqueue(struct stack s1,int x){
push(&s1,x);
}
int dequeue(struct stack s1,struct stack s2){
int x=-1;
if(isempty(&s2)){
if(isempty(&s1)){
printf("QUEUE IS EMPTY!!\n");
return x;
}
else{
while(!isempty(&s1)){
push(&s2,pop(&s1));
}
}
}
return pop(&s2);
}
void display(struct stack st){
int i;
for(i=0;i<=st.top;i++){
printf("%d",st.s[i]);
}
}
int main(){
int n,choice;
struct stack s1,s2;
printf("ENTER SIZE OF QUEUE:");
scanf("%d",&n);
s1.size=n;
s2.size=n;
s1.top=-1;
s2.top=-1;
s1.s=(int *)malloc(s1.size*sizeof(int));
s2.s=(int *)malloc(s2.size*sizeof(int));
while(1){
printf("1.ENQUEUE\n");
printf("2.DEQUEUE\n");
printf("3.DISPLAY\n");
printf("4.EXIT\n");
printf("ENTER YOUR CHOICE:");
scanf("%d",&choice);
switch(choice){
case(1):
int x;
printf("ENTER DATA:");
scanf("%d",&x);
enqueue(s1,x);
break;
case(2):
int m;
m=dequeue(s1,s2);
printf("ELEMENT DELETED IS:%d\n",m);
break;
case(3):
display(s2);
break;
case(4):
exit(0);
}
}
return 0;
}
What is the error? I think there might be an issue with passing the values to the function.
The main issue is that the enqueue and dequeue don't take pointers as arguments, but struct stack. This means the function gets a copy of the given struct, and that the pointer you pass to push and pop (like &s1) is pointing to that local structure, not to the one in main. By consequence any update to the top member of that stack will not be seen by the caller.
I would suggest to:
Consistently pass pointers to struct typed arguments. This was well done for the push and pop functions, and there is no reason why it should not be done the same way for enqueue and dequeue functions.
Define a struct queue so that you abstract a bit that there are two stacks involved and don't have to pass both of them as argument to dequeue.
Create separate functions for:
creating a new stack
displaying a stack
creating a new queue
displaying a queue
checking if a queue is empty
Here is how your code would then look:
#include <stdio.h>
#include <stdlib.h>
struct stack {
int top;
int size;
int *s;
};
struct stack* newstack(int size) {
struct stack *s = malloc(sizeof(struct stack));
s->size = size;
s->s = malloc(size*sizeof(int));
s->top = -1;
return s;
}
int isfull(struct stack *st) {
return st->top == st->size - 1;
}
int isempty(struct stack *st) {
return st->top == -1;
}
void push(struct stack *st, int x) {
if (isfull(st)){
printf("Full!\n");
} else {
st->top++;
st->s[st->top] = x;
}
}
int pop(struct stack *st) {
int x = -1;
if (isempty(st)){
printf("Empty!\n");
} else {
x = st->s[st->top];
st->top--;
}
return x;
}
void displaystack(struct stack *st) {
for(int i = 0; i <= st->top; i++) {
printf("%d ", st->s[i]);
}
}
struct queue {
struct stack *s1;
struct stack *s2;
};
struct queue* newqueue(int size) {
struct queue *q = malloc(sizeof(struct queue));
q->s1 = newstack(size);
q->s2 = newstack(size);
return q;
}
int isemptyqueue(struct queue *q) {
return isempty(q->s1) && isempty(q->s2);
}
void enqueue(struct queue *q, int x) {
push(q->s1, x);
}
int dequeue(struct queue *q) {
int x = -1;
if (isemptyqueue(q)) {
printf("Queue is empty!\n");
return -1;
}
if (isempty(q->s2)) {
while (!isempty(q->s1)) {
push(q->s2, pop(q->s1));
}
}
return pop(q->s2);
}
void displayqueue(struct queue *q) {
displaystack(q->s1);
printf("| ");
displaystack(q->s2);
printf("\n");
}
int main() {
int n, choice, x, m;
printf("Enter the size of the queue: ");
scanf("%d", &n);
struct queue *q = newqueue(n);
while (choice != 4) {
printf("1. Enqueue\n");
printf("2. Dequeue\n");
printf("3. Display\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter data: ");
scanf("%d", &x);
enqueue(q, x);
break;
case 2:
m = dequeue(q);
printf("The deleted element is: %d\n", m);
break;
case 3:
displayqueue(q);
break;
}
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#define type int
#define qsize 40
typedef struct {
int top;
type array[qsize];
}stack;
stack *initstack (){
stack *s=malloc(sizeof(stack)); s->top =0;
return s;
}
void push(stack *s,type x){
s->array[s->top++]=x;
}
type pop(stack *s){
return s->array[--s->top];
}
type isfulls(stack *s){
return s->top>=qsize;
}
type isemptys(stack *s){
return !s->top;
}
type peek(stack *s){
return s->array[s->top-1];
}
//----------------------------------------------------
typedef struct {
type head;
type tail;
int Qnoe;
type elements[qsize];
}queue;
queue *initqueue(){
queue *s=malloc(sizeof(queue));
s->Qnoe=0; s->head=0; s->tail=-1;
return s;
}
void enqueue(queue *s,type e){
s->elements[++s->tail%qsize]=e; s->Qnoe++;
}
type dequeue(queue *s){
type temp=s->elements[s->head++%qsize];
s->Qnoe--; return temp;
}
int isempty(queue *s){
return !s->Qnoe;
}
int isfull(queue *s){
return s->Qnoe==qsize;
}
type gethead(queue *s){
return s->elements[s->head];
}
type gettail(queue *s){
return s->elements[s->tail];
}
void display(queue *s){ ///used just to display the functions
queue *temp=initqueue();
while(!isempty(s)){
type x=dequeue(s);
printf("%d ",x);
enqueue(temp,x);
}
printf("\n\n");
while(!isempty(temp)) enqueue(s,dequeue(temp));
}
int maxvalue(queue *s){
type max,head;
queue *temp=initqueue();if(!isempty(s)) {enqueue(temp,dequeue(s)); max=gethead(s);}
while (!isempty(s)){
head=gethead(s);
if(max<head) max=head;
enqueue(temp,dequeue(s));
}
while(!isempty(temp)) enqueue(s,dequeue(temp));
return max;
}
void swap(queue *s){
type head=gethead(s),tail=gettail(s);int i=0,j=1;
queue *temp=initqueue();
while(!isempty(s)) {i++; enqueue(temp,dequeue(s));}
dequeue(temp); enqueue(s,tail); ;
while(!isempty(temp)){
if(j++==i-1) {enqueue(s,head); break;}
else {enqueue(s,dequeue(temp));}
} free(temp);
}
void insert(queue *s,type e,int index){
queue *temp=initqueue(); int i=1;
while(!isempty(s)){
if(index==i++) {enqueue(temp,e); dequeue(s);}
else enqueue(temp,dequeue(s));
}
while(!isempty(temp)) enqueue(s,dequeue(temp));
}
void revercOdd(queue *s){
queue *tempq=initqueue();
stack *temps=initstack();
while(!isempty(s)){
if(gethead(s)%2){push(temps,dequeue(s)); enqueue(tempq,1);}
else enqueue(tempq,dequeue(s));
}
while(!isempty(tempq)){
if(gethead(tempq)==1){enqueue(s,pop(temps)); dequeue(tempq);}
else enqueue(s,dequeue(tempq));
}
}
int main()
{
queue *s=initqueue();
enqueue(s,5);
enqueue(s,8);
enqueue(s,3);
enqueue(s,2);
enqueue(s,1);
enqueue(s,0);
enqueue(s,112);
printf("the queue: \n");
display(s);
printf("max value in queue : %d\n\n",maxvalue(s));
swap(s);
printf("the queue after swapping the head and tail: \n");
display(s);
printf("\nthe queue: \n");
display(s);
type e,index;
printf("enter he value of the elemnt and the index: ");
scanf("%d%d",&e,&index);
insert(s,e,index);
printf("the queue after inserting %d in the index %d: \n",e,index);
display(s);
printf("\nthe queue: \n");
display(s);
revercOdd(s);
printf("the queue after reversing odd number only: \n");
display(s);
return 0;
}
I made 4 functions 1 to get the max element in the queue 1 to get swap the head and tail of the queue 1 to take an element and index and put it in that index in the queue and 1 to reverse odd numbers only in a queue using queue and stack and a function to display the element of a given queue to check results and in the main, I check the outputs of every function work on its own, but when I use them together the last display shows the garbage element in the queue:
I'm implementing a stack using two queues. I'm quite familiar with the algorithm, and have prepared the following code where the push operation is costly:
#include <stdio.h>
#include <stdlib.h>
struct queue_struct{
int ele;
struct queue_struct *next;
};
struct Queue{
struct queue_struct *front, *rear;
};
struct Stack{
struct Queue *q1, *q2;
};
void enqueue(struct Queue *q, int x){
struct queue_struct *temp=malloc(sizeof(*temp));
temp->ele=x;
temp->next=NULL;
if(q->rear!=NULL){
q->rear->next=temp;
}
q->rear=temp;
if(q->front==NULL)
q->front=temp;
//printf("The item %d has been enqueued into the queue\n", x);
}
void dequeue(struct Queue *q){
struct queue_struct *temp=q->front;
if(temp==NULL){
//printf("The queue is already empty. No more elements can be removed!\n");
return;
}
printf("The item %d has been popped from the stack\n", temp->ele);
q->front=temp->next;
if(q->rear==temp)
q->rear=NULL;
free(temp);
}
void push(struct Stack *s, int x){
enqueue(s->q2, x);
while(!(s->q1->front==NULL)){
enqueue(s->q2, s->q1->front->ele);
dequeue(s->q1);
}
struct Queue *q=s->q1;
s->q1=s->q2;
s->q2=q;
printf("The item %d has been pushed into the stack\n", x);
}
void pop(struct Stack *s){
if(s->q1->front==NULL){
printf("The stack is already empty. No more elements can be removed!\n");
return;
}
dequeue(s->q1);
}
void display(struct Stack *s){
struct queue_struct *temp=s->q1->front;
printf("The contents of the queue are:\n");
if(temp==NULL){
printf("Nothing to be shown, the queue is empty.\n");
return;
}
for(int i=0;temp!=NULL;temp=temp->next){
if(i){
printf(" ------ \n");
}
printf("| %d |\n", temp->ele);
i=1;
}
}
int main()
{
int choice, element;
struct Stack *s=malloc(sizeof(*s));
s->q1->front=s->q1->rear=NULL;
s->q2->front=s->q2->rear=NULL;
printf("LET'S START WITH AN EMPTY STACK\n\n");
while(1){
printf("MENU\n");
printf("----\n");
printf("\t1. Push an element\n");
printf("\t2. Pop an element\n");
printf("\t3. Display stack\n");
printf("\t4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice){
case 1: printf("Enter the element to be pushed: ");
scanf("%d", &element);
push(s, element);
break;
case 2: pop(s);
break;
case 3: display(s);
break;
case 4: printf("Program terminated successfully!");
return 0;
default: printf("Invalid input");
}
}
}
However, I'm getting a Segmentation fault for the line(s): s->q1->front=s->q1->rear=NULL;. I'm not really sure why it is happening and how to fix it. Any help is appreciated.
You invoked undefined behavior by using values in buffer allocated via malloc() and not initialized.
You have to initialize s->q1 and s->q2 before dereferencing them.
struct Stack *s=malloc(sizeof(*s));
if (s == NULL) return 1; /* check if allocation succeeded */
s->q1 = malloc(sizeof(*s->q1)); /* allocate for s->q1 */
s->q2 = malloc(sizeof(*s->q2)); /* allocate for s->q2 */
if (s->q1 == NULL || s->q2 == NULL) return 1; /* check if allocations succeeded */
s->q1->front=s->q1->rear=NULL;
s->q2->front=s->q2->rear=NULL;
I'm having issues implementing a FIFO queue in C that acts as a circular buffer.. I have only managed to write an enqueue method so far and I'm having problems with the output. It doesn't display what it should and I'm not sure where the problem lies.
The code is as follows:
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 *init_Queue(){
struct queue * q = malloc(sizeof (q));
q->count=0;
q->front=q->array;
q->rear=q->array;
return q;
}
int isFull(struct queue *q){
if(q->count==30){
printf("\n Buffer is full!");
return 1;
}
return 0;
}
int isEmpty(struct queue *q){
if(q->count==0){
printf("\n Buffer 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;
printf("\n %d",k);
q->array[k]=i;
q->rear+1;
printf("\n Enqueue success!");
q->count++;
return 1;
}
int main(int argc, char**argv)
{
int i=10;
int k=12;
struct queue *q=init_Queue();
enqueue(q,i);
int j= q->count;
printf("\n %d",j);
printf("\n %d",q->array[0]);
printf("\n %d",q->rear);
enqueue(q,k);
int z= q->count;
printf("\n %d", z);
printf("\n %d", q->array[1]);
printf("\n %d",q->rear);
free(q);
}
The output is as follows:
Buffer is empty!
1
Enqueue success!
1 <<**value of count**
0 << **value stored in array[0]...it should say 10**
1070400 << **q->rear...should be pointing to 10?**
1
Enqueue success!
2 **<<value of count**
12 **<<value stored in array[1]**
1070400 **<< q-rear...should be incremented and pointing to 12?**
Program ended with exit code: 0
q->rear+1;
This do nothing
if(isEmpty(q)){
q->front+1;
}
Why ?
My proposition:
int enqueue(struct queue * q,int i){
if(isFull(q)){
return 0;
}
if(!isEmpty(q)){
q->rear++;
}
*(q->rear) = i;
printf("\n Enqueue success!");
q->count++;
return 1;
}
remark prefer return 0 on success and another on error like -1
Here is my program which creates a link list and also reverses it.
#include<stdio.h>
#include<stdlib.h>
struct node {
int data;
struct node *next;
};
struct node *list=NULL;
struct node *root=NULL;
static int count=0;
struct node *create_node(int);//function to create node
void travel_list(void);
void create_list(int);
void reverse_list(void);
int main()
{
int i, j, choice;
printf("Enter a number this will be root of tree\n");
scanf("%d", &i);
create_list(i);
printf("Enter 1 to enter more numbers \n 0 to quit\n");
scanf("%d", &choice);
while (choice!=0){
printf("Enter a no for link list\n");
scanf("%d",&i);
// printf("going to create list in while\n");
create_list(i);
travel_list();
printf("Enter 1 to enter more numbers \n 0 to quit\n");
scanf("%d", &choice);
}
printf("reversing list\n");
reverse_list();
travel_list();
}
// end of function main
void create_list (int data)
{
struct node *t1,*t2;
//printf("in function create_list\n");
t1=create_node(data);
t2=list;
if( count!=0)
{
while(t2->next!=NULL)
{
t2=t2->next;
}
t2->next=t1;
count++;
}
else
{
root=t1;
list=t1;
count++;
}
}
struct node *create_node(int data)
{
struct node *temp;
temp = (struct node *)malloc(sizeof(struct node));
temp->data=data;
temp->next=NULL;
// printf("create node temp->data=%d\n",temp->data);
// printf("the adress of node created %p\n",temp);
return temp;
}
void travel_list(void )
{
struct node *t1;
t1=list;
printf("in travel list\n");
while(t1!=NULL)
{
printf("%d-->",t1->data);
t1=t1->next;
}
printf("\n");
}
void reverse_list(void)
{
struct node *t1,*t2,*t3;
t1=list;
t2=list->next;
t3=list->next->next;
int reverse=0;
if(reverse==0)
{
t1->next=NULL;
t2->next=t1;
t1=t2;
t2=t3;
t3=t3->next;
reverse++;
}
while(t3!=NULL)
{
t2->next=t1;
t1=t2;
t2=t3;
list=t1;
travel_list();
t3=t3->next;
}
t2->next=t1;
list=t2;
}
Above is a fully working code.
I want to know if there can be further enhancement to the above code?
Make your indentation and whitespace usage consistent
Use meaningful identifiers rather than t1, t2 and t3
Make the data member a generic type, for example void * rather than int.
Don't use global variables, pass struct node * pointers to your functions.