获取“向不完整类型'struct node'的引用指针”错误

时间:2019-05-02 23:37:51

标签: c

我正在使用堆栈和链接列表制作一个推/弹出程序。在函数pushpancake()中遇到两个错误。一种是“取消指向不完整类型'结构节点'的指针”,另一种是未声明我的“顶部”。非常感谢您的帮助!!!!!!!

Container2

1 个答案:

答案 0 :(得分:0)

这是我的答案,希望对您有用。

#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