當前位置:九游会j9娱乐平台-九游ag登录中心网址 » 編程語言 » c語言棧的實現

c語言棧的實現-九游会j9娱乐平台

發布時間: 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
#include

#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
#include
//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

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
#include
#include
#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
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"<<":"<cout<<"("<for(int val=1;val{
cout<("<if((val 1)%5==0)
cout<}
cout<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<}
int xstart,ystart,xend,yend;
cout<<"請輸入迷宮起點坐標,用空格隔開(左上角坐標為(0,0)):";
cin>>xstart>>ystart;
cout<<"請輸入迷宮終點坐標,用空格隔開(右上角坐標為(13,27)):";
cin>>xend>>yend;
findway(xstart,ystart,xend,yend);
return 0;
}
滿意請採納!

熱點內容
resin下jsp不能正常編譯 發布:2024-07-17 16:34:44 瀏覽:229
sqlserver如何切換主備伺服器 發布:2024-07-17 16:23:02 瀏覽:299
mc18伺服器ip 發布:2024-07-17 16:23:02 瀏覽:379
仙境傳說手游腳本 發布:2024-07-17 16:09:24 瀏覽:691
matlab命令窗口和新建腳本 發布:2024-07-17 15:51:26 瀏覽:375
建ftp文件夾 發布:2024-07-17 15:51:26 瀏覽:955
魔獸撿物腳本 發布:2024-07-17 15:27:56 瀏覽:130
開發ip伺服器 發布:2024-07-17 15:24:42 瀏覽:388
安卓系統視頻製作哪個好用 發布:2024-07-17 15:10:47 瀏覽:210
androidapk結構 發布:2024-07-17 15:10:43 瀏覽:945
网站地图