|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
去年初学C语言按着课本敲了一些代码,有头文件和源文件,其中源文件是CPP类型的,今天才发现,然后改回C类型头文件一直报错,求助怎么调。
VS用的是C++桌面开发(貌似没找到C语言的)
头文件.h
- #pragma once
- #define MaxSize 100 //顺序栈的初始分配空间大小
- typedef char ElemType;
- typedef struct {
- ElemType data[MaxSize]; //保存栈中元素,这里假设ElemType为char类型
- int top; //栈顶指针
- }SqStack;
- //初始化栈运算算法
- void InitStack(SqStack& st) { //st为引用型参数
- st.top = -1;
- }
- //销毁栈运算算法
- void DestroyStack(SqStack st) {
- }
- //进栈运算算法
- int Push(SqStack& st, ElemType x) {
- if (st.top == MaxSize - 1) //栈满上溢出返回0
- return 0;
- else {
- st.top++;
- st.data[st.top] = x;
- return 1; //成功进栈返回1
- }
- }
- //出栈运算算法
- int Pop(SqStack& st, ElemType& x) { //x为引用型参数
- if (st.top == -1) //栈空返回0
- return 0;
- else {
- x = st.data[st.top];
- st.top--;
- return 1; //成功栈返回1
- }
- }
- //取栈顶元素运算算法
- int GetTop(SqStack st, ElemType& x) { //x为引用型参数
- if (st.top == -1) //栈空返回0
- return 0;
- else {
- x = st.data[st.top];
- return 1; //成功取栈顶元素返回1
- }
- }
- //判断空栈运算算法
- int StackEmpty(SqStack st) {
- if (st.top == -1)
- return 1; //栈空返回1
- else
- return 0; //栈不空返回0
- }
复制代码
源文件.c
- #include<stdio.h>
- #include"test06.h"
- void main() {
- SqStack st; //定义一个顺序栈st
- ElemType e;
- printf("初始化栈st\n");
- InitStack(st);
- printf("栈%s\n", (StackEmpty(st) == 1 ? "空" : "不空"));
- printf("a进栈\n"); Push(st, 'a');
- printf("b进栈\n"); Push(st, 'b');
- printf("c进栈\n"); Push(st, 'c');
- printf("d进栈\n"); Push(st, 'd');
- printf("栈%s\n", (StackEmpty(st) == 1 ? "空" : "不空"));
- GetTop(st, e);
- printf("栈顶元素:%c\n", e);
- printf("出栈次序:");
- while (!StackEmpty(st)) { //栈不空循环
- Pop(st, e); //出栈元素e并输出
- printf("%c", e);
- }
- printf("\n");
- DestroyStack(st);
- }
复制代码
图片发不了。。。头文件类型显示是C++头文件
|
|