当前位置:首页 » 编程语言 » c语言基于栈的应用
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

c语言基于栈的应用

发布时间: 2022-05-23 19:39:05

A. c语言 利用栈实现数据分类

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

intconstMAX=20;

typedefstructstack{
int*data;
intsize;
}*STACK,*pStack;

STACKinitstack(intn){//建栈
STACKs=(STACK)malloc(sizeof(structstack));
s->data=(int*)malloc(n*sizeof(int));
s->size=0;
returns;
}

voidpush(STACKs,inte){//数据进栈
s->data[s->size]=e;
++s->size;
}

intpop(STACKs,int*e){//出栈
if(s->size>0){
--s->size;
*e=s->data[s->size];
return1;
}
return0;
}

intmain(){//主函数
intnum,i;
STACKodd=initstack(MAX);
STACKeven=initstack(MAX);
srand(time(0));
printf("原始序列: ");
for(i=0;i<MAX;i++){
num=rand()%100;
if(num%2)push(odd,num);
elsepush(even,num);
printf("%d",num);
}
printf(" 奇数栈: ");
while(pop(odd,&num))printf("%d",num);
printf(" 偶数栈: ");
while(pop(even,&num))printf("%d",num);
printf(" ");
return0;
}

B. 那位大神能讲下C语言中栈的使用啊

堆栈就是先入后出的数据结构。
如果用c语言来实现的话用个struct
先定义一个栈的节点
struct
node;
typedef
strcut
node
*
position;
typedef
position
stack;
stack
creat();
void
push(int,stack);
int
pop(stack);
int
top();
struct
node
{
int
element;
position
next;
}
stack
creat()
{
stack
s;
s=malloc(sizeof(struct
node));
s->next==NULL;
}
void
push(int
a,stack
s)
{
position
temp;
temp=malloc(sizeof(struct
node));
temp->element=a;
temp->next=s->next;:
s->next=temp;
}
int
pop(stack
s)
{
int
res=s->next->element;
position
temp=s->next;
s->next=temp->next;
free
temp;
}
int
top(stack
s)
{
return
s->next->element;
}
好啦,先creat()一个栈,再进行push
pop等。程序中忽略了麻烦的错误检测给出了重点,当然还可以添加其他操作。。对了,头文件也要加上。本人还是习惯用c++弄成一个类^_^

C. C语言栈的应用

这段程序看得我湿哒哒的。。。。将#include "stdafx.h"改为#include "stdio.h"
加上#include<malloc.h>之后,出现死循环。。。从命名规则来看,照抄书上的程序吧?
。。。自己写一个吧,这个改起来还不如自己写

D. c语言 栈是怎么应用的 哪位大神能说的好理解点

栈分为出栈和入栈,入栈是为了保护你刚刚正在进行的程序,把它放进指定的空闲位置,出栈是你执行完另一件事后把之前保存入栈的东西在从存放的地方拿出来。这是为了保护数据,防止丢失。!
比如你正看着书呢,小伙伴叫你去玩,你就加个书签在读过的那一页放进书橱,玩回来了拿出书翻到那一页接着读。而你的书签就相当于起到保护作用,回来翻到那一页就相当于取出之前存入栈中的内容。

E. 请问一下C语言关于栈的基本应用

#include <stdlib.h>

010 #include <malloc.h>

011 #include <memory.h>

012 #include <assert.h>
/*------------------------------------------------------------

12 // 栈结构的定义

13 ------------------------------------------------------------*/

14 typedef struct Stack

15 {

16 ElemType *base; // 栈基址

17 ElemType *top; // 栈顶

18 int stacksize; // 栈存储空间的尺寸

19 } SqStack;

20

21 /*------------------------------------------------------------

22 // 栈的基本操作

23 ------------------------------------------------------------*/

24

25 bool InitStack(SqStack *S);

26 void DestroyStack(SqStack *S);

27 bool StackEmpty(SqStack S);

28 int StackLength(SqStack S);

29 bool GetTop(SqStack S, ElemType *e);

30 void StackTraverse(SqStack S, void (*fp)(ElemType));

31 bool Push(SqStack *S, ElemType e);

32 bool Pop(SqStack *S, ElemType *e);

33 bool bracketMatch(SqStack *S,const char *arr);

const int STACK_INIT_SIZE = 100; // 初始分配的长度

016 const int STACKINCREMENT = 10; // 分配内存的增量

017

