当前位置:首页 » 编程语言 » c语言简单的栈

c语言简单的栈

发布时间: 2022-12-06 01:40:02

c语言的简单的进栈出栈

就用这堆函数就可以了,不懂再追问
#include
<string.h>
#define
MaxSize
100
int
mystack[MaxSize];/*
第0个单元保存现在的长度
*/
/*
初始化函数
*/
void
init_stack(int*
stack){
memset(stack,0,sizeof(stack));
}
/*
入栈函数
*/
void
push_back(int*
stack,int&
num){
if(stack[0]<MaxSize-1){
++stack[0];
stack[
stack[0]
]
=
num;
}
else{
printf("ERORR!\n");
}
}
/*
返回栈空间
*/
int
size(int*
stack){
return
stack[0];
}
/*
返回栈顶函数
*/
int
top(int*
stack){
if(stack[0]>0){
return
stack[
stack[0]
];
}
else{
printf("ERORR!\n");
return
-1;
}
}
/*
出栈函数
*/
void
pop(int*
stack){
if(stack[0]>0){
--stack[0];
}
else{
printf("ERORR!\n");
}
}

❷ C语言 求栈的简单例子

#include"iostream.h"
const int maxsize=6;
class stack{
float data[maxsize];
int top;
public:
stack(void);
~stack(void);
void push(float a);
bool empty(void);
float pop(void);
};
stack::stack(void){
top=0;
cout<<"stack initialized."<<endl;
}
stack::~stack(void){
cout<<"stack destoryed"<<endl;
}
bool stack::empty(void){
return top==0;
}
void stack::push(float a){
if(top==maxsize){
cout<<"Stack overflow!"<<endl;
return ;
}
data[top]=a;
top++;
}
float stack::pop(void){
if(top==0){
cout<<"An empty stack!"<<endl;
return 0;
}
top--;
return data[top];
}
int main()
{
stack s1,s2;
float i=0.0;
for(;i<=maxsize;i++)
s1.push(2*i);
for(i=1;i<=maxsize;i++)
cout<<s1.pop()<<" ";
for(i=1;i<=maxsize;i++)
s1.push(2.5*i);
for(i=1;i<=maxsize;i++)
s2.push(s1.pop());
cout<<endl;
do
cout<<s2.pop()<<" ";
while(!(s2.empty()));
cout<<endl;
return 0;
}

❸ 数据结构C语言 栈

肯定是你指针指向不对
太长不太想看,只给你指出一处错误

scanf("%d,&c1");//通过键盘输入为变量c1赋值

这一句,看到啥错误了吧?还有你声明了char干嘛又用%d呢,如果用%c的时候注意这样写
scanf(" %c",&c1);

❹ 急!用C语言编写个使用栈的程序,简单点的,包含入栈,出栈等几个基本操作就行。

就用这堆函数就可以了,不懂再追问
#include <string.h>

#define MaxSize 100
int mystack[MaxSize];/* 第0个单元保存现在的长度 */

/* 初始化函数 */
void init_stack(int* stack){
memset(stack,0,sizeof(stack));
}

/* 入栈函数 */
void push_back(int* stack,int& num){
if(stack[0]<MaxSize-1){
++stack[0];
stack[ stack[0] ] = num;
}
else{
printf("ERORR!\n");
}
}

/* 返回栈空间 */
int size(int* stack){
return stack[0];
}

/* 返回栈顶函数 */
int top(int* stack){
if(stack[0]>0){
return stack[ stack[0] ];
}
else{
printf("ERORR!\n");
return -1;
}
}

/* 出栈函数 */
void pop(int* stack){
if(stack[0]>0){
--stack[0];
}
else{
printf("ERORR!\n");
}
}

❺ 什么是C语言中的栈

栈是个数据结构,指数据的后进先出的方法。c语言可以编写这种数据结构。另外在c中参数的传递实际上也是入栈和出栈的过程。如果不明白,建议看一下数据结构中对栈的讲解。

❻ C语言栈的简单实现

