1. 急!用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");
}
}
2. 用C语言实现栈的基本操作(数制的转换)
//顺序栈以及基本操作如下:
#include<iostream.h>
enum
{
MAX_SIZE=20
};
typedef struct
{
int* base;
int* top;
int stacksize;
}SqStack;
void InitStack(SqStack& S)
{
S.base=new int[MAX_SIZE];
S.top=S.base;
S.stacksize=MAX_SIZE;
}
bool Push(SqStack& S,int e)
{
if(S.top-S.base>=S.stacksize)
return false;
*S.top=e;
S.top++;
return true;
}
bool Pop(SqStack& S,int& e)
{
if(S.top==S.base)
return false;
S.top--;
e=*S.top;
return true;
}
void DestroyStack(SqStack& S)
{
if(S.base)
delete S.base;
S.top=S.base=NULL;
S.stacksize=0;
}
bool StackEmpty(SqStack S)
{
return (S.top==S.base);
}
void Print(SqStack S)
{
int i=0;
while(i<S.top-S.base)
{
cout<<S.base[i++]<<endl;
}
}
3. 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;
}
4. 急!用c语言实现链栈的操作
typedef struct node
{ ElemType data;
struct node *next;
} LinkStack;
⑴置空栈
void InitLinkStack( LinkStack * & s)
{ s=NULL;
}
⑵判栈空
int IsEmptyLinkStack(LinkStack *s )
{ if(s==NULL)
return 1;
else
return 0;
}
⑶ 入栈/*将元素x插入链栈top的栈顶*/
void PushLinkStack(LinkStack* &s , ElemType x)
{ LinkStack *p;
p=malloc(sizeof(LinkStack)); /*生成新结点*s */
p->data=x;
p->next=s;
s=p;
}
⑷出栈/*删除链栈top的栈顶结点*/
int PopLinkStack (LinkStack* & s, ElemType &x)
{ LinkStack *p;
if(s==NULL) return 0;
x = s->data; /*将栈顶数据存入*x */
p = s; /*保存栈顶结点地址*/
s = s->next; /*删除原栈顶结点*/
free (p); /*释放原栈顶结点*/
return 1; /*返回新栈顶指针*/
}
(5) 取栈顶元素
int GetLinkStackTop (LinkStack* s, ElemType &x)
{
if(s==NULL) return 0;
x = s->data; /*将栈顶数据存入*x */
return 1; /*返回新栈顶指针*/
}
主函数怎么写吧
5. 如何采用循环的方式入栈呢用c语言
用数组的话:
voidPush(structDStack*PtrS,ElementTypeitem,intTag)
{/*Tag作为区分两个堆栈的标志,取值为1和2*/
if(PtrS->Top2–PtrS->Top1==1){/*堆栈满*/printf(“堆栈满”);return;
}
if(Tag==1)/*对第一个堆栈操作*/
PtrS->Data[++(PtrS->Top1)]=item;
else/*对第二个堆栈操作*/
PtrS->Data[--(PtrS->Top2)]=item;
}
用链表的话:
voidPush(ElementTypeitem,LinkStack*S)
{/*将元素item压入堆栈S*/
structNode*TmpCell;
TmpCell=(LinkStack*)malloc(sizeof(structNode));TmpCell->Element=item;
TmpCell->Next=S->Next;
S->Next=TmpCell;
}
6. 栈的操作算法c语言实现
#include<iostream>
using namespace std;
#define STACK_INIT_SIZE 100
#define STACKINCREASE 1ypedef struct{
int *base;
int *top;
int stacksize;
}SqStack;
int InitStack (SqStack &S)//构造空栈
{
S.base=(int *)malloc(STACK_INIT_SIZE*sizeof(int));
if(!S.base)exit(OVERFLOW);//存储分配失败
S.top=S.base;
return 0;
}
int Push(SqStack &S,int e)//插入新的元素为新的栈顶元素
{
if(S.top-S.base>=S.stacksize)//z栈满,追加存储空间
{
S.base=(int *)realloc(S.base,(S.stacksize+STACKINCREASE)*sizeof(int));
if(!S.base)exit(OVERFLOW);//存储失败
S.top=S.base+S.stacksize;
S.stacksize+=STACKINCREASE;
}
*S.top++ =e;//认为是先赋值再加加?
return 0;
}
void Insert(SqStack &S,int e)
{
int *p;
int temp;
p=S.base;
if(S.top-S.base>=S.stacksize)//z栈满,追加存储空间
{
S.base=(int *)realloc(S.base,(S.stacksize+STACKINCREASE)*sizeof(int));
if(!S.base)exit(OVERFLOW);//存储失败
S.top=S.base+S.stacksize;
S.stacksize+=STACKINCREASE;
}
while(*p<=e)
p++;
while(p<S.top)
{
temp=*p;
*p=e;
e=temp;
p++;
}
}0
void Pop(SqStack S)
{
if(S.base==S.top)
cout<<"该栈是空栈"<<endl;
while(S.base<S.top)
{
cout<<*++S.base<<" ";
}
}
void main()
{
SqStack S;
int j,i;
InitStack(S);
for(i=1;i<=5;i++)
{
cout<<"请输入"<<i<<"个栈内元素"<<endl;
cin>>j;
Push(S,j);
}
Pop(S);
cout<<"请输入你要插入的元素"<<endl;
cin>>j;
Insert(S,j);
cout<<"输出栈内元素"<<endl;
Pop(S);
}
7. 栈的基本操作的实现(c语言),高手速来!!
/*程序错误太多*/ #include"stdio.h"
#include"stdlib.h"
#include"time.h"
#include"malloc.h"
#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
\n",Pop(S));
printf("栈的长度为:%d
\n",StackLength(S));
Print_S(S);
return
0;
} void
InitStack(SqStack
&S)
//构造空栈S
{
S.base
=
(int
*)malloc(STACK_INIT_SIZE
*
sizeof(int));
//分配组数空间,长度STACK_INIT_SIZE
if
(S.base==NULL)
{
printf("内存分配失败!\n");
return;
}
S.top=-1;
} int
Push(SqStack
&S,int
e)
{
if(S.top>=STACK_INIT_SIZE)
{
printf("栈空间已满,入栈失败!\n");
return
-1;
}
else
{
S.base[++S.top]=e;
return
0;
}
} int
Pop(SqStack
&S)
//返回栈顶数据
{
if
(S.top>=0)
//栈内有数据
{
return
S.base[S.top--];
}
else
{
printf("空栈,无数据弹出!\n");
return
-1;
}
} int
StackLength(SqStack
S)
{
return
S.top+1;
} void
Print_S(SqStack
S)
{
printf("\n出栈显示:");
if(S.top
==
-1)
printf("栈内无数据!\n");
else
{
while(S.top>=0
)
printf("%d
",Pop(S));
putchar('\n');
}
}
8. 用c语言实现栈的建立
#include <stdio.h>
#include <stdlib.h>
typedef struct _stack {
int size;
int* base;
int* sp;
} stack;
void init(stack* s, int n)
{
s->base = (int*)malloc(sizeof(int)*n);
s->size = n;
s->sp = s->base;
}
int push(stack* s, int val)
{
if(s->sp - s->base == s->size) {
puts("overflow");
exit(1);
}
return *s->sp++ = val;
}
int pop(stack* s)
{
if(s->sp == s->base) {
puts("underflow");
exit(2);
}
return *--s->sp;
}
int empty(stack* s)
{
return s->sp == s->base;
}
void clean(stack* s)
{
if(s->base)
free(s->base);
}
int main(void)
{
stack s;
int i;
init(&s, 100);
for(i = 0; i < 10; ++i)
printf("%d ", push(&s, i));
putchar('\n');
while(!empty(&s))
printf("%d ", pop(&s));
clean(&s);
return 0;
}
9. 数据结构实验(用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');
}
10. 栈的c语言实现基本操作
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#definestack_init_size20
#defineincreasesize10;
typedefintElemType;
typedefstructnode{
ElemType*base;
ElemType*top;
intsize;
}stack;
voidcreat(stack*s)
{
s->base=(ElemType*)malloc(sizeof(ElemType));
if(!s->base)
{
exit(0);
}
s->base=s->top;
s->size=stack_init_size;
}
voidpush(stack*s,ElemTypee)
{
if(s->top-s->base>=s->size)
{
s->base=(ElemType*)realloc(s->base,(s->size+increasesize)*sizeof(ElemType));
if(!s->base)
{
exit(0);
}
s->top=s->base+s->size;
s->size+=increasesize;
}
*(s->top)=e;
s->top++;
}
voidpop(stack*s,ElemType*e)
{
if(s->top==s->base)
{
return;
}
*e=*--(s->top);
}
intstacklen(stacks)
{
return(s.top-s.base);
}
intmain()
{
stacks;
intn;
ElemTypee1,e2,d;
inti=1,j=1;
push(&s,i);
push(&s,j);
scanf("%d",&n);
while(n--)
{
pop(&s,&e1);
pop(&s,&e2);
d=e1+e2;
push(&s,e2);
push(&s,d);
}
pop(&s,&d);
printf("%d",d);
return0;
}