【问题标题】:how to create a function that enqueues an entire structure and not just an integer如何创建一个将整个结构排入队列而不仅仅是整数的函数
【发布时间】:2018-12-08 22:11:01
【问题描述】:

编译后我会收到这些消息

[error] 变量或字段 'Enqueue' 声明为 void 49 14 C:\weblinky1.cpp [错误] 变量或字段“入队”声明为无效

49 20 C:\weblinky1.cpp [Error] no match for 'operator*'(操作数类型是 'documents' 和 'node')

49 35 C:\weblinky1.cpp [错误] 'operator*' 不匹配(操作数类型为'documents' 和'node')

49 45 C:\weblinky1.cpp [错误] 'struct' 之前的预期主表达式

50 14 C:\weblinky1.cpp [Error] 变量或字段“dequeue”声明为无效

50 20 C:\weblinky1.cpp [Error] no match for 'operator*'(操作数类型是 'documents' 和 'node')

51 14 C:\weblinky1.cpp [Error] 变量或字段 'getRear' 声明为 void

51 20 C:\weblinky1.cpp [Error] no match for 'operator*'(操作数类型是 'documents' 和 'node*')

52 15 C:/weblinky1.cpp [Error] 变量或字段 'getFront' 声明为 void

52 21 C:\weblinky1.cpp [Error] no match for 'operator*'(操作数类型是 'documents' 和 'node*')

C:\Users\marshalee\Desktop\DSLabTest1_1300054246\weblinky1.cpp 在函数'int main()'中:

60 11 C:\weblinky1.cpp [Error] no match for 'operator*'(操作数类型是 'documents' 和 'node*')

114 49 C:\weblinky1.cpp [错误] 'enqueue' 未在此范围内声明

122 39 C:\weblinky1.cpp [错误] 'dequeue' 未在此范围内声明

126 27 C:\weblinky1.cpp [错误] 'operator==' 不匹配(操作数类型为 'documents' 和 'long long int')

138 42 C:\weblinky1.cpp [错误] 'deletedocsize' 未在此范围内声明

147 48 C:\weblinky1.cpp [错误] 'searchfoldername' 未在此范围内声明

157 32 C:\weblinky1.cpp [错误] 'searchID' 未在此范围内声明

165 33 C:\weblinky1.cpp [错误] 'averagedocsize' 未在此范围内声明

173 29 C:\weblinky1.cpp [错误] 无法将 'isEmpty()' 从 'void' 转换为 'bool'

182 36 C:\weblinky1.cpp [错误] 'getRear' 未在此范围内声明

193 45 C:\weblinky1.cpp [错误] 'getFront' 未在此范围内声明

C:\weblinky1.cpp 在全局范围内:

221 19 C:\weblinky1.cpp [错误] 变量或字段“入队”声明为无效

下面是我的完整代码

    enter code here/**
 * Queue implementation using linked list in C.
 */

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>

#define CAPACITY 100    // Queue max capacity


/* Queue structure definition */
   struct documents 
{
     int id;
    char docname[50];
    char foldername[50];
    float docsize;
    struct document * next;
    struct document * previous;

};    



//Delacre document variable
struct documents Queue;

//Create node for linked list
struct node
{
    struct documents Queue;
    struct node* previous;
    struct node* next;
};

// Creating an empty linked list
struct node * rear = NULL;
struct node * front = NULL;


/* Queue size */
unsigned int size = 0;


void enqueue(Queue ** rear, Queue ** front, struct documents Queue);
void dequeue(Queue ** front);
void getRear(Queue * rear);
void getFront(Queue * front);
void isEmpty();
void isFull();


