注册 登录
编程论坛 数据结构与算法

编译时出现这个错误“功能':不兼容的类型”红色的那行,高手帮忙看下

bccnjj 发布于 2012-07-24 15:44, 569 次点击

#include "stdio.h"
#include "malloc.h"
#include "stdlib.h"
typedef struct Node
{
    int date;
    struct Node *pNext;
}NODE, *PNODE;
typedef struct Stack
{
    PNODE pTop;
    PNODE pBottom;
}STACK, *PSTACK;//PSTACK等价于struct Stack
void init_Stack(STACK pS);
int main(void)
{
    STACK S;//STACK等价于struct Stack;
    init_Stack(&S);
//    pushStack(&S, 1);
//    pushStack(&S, 2);
//    traverse_Stack(&S);
    return 0;
}
void init_Stack(STACK pS)
{
    pS->pTop = (PNODE)malloc(sizeof(NODE));
    if (NULL == pS->pTop)
    {
        printf("分配失败!\n");
        exit(-1);
    }
    else
    {
        pS->pBottom = pS->pTop;
        pS->pTop->pNext = NULL;
    }
}
4 回复
#2
silent_world2012-07-24 17:26
类型不一致
一个是结构体,一个是指针
#3
westfall9992012-07-24 19:12
把void init_Stack(STACK pS);改成void init_Stack(STACK *pS);就成了。
或者改成void init_Stack(PSTACK pS);
下面实现函数init_Stack的地方也要改一下。
#4
bccnjj2012-07-24 19:40
回复 3楼 westfall999
程序代码:
#include "stdio.h"
#include "malloc.h"
#include "stdlib.h"
#define bool int;
#define true 1;
#define false 0;
typedef struct Node
{
    int date;
    struct Node *pNext;
}NODE, *PNODE;
typedef struct Stack
{
    PNODE pTop;
    PNODE pBottom;

}STACK, *PSTACK;

void init_Stack(PSTACK);
void push(PSTACK pS, int Val);
void traverse(PSTACK);
bool pop(PSTACK, int *);

int main()
{
    STACK S;
    //int val;
    init_Stack(&S);
    push(&S, 1);
    push(&S, 2);
    push(&S, 3);
    push(&S, 4);
    push(&S, 5);

    traverse(&S);
    return 0;
}

void init_Stack(PSTACK pS)
{
    pS->pTop = (PNODE)malloc(sizeof(NODE));
    if (NULL == pS->pTop)
    {
        printf("动态内存分配失败!\n");
        exit(-1);
    }
    else
    {
        pS->pBottom = pS->pTop;
        pS->pBottom->pNext = NULL;
    }
}

void push(PSTACK pS, int val)
{
   
    PNODE pNew = (PNODE)malloc(sizeof(NODE));
    pNew->date = val;
    pNew->pNext = pS->pTop;
    pS->pTop = pNew;
    return;
}

void traverse(PSTACK pS)
{
    PNODE p = pS->pTop;
    while (p != pS->pBottom)
    {
        printf("%d ", p->date);
        p = p->pNext;
    }
    printf("\n");
    return;
}

bool empty(PSTACK pS)
{
    if (pS->pTop == pS->pBottom)
        return true;
    else
        return false;
}

bool pop(PSTACK pS, int *pVal)
{
    if ( empty(pS) )
    {
        return false;
    }
    else
    {
        PNODE r = pS->pTop;
        *pVal = r->date;
        pS->pTop = r->pNext;
        free(r);
        r = NULL;
        return true;
    }
}


//红色的那名在编译的时候提示warning C4091: ' ' : ignored on left of 'int ' when no variable is declared这个错误,我检查不知道哪里出错误,麻烦帮忙解决
#5
hao021719902012-08-14 11:20
太帅了!
1