Получение ошибки «разыменование указателя на неполный тип« struct node »» - PullRequest
0 голосов
/ 03 мая 2019

Я делаю программу push / pop, используя стеки и связанные списки.Встречаем две ошибки в функции pushpancake ().Один из них - «разыменование указателя на неполный тип« struct node »», а другой говорит, что мой «top» не объявлен.Некоторая помощь будет принята с благодарностью !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

int main ()
{
struct node {
    int data;
    struct node* next;
    };
struct node* top=NULL;

int choice = 0;
printMenu ();


do
{


    printf ("\nEnter a choice. ");  
    scanf ("%d", &choice);


    switch (choice)
    {
        case 0:
        {
            exitProgram();
            break;
        }
        case 1:
        {
            clrScreen();
            break;
        }
        case 3:
        {
            pushpancake();
            break;
        }
         default:
        printf("\nInvalid Choice!\n");
    }
}
    while (choice != 0); 
    return 0;
}


void printMenu ()
{
    printf ("0) Exit program.\n\n");
    printf ("1) Clear Screen.\n\n");
    printf ("2) Display the pancake stack.\n\n");
    printf ("3) Push a pancake.\n\n");
    printf ("4) Pop a pancake.\n\n");

}

void pushpancake()
{
    char pancake[30];
    int calories;
    printf("Enter pancake name and its calories:");
    scanf("%s %d,", pancake, &calories);
    printf("The pancake has been added at the top of the stack.");

    struct node *temp=(struct node*)malloc(sizeof(struct node));
    strcpy(temp->pancake,pancake);
    temp->data=calories;
    temp->next=top;
    top=temp;
}

1 Ответ

0 голосов
/ 03 мая 2019

Это мой ответ, надеюсь, он вам пригодится.

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

#define OK 1
#define ERROR 0

// infinite stack struct
typedef struct node {
    int data;
    struct node* next;
    char name[100];
}StackNode,*LinkStackPtr;
typedef struct
{
        LinkStackPtr top;
        int count;
}LinkStack;


void printMenu ();
void clrScreen(); 
void pushpancake(LinkStack *S);
int popancake(LinkStack *S);   
int PrintStackTop(LinkStack S);
int StackLength(LinkStack S);
int ClearStack(LinkStack *S);

// get length of stack
int StackLength(LinkStack S)
{ 
        return S.count;
}

// judge stack if or not empty
int StackEmpty(LinkStack S)
{ 
        if (S.count==0)
                return OK;
        else
                return ERROR;
}

int look(char *strP,int c)
{
        printf("%s ",strP);
        printf("%d     ",c);
        return OK;
}

// print stack 
int PrintStackTop(LinkStack S)
{
        LinkStackPtr p;
        p=S.top;
        if(S.count == 0){
            printf("stack is empty, can't print\n");
            return ERROR;
        }
        else{
            printf("The count of stack value is: %d\n",StackLength(S));
            printf("\nThe value of stack is:\n");
            while(p && S.count){
                look(p->name,p->data);
                p=p->next;
                S.count--;
            }
            printf("\n");
            return OK;
        }
}

// pop a number from stack
int popancake(LinkStack *S)   
{
    LinkStackPtr p;
    if(StackEmpty(*S))
        return ERROR;
    p = S->top;
    S->top = S->top->next;
    free(p);
    S->count--;
    return OK;
}

//push a number to stack
void pushpancake(LinkStack *S)
{
    char pancake[100];
    int calories;
    printf("Enter pancake name and its calories : (format : apple 10) ");
    scanf("%s %d,", pancake, &calories);

    struct node *temp=(struct node*)malloc(sizeof(struct node));
    strcpy(temp->name,pancake);
    temp->data=calories;

    temp->next=S->top;
    S->top=temp;
    S->count++;

    printf("The pancake has been added at the top of the stack.");
}

// init stack
int InitStack(LinkStack *S)
{ 
        S->top = (LinkStackPtr)malloc(sizeof(StackNode));
        if(!S->top)
                return ERROR;
        S->top=NULL;
        S->count=0;
        return OK;
}

void clrScreen()
{
    system("clear");   // may be error at windows, if yes, commment it.
}

void printMenu ()
{
    printf ("0) Exit program.\n\n");
    printf ("1) Clear Screen.\n\n");
    printf ("2) Display the pancake stack.\n\n");
    printf ("3) Push a pancake.\n\n");
    printf ("4) Pop a pancake.\n\n");

}
int ClearStack(LinkStack *S)
{ 
        LinkStackPtr p,q;
        p=S->top;
        while(p)
        {  
                q=p;
                p=p->next;
                free(q);
        } 
        S->count=0;
        return OK;
}



int main ()
{

    LinkStack s;
    int choice = 0;
    int booL;
    printMenu ();
    InitStack(&s);  

    do
    {


        printf ("\nEnter a choice. (0-4) ");
        scanf ("%d", &choice);


        switch (choice)
        {
            case 0:
            {
                booL = ClearStack(&s);
                if(booL == OK){
                    printf("stack have been clear!\n");
                }
                PrintStackTop(s);
                printf("exit the program! bye bye\n");
                exit(0);   // exit the program
                break;
            }
            case 1:
            {
                clrScreen();
                break;
            }
            case 2:
            {
                PrintStackTop(s);
                break;
            }
            case 3:
            {
                pushpancake(&s);
                break;
            }
            case 4:
            {
                booL = popancake(&s);
                if(booL)
                    printf("delete top of stack by successful!");
                else
                    printf("delete top of stack by failed!");
                break;
            }
            default:
                printf("\nInvalid Choice!\n");
                break;
        }
    }
        while (choice != 0);   // if choice is 0, then exit the program
        return 0;
}

это вывод:

0) Exit program.

1) Clear Screen.

2) Display the pancake stack.

3) Push a pancake.

4) Pop a pancake.


Enter a choice. (0-4) 2
stack is empty, can't print

Enter a choice. (0-4) 3
Enter pancake name and its calories : (format : apple 10) aa 1
The pancake has been added at the top of the stack.
Enter a choice. (0-4) 2
The count of stack value is: 1

The value of stack is:
aa 1     

Enter a choice. (0-4) 3
Enter pancake name and its calories : (format : apple 10) bb 2
The pancake has been added at the top of the stack.
Enter a choice. (0-4) 2
The count of stack value is: 2

The value of stack is:
bb 2     aa 1     

Enter a choice. (0-4) 3
Enter pancake name and its calories : (format : apple 10) cc 3
The pancake has been added at the top of the stack.
Enter a choice. (0-4) 2
The count of stack value is: 3

The value of stack is:
cc 3     bb 2     aa 1     

Enter a choice. (0-4) 4
delete top of stack by successful!
Enter a choice. (0-4) 2
The count of stack value is: 2

The value of stack is:
bb 2     aa 1     

Enter a choice. (0-4) 0
stack have been clear!
stack is empty, can't print
exit the program! bye bye
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...