数据结构--栈相关操作的代码:

简介: 数据结构--栈相关操作的代码:

👨‍💻个人主页:@元宇宙-秩沅

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

目录
相关文章
|
前端开发 Java
java实现队列数据结构代码详解
本文详细解析了Java中队列数据结构的实现,包括队列的基本概念、应用场景及代码实现。队列是一种遵循“先进先出”原则的线性结构,支持在队尾插入和队头删除操作。文章介绍了顺序队列与链式队列,并重点分析了循环队列的实现方式以解决溢出问题。通过具体代码示例(如`enqueue`入队和`dequeue`出队),展示了队列的操作逻辑,帮助读者深入理解其工作机制。
631 1
|
存储 C语言 C++
【C++数据结构——栈与队列】顺序栈的基本运算(头歌实践教学平台习题)【合集】
本关任务:编写一个程序实现顺序栈的基本运算。开始你的任务吧,祝你成功!​ 相关知识 初始化栈 销毁栈 判断栈是否为空 进栈 出栈 取栈顶元素 1.初始化栈 概念:初始化栈是为栈的使用做准备,包括分配内存空间(如果是动态分配)和设置栈的初始状态。栈有顺序栈和链式栈两种常见形式。对于顺序栈,通常需要定义一个数组来存储栈元素,并设置一个变量来记录栈顶位置;对于链式栈,需要定义节点结构,包含数据域和指针域,同时初始化栈顶指针。 示例(顺序栈): 以下是一个简单的顺序栈初始化示例,假设用C语言实现,栈中存储
1021 77
|
11月前
|
编译器 C语言 C++
栈区的非法访问导致的死循环(x64)
这段内容主要分析了一段C语言代码在VS2022中形成死循环的原因,涉及栈区内存布局和数组越界问题。代码中`arr[15]`越界访问,修改了变量`i`的值,导致`for`循环条件始终为真,形成死循环。原因是VS2022栈区从低地址到高地址分配内存,`arr`数组与`i`相邻,`arr[15]`恰好覆盖`i`的地址。而在VS2019中,栈区先分配高地址再分配低地址,因此相同代码表现不同。这说明编译器对栈区内存分配顺序的实现差异会导致程序行为不一致,需避免数组越界以确保代码健壮性。
244 0
栈区的非法访问导致的死循环(x64)
|
11月前
232.用栈实现队列,225. 用队列实现栈
在232题中,通过两个栈(`stIn`和`stOut`)模拟队列的先入先出(FIFO)行为。`push`操作将元素压入`stIn`,`pop`和`peek`操作则通过将`stIn`的元素转移到`stOut`来实现队列的顺序访问。 225题则是利用单个队列(`que`)模拟栈的后入先出(LIFO)特性。通过多次调整队列头部元素的位置,确保弹出顺序符合栈的要求。`top`操作直接返回队列尾部元素,`empty`判断队列是否为空。 两题均仅使用基础数据结构操作,展示了栈与队列之间的转换逻辑。
|
存储 C++ 索引
【C++数据结构——栈与队列】环形队列的基本运算(头歌实践教学平台习题)【合集】
【数据结构——栈与队列】环形队列的基本运算(头歌实践教学平台习题)【合集】初始化队列、销毁队列、判断队列是否为空、进队列、出队列等。本关任务:编写一个程序实现环形队列的基本运算。(6)出队列序列:yzopq2*(5)依次进队列元素:opq2*(6)出队列序列:bcdef。(2)依次进队列元素:abc。(5)依次进队列元素:def。(2)依次进队列元素:xyz。开始你的任务吧,祝你成功!(4)出队一个元素a。(4)出队一个元素x。
557 13
【C++数据结构——栈与队列】环形队列的基本运算(头歌实践教学平台习题)【合集】
|
算法 调度 C++
STL——栈和队列和优先队列
通过以上对栈、队列和优先队列的详细解释和示例,希望能帮助读者更好地理解和应用这些重要的数据结构。
370 11
☀☀☀☀☀☀☀有关栈和队列应用的oj题讲解☼☼☼☼☼☼☼
### 简介 本文介绍了三种数据结构的实现方法:用两个队列实现栈、用两个栈实现队列以及设计循环队列。具体思路如下: 1. **用两个队列实现栈**: - 插入元素时,选择非空队列进行插入。 - 移除栈顶元素时,将非空队列中的元素依次转移到另一个队列,直到只剩下一个元素,然后弹出该元素。 - 判空条件为两个队列均为空。 2. **用两个栈实现队列**: - 插入元素时,选择非空栈进行插入。 - 移除队首元素时,将非空栈中的元素依次转移到另一个栈,再将这些元素重新放回原栈以保持顺序。 - 判空条件为两个栈均为空。
|
存储 C语言 C++
【C++数据结构——栈与队列】链栈的基本运算(头歌实践教学平台习题)【合集】
本关任务:编写一个程序实现链栈的基本运算。开始你的任务吧,祝你成功!​ 相关知识 初始化栈 销毁栈 判断栈是否为空 进栈 出栈 取栈顶元素 初始化栈 概念:初始化栈是为栈的使用做准备,包括分配内存空间(如果是动态分配)和设置栈的初始状态。栈有顺序栈和链式栈两种常见形式。对于顺序栈,通常需要定义一个数组来存储栈元素,并设置一个变量来记录栈顶位置;对于链式栈,需要定义节点结构,包含数据域和指针域,同时初始化栈顶指针。 示例(顺序栈): 以下是一个简单的顺序栈初始化示例,假设用C语言实现,栈中存储整数,最大
352 9
|
C++
【C++数据结构——栈和队列】括号配对(头歌实践教学平台习题)【合集】
【数据结构——栈和队列】括号配对(头歌实践教学平台习题)【合集】(1)遇到左括号:进栈Push()(2)遇到右括号:若栈顶元素为左括号,则出栈Pop();否则返回false。(3)当遍历表达式结束,且栈为空时,则返回true,否则返回false。本关任务:编写一个程序利用栈判断左、右圆括号是否配对。为了完成本关任务,你需要掌握:栈对括号的处理。(1)遇到左括号:进栈Push()开始你的任务吧,祝你成功!测试输入:(()))
500 7
|
存储 缓存 算法
在C语言中,数据结构是构建高效程序的基石。本文探讨了数组、链表、栈、队列、树和图等常见数据结构的特点、应用及实现方式
在C语言中,数据结构是构建高效程序的基石。本文探讨了数组、链表、栈、队列、树和图等常见数据结构的特点、应用及实现方式,强调了合理选择数据结构的重要性,并通过案例分析展示了其在实际项目中的应用,旨在帮助读者提升编程能力。
542 5

热门文章

最新文章