栈的基本 *** 作的实现(c语言),高手速来!!

栈的基本 *** 作的实现(c语言),高手速来!!,第1张

/程序错误太多/ #include"stdioh"

#include"stdlibh"

#include"timeh"

#include"malloch"

#define

STACK_INIT_SIZE

10

//栈容量 typedef

struct

SqStack

{

int

top;

//栈顶当前指针

int

base;

//栈空间数组

}SqStack; void

InitStack(SqStack

&S);

//构造空栈S

int

Push(SqStack

&S,int

e);

//入栈(栈地址,入栈数据

返回0对,-1错

int

Pop(SqStack

&S);

//出栈

(栈地址)返回栈顶数据

int

StackLength(SqStack

S);

//返回站的元素个数,即求栈长

void

Print_S(SqStack

S);

//显示栈内数据 int

main()

{

SqStack

S;

int

i=0;

int

a,e;

InitStack(S);

srand((unsigned)time(NULL));

//srand((unsigned)time(NULL))以time函数值(当前时间)作为种子

printf("随机填充5个元素为:

");

while(

i<5)

{

a

=

rand()%100;

printf("%d

",

a);

Push(S,a);

i++;

}

Print_S(S);

printf("请输入要插入栈顶的元素:");

scanf("%d",&e);

Push(S,e);

Print_S(S);

printf("再d出的栈顶元素为:%d

\n",Pop(S));

printf("栈的长度为:%d

\n",StackLength(S));

Print_S(S);

return

0;

} void

InitStack(SqStack

&S)

//构造空栈S

{

Sbase

=

(int

)malloc(STACK_INIT_SIZE

sizeof(int));

//分配组数空间,长度STACK_INIT_SIZE

if

(Sbase==NULL)

{

printf("内存分配失败!\n");

return;

}

Stop=-1;

} int

Push(SqStack

&S,int

e)

{

if(Stop>=STACK_INIT_SIZE)

{

printf("栈空间已满,入栈失败!\n");

return

-1;

}

else

{

Sbase[++Stop]=e;

return

0;

}

} int

Pop(SqStack

&S)

//返回栈顶数据

{

if

(Stop>=0)

//栈内有数据

{

return

Sbase[Stop--];

}

else

{

printf("空栈,无数据d出!\n");

return

-1;

}

} int

StackLength(SqStack

S)

{

return

Stop+1;

} void

Print_S(SqStack

S)

{

printf("\n出栈显示:");

if(Stop

==

-1)

printf("栈内无数据!\n");

else

{

while(Stop>=0

)

printf("%d

",Pop(S));

putchar('\n');

}

}

class CStack

{

typedef struct tagStack_Data

{

bool bPos;

int Pos;

int Value;

}Stack_Data;

typedef struct tagStack

{

int Top;

int Size;

Stack_Data Stack;

}Stack;

private:

unsigned int m_iDataSize;

unsigned int m_iDataCount;

Stack m_Stack;

CStack::CStack(void);

CStack(const CStack& Other);

CStack &operator = (const CStack &Other);

public:

CStack::CStack(int Size, bool bStrict);

CStack::~CStack();

void __fastcall ConstructStack(int Size, bool bStrict);

void __fastcall DestoryStack(void);

void __fastcall InitStack(void);

void __fastcall ClearStack(void);

bool __fastcall StackEmpty(void) const;

bool __fastcall StackFull(void) const;

bool __fastcall StackPeek(int& Val) const;

bool __fastcall StackPeek(int& Val, int& Pos) const;

bool __fastcall InStack(int Val) const;

bool __fastcall StackPop(void);

bool __fastcall StackPop(int& Value);

bool __fastcall StackPop(int& Value, int& Pos);

bool __fastcall StackPush(int Value);

bool __fastcall StackPush(int Value, int Pos);

};

CStack::CStack(void)

{

}

CStack::CStack(int Size, bool bStrict)

{

m_StackStack = NULL;

ConstructStack(Size, bStrict);

}

CStack::~CStack()

{

DestoryStack();

}

void __fastcall CStack::ConstructStack(int Size, bool bStrict)//构建栈

{

if (Size < 1)

Size = 1;

if ((bStrict && m_StackSize != Size) || m_StackSize < Size)

{

delete[] m_StackStack;

m_StackStack = new Stack_Data[Size + 1];

m_StackSize = Size;

}

m_StackTop = -1;

}

void __fastcall CStack::DestoryStack(void)//销毁栈

{

m_StackTop = -1;

m_StackSize = 0;

delete[] m_StackStack;

m_StackStack = NULL;

}

void __fastcall CStack::InitStack(void)//初始化栈

{

m_StackTop = -1;

}

void __fastcall CStack::ClearStack(void)//清空栈

{

m_StackTop = -1;

}

bool __fastcall CStack::StackEmpty(void) const //栈是否为空

{

return (-1 == m_StackTop) true : false;

}

bool __fastcall CStack::StackFull(void) const//栈是否满

{

return (m_StackTop == m_StackSize - 1) true : false;

}

bool __fastcall CStack::StackPeek(int& Val) const//取栈顶元素值(不是d出)

{

if (-1 != m_StackTop)

{

Val = m_StackStack[m_StackTop]Value;

return true;

}

else

{

return false;

}

}

bool __fastcall CStack::StackPeek(int& Val, int& Pos) const//取栈顶元素值(不是d出)

{

if (-1 != m_StackTop)

{

Val = m_StackStack[m_StackTop]Value;

Pos = m_StackStack[m_StackTop]Pos;

if (m_StackStack[m_StackTop]bPos)

{

return true;

}

else

{

return false;

}

}

else

{

return false;

}

}

bool __fastcall CStack::InStack(int Val) const//值是否在栈里

{

for (int i = m_StackTop; i > -1; i--)

{

if (m_StackStack[i]Value == Val)

{

return true;

}

return false;

}

}

bool __fastcall CStack::StackPop(void)//d出栈顶值

{

if (-1 != m_StackTop)

{

m_StackTop--;

return true;

}

else

{

return false;

}

}

bool __fastcall CStack::StackPop(int& Value)//d出栈顶值到变量

{

if (-1 != m_StackTop)

{

Value = m_StackStack[m_StackTop]Value;

m_StackTop--;

return true;

}

else

{

return false;

}

}

bool __fastcall CStack::StackPop(int& Value, int& Pos)//d出栈顶值到变量

{

if (-1 != m_StackTop)

{

Value = m_StackStack[m_StackTop]Value;

Pos = m_StackStack[m_StackTop]Pos;

if (m_StackStack[m_StackTop--]bPos)

{

return true;

}

else

{

return false;

}

}

else

{

return false;

}

}

bool __fastcall CStack::StackPush(int Value)//值压入栈

{

if (m_StackTop < m_StackSize - 1)

{

m_StackTop++;

m_StackStack[m_StackTop]Value = Value;

m_StackStack[m_StackTop]bPos = false;

return true;

}

else

{

return false;

}

}

bool __fastcall CStack::StackPush(int Value, int Pos)//值压入栈

{

if (m_StackTop < m_StackSize - 1)

{

m_StackTop++;

m_StackStack[m_StackTop]Value = Value;

m_StackStack[m_StackTop]Pos = Pos;

m_StackStack[m_StackTop]bPos = true;

return true;

}

else

{

return false;

}

}

 从计算机科学的角度来看,栈指的是一种数据结构,是一种先进后出的数据表。栈的最常见 *** 作有两种:压栈(PUSH)、d栈(POP);用于标识栈的属性也有两个:栈顶(TOP)、栈底(BASE)

PUSH:为栈增加一个元素的 *** 作叫做PUSH,相当于在这摞扑克牌的最上面再放上一张。

以上就是关于栈的基本 *** 作的实现(c语言),高手速来!!全部的内容,包括:栈的基本 *** 作的实现(c语言),高手速来!!、一个关于栈的问题、计算机网络里面的栈长是什么意思等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/9814409.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-02
下一篇 2023-05-02

发表评论

登录后才能评论

评论列表(0条)

保存