c Language Fibonacci sequence is queued (single link list)

Asked 1 years ago, Updated 1 years ago, 87 views

#include < stdio.h>
#include < stdlib.h>
#define Q_SIZE 100

typeef element; //int type is defined as the data type of the queue element

typedef struct{

element queue[Q_SIZE];  
    int front, rear;

    } } QueueType;

QueueType *createQueue()  
   {

QueueType *Q;

Q = (QueueType *)malloc(sizeof(QueueType));

    Q->front=-1; // front initial value setting
    Q->rear=-1; // rear initial value setting
    return Q;
   }

int isEmpty(QueueType *Q)   
   {

    if (Q->front == Q->rear) {
       printf("\n Queue is empty! \n");
       return 1;
    }
    else return 0;
   }

int isFull(QueueType *Q)     
   {

if (Q->rear == Q_SIZE-1) 
{

 printf("\n Queue is full! \n");

     return 1;
    }
    else return 0;
   }

void enQueue (QueueType *Q, element item) // operation to insert an element into the rear of the queue
   {

if(isFull(Q)) exit(1);

    else
    {
       Q->rear++;
       Q->queue[Q->rear] = item;
    }
   }

element deQueue (QueueType *Q) // Operation to delete and return elements from the front of the queue
{

    if (isEmpty(Q)) exit(1);

    else {
       Q->front++;
       return Q->queue[Q->front];
    }
}

void del(QueueType *Q) // Operation to delete an element from the front of the queue

  {

if (isEmpty(Q)) exit(1);

    else Q->front++;
   }

element peek(QueueType *Q) 
                  // An operation that searches for and returns the element in front of the queue
   {

if (isEmpty(Q)) exit(1);

    else return Q->queue[Q->front+1];
  }

void printQ (QueueType *Q) // Operation to output the contents of the queue

{
    int i;

    printf(" Queue : [");
    for(i=Q->front+1; i<=Q->rear; i++)
    {
        printf("%d", Q->queue[i]);
        printf("|");
    }
    printf(" ] \n");
   }

void main(void)
   {

    QueueType *Q1 = createQueue();
    element data1, data2,data3;
    int i,change;


    printf("Please enter only integer numbers greater than 0" \n");
    printf ("Enter the first digit: ");
    scanf("%d",&data1);
    printf ("Enter the second digit: ");
    scanf("%d",&data2);

    if(data2<data1)
    {
        change = data2;
        data2 = data1;
        data1 = change;
    }

    enQueue(Q1,data1); //Insert first and second values in queue
    enQueue(Q1,data2);
    printQ(Q1);

    for(i=0; i<=10; i++)
    {
        data1 = *Q1 -> &queue[i]; // Error Location
        data2 = *Q1 -> &queue[i+1]; // Error Location

        data3 = data1 + data2;
        enQueue(Q1,data3);
    }
    printQ(Q1);

    getchar();
   }

Take two numbers, put them in the queue one after another, and then add two numbers like the Fibonacci sequence You want to create a program that queues the results again after adding them. The location of the error is data1 = *Q1-> &queue[i]; this part of the main I'm trying to put the element of the queue array of the structure pointed to by pointer Q1 into data1, but the & operator fails When I subtract the & operator, I get an error that the operand of *Q1 is not a pointer. Please tell me the solution.

c queue

2022-09-22 20:33

1 Answers

Because Q1 is QueueType *, it is not *Q1-> &queue[i];

You have to use it with


2022-09-22 20:33

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.