数据结构4(栈和队列之林大锐格)
#include <iostream>
#include <bits/stdc++.h>
using namespace std
;
#define maxleng 100
typedef int ElemType
;
typedef struct link
{
ElemType elem
;
struct link
*next
;
}* linkstack
;
void initstack
(linkstack
&s
)
{
s
=NULL;
}
void push_link(linkstack
&s
,ElemType e
)
{
linkstack p
;
p
=new link
;
p
->elem
=e
;
p
->next
=s
;
s
=p
;
}
void pop_link(linkstack
&s
)
{
linkstack p
;
while(s
!=NULL)
{
p
=s
;printf("%d ",p
->elem
);s
=s
->next
;
}
}
int main()
{ linkstack s
;
initstack(s
);
ElemType m
,i
;
while(~ scanf("%d",&m
)&&m
!=0)
{
push_link(s
,m
);
}
pop_link(s
);
return 0;
}
#include <iostream>
#include <bits/stdc++.h>
using namespace std
;
#define maxleng 100
typedef int ElemType
;
typedef struct link
{
ElemType elem
[maxleng
];
int top
;
}sqstack
;
void initstack(sqstack
&s
)
{
s
.top
=0;
}
int seg_push(sqstack
&s
,ElemType e
)
{
if(s
.top
>maxleng
)
{
return 1;
}
s
.elem
[s
.top
]=e
;
s
.top
++;
return 1;
}
void seg_pop(sqstack
&s
)
{
while(s
.top
!=0)
{
printf("%d ",s
.elem
[s
.top
-1]);
s
.top
--;
}
}
int main()
{ int n
,m
,j
,e
;
sqstack s
;
initstack(s
);
while(~scanf("%d",&e
)&&e
!=0)
{
seg_push(s
,e
);
}
seg_pop(s
);
return 0;
}
转载请注明原文地址: https://lol.8miu.com/read-35614.html