永发信息网

有关数据结构的 栈的基本操作的代码

答案:1  悬赏:50  手机版
解决时间 2021-03-19 10:59
  • 提问者网友:辞取
  • 2021-03-19 04:05
有关数据结构的 栈的基本操作的代码
最佳答案
  • 五星知识达人网友:几近狂妄
  • 2021-03-19 04:25

#include
#include
#include

typedef struct Node
{
int Data;
struct Node * NEXT;

}NODE, * PNODE;
typedef struct stack
{
PNODE PTOP;
PNODE PBOTTOM;
}*STACK, stack;

void init(STACK);
bool push(STACK, int);
bool pop(STACK);
void trversion(STACK);
bool empty(STACK);
bool clear(STACK);

int main(void)
{
stack pS;
init(&pS);
trversion(&pS);
push(&pS, 1);
push(&pS, 2);
push(&pS, 3);
push(&pS, 4);
trversion(&pS);
pop(&pS);
pop(&pS);
//clear(&pS);
trversion(&pS);

return 0;
}
void init(STACK pS)
{
pS->PTOP = (PNODE)malloc(sizeof(NODE));
if (NULL == pS->PTOP)
{
printf("Error on initilizing the Stack!\n");
exit(1);
}
pS->PTOP->NEXT = NULL;
pS->PBOTTOM = pS->PTOP;
return;

}

bool push(STACK pS, int val)
{
PNODE pNew = (PNODE)malloc(sizeof(NODE));
if (NULL == pNew)
{
printf("Error on pushing the value\n");
exit(1);
}
pNew->Data = val;
pNew->NEXT = pS->PTOP;
pS->PTOP = pNew;
return true;
}
void trversion(STACK pS)
{
int i = 1;
if (empty(pS))
{
printf("栈为空\n");
return;
}
PNODE p = pS->PTOP;
while (p != pS->PBOTTOM)
{
printf("栈顶第%d个元素为:%d\n", i++, p->Data);
p = p->NEXT;
}
return;
}
bool empty(STACK pS)
{
if (pS->PBOTTOM == pS->PTOP)
{
return true;
}
else
{
return false;
}
}
bool pop(STACK pS)
{
int val, i=1;
if (empty(pS))
{
printf("栈为空\n");
exit(1);
}


//单个出栈
else
{
PNODE p = pS->PTOP;
pS->PTOP = pS->PTOP->NEXT;
val = p->Data;
printf("出栈元素为:%d\n", val);
free(p);
p = NULL;
p = pS->PTOP;
}
return true;
}

bool clear(STACK pS)
{
if (empty(pS))
{
printf("栈为空\n");
exit(1);
}
PNODE p;
p = pS->PTOP;
while (p !=pS->PBOTTOM)
{
pS->PTOP = pS->PTOP->NEXT;
free(p);
p = NULL;
p = pS->PTOP;
}
return true;
}

#include
#include
#include

typedef struct stack
{
int * Pstop;
int * Psbase;
int stacksize;
}STACK;
void InitStack(STACK *);
bool Empty(STACK *);
bool Full(STACK *);
bool Push(STACK *, int);
bool Pop(STACK *);
bool Destroy(STACK *);
void Show(STACK *);
int Length(STACK *);
void ClearStack(STACK *);
int main(void)
{
STACK S;
InitStack(&S);
Push(&S, 1);
Push(&S, 2);
Push(&S, 3);
Push(&S, 4);
Show(&S);
if (!Push(&S, 5))
{
printf("压栈失败!!!\n");
}
printf("出栈前的有效数据个数为%d\n", Length(&S));
Pop(&S);
printf("出栈后的有效数据个数为%d\n", Length(&S));
ClearStack(&S);
if (Destroy(&S))
{
printf("栈被销毁!!!\n");
}
return 0;
}
void InitStack(STACK * S)
{
printf("请设定栈的存储空间的容量\n");
scanf("%d", &S->stacksize);
if (S->stacksize < 0)
{
printf("分配的存储容量非法!!!\n");
exit(1);
}
S->Psbase = S->Pstop = (int *)malloc(sizeof(int)*S->stacksize);
if (NULL == S->Psbase)
{
printf("为栈分配内存失败\n");
exit(-1);
}
return;
}
bool Empty(STACK * S)
{
if (S->Psbase == S->Pstop)
{
return true;
}
else
{
return false;
}
}
bool Full(STACK * S)
{
if (S->Pstop-S->Psbase >= S->stacksize)
{
return true;
}
else
{
return false;
}
}
bool Push(STACK * S, int val)
{
if (Full(S))
{
S->Psbase = (int *)realloc(S->Psbase, sizeof(int)*S->stacksize*2);
if (NULL == S->Psbase)
{
printf("问题解决失败!!!\n");
return false;
}
}
*(S->Pstop) = val;
S->Pstop++;
return true;
}
bool Destroy(STACK * S)
{
if (Empty(S))
{
printf("栈为空!!!\n");
}
else{
free(S->Psbase);
S->Psbase = S->Pstop = NULL;
}
return true;
}
bool Pop(STACK * S)
{
int val;
if (Empty(S))
{
printf("栈为空!!!\n");
return false;
}
else
{
while (S->Psbase != S->Pstop)
{
S->Pstop--;
val = *(S->Pstop);
printf("出栈数据依次为%d\n", val);
}
return true;
}
}
void Show(STACK * S)
{
int * pS = S->Pstop;
int i = 1;
if (Empty(S))
{
printf("栈为空不能显示\n");
exit(1);
}
else
{
while (S->Psbase != pS)
{
pS--;
printf("栈顶第%d个元素为%d\n", i, *(pS));
i++;
}
}
return;
}
int Length(STACK * S)
{
int count = 0;
int * pTemp = S->Pstop;
while (S->Psbase != pTemp)
{
pTemp--;
count++;
}
return count;
}
void ClearStack(STACK * S)
{
if (!Empty(S))
{
S->Pstop = S->Psbase;
}
return;
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