栈的c语言实现
⑴ 用c语言实现入栈出栈
#include
<stdio.h>
int
stack[100];
/*100个栈空间*/
int*
sp
=
stack;
/*栈指针指向栈底*/
#define
push(
i
)
{
*sp++
=
i;
}
/*push一个数*/
#define
pop()
(*--sp)
/*pop一个数并返回*/
int
main()
{
int
i;
for
(
i
=
0;
i
<
10;
++i
)/*push
0~9*/
push(
i
);
for
(
i
=
0;
i
<
10;
++i
)/*输出9~0*/
printf(
"%d
",
pop()
)
;
}
⑵ 栈的基本操作的实现(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');
}
}
⑶ c语言实现队列和栈的方法
#define OVERFLOW -1
#define OK 1
#define ERROR 0
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define N 20
typedef char SElemType;
typedef int Status;typedef struct {
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;#include<stdio.h>
#include<stdlib.h>Status CreatStack(SqStack &S){
//创建栈
S.base=(SElemType*)malloc(STACK_INIT_SIZE*sizeof(SElemType));
if(!S.base)exit(OVERFLOW);
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
return OK;
}//CreatStackStatus Push(SqStack &S,SElemType e){
//插入e为新的栈顶元素
if(S.top-S.base>=S.stacksize){//栈满,追加存储空间
S.base=(SElemType*)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(SElemType));
if(!S.base)exit (OVERFLOW);//存储空间分配失败
S.top=S.base+S.stacksize;
S.stacksize+=STACKINCREMENT;
}
*S.top++=e;
return OK;
}//PushStatus Pop(SqStack &S ,SElemType &e){
//若栈不空,删除栈顶元素,并用e返回其值
if(S.top==S.base) return ERROR;
e=*--S.top;
return OK;
}//Pop typedef char QElemType;
typedef struct QNode{
QElemType data;
struct QNode *next;
}QNode,*QNodePtr;typedef struct{
QNodePtr front;
QNodePtr rear;
}LinkQueue;Status CreatQueue(LinkQueue &Q){
//建立一个空的链式栈
Q.front=Q.rear=(QNodePtr)malloc(sizeof(QNode));
if(!Q.front)exit(OVERFLOW);
Q.front->next=NULL;
return OK;
}Status EnQueue(LinkQueue &Q,QElemType e){ QNodePtr p;
p=(QNodePtr)malloc(sizeof(QNode));
if(!p)exit(OVERFLOW);
p->data=e;p->next=NULL;
Q.rear->next=p;
Q.rear=p;
return OK;
}Status DeQueue(LinkQueue &Q,QElemType &e){QNodePtr p;<br>if(Q.front==Q.rear) return ERROR;<br>p=Q.front->next; e=p->data;<br>Q.front->next=p->next;<br>if(Q.rear==p) Q.rear=Q.front;<br>free(p);<br>return OK;<br>}int stackPalindrom_Test()//判别输入的字符串是否回文序列,是则返回1,否则返回0
{
printf("栈练习,请输入要判断的字符串以#作为结束符,不要超过二十个字符\n");
SqStack S;
CreatStack(S);
char c;
SElemType a;
SElemType b[N];
int count = 0;
fgets( b, N, stdin );
while((b[count])!='#')
{
Push(S,b[count]); //进栈
count++;
}
int i = 0;
while(i < count)
{
Pop(S,a);
if(a!=b[i])
return ERROR;
i++;
}
return OK;}//stackPalindrom_Test int queuePalindrom_Test()//判别输入的字符串是否回文序列,是则返回1,否则返回0
{
printf("队列练习,请输入要判断的字符串以#作为结束符,不要超过二十个字符\n");
LinkQueue Q;
CreatQueue(Q); char c;
SElemType a;
SElemType b[N];
int count = 0;
fgets( b, N, stdin );
while((b[count])!='#')
{
EnQueue(Q,b[count]);; //入列
count++;
} while(count-- > 0)
{
DeQueue(Q,a);
if(a!=b[count])
return ERROR;
}
return OK;
}//queuePalindrom_Test Status main(){ if(stackPalindrom_Test()==1)
printf("是回文\n");
else printf("不是回文\n"); if(queuePalindrom_Test()==1)
printf("是回文\n");
else printf("不是回文\n");
return OK;
}
⑷ 栈的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;
}
⑸ 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 ;
};