堆栈实现将代码放在多个文件中

时间:2016-09-05 08:23:35

标签: c

我试图通过将所有函数写在一个单独的源文件中来实现堆栈。但是我收到很多错误,指出不兼容的指针类型。当我在主要包含函数时,这些错误不会显示出来C file.these是我的文件。我是新手。帮助我纠正它们。 谢谢。 我的主要代码是

#include "myfunctions.h"
int main()
{
    int operation,data;
    struct stack *One = (struct stack *)malloc(sizeof(struct stack));
    One->top = -1;
    printf("stack functionality \n");
    while(1)
    {
        if (isEmpty(One))
        {
            printf("enter 1 to push \n");
            scanf("%d",&operation);
        }
        else if (isFull(One))
        {
            printf("stack is full.enter 2 to pop or 3 to diplay \n");
            scanf("%d",&operation);
        }
        else
        {
            printf("enter 1 to push,2 to pop,3 to display 4 to exit \n");
            scanf("%d",&operation);
        }
        switch (operation)
        {
            case 1: printf("enter data to be pushed \n");
                scanf("%d",&data);
                push(One,data);
                break;
            case 2: printf("%d \n",pop(One));
                break;
            case 3: display(One);
                break;
            case 4:exit(0);
        }
    }   
}

stackfns代码

#include <myfunctions.h>
bool isEmpty(struct stack *b)
{
    if(b->top == -1) return true;
    else return false;
}
bool isFull(struct stack *b)
{
    if(b->top == 9) return true;
    else return false;
}
void push(struct stack *b,int data)
{
    b->a[++(b->top)] = data;
}
int pop(struct stack *b)
{
    return (b->a[(b->top)--]);
}
void display(struct stack *b)
{
    int i;
    for (i=0;i<10;i++)
        printf("%d ",b->a[i]);
    printf("\n");
}

头文件

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
extern bool isEmpty(struct stack *b);
extern bool isFull(struct stack *b);
extern void push(struct stack *b,int data);
extern int pop(struct stack *b);
extern void display(struct stack *b);
#define max_size 10
struct stack
{
    int a[max_size];
    int top;
};

1 个答案:

答案 0 :(得分:1)

在头文件中,您需要先对结构进行十分转换:

JSON.parse(JSON.stringify($scope.ContactList))

此外,不需要公开数据结构的内部实现。你可以这样做:

.h文件:

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

#define max_size 10
struct stack
{
    int a[max_size];
    int top;
};

extern bool isEmpty(struct stack *b);
extern bool isFull(struct stack *b);
extern void push(struct stack *b,int data);
extern int pop(struct stack *b);
extern void display(struct stack *b);

.c文件:

typedef struct xx_t xx_t;