#include<stdio.h>
#include<malloc.h>
//enum
bool
{false,true};
typedef
struct
Node{
int
a;
int
Number;
//在栈中的序号,栈底为0
struct
Node
*next;
}Node,*LpNode;
typedef
struct
SqStack{
Node
*top;
Node
*prev;
Node
*base;
int
length;
}*LpSqStack;
//将e的能容复制到S中并将e摧毁
bool
Node_evaluation(LpNode
S,LpNode
e,bool
M)
{
//赋值操作
//S->Number
=
e->Number;
if(M
==
true)
free(e);
return
true;
}
bool
InitStack(LpSqStack
S)
{
S->length
=
0;
S->base
=
(LpNode)malloc(sizeof(Node));
if(!S->base)
return
false;
S->top
=
S->base;
S->prev
=
S->base;
S->base->Number
=
0;
return
true;
}
bool
StackEmpty(LpSqStack
S)
{
if(S->top
!=
S->base)
return
false;
return
true;
}
bool
GetTop(LpSqStack
S,LpNode
e)
{
if(S->top
==
S->base)
return
false;
e
=
S->top;
return
true;
}
bool
Push(LpSqStack
S,LpNode
e)
{
if(!Node_evaluation(S->top,e,true))
return
false;
S->top->Number
=
S->prev->Number
+
1;
S->prev
=
S->top;
S->top
=
(LpNode)malloc(sizeof(Node));
S->prev->next
=
S->top;
S->top->next
=
NULL;
return
true;
}
bool
Pop(LpSqStack
S,LpNode
e)
{
if(S->top
==
S->base)
return
false;
if(!Node_evaluation(e,S->top,true))
return
false;
S->top
=
S->prev;
S->top->next
=
NULL;
return
true;
}
bool
Vistit(LpSqStack
S,LpNode
e,int
i)
{
LpNode
p;
p
=
S->base;
for(int
j
=
0;
j
=
i;
j++)
{
if(p->next
==
NULL)
return
false;
p
=
p->next;
}
if(!Node_evaluation(p,e,false))
return
false;
return
true;
}
int
main()
{
SqStack
a;
InitStack(&a);
LpNode
b=new
Node;
LpNode
c=new
Node;
LpNode
d=new
Node;
//free(&b);这free了你下面又赋值。。。
b->a=1;
Push(&a,c);
GetTop(&a,c);
printf("%d",c->a);
return
0;
}
栈里的内存是不能free的,你要free你就自己在堆里分配。

❼ c语言 栈的操作

#include
#include

#define Max 100

typedef char T;

typedef struct MyStack
{
T aa[Max];
unsigned int p;

} stack;

//创建空栈
stack* createEmptyStack()
{
stack* st = (stack *)malloc(sizeof(stack));
int i=0;
for(i=0;i<Max;i++)
st->aa[i]=0;
st->p=0;
return st;
};

//栈判空
int isEmpty(const stack* st)
{
if(st->p==0) return 1;
else return 0;
};

//求栈的大小
unsigned int size(const stack* st)
{
return st->p;
};

//push操作
void push(stack* st,const T a)
{
st->p=st->p+1;
if(st->p==Max)
{
printf("栈满\n");
st->p--;
return;
}
st->aa[st->p]=a;
};

//pop操作
T pop(stack* st)
{
if(isEmpty(st))
{
printf("栈空");
return NULL;
}
char t=st->aa[st->p];
st->p=st->p-1;
printf("%c ",t);
return t;
};

//栈销毁
void destroy(stack* st)
{
free(st);
};

int main()
{

stack* st = createEmptyStack();
if(isEmpty(st)) printf("MyStack is empty\n");
else printf("MyStack is not empty\n");
push(st,'a');
push(st,'b');
push(st,'c');
push(st,'d');
push(st,'e');
printf("%d\n",size(st));
while(!isEmpty(st)) pop(st);
destroy(st);
system("pause");
return 0;
}

❽ 数据结构实验(用c语言写) 栈的基本操作

//顺序栈
#include<stdio.h>

#include<stdlib.h>

#include<malloc.h>

#define STACK_INIT_SIZE 100;

#define STACKINCREMENT 10;

typedef struct
{
int *base;
int *top;
int stacksize;
}SqStack;

typedef int ElemType;

int InitStack(SqStack &S) //为栈S分配存储空间,并置S为空栈
{
int size = STACK_INIT_SIZE;
S.base=(int *)malloc(size*sizeof(ElemType));
if(!S.base)
return 0;
S.top=S.base; //置栈S为空栈
S.stacksize=STACK_INIT_SIZE;
return 1;
}

int GetTop(SqStack S,int &e) //若栈不空,则用e返回S的栈顶元素
{
if(S.top==S.base) return 0;
e=*(S.top-1);
return 1;

}

int Push(SqStack &S, int e) /*进栈函数,将e插入栈S中,并使之成为栈顶元素*/
{ if(S.top-S.base>=S.stacksize) /*栈满,追加存储空间*/
{
int stackinvrement = STACKINCREMENT;

S.base=(ElemType *) realloc(S.base,(S.stacksize+stackinvrement)*sizeof(ElemType));
if(!S.base)
return 0; /*存储分配失败*/
S.stacksize+=STACKINCREMENT;
}

*S.top++=e;
return 1;
}

int Pop(SqStack &S,int &e)/*出栈函数,若栈S不空,则删除S的栈顶元素,用e返回其值*/

{ if(S.top==S.base) return 0;

e=*--S.top;
return 1;
}

void OutputStack(SqStack &S)

{int *q;
q=S.top-1;
for(int i=0;i<S.top-S.base;i++)
{

printf("%3d ",*q);q--;}

}

void main()

