如果是下文的代码段,就会显示IsFull和IsEmpty的L未初始化;
但如果在IsFull和IsEmpty里使用指针引用,仿佛也不太对?(当然我是菜鸡可能使用指针引用 也是对的)
还望各位大佬赐教!
#include"stdafx.h"
#include<iostream>
using namespace std;
const int MAX=100;
typedef struct LinearStack
{ int length;
int top;
int a[100];}linearstack;
linearstack *CreatStack()
{ linearstack *L;
L=new LinearStack();
if(L==NULL)
return L;
L->top=-1;
return L;}
bool IsEmpty()
{linearstack*L;
if(L->top==-1)
return false;
return true;}
bool IsFull()
{linearstack *L;
if( L->top==MAX)
{ cout<<"此栈已满"<<endl;
return false;}
cout<<"可以往此栈增添元素"<<endl;
return true;}
linearstack Push(linearstack *&L)
{ int x=0;
if (IsFull())
cout<<"此栈是空";
cout<<"依次输入各元素:"<<endl;
for(int i=0;i<L->length;i++)
{cin>>L->a[i];}
cout<<"请依次输入需要入栈的元素:"<<endl;
for(int i=0;i<L->length;i++)
cin>>L->a[i];
L->top++;
L->a[L->top]=x;
return *L;}
linearstack Pop(linearstack *&L)
{ int x=0;
if (IsEmpty())
cout<<"此栈是空栈"<<endl;
else
{ x=L->a[L->top];
L->top--;
cout<<"创建成功"<<endl;}
cout<<"依次输出各元素:"<<endl;
for(int i=0;i<L->length;i++)
{cout<<L->a[i];}
return *L;}
int main()
{linearstack* L=CreatStack();
Push(L);
Pop(L);
return 0;}
在函数IsFull和IsEmpty中有如下变量的声明linearstack *L;但是没有对它进行初始化,你把这个变量L定义成一个全局变量,然后在CreatStack中进行初始化,在IsFull和IsEmpty中直接调用
问题:指针不创建就使用,函数传参连类型名都传进去了。代码不可读乱的一塌糊涂。