关于栈

h248080441 2011-03-07 09:30:48
请问如果要使用数据结构“栈”,那是否必须自己先定义,并且其操作函数例如创建栈,清空栈,Pop,Push等都要自己定义后才能使用?是否有现成的函数来创建及操作栈?
...全文
245 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2011-03-08
  • 打赏
  • 举报
回复
要知道你的程序本来就在使用栈,不然怎么实现函数调用和返回的?
为什么要自己再创建一个栈呢?多半是因为程序本来提供的栈空间不够大。
hicaru000000 2011-03-08
  • 打赏
  • 举报
回复
对于嵌入式,有些编译器会自动出入栈系统内寄存器。
dcw0402 2011-03-08
  • 打赏
  • 举报
回复
用stl模板吧
AnYidan 2011-03-08
  • 打赏
  • 举报
回复
Stack 是一种数据结构,用于管理内存,在不同的环境中,有不同的实现,
单片机中 -- 汇编提供的有现成的 (c 语言一般没有)
pc 上的 C 语言也没有
pc 上的 C++ 语言有

楼主在哪中环境下编程?
fengzhw 2011-03-08
  • 打赏
  • 举报
回复
找一本数据结构的书,自己照样写一个C的栈就行了。
至于“栈”这样强大而有用的东西为啥还要一次次重复写,

C的答案是:程序员自己写呗,因为每个人想往栈里头放的东西都不一样。

C++的答案是:好吧,模板技术可以在变(保存的数据)和不变(操作的方式)之间做到统一。

cao_julians 2011-03-08
  • 打赏
  • 举报
回复
1.在学习的过程中,还是自己编写实现一个栈的全部操作函数包,对自己的能力是个检验和培养
2.在工作的过程中,还是使用久经考验的STL/stack,仅当它的性能达不到特别要求时,再自己编写。
nanbazhangbiao 2011-03-08
  • 打赏
  • 举报
回复
c语言没有现成的栈定义。。要自己写。不过可以参考STL中对栈的实现。
yjjlyyj151 2011-03-08
  • 打赏
  • 举报
回复
用STL
h248080441 2011-03-07
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 pengzhixi 的回复:]
额,C的话,自己实现吧。
[/Quote]
好像STL是C++下的。我是在C下用的,那么得自己定义咯?
pengzhixi 2011-03-07
  • 打赏
  • 举报
回复
额,C的话,自己实现吧。
碎碎念 2011-03-07
  • 打赏
  • 举报
回复
可以自己编写栈的结构体,也可以用现成的STL库...
dingshaofengbinbin 2011-03-07
  • 打赏
  • 举报
回复
用现成的啊!!
<stack.h>
自己定义也可以啊!!
無_1024 2011-03-07
  • 打赏
  • 举报
回复
在那个STL中有stack的容器已经历过定义了一些基本的用法

#include <stdio.h>
#include <malloc.h>
typedef struct STACK
{
int data;
struct STACK *next;
}Stack;

//初始化
void InitStack(Stack *&s)
{

s=(Stack *)malloc(sizeof(Stack));
s->next=NULL;
}

//释放栈
void ClearStack(Stack *s)
{
Stack *p=s->next;
while(p!=NULL)
{
free(s);
s=p;
p=p->next;
}
}

//判断栈是否为空
int StackEmpty(Stack *s)
{
return(s->next==NULL);
}

int length(Stack *s)
{
int i=0;
Stack *p;
p=s->next;
while(p!=NULL)
{
i++;
p=p->next;
}
return i;
}

//得到栈顶元素
int GetTop(Stack *S,int &e)
{
if(S->next==NULL)
return 0;
e=S->next->data;
return 1;
}

//入栈
void Push(Stack *s,int e)
{
Stack *p;
p=(Stack *)malloc(sizeof(Stack));
p->data=e;
p->next=s->next;
s->next=p;

}

//出栈
int Pop(Stack *s,int &e)
{
Stack *p;
if(s->next==NULL)
{
printf("空栈...\n");
return 0;
}
p=s->next;
e=p->data;
s->next=p->next;
free(p);
return 1;
}

//输出栈的元素
void OutputStack(Stack *s)
{
Stack *p;
p=s->next;
if(s->next==NULL) {
printf("空栈...\n");
return;
}
while(p!=NULL) {
printf("%4d",p->data);
p=p->next;
}
printf("\n");
}

int main()
{
int e;
Stack *s;
printf("初始化栈:\n");
InitStack(s);
printf("依次进栈元素1,2,3,4,5\n");
Push(s,1);
Push(s,2);
Push(s,3);
Push(s,4);
Push(s,5);
printf("栈为%s\n",(StackEmpty(s)?"空":"非空"));
printf("栈的长度:%d\n",length(s));
printf("从栈顶到栈底的元素:");
OutputStack(s);
printf("出栈序列:");
while(!StackEmpty(s))
{
Pop(s,e);
printf("%d",e);
}
ClearStack(s);
system("pause");
}

delphiwcdj 2011-03-07
  • 打赏
  • 举报
回复
建议自己实现一遍较好
delphiwcdj 2011-03-07
  • 打赏
  • 举报
回复
Stack Adaptor

There are five operations provided by a stack are listed as below:

s.empty()
Returns true if the stack is empty; false otherwise.

s.size()
Returns a count of the number of elements on the stack.

s.pop()
Removes, but does not return, the top element from the stack.

s.top()
Returns, but does not remove, the top element on the stack.

s.push(item)
Places a new top element on the stack.
delphiwcdj 2011-03-07
  • 打赏
  • 举报
回复
一个简单例子

/*
C++ Primer 9.42 P.302 栈适配器
编写程序读入一系列单词,并将它们存储在stack对象中,然后逆序输出。
*/
#include <iostream>
#include <string>
#include <stack>
using namespace std;

int main()
{
stack<string> sstack;
string str;

// 读入单词并存储在stack对象中
cout<<"Enter some words(Ctrl+Z to end):"<<endl;// 按两次"Ctrl+z"退出

while (cin>>str)
{
sstack.push(str);// 在栈顶压入元素
}

cout<<"The size of stack is: "<<sstack.size()<<endl;// 返回栈中元素的个数

if (sstack.empty())// 如果栈为空,则返回true,否则返回false
{
cout<<"The stack is empty"<<endl;
}

while(!sstack.empty())
{
str=sstack.top();// 返回栈顶元素,但不删除该元素
cout<<str<<endl;
sstack.pop();// 删除栈顶元素,但不返回其值
}

return 0;
}

h248080441 2011-03-07
  • 打赏
  • 举报
回复
那是不是说,比如编写一个程序,里面需要用到栈这种数据结构,那么我必须先定义栈,然后定义其操作函数pop,push等。如果没有定义的话就不能使用栈这种数据结构了
请问2L你说的STL要如何使用
delphiwcdj 2011-03-07
  • 打赏
  • 举报
回复
参考
http://www.cplusplus.com/reference/stl/stack/
http://blog.csdn.net/housisong/archive/2005/10/17/505254.aspx
delphiwcdj 2011-03-07
  • 打赏
  • 举报
回复
可以自己定义。现成的可以用stl中的stack

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