018 /*------------------------------------------------------------

019 操作目的: 初始化栈

020 初始条件: 无

021 操作结果: 构造一个空的栈

022 函数参数:

023 SqStack *S 待初始化的栈

024 返回值:

025 bool 操作是否成功

026 ------------------------------------------------------------*/

027 bool InitStack(SqStack *S)

028 {

029 S->base = (ElemType *)malloc(STACK_INIT_SIZE *sizeof(ElemType));

030 if(S ->base == NULL)

031 return false;

032 S ->top = S ->base;

033 S->stacksize = STACK_INIT_SIZE;

034 return true;

035 }

036

037 /*------------------------------------------------------------

038 操作目的: 销毁栈

039 初始条件: 栈S已存在

040 操作结果: 销毁栈S

041 函数参数:

042 SqStack *S 待销毁的栈

043 返回值:

044 无

045 ------------------------------------------------------------*/

046 void DestroyStack(SqStack *S)

047 {

048 assert(S != NULL);

049 free(S ->base);

050 S->base = S->top = NULL;

051 }

052

053 /*------------------------------------------------------------

054 操作目的: 判断栈是否为空

055 初始条件: 栈S已存在

056 操作结果: 若S为空栈,则返回true,否则返回false

057 函数参数:

058 SqStack S 待判断的栈

059 返回值:

060 bool 是否为空

061 ------------------------------------------------------------*/

062 bool StackEmpty(SqStack S)

063 {

064 assert((S.base != NULL)&&(S.top != NULL));

065 return (S.base == S.top);

066 }

067

068 /*------------------------------------------------------------

069 操作目的: 得到栈的长度

070 初始条件: 栈S已存在

071 操作结果: 返回S中数据元素的个数

072 函数参数:

073 SqStack S 栈S

074 返回值:

075 int 数据元素的个数

076 ------------------------------------------------------------*/

077 int StackLength(SqStack S)

078 {

079 assert((S.base != NULL)&&(S.top != NULL));

080 return (S.top - S.base);

081 }

082

083 /*------------------------------------------------------------

084 操作目的: 得到栈顶元素

085 初始条件: 栈S已存在

086 操作结果: 用e返回栈顶元素

087 函数参数:

088 SqStack S 栈S

089 ElemType *e 栈顶元素的值

090 返回值:

091 bool 操作是否成功

092 ------------------------------------------------------------*/

093 bool GetTop(SqStack S, ElemType *e)

094 {

095 assert((S.base != NULL) && (S.top != NULL));

096 if(S.top == S.base)

097 exit(0);

098 *e = *(S.top - 1);

099 return true;

100 }

101

102 /*------------------------------------------------------------

103 操作目的: 遍历栈

104 初始条件: 栈S已存在

105 操作结果: 依次对S的每个元素调用函数fp

106 函数参数:

107 SqStack S 栈S

108 void (*fp)() 访问每个数据元素的函数指针

109 返回值:

110 无

111 ------------------------------------------------------------*/

112 void StackTraverse(SqStack S, void (*fp)(ElemType))

113 {

114 assert((S.base != NULL) && (S.top != NULL));

115 while(S.base < S.top)

116 {

117 (*fp)(*S.base);

118 S.base++;

119 }

120 }

121

122 /*------------------------------------------------------------

123 操作目的: 压栈——插入元素e为新的栈顶元素

124 初始条件: 栈S已存在

125 操作结果: 插入数据元素e作为新的栈顶

126 函数参数:

127 SqStack *S 栈S

128 ElemType e 待插入的数据元素

129 返回值:

130 bool 操作是否成功

131 ------------------------------------------------------------*/

132 bool Push(SqStack *S, ElemType e)

133 {

134 if(S == NULL)

135 return false;

136 assert((S->base != NULL) && (S ->top != NULL));

137 if(S ->top - S ->base >= S->stacksize)

138 {

139 S ->base = (ElemType *)realloc(S ->base,(S ->stacksize + STACKINCREMENT) * sizeof(ElemType));

140 if(! S ->base)

141 exit(0);

142 S ->top = S->base + S ->stacksize;

143 S ->stacksize += STACKINCREMENT;

144 }

145 *(S ->top++) = e;

146 return true;

147 }

148

149 /*------------------------------------------------------------

150 操作目的: 弹栈——删除栈顶元素

151 初始条件: 栈S已存在且非空

152 操作结果: 删除S的栈顶元素,并用e返回其值

153 函数参数:

154 SqStack *S 栈S

155 ElemType *e 被删除的数据元素值

156 返回值:

157 bool 操作是否成功

158 ------------------------------------------------------------*/

