c语言中,栈是具体应用方法和步骤
答案:2 悬赏:70 手机版
解决时间 2021-03-20 06:08
- 提问者网友:动次大次蹦擦擦
- 2021-03-20 00:51
c语言中,栈是具体应用方法和步骤
最佳答案
- 五星知识达人网友:旧脸谱
- 2021-03-20 02:23
栈简单的讲就是一片存储区域(存储区的首地址即为栈顶)
你可以向栈中存入数据取出数据删除数据
#include "stdio.h"
#define m 100
struct Mystack
{
char element[m];
int top;
};
void push(struct Mystack *s,char x)
{
s->element[s->top]=x;
s->top++;
}
void pop(struct Mystack *s)
{
s->top--;
}
int IsEmpty(struct Mystack *s)
{
if(s->top==0)
return 1;
else
return 0;
}
void Clearstack(struct Mystack *s)
{
s->top=0;
}
void Displaystack(struct Mystack *s)
{
int i;
for(i=0;itop;i++)
printf("%c",s->element[i]);
}
main()
{
struct Mystack st;
int i;
char ch;
for(i=0;i<100;i++)
st.element[i]='\0';
st.top=0;
printf("please write a string:\n");
ch=getchar();
while(ch!='\n')
{
switch(ch)
{
case '#':
if(!IsEmpty(&st))
pop(&st);
break;
case '@':
if(!IsEmpty(&st))
Clearstack(&st);
break;
default:
push(&st,ch);
}
ch=getchar();
}
printf("the string is :\n");
Displaystack(&st);
}
你可以向栈中存入数据取出数据删除数据
#include "stdio.h"
#define m 100
struct Mystack
{
char element[m];
int top;
};
void push(struct Mystack *s,char x)
{
s->element[s->top]=x;
s->top++;
}
void pop(struct Mystack *s)
{
s->top--;
}
int IsEmpty(struct Mystack *s)
{
if(s->top==0)
return 1;
else
return 0;
}
void Clearstack(struct Mystack *s)
{
s->top=0;
}
void Displaystack(struct Mystack *s)
{
int i;
for(i=0;i
printf("%c",s->element[i]);
}
main()
{
struct Mystack st;
int i;
char ch;
for(i=0;i<100;i++)
st.element[i]='\0';
st.top=0;
printf("please write a string:\n");
ch=getchar();
while(ch!='\n')
{
switch(ch)
{
case '#':
if(!IsEmpty(&st))
pop(&st);
break;
case '@':
if(!IsEmpty(&st))
Clearstack(&st);
break;
default:
push(&st,ch);
}
ch=getchar();
}
printf("the string is :\n");
Displaystack(&st);
}
全部回答
- 1楼网友:大漠
- 2021-03-20 03:57
# include
# include
# include
typedef struct Node
{
int data;
struct Node *pNext;
}NODE,* PNODE;
typedef struct Stack
{
PNODE pTop;
PNODE pBottom;
}STACK, * PSTACK;//STACK 等价于 struct Stack,PSTACK 等价于 struct Stack *
//函数声明
void init(PSTACK pS);
void push(PSTACK pS, int val );
void traverse(PSTACK ps);
bool pop(PSTACK pS, int * val) ;
void clear(PSTACK pS);
int main(void)
{ int val;
STACK S;
init(&S);
push(&S, 1);
push(&S, 2);
push(&S, 3);
push(&S, 4);
push(&S, 5);
traverse(&S);
if(pop(&S, &val))
{
printf("出栈成功!出栈的元素为%d\n",val);
}
else
{
printf("出栈失败!\n");
}
traverse(&S);
clear(&S);
traverse(&S);
return 0;
}
//栈的初始化
void init(PSTACK pS)
{
pS->pTop=(PNODE)malloc(sizeof(NODE));//使pTop指向一个头节点
if (NULL == pS->pTop)//
{
printf("动态内存分配失败!\n");
exit(-1);
}
else
{
pS->pBottom = pS->pTop;
pS->pBottom->pNext = NULL;//同pS->pTop->pNext = NULL;一样,意为将pTop所指向的节点的指针域清空
}
}
//压栈
void push(PSTACK pS, int val )
{
PNODE pNew = (PNODE)malloc(sizeof(NODE));
pNew->data = val;
pNew->pNext = pS->pTop;//pS->pTop不能改成pBottom
pS->pTop = pNew;
return;
}
//栈的遍历
void traverse(PSTACK pS)
{
PNODE p=pS->pTop;//定义一个变量p=pS-pTop,不能改变pTop的值,否则链表就乱了
while(p != pS->pBottom) //p移动到等于pS-pBottom的位置
{
printf("%d ",p->data);
p=p->pNext;
}
printf("\n");
return;
}
//判断栈是否为空
bool empty(PSTACK pS)
{
if(pS->pTop == pS->pBottom)
return true;
else return false;
}
//把pS所指向的栈出栈一次,并把出栈的元素存入* val形参所指向的变量中
bool pop(PSTACK pS, int * val)
{
PNODE r;
if(empty(pS))
{
return false;
}
else
{
r=pS->pTop;
* val=r->data;
pS->pTop=pS->pTop->pNext;
free (r);
}
return true;
}
//清空栈,保留框架,注意区别于销毁
void clear(PSTACK pS)
{
if (empty(pS))
{
return;
}
else
{
PNODE p=pS->pTop;
while(p->pNext != pS->pBottom)
{
PNODE q=p->pNext;
free (p);
p = q;
}
pS->pTop=pS->pBottom;
}
}
# include
# include
typedef struct Node
{
int data;
struct Node *pNext;
}NODE,* PNODE;
typedef struct Stack
{
PNODE pTop;
PNODE pBottom;
}STACK, * PSTACK;//STACK 等价于 struct Stack,PSTACK 等价于 struct Stack *
//函数声明
void init(PSTACK pS);
void push(PSTACK pS, int val );
void traverse(PSTACK ps);
bool pop(PSTACK pS, int * val) ;
void clear(PSTACK pS);
int main(void)
{ int val;
STACK S;
init(&S);
push(&S, 1);
push(&S, 2);
push(&S, 3);
push(&S, 4);
push(&S, 5);
traverse(&S);
if(pop(&S, &val))
{
printf("出栈成功!出栈的元素为%d\n",val);
}
else
{
printf("出栈失败!\n");
}
traverse(&S);
clear(&S);
traverse(&S);
return 0;
}
//栈的初始化
void init(PSTACK pS)
{
pS->pTop=(PNODE)malloc(sizeof(NODE));//使pTop指向一个头节点
if (NULL == pS->pTop)//
{
printf("动态内存分配失败!\n");
exit(-1);
}
else
{
pS->pBottom = pS->pTop;
pS->pBottom->pNext = NULL;//同pS->pTop->pNext = NULL;一样,意为将pTop所指向的节点的指针域清空
}
}
//压栈
void push(PSTACK pS, int val )
{
PNODE pNew = (PNODE)malloc(sizeof(NODE));
pNew->data = val;
pNew->pNext = pS->pTop;//pS->pTop不能改成pBottom
pS->pTop = pNew;
return;
}
//栈的遍历
void traverse(PSTACK pS)
{
PNODE p=pS->pTop;//定义一个变量p=pS-pTop,不能改变pTop的值,否则链表就乱了
while(p != pS->pBottom) //p移动到等于pS-pBottom的位置
{
printf("%d ",p->data);
p=p->pNext;
}
printf("\n");
return;
}
//判断栈是否为空
bool empty(PSTACK pS)
{
if(pS->pTop == pS->pBottom)
return true;
else return false;
}
//把pS所指向的栈出栈一次,并把出栈的元素存入* val形参所指向的变量中
bool pop(PSTACK pS, int * val)
{
PNODE r;
if(empty(pS))
{
return false;
}
else
{
r=pS->pTop;
* val=r->data;
pS->pTop=pS->pTop->pNext;
free (r);
}
return true;
}
//清空栈,保留框架,注意区别于销毁
void clear(PSTACK pS)
{
if (empty(pS))
{
return;
}
else
{
PNODE p=pS->pTop;
while(p->pNext != pS->pBottom)
{
PNODE q=p->pNext;
free (p);
p = q;
}
pS->pTop=pS->pBottom;
}
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