int main()
{
    int ch, data;
    Queue *rear, *front;
     struct documents Queue;
    int id;
    float docsize;
    char* docname;
    char* foldername;

    rear  = NULL;
    front = NULL;

    /* Run indefinitely until user manually terminates */
    while (1)
    {
        /* Queue menu */
        printf("--------------------------------------------\n");
        printf("  QUEUE LINKED LIST IMPLEMENTATION PROGRAM  \n");
        printf("--------------------------------------------\n");
        printf("1. Enqueue\n");
        printf("2. Dequeue\n");
        printf("3. Delete by Size of Document\n");
        printf("4. Display Documents based on Folder Name\n");
        printf("5. Enter ID to Display Index where Documents was 
          found\n");
        printf("6. Display Document Size Average\n");
        printf("7. Size of the Queue\n");
        printf("8. Get Rear\n");
        printf("9. Get Front\n");
        printf("0. Exit\n");
        printf("--------------------------------------------\n");
        printf("Select an option: ");

        scanf("%d", &ch);


        /* Menu control switch */
        switch (ch)
        {
            case 1:

                 printf("\nEnter Document ID#: ");
                   scanf("%d", &Queue.id);
                    Queue.id = id;
                    printf("Enter Document size#: ");
                    scanf("%f", &Queue.docsize);
                     Queue.docsize = docsize;
                     printf("Enter Document name: ");
                      scanf("%s", &Queue.docname);
                       strcpy(Queue.docname, docname);
                       printf("Enter folder name: ");
                        scanf("%s", foldername);
                         strcpy(Queue.foldername, foldername);


                // Enqueue function returns 1 on success
                // otherwise 0
                if (enqueue(&rear, &front, Queue))
                    printf("Document added to queue.");
                else
                    printf("Queue is full.");

                break;

            case 2:
                Queue = dequeue(&front);

                // on success dequeue returns element removed

                if (Queue == NULL)
                    printf("Queue is empty.");
                else
                    printf("Queue => %d", Queue.front);

                break;

           //Delete by Size of Document
           case 3:

                 printf("Enter Document size: ");
                    scanf("%f", &docsize);
                    deletedocsize(docsize);

                break;

        //Display Documents based on Folder Name
        case 4:

                 printf("Enter Document Folder Name: ");
                     scanf("%s", foldername);
                    searchfoldername(foldername); 

                break;


        //Enter ID to Display Index where Documents was found
        case 5:

                 printf("Enter Document ID#: ");
                     scanf("%f", &id);
                    searchID(id); 

                break;


        //Display Document Size Average
        case 6:

                 averagedocsize(); 

                break;

            case 7: 

                // isEmpty() function returns 1 if queue is emtpy 
                // otherwise returns 0
                if (isEmpty())
                    printf("Queue is empty.");
                else 
                    printf("Queue size => %d", size);

                break;

            //get rear of document queue
            case 8: 
                data = getRear(rear);

                if (Queue == Queue.rear)
                    printf("Queue is empty.");
                else 
                    printf("Rear => %d", Queue.rear);

                break;

            case 9: 

                Queue = getFront(front);

                if (Queue == NULL)
                    printf("Queue is empty.");
                else 
                    printf("Front => %d", Queue.front);

                break;

            case 0:
                printf("Exiting from application.\n");
                exit(0);

            default:
                printf("Invalid choice, please input number between (0- 
       5).");
                break;
        }

        printf("\n\n");
    }
}



/**
 * Enqueues/Insert an element at the rear of a queue.
 * Function returns 1 on success otherwise returns 0.
 */
void enqueue(Queue *q)
{
     Queue * temp= NULL;

    // Check queue out of capacity error
    if (isFull())
    {
        return 0;
    }

    // Create a new node of queue type
    temp = (Queue *) malloc (sizeof(Queue));

    // Assign data to new node
    temp->docs= en;

    // Initially new node does not point anything
    temp->next = NULL;
    temp->previous = NULL;

    // Link new node with existing last node 
    if ( (*rear) )
    {
        (*rear)->next = temp;
    }


    // Make sure newly created node is at rear
    *rear = temp;

    // Link first node to front if its NULL
    if ( !( *front) )
    {
        *front = *rear;
    }

    // Increment quque size
    size++;

    return 1;
}


/**
 * Gets, element at rear of the queue. It returns the element
 * at rear of the queue on success otherwise return as 
 * error code.
 */
int getRear(Queue * rear)
{
    //  if queue is empty otherwise rear.
    return (isEmpty())
            ? NULL
            : rear->Queue.rear;
}


/**
 * Gets, element at front of the queue. It returns the element
 * at front of the queue on success otherwise return  as 
 * error code.
 */
int getFront(Queue * front)
{
    // if queue is empty otherwise front.
    return (isEmpty())
            ? NULL
            : front->Queue.front;
}


/**
 * Checks, if queue is empty or not.
 */
int isEmpty()
{
    return (size <= 0);
}


/**
 * Checks, if queue is within the maximum queue capacity.
 */
int isFull()
{
    return (size > CAPACITY);
}

// deleting any node based on position
void Delete(int n)
{
    struct documents temp1 =front;

      if(n == 1){
        front= temp1->next;
        free(temp1);
        return;
      }
      int i for(i =0; i<n-2; i++)
        temp1 = temp1->next;
      struct node* temp2 = temp1->next;
        temp1->next = temp2 -> next;
        free(temp2);
}

 //or using this method
void Dequeue(int n)
{
    struct documents temp1;

    if(Front == NULL)
    {
        printf("Queue is empty.\n");
    }
    else
    {
       int currentIndex = 0;
       struct documents *temp1 = (struct documents*)malloc(sizeof(struct 
        documents));
        bool traversing = true;


        tem1 = Front;
        temp1 = temp1->Queue;


        while(temp1 !=NULL) {
            if(temp1.id == n)
                {
                    temp1->previous->next=temp1->next;
                    free(temp1);
                printf("Item Deleted", currentIndex);
                return;
            }

            currentIndex++;

            temp1 = temp1->next;
            if (temp1 == NULL) {
                temp1 = temp1->previous;
            }
            temp1 = temp1->Queue;
        }
    }
}

//search by foldername
void searchfoldername(char foldername[])
{
    struct documents Queue;

    if(Front == NULL)
    {
        printf("Queue is empty.\n");
    }
    else
    {
    struct documents *tempp = (struct documents*)malloc(sizeof(struct 
     documents));
        bool traversing = true;
        int currIndex = 0;

        tempp = Front;
        tempy = tempp->Queue;
        Front = Front->next;

        while(traversing) {
            if(strcmp(tempy->foldername, foldername) == 0))
            {
                printf(" ID: %d\n", tempy->id);
                printf("Document Name: %s\n", tempy->docname);
                printf("Folder Name: %s\n", tempy->foldername);
                printf("Document Size: %d\n", tempy->docsize);
                printf("Document positioned at index: %d", currIndex);
                return tempy.foldername;
            }

            currIndex++;

            tempp = tempp->next;
            if (tempp == NULL) {
                tempp = tempp->previous;
            }
            tempy = tempp->Queue;
        }
    }
}