159 bool Pop(SqStack *S, ElemType *e)

160 {

161 if((* S).top == (*S).base)

162 return false;

163 *e = * --(* S).top;

164 return true;

165 }

F. C语言 栈的应用 数制转换

#include "stdafx.h"
#include <iostream.h>
#include <stdio.h>
#define MaxSize 100

//用栈的方法
typedef struct {
int data[MaxSize];
int top;
}Stack;//顺序栈,也可用链栈

void InitStack(Stack &S)//初始化栈
{
S.top=-1;
}

int push(Stack &S,int x)//入栈
{
if(S.top==MaxSize)
return 0; //栈满
else{
S.top++;
S.data[S.top]=x;
return 1;
}
}

int pop(Stack &S,int &x)//出栈
{
if(S.top==-1)
return 0; //栈空
else{
x=S.data[S.top];
S.top--;
return 1;
}
}

void display(Stack &S)
{
int x;
while(S.top!=-1)
{
pop(S,x);
printf("%x",x);
}

}
void func(int n,int m)//n为十进制数,m为2,8,或16
{
Stack S;
InitStack(S);
while(n!=0)
{
push(S,n%m);
n=n/m;
}
display(S);
}

int main(int argc, char* argv[])
{
int n;
printf("\n请输入一个数:");
scanf("%d",&n);
int m=2;
func(n,m);
m=8;
func(n,m);
m=16;
func(n,m);
return 0;
}

G. 数据结构C语言,栈的应用,请补充代码

void
InitStack(SqStack
*s)

{

s.top=0;

}
int
StackEmpty(SqStack
S) {

return (S.top==0);
} //空返回1, 非空则返回
void Push(SqStack &S,SElemType e)
{
if (S.top==Maxlen){ //栈满
printf(“Stackis overflow.\n”);
exit(1); }
S.top++;
S.data[S.top]=e;
}
elemtypePop(SqStack &S,SElemType &e) {
if (S.top==0) { //栈空
printf(“The stack is empty!\n”); //出错提示信息
return (NULLELEM); } //返回空数据值
S.top--;
e=S.data[S.top+1];
return e;
} //返回原栈顶元素

H. c语言中什么是堆栈,堆栈的具体应用,求高手帮我解释下,最好能详细点,谢谢

也就是一个井喽,然后丢石头进去,要的话就从最上面的拿上来。如
abc要进栈,则: 元素 栈中的元素
1.a进栈。 cb a
2.可以a出栈也可以b进栈。 c ba
3.可以b出栈也可以c进栈。 无 cba
如果按照示例的话,3次后abc都进入栈了,则从上往下的排序分别是c b a,此时只可以取出c,不可以取出其他的,也就是说,栈就是:
可以往里面丢东西,填平了或者无聊的时候就取出最上面的东西。
你可以理解成人的肝脏。如果糖分多的话,多余的就存在肝脏里,如果少了,就从肝脏里面取。但是你不把靠近血液的糖取出来,你是没有办法取更里面的糖分的......

它的应用吧,应该是可以用来加密。如可执行文件的出栈、入栈。不过一般我们电脑里面的计算就用到了栈,只不过没有察觉到。
原创!

I. c语言中,栈是具体应用方法和步骤

栈简单的讲就是一片存储区域(存储区的首地址即为栈顶)
你可以向栈中存入数据取出数据删除数据
/*
Note:Your
choice
is
C
IDE
*/
#include
"stdio.h"
#define
m
100
struct
Mystack/*定义栈结构*/
{
char
element[m];
int
top;/*栈顶*/
};
void
push(struct
Mystack
*s,char
x)
/*将x的值压入栈顶*/
{
/*
s->element[s->top]=x;
s->top++;*/
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
top;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);
}

J. 求大神精简的讲讲c语言中栈的用法

堆栈用于特殊的数据存储,一般用于函数调用,特点是先入后出。
在进行子程序(函数)调用时,它可以存储调用时调用者给出的参数,子程序通过堆栈指针可找到所有传入的参数,即使修改它们由于返回时设置堆栈指针也不会影响参数原型数据;子程序开设的普通变量也会在堆栈中开辟。
在进行中断函数调用时,堆栈用于保护现场,可以将断点处程序执行的关键寄存器压栈保存,执行完后弹栈恢复现场。
堆栈的好处在于,cpu有专用的堆栈地址寄存器,压栈、弹栈有专用语句,对于栈内数据存取以堆栈指针为参照存取方便,利于最后舍弃无需的数据。