当前位置:首页 » 编程语言 » c语言栈的实现

c语言栈的实现

发布时间: 2022-01-08 03:22:49

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语言实现进栈程序的全过程

/**************
sstack.h
***************/
/* 顺序栈表示:类型和界面函数声明 */

enum { MAXNUM = 20 /* 栈中最大元素个数,应根据需要定义 */
};

typedef int DataType; /* 栈中元素类型,应根据需要定义 */

struct SeqStack { /* 顺序栈类型定义 */
int t; /* 栈顶位置指示 */
DataType s[MAXNUM];
};

typedef struct SeqStack SeqSack, *PSeqStack; /* 顺序栈类型和指针类型 */

/*创建一个空栈;为栈结构申请空间,并将栈顶变量赋值为-1*/
PSeqStack createEmptyStack_seq( void );

/*判断pastack所指的栈是否为空栈,当pastack所指的栈为空栈时,则返回1,否则返回0*/
int isEmptyStack_seq( PSeqStack pastack );

/* 在栈中压入一元素x */
void push_seq( PSeqStack pastack, DataType x );

/* 删除栈顶元素 */
void pop_seq( PSeqStack pastack );

/* 当pastack所指的栈不为空栈时,求栈顶元素的值 */
DataType top_seq( PSeqStack pastack );

/**************
sstack.c
***************/

/* 顺序栈表示:函数定义 */

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

#include "sstack.h"

/*创建一个空栈;为栈结构申请空间,并将栈顶变量赋值为-1*/
PSeqStack createEmptyStack_seq( void ) {
PSeqStack pastack = (PSeqStack)malloc(sizeof(struct SeqStack));
if (pastack==NULL)
printf("Out of space!! \n");
else
pastack->t = -1;
return pastack;
}

/*判断pastack所指的栈是否为空栈,当pastack所指的栈为空栈时,则返回1,否则返回0*/
int isEmptyStack_seq( PSeqStack pastack ) {
return pastack->t == -1;
}

/* 在栈中压入一元素x */
void push_seq( PSeqStack pastack, DataType x ) {
if( pastack->t >= MAXNUM - 1 )
printf( "Stack Overflow! \n" );
else {
pastack->t++;
pastack->s[pastack->t] = x;
}
}

/* 删除栈顶元素 */
void pop_seq( PSeqStack pastack ) {
if (pastack->t == -1 )
printf( "Underflow!\n" );
else
pastack->t--;
}

/* 当pastack所指的栈不为空栈时,求栈顶元素的值 */
DataType top_seq( PSeqStack pastack ) {
return pastack->s[pastack->t];
}

❸ 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语言编写一个程序实现顺序栈的初始化,出栈和入栈。急需,谢谢

#defineSTACK_SIZE100
#definePUSH_POP_SUCCESS1
#definePUSH_POP_ERROR0

struct_stackbuf{
int_collection[STACK_SIZE];
int_top;
};
typedefstruct_stackbufS_STACK;
typedefunsignedintu_int_f;

//入栈
u_int_fpush(S_STACK*stack,intd){
if(stack->_top>=STACK_SIZE)returnPUSH_POP_ERROR;
stack->_collection[stack->_top++]=d;
returnPUSH_POP_SUCCESS;
}

//出栈
u_int_fpop(S_STACK*stack,int*e){
if(!stack->_top)returnPUSH_POP_ERROR;
*e=stack->_collection[--(stack->_top)];
returnPUSH_POP_SUCCESS;
}


intmain(){
S_STACKstack={{0},0};
push(&stack,1);
push(&stack,2);
push(&stack,3);

intgv=0;
pop(&stack,&gv);
printf("%d ",gv);

system("PAUSE");
return0;
}

❺ c语言 出栈 入栈实现

1.声明个数组a[10];
2.编写入栈函数push(int x)
取得当前栈顶索引,将x存于数组a的索引处,返回成功与否。
3.编写出栈函数pop()
取得当前栈顶索引,将数组a中索引处的值取出,返回给调用函数
4.将上述过程用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<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语言堆栈编程实现问题

修改完成:
#define MAXLEN 1000
#define EMPTY -1
#define FULL (MAXLEN-1)
#include <stdio.h>
typedef enum boolean
{
false, true
} boolean;
typedef struct stack
{
char s[MAXLEN];
int top;
} stack;
void reset(stack *stk)
{
stk->top = EMPTY;
}
void push(char c, stack *stk)
{
stk->top++;
stk->s[stk->top] = c;
}
char pop(stack *stk)
{
return (stk->s[stk->top--]);
}
void top(const stack *stk)
{
stk->s[stk->top];
}
boolean empty(const stack *stk)
{
return((boolean)(stk->top==EMPTY));
}
boolean full(const stack *stk)
{
return((boolean)(stk->top==FULL));
}
int main(void)
{
char str[] = "my name is laura";
int i;
stack s;
reset(&s);
printf("in the string: %s\n", str);
for (i = 0; str[i] != '\0'; ++i)
if(!full(&s))
push(str[i],&s);
printf("from the stack: ");
while (!empty(&s))
putchar(pop(&s));
putchar('\n');
return 0;
}