//delete document size
Void deletedocsize(int docsize)
{
    struct documents Queue;

    if(Front == NULL)
    {
        printf("Queue is empty.\n");
    }
    else
    {
        struct documents *tempp = (struct documents*)malloc(sizeof(struct 
          documents));
     bool traversE = true;
        int currIndex = 0;

        tempp = Front;
        tempy = tempp->Queue;
        front = front->next;
            while(traversE) {
            if(strcmp(tempy->docsize, docsize) <= Queue.docsize))

            {
                free(tempp);
                    return;
            }

            currIndex++;
              tempp = tempp->next;
            if (tempp == NULL) {
                tempp = tempp->previous;
            }
            tempy = tempp->Queue;
        }
    }
}
};

//Enter ID to Display Index where Documents was found
void searchID(int id)
{
    struct documents Queue;

    if(Front == NULL)
    {
        printf("Queue is empty.\n");
    }
    else
    {
        struct documents *tempp = (struct documents*)malloc(sizeof(struct 
         documents));
        bool traversing = true;
        int currIndex = 0;

        tempp = Front;
        tempy = tempp->Queue;
        Front = Front->next;

        while(traversing) {
            if(strcmp(tempy->id, id) == Queue.id))
            {
                printf("Document positioned at index: %d", currIndex);
                return tempy.id;
            }

            currIndex++;

            tempp = tempp->next;
            if (tempp == NULL) {
                tempp = tempp->previous;
            }
            tempy = tempp->Queue;
        }
    }
}

//find and display the Average of all document size
void averagedocsize()
{   
    struct documents Queue;

    if (front == NULL) 
    {
        printf("\n ooops List is Empty\n");
    }
    else
    {
        struct documents *tempp = (struct documents*)malloc(sizeof(struct 
           documents));
        int count = 0;
        tempp = front;
        Queue = tempp->Queue;
        while (tempp != NULL) 
        {
            if(strcmp(Queue.docsize, docsize) == 0) {
                sum=0;
                while (count<=rearindex)
                {
                    sum = sum + docsize;
                    average =rearindex;
                }

                count++;
            }
            tempp = tempp->next;
            if (tempp == NULL) {
                printf("Average Document Size is : %d\n\n", average);
                break;
            }
            Queue = tempp->;
        }
        free(tempp);
    }
}

【问题讨论】:

  • Queuenot 声明为类型,因此Queue ** [或Queue *] 无效。而且,您的 typedef 声明了 struct 名称 Book不是 Book 的类型(即它是合法的,但格式有点错误)。试试:typedef struct Book { what_you_already_have } Book; 然后,做void Enqueue(Book **rear, Book **front, Book *en)。当您执行struct Book Queue; 时,Queue 仅仅是struct Book 类型的全局实例——它确实 声明了Queuetype
  • 当我编译这段代码时,我得到了一堆错误,但没有一个是你说的错误。请阅读并关注instructions for producing a minimal, complete, verifiable example program
  • 请不要大喊大叫。
  • 虽然代码肯定不正确,但很难看出它将如何产生[error] variable or field 'Enqueue' declared void - 似乎这不是产生错误的代码。仔细复制并粘贴您的代码和错误 - 不要删除内容 - 例如错误消息将包含行号等信息 - 不要删除它,并确保您发布的代码包含该行。此外,您的标题与正文无关 - 标题更广泛(太宽泛),而正文非常具体(关于错误消息)。
  • 从风格上看,Queue这个名字太笼统了,不能专门指书的队列,而且类型不是队列,而是队列的一个元素。 typedef Book* BookQueue ; 可能更有意义,那么Enqueue 参数将具有BookQueue* 类型。

标签: c linked-list


【解决方案1】:

您定义了一个变量 Queue 而不是一个类型,而typedef 是不完整的:

typedef struct Book
{
    int bID;
    char bName;
    char FolderName;
    struct Book * next
    struct Book * next
} Queue ;

void Enqueue(Queue ** rear, Queue ** front, Queue en) ;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多