👨💻个人主页:@元宇宙-秩沅
hallo 欢迎 点赞👍 收藏⭐ 留言📝 加关注✅!
本文由 秩沅 原创
收录于专栏 C++
include
include
define maxsize 5
using namespace std;
typedef struct//栈顺序表的定义
{
int base;//尾指针
int top;//头指针
int stacksize;//栈的分配空间
}sqstact;
sqstact s;
bool initsqstact(sqstact& s)//初始化栈
{
s.base = new int[maxsize];
if (!s.base) { cout << "空间分配失败!" << endl; exit(0); }
s.top = s.base;
s.stacksize = maxsize;
return true;
}
int lengthsqtact(sqstact s)//判断栈的长度
{
return (s.top - s.base);
}
bool clearsqstact(sqstact& s)//清空栈
{
if (s.base) s.top = s.base;
return true;
}
bool push(sqstact& s)//进栈(一次性进栈)
{
cout << "请输入要进栈的值:" << endl;
if (s.top - s.base == s.stacksize) { cout << "已满栈无法进栈!" << endl; return false; }
while (s.top-s.base != s.stacksize)//到达最顶上的时候是为空的
{
int e; cin >> e;
*s.top++ = e;
}
return true;
}
bool pop(sqstact s)//出栈(一次性出栈)
{
if (s.top==s.base ) { cout<<"空栈无法出栈!" << endl; return false; }
do
{
int e; --s.top;
e = *s.top;
cout << e <<" ";
} while (s.top != s.base);
return true;
}
int main()
{
initsqstact(s);
push(s);
cout<<"出栈后的元素如下" << endl;
pop(s);
clearsqstact(s);
cout<<endl<<"栈清除成功!" << endl;
pop(s);
return 0;
}
include
include
define maxsize 6
using namespace std;
typedef struct
{
int top;
int base;
int stacksize;
}sqstact;
sqstact s;
bool initsqstact(sqstact& s)//初始话顺序栈
{
s.base = new int [maxsize];
if (!s.base) { cout<<"栈初始化失败!" << endl; exit(0); }
s.top = s.base;
s.stacksize = maxsize;
return true;
}
bool push(sqstact& s, int e)//进栈
{
if (s.top - s.base == s.stacksize) { cout<<"顺序栈已满进栈失败!" << endl; return false; }
s.top++ = e;
return true;
}
int pop(sqstact s)//出栈
{
int e;
if (s.base == s.top) { cout<<"空栈出栈失败!" << endl; return false; }
e = --s.top;
return e;
}
int lengthsqstact(sqstact s)
{
return s.top - s.base;
}
int main()
{
int e;
cout<<"请输入需要进栈的值!" << endl;
cin >> e;
initsqstact(s);
push(s, e);
cout << "出栈后的元素如下:" << endl;
cout << pop(s);
cout << endl << "此时栈的长度为:";
cout << endl << lengthsqstact(s) << endl;
return 0;
}/
/
include
include
define maxsize 1
using namespace std;
typedef struct
{
int top;
int base;
int stacksize;
}sqstact;
sqstact s;
bool initsqstact(sqstact& s)//初始话顺序栈
{
s.base = new int[maxsize];
if (!s.base) { cout << "栈初始化失败!" << endl; exit(0); }
s.top = s.base;
s.stacksize = maxsize;
return true;
}
bool push(sqstact &s,int e)//向下生长的栈.进栈
{
if (s.base - s.top == s.stacksize) { cout << "栈满!" << endl; return false; }
s.top-- = e;
return true;
}
int pop(sqstact s)
{
if (s.top == s.base) { cout<<"栈空!" << endl; exit(0); }
int e;
e = ++s.top;
return e;
}
int main()
{
int e;
cin >> e;
initsqstact(s);
push(s, e);
cout << endl << pop(s);
return 0;
}/
/
include
include
using namespace std;
typedef struct stactNode
{
int data;
struct stactNode* next;
}stactnode, * linkstact;
linkstact s;
bool initstact(linkstact& s)//没必要开设头节点,直接置它为空栈//链栈不可能满!
{
s->next = NULL;
return true;
}
bool push(linkstact &s,int e)//一次性进栈
{
while (e--)
{
linkstact p;
p = new stactnode;
cout<<"请输入需要进栈的元素:" << endl;
cin >> p->data;
p->next = s;
s = p;
}
return true;
}
bool pop(linkstact s)
{
if (!s){ cout << "栈空!" << endl; return false; }
linkstact q; q = s;
while (q)
{
cout << q->data << " ";
q = q->next;
}
return true;
}
int main()
{
int e; int m;
cout << "请输入需要进栈元素的数量" << endl;
cin >> e;
push(s, e);
pop(s);
return 0;
}/
/
include
include
define maxsize 3;
using namespace std;
typedef struct stactNode
{
int data;
struct stactNode next;
}stactnode,linkstact;
linkstact s;
bool initstact(linkstact& s)
{
s= NULL;
return true;
}
int push(linkstact& s)
{
static int m = maxsize;
linkstact q;
q = new stactnode;
cout<<"请输入要进入栈的值:" << endl;
cin >> q->data;
q->next = s;
s = q; m--;
if (m == 0) return 1;
else push(s);
}
int pop(linkstact s)
{
if (s == NULL)return 1;
else {
cout << s->data << " ";
pop(s->next);
}
}
int main()
{
initstact(s);
push(s); pop(s);
return 0;
}*/
include
include
define maxsize 5
using namespace std;
typedef struct {
int top;
int base;
int stactsize;
}sqstact;
sqstact s;
bool initsqstact(sqstact& s)//初始化
{
s.base = new int[maxsize];
if (!s.base) { cout<<"初始化失败!" << endl; exit(1); }
s.top = s.base;
s.stactsize = maxsize;
return true;
}
bool push(sqstact& s)//顺序栈的进栈
{
if (s.top - s.base == s.stactsize) { cout<<"栈满!" << endl; return false; }
int e;
cout<<"请输入需要进栈的数量:" << endl;
cin >> e;
while (e--)
{
int m; cin >> m;
s.top++ = m;
}
return true;
}
bool pop(sqstact s)
{
if (s.base == s.top) { cout<<"栈空!" << endl; return false; }
while (s.top != s.base)
{
cout <<--s.top<<" ";
}
return true;
}
int main()
{
initsqstact(s);
push(s);
pop(s);
return 0;
}
/*#include
include
define maxsize 5
using namespace std;
typedef struct//栈顺序表的定义
{
int base;//尾指针
int top;//头指针
int stacksize;//栈的分配空间
}sqstact;
sqstact s;
bool initsqstact(sqstact& s)//初始化栈
{
s.base = new int[maxsize];
if (!s.base) { cout << "空间分配失败!" << endl; exit(0); }
s.top = s.base;
s.stacksize = maxsize;
return true;
}
int lengthsqtact(sqstact s)//判断栈的长度
{
return (s.top - s.base);
}
bool clearsqstact(sqstact& s)//清空栈
{
if (s.base) s.top = s.base;
return true;
}
bool push(sqstact& s)//进栈(一次性进栈)
{
cout << "请输入要进栈的值:" << endl;
if (s.top - s.base == s.stacksize) { cout << "已满栈无法进栈!" << endl; return false; }
while (s.top-s.base != s.stacksize)//到达最顶上的时候是为空的
{
int e; cin >> e;
*s.top++ = e;
}
return true;
}
bool pop(sqstact s)//出栈(一次性出栈)
{
if (s.top==s.base ) { cout<<"空栈无法出栈!" << endl; return false; }
do
{
int e; --s.top;
e = *s.top;
cout << e <<" ";
} while (s.top != s.base);
return true;
}
int main()
{
initsqstact(s);
push(s);
cout<<"出栈后的元素如下" << endl;
pop(s);
clearsqstact(s);
cout<<endl<<"栈清除成功!" << endl;
pop(s);
return 0;
}*/
/*
include
include
define maxsize 6
using namespace std;
typedef struct
{
int top;
int base;
int stacksize;
}sqstact;
sqstact s;
bool initsqstact(sqstact& s)//初始话顺序栈
{
s.base = new int [maxsize];
if (!s.base) { cout<<"栈初始化失败!" << endl; exit(0); }
s.top = s.base;
s.stacksize = maxsize;
return true;
}
bool push(sqstact& s, int e)//进栈
{
if (s.top - s.base == s.stacksize) { cout<<"顺序栈已满进栈失败!" << endl; return false; }
s.top++ = e;
return true;
}
int pop(sqstact s)//出栈
{
int e;
if (s.base == s.top) { cout<<"空栈出栈失败!" << endl; return false; }
e = --s.top;
return e;
}
int lengthsqstact(sqstact s)
{
return s.top - s.base;
}
int main()
{
int e;
cout<<"请输入需要进栈的值!" << endl;
cin >> e;
initsqstact(s);
push(s, e);
cout << "出栈后的元素如下:" << endl;
cout << pop(s);
cout << endl << "此时栈的长度为:";
cout << endl << lengthsqstact(s) << endl;
return 0;
}/
/
include
include
define maxsize 1
using namespace std;
typedef struct
{
int top;
int base;
int stacksize;
}sqstact;
sqstact s;
bool initsqstact(sqstact& s)//初始话顺序栈
{
s.base = new int[maxsize];
if (!s.base) { cout << "栈初始化失败!" << endl; exit(0); }
s.top = s.base;
s.stacksize = maxsize;
return true;
}
bool push(sqstact &s,int e)//向下生长的栈.进栈
{
if (s.base - s.top == s.stacksize) { cout << "栈满!" << endl; return false; }
s.top-- = e;
return true;
}
int pop(sqstact s)
{
if (s.top == s.base) { cout<<"栈空!" << endl; exit(0); }
int e;
e = ++s.top;
return e;
}
int main()
{
int e;
cin >> e;
initsqstact(s);
push(s, e);
cout << endl << pop(s);
return 0;
}/
/
include
include
using namespace std;
typedef struct stactNode
{
int data;
struct stactNode* next;
}stactnode, * linkstact;
linkstact s;
bool initstact(linkstact& s)//没必要开设头节点,直接置它为空栈//链栈不可能满!
{
s->next = NULL;
return true;
}
bool push(linkstact &s,int e)//一次性进栈
{
while (e--)
{
linkstact p;
p = new stactnode;
cout<<"请输入需要进栈的元素:" << endl;
cin >> p->data;
p->next = s;
s = p;
}
return true;
}
bool pop(linkstact s)
{
if (!s){ cout << "栈空!" << endl; return false; }
linkstact q; q = s;
while (q)
{
cout << q->data << " ";
q = q->next;
}
return true;
}
int main()
{
int e; int m;
cout << "请输入需要进栈元素的数量" << endl;
cin >> e;
push(s, e);
pop(s);
return 0;
}/
/
include
include
define maxsize 3;
using namespace std;
typedef struct stactNode
{
int data;
struct stactNode next;
}stactnode,linkstact;
linkstact s;
bool initstact(linkstact& s)
{
s= NULL;
return true;
}
int push(linkstact& s)
{
static int m = maxsize;
linkstact q;
q = new stactnode;
cout<<"请输入要进入栈的值:" << endl;
cin >> q->data;
q->next = s;
s = q; m--;
if (m == 0) return 1;
else push(s);
}
int pop(linkstact s)
{
if (s == NULL)return 1;
else {
cout << s->data << " ";
pop(s->next);
}
}
int main()
{
initstact(s);
push(s); pop(s);
return 0;
}*/
include
include
define maxsize 5
using namespace std;
typedef struct {
int top;
int base;
int stactsize;
}sqstact;
sqstact s;
bool initsqstact(sqstact& s)//初始化
{
s.base = new int[maxsize];
if (!s.base) { cout<<"初始化失败!" << endl; exit(1); }
s.top = s.base;
s.stactsize = maxsize;
return true;
}
bool push(sqstact& s)//顺序栈的进栈
{
if (s.top - s.base == s.stactsize) { cout<<"栈满!" << endl; return false; }
int e;
cout<<"请输入需要进栈的数量:" << endl;
cin >> e;
while (e--)
{
int m; cin >> m;
s.top++ = m;
}
return true;
}
bool pop(sqstact s)
{
if (s.base == s.top) { cout<<"栈空!" << endl; return false; }
while (s.top != s.base)
{
cout <<--s.top<<" ";
}
return true;
}
int main()
{
initsqstact(s);
push(s);
pop(s);
return 0;
}
你们的点赞👍 收藏⭐ 留言📝 关注✅是我持续创作,输出优质内容的最大动力!
栓Q