❾ 用C语言实现栈的操作,包括创建空栈,PUSH,和POP。用标准C,就是能在TC上运行的。

...中国出的这些书太应试化了,如果真心想学买一本机械工业出版社的数据结构(C语言版)写的很好,算法都是有C描述的包括你需要的东西...我懒得打那么长的算法程序了...

❿ C语言中用栈实现迷宫问题

#include
#define MAXSIZE 100
using namespace std;
struct stack{
int iway;
int jway;
int dire;
};
stack maze[MAXSIZE];
int top;
int map[14][28]={{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,1,1,1,1,1},
{1,1,0,0,1,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,1,0,0,1,1,0,0,1},
{1,0,0,0,0,0,0,0,1,1,1,0,0,0,1,0,0,0,0,1,1,0,1,1,1,1,0,1},
{1,0,0,0,0,0,0,0,1,1,1,0,1,1,1,1,0,0,0,1,1,0,1,1,1,1,0,1},
{1,0,0,0,0,0,0,0,1,1,1,0,0,0,1,0,0,0,0,1,1,0,0,1,1,0,0,1},
{1,0,0,0,0,0,0,0,1,1,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,1,1,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,1,1,0,1,1,1,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}};

void findway(int xS,int yS,int xE,int yE)
{
top=0;
maze[top].iway=xS;
maze[top].jway=yS;
map[xS][yS]=-1;
int i,j,di,find;
while(top>-1)
{
i=maze[top].iway;
j=maze[top].jway;
di=maze[top].dire;
if(i==xE&&j==yE)
{
cout<<"***********************************\n";
cout<<"path"<<":"<<endl;
cout<<"("<<maze[0].iway<<","<<maze[0].jway<<")";
for(int val=1;val<top+1;val++)
{
cout<("<<maze[val].iway<<","<<maze[val].jway<<")";
if((val+1)%5==0)
cout<<endl;
}
cout<<endl;
cout<<"***********************************\n";
return;
}
find=0;
while(find==0&&di<4)
{
di++;
switch(di)
{
case(0):i=maze[top].iway;
j=maze[top].jway+1;
break;
case(1):i=maze[top].iway;
j=maze[top].jway-1;
break;
case(2):i=maze[top].iway+1;
j=maze[top].jway;
break;
case(3):i=maze[top].iway-1;
j=maze[top].jway;
break;
}
if(map[i][j]==0)
{
find=1;
}
}
if(find==1)
{
maze[top].dire=di;
top++;
maze[top].iway=i;
maze[top].jway=j;
maze[top].dire=-1;
map[i][j]=-1;
}
else
{
map[maze[top].iway][maze[top].jway]=0;
top--;
}
}
}
int main()
{
for(int i=0;i<14;i++) //迷宫图形化输出
{
for(int j=0;j<28;j++)
{
if(map[i][j]==1)
cout<<"■";
else cout<<"□";
}
cout<<endl;
}
int xStart,yStart,xEnd,yEnd;
cout<<"请输入迷宫起点坐标,用空格隔开(左上角坐标为(0,0)):";
cin>>xStart>>yStart;
cout<<"请输入迷宫终点坐标,用空格隔开(右上角坐标为(13,27)):";
cin>>xEnd>>yEnd;
findway(xStart,yStart,xEnd,yEnd);
return 0;
}
满意请采纳!

热点内容
好医生连锁店密码多少 发布:2024-09-20 05:09:38 浏览:14
魔兽脚本代理 发布:2024-09-20 05:09:35 浏览:96
python登陆网页 发布:2024-09-20 05:08:39 浏览:755
安卓qq飞车如何转苹果 发布:2024-09-20 04:54:30 浏览:177
存储过程中in什么意思 发布:2024-09-20 04:24:20 浏览:314
php显示数据 发布:2024-09-20 03:48:38 浏览:499
源码安装软件 发布:2024-09-20 03:44:31 浏览:353
入门编程游戏的书 发布:2024-09-20 03:31:26 浏览:235
e盒的算法 发布:2024-09-20 03:30:52 浏览:143
win10登录密码如何修改登录密码 发布:2024-09-20 03:09:43 浏览:70