{

int a,b,c ;
char m;

SqStack s;

InitStack(s);
printf("请输入要进栈的元素个数是:");

scanf("%d",&a);

printf("\n请输入要进栈的%d个元素:",a);

for(b=0;b<a;b++) {
scanf("%d",&c);
Push(s,c); }
do { printf("\n");
printf("*********** 1.输出栈的元素**********\n");
printf("*********** 2.取栈顶元素************\n");
printf("*********** 3.删除栈顶元素**********\n");
printf("*********** 4.退出程序**********\n");
printf("\n请选择一个字符:");
getchar();
scanf("%c",&m);
switch(m) {
case '1': printf("\n输出的栈为:");
OutputStack(s);
break;

case '2': GetTop(s,c);
printf("\n栈顶元素为:%d",c);
printf("\n输出的栈为:");
OutputStack(s);
break;
case '3': Pop(s,c);
printf("\n删除的栈顶元素:%d",c);
printf("\n输出的栈为:");
OutputStack(s);
printf("\n");
break;
case '4':break;
default: printf("输入的数字有错,请重新选择!\n"); break;

}

}while(m!='4');

}
//链栈

#include<stdio.h>
#include<stdlib.h>
typedef struct SNode
{
int data;
struct SNode *next;
}SNode,*LinkStack;
LinkStack top;
LinkStack PushStack(LinkStack top,int x) //入栈
{
LinkStack s;
s=(LinkStack)malloc(sizeof(SNode));
s->data=x;
s->next=top;
top=s;
return top;
}
LinkStack PopStack(LinkStack top) //退栈
{
LinkStack p;
if(top!=NULL)
{
p=top;
top=top->next;
free(p);
printf("退栈已完成\n");
return top;
}
else printf("栈是空的,无法退栈!\n"); return 0;
}
int GetStackTop(LinkStack top) //取栈顶元素
{
return top->data;

}
bool IsEmpty()//bool取值false和true,是0和1的区别,bool只有一个字节,BOOL为int型,bool为布尔型

{
return top==NULL ? true:false;
}
void Print()
{
SNode *p;
p=top;
if(IsEmpty())
{
printf("The stack is empty!\n");
return;
}
while(p)
{
printf("%d ", p->data);
p=p->next;
}
printf("\n");
}

void main()
{

int x,a,b;
char m;
do { printf("\n");
printf("###############链栈的基本操作##################\n");
printf("××××××××1.置空栈××××××××××\n");
printf("××××××××2.进栈×××××××××××\n");
printf("××××××××3.退栈×××××××××××\n");
printf("××××××××4.取栈顶元素××××××××\n");
printf("××××××××5.退出程序×××××××××\n");
printf("##############################################\n");
printf("\n请选择一个字符:");
scanf("%c",&m);
switch(m){
case '1':
top=NULL;
printf("\n栈已置空!");
break;
case '2':
printf("\n请输入要进栈的元素个数是:");
scanf("%d",&a);
printf("\n请输入要进栈的%d个元素:",a);
for(b=0;b<a;b++) {
scanf("%d",&x);
top=PushStack(top,x); }
printf("进栈已完成!\n");
printf("\n输出栈为:");
Print();
break;
case '3':
printf("\n操作之前的输出栈为:");
Print();
top=PopStack(top);
printf("\n操作过后的输出栈为:");
Print();
break;
case '4':
printf("\n输出栈为:");
Print();
if(top!=NULL)
printf("\n栈顶元素是:%d\n",GetStackTop(top));
else
printf("\n栈是空的,没有元素!");
break;
case '5':break;
default:
printf("\n输入的字符不对,请重新输入!");
break;

}
getchar();
}while(m!='5');

}

❾ C语言,栈的实现~

你写的太复杂,这个拿去用吧
// Stack node
struct Node
{
int data ;
Node* next ;
Node(int d, Node* p):data(d), next(p){}
};

class Stack
{
public:
Stack():top(NULL){}

void Push(int d)
{
top = new Node(d, top) ;
}

int Pop()
{
Node* temp = top ;
top = top->next ;
return temp->data ;
}

bool Empty()
{
return top == NULL ;
}

private:
Node* top ;
};

❿ 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<s->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);
}

热点内容
ps存储显示不含通道 发布:2024-05-05 09:32:35 浏览:101
如何用安卓做一个识物界面表 发布:2024-05-05 09:29:28 浏览:97
如何编译linux内核模块 发布:2024-05-05 09:27:25 浏览:167
为什么apple验证无法连接服务器 发布:2024-05-05 09:20:35 浏览:659
linuxscript 发布:2024-05-05 09:19:49 浏览:323
asp编程词典 发布:2024-05-05 09:09:50 浏览:525
引流群控脚本 发布:2024-05-05 08:42:14 浏览:178
sql测试题和答案 发布:2024-05-05 08:42:09 浏览:893
https加密链接 发布:2024-05-05 08:28:35 浏览:445
对等局域网与客户机服务器有什么不同 发布:2024-05-05 07:51:15 浏览:175