顺序栈
#include<cstdio>
#include<cmath>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<map>
#include<vector>
#define N 100005
#define ll long long
using namespace std;
#define size 100
#define STACKINCREAMENT 10
struct sqstack {
int *base;
int *top;
int stacksize;
};
int initstack(sqstack &s) {
s.base=new int[size];
if(!s.base) {
return 0;
}
s.top=s.base;
s.stacksize=size;
return 1;
}
int empty(sqstack s) {
if(s.base==s.top)
return 0;
return 1;
}
int top(sqstack s) {
return *(s.top-1);
}
int push(sqstack &s,int e) {
if(s.top-s.base==s.stacksize)
return 0;
*s.top++=e;
return 1;
}
int pop(sqstack &s) {
int e;
if(s.top==s.base) {
return 0;
}
e=*--s.top;
return e;
}
int main() {
sqstack s;
initstack(s);
int n;
cout<<"输入一个非负十进制数:"<<endl;
while(cin>>n) {
while(n) {
int temp=n%8;
n/=8;
push(s,temp);
}
cout<<"与其等值的八进制数是:"<<endl;
while(empty(s)) {
int e;
cout<<pop(s);
}
cout<<endl;
}
return 0;
}
链栈
#include<cstdio>
#include<cmath>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<map>
#include<vector>
#define N 100005
#define ll long long
using namespace std;
struct Lnode {
int data;
Lnode *next;
};
int initstack(Lnode *&s) {
s=NULL;
return 0;
}
int empty(Lnode *&L) {
if(L!=NULL)
return 1;
return 0;
}
int push(Lnode *&L,int e) {
Lnode *p;
p=new Lnode;
p->data=e;
p->next=L;
L=p;
return 1;
}
int top(Lnode *L) {
if(L!=NULL)
return L->data;
}
int pop(Lnode *&L,int e) {
if(L==NULL)
return 0;
e=L->data;
Lnode *p;
p=L;
L=L->next;
delete p;
return e;
}
int main() {
Lnode *head;
initstack(head);
int n;
cout<<"输入一个非负十进制数:"<<endl;
cin>>n;
while(n) {
int temp=n%8;
n/=8;
push(head,temp);
}
cout<<"与其等值的八进制数是:"<<endl;
while(empty(head)) {
int e;
cout<<pop(head,e);
}
return 0;
}
。。还是对 指针 领悟不够。。再次学习一下 https://blog.csdn.net/raoshihong/article/details/42580657