當前位置:首頁 » 編程語言 » 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-11-24 10:54:18 瀏覽:597
uniqidphp 發布:2024-11-24 10:54:15 瀏覽:658
linux設備驅動程序pdf 發布:2024-11-24 10:40:26 瀏覽:804
金盾pdf加密提取 發布:2024-11-24 10:37:01 瀏覽:809
sqlserver2005報表 發布:2024-11-24 10:33:23 瀏覽:584
直男Qq密碼一般會設成什麼 發布:2024-11-24 10:28:00 瀏覽:198
蘋果怎麼傳照片藍牙到安卓 發布:2024-11-24 10:21:04 瀏覽:856
比亞迪秦值得買哪個配置 發布:2024-11-24 10:20:23 瀏覽:73
河北智慧網關伺服器雲伺服器 發布:2024-11-24 10:20:22 瀏覽:761
linux啟動服務命令 發布:2024-11-24 10:15:25 瀏覽:703