当前位置:首页 » 编程语言 » c语言中如何使用stack
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

c语言中如何使用stack

发布时间: 2023-01-31 22:25:34

c语言 stack STL应用

#include <iostream>
#include <Stack>
using namespace std;
typedef struct
{
int x;
int y;
}element;
void main()
{
stack<element> s;
element ele;
for( int i=0; i < 10; i++ )
{
ele.x = i;
ele.y = i;
s.push(ele);
}

while(!s.empty())
{
ele = s.top();
printf("%d %d ",ele.x,ele.y);
s.pop();
}
cout << "This stack has a size of " << s.size() << endl;
system("pause");
}


这样?

Ⅱ C语言栈的调用。

可以。
使用STL的stack,例如:
#include<cstdio>
#include<stack>
using namespace std;
stack<int>s;
int main()
{
int a;
scanf("%d",&a);
getchar();
s.push(a);
printf("%d\n",s.top());
s.pop();
getchar();
return 0;
}
另外,这是C++,C标准库没有STL。
----
以下关于stack的成员,抄自MSDN:
Constructors
stack
Constructs a stack that is empty or that is a of a base container object.

Typedefs
container_type
A type that provides the base container to be adapted by a stack.

size_type
An unsigned integer type that can represent the number of elements in a stack.

value_type
A type that represents the type of object stored as an element in a stack.

Member Functions
empty
Tests if the stack is empty.

pop
Removes the element from the top of the stack.

push
Adds an element to the top of the stack.

size
Returns the number of elements in the stack.

top
Returns a reference to an element at the top of the stack.

Ⅲ C语言如何使用堆栈判断回文

#include <iosteam>
#include <string>
using namespace std;

#define EMPTY 0
#define FULL 10000
#define MAX 10000

typedef char data;

typedef struct elem {
data d;
struct elem *next;
}elem;

typedef struct stack {
int cnt;
elem *top;
}stack;

void initialize(stack *stk);
void push(data d, stack *stk);
data pop(stack *stk);
bool empty(const stack *stk);
bool full(const stack *stk); //栈操作函数

void initialize(stack *stk)
{
stk->cnt = 0;
stk->top = NULL;
}

bool empty(const stack *stk)
{
return stk->cnt == EMPTY;
}

bool full(const stack *stk)
{
return stk->cnt == FULL;
}

void push(data d, stack *stk)
{
elem *p;

if (!full(stk))
{
p = (elem *)malloc(sizeof(elem));
p->d = d;
p->next = stk->top;
stk->top = p;
stk->cnt++;
}

}

data pop(stack *stk)
{
data d;
elem *p;

if(!empty(stk))
{
d = stk->top->d;
p = stk->top;
stk->top = stk->top->next;
stk->cnt--;
delete p;
}

return d;
}

int main(void)
{
data input[MAX];
stack temp;
int i = 0;
int flag = 0;

initialize(&temp); //初始化临时栈
cin>>&input; //输入字符串

while (input[i] != '@')
{//字符串入栈
push(input[i], &temp);
i++;
}

while (!empty(&temp))
{//字符依次出栈和字符数组比较,判断是否回文数
if (temp.top->d == input[flag])
{
pop(&temp);
flag++;
}
else
{
cout<<"此字符序列不是回文数!\n";
break;
}
}

if (empty(&temp))
cout<<"此字符序列是回文数!\n";

return 1;
}

Ⅳ 那位大神能讲下C语言中栈的使用啊

堆栈就是先入后出的数据结构。
如果用c语言来实现的话用个struct
先定义一个栈的节点
struct
node;
typedef
strcut
node
*
position;
typedef
position
stack;
stack
creat();
void
push(int,stack);
int
pop(stack);
int
top();
struct
node
{
int
element;
position
next;
}
stack
creat()
{
stack
s;
s=malloc(sizeof(struct
node));
s->next==NULL;
}
void
push(int
a,stack
s)
{
position
temp;
temp=malloc(sizeof(struct
node));
temp->element=a;
temp->next=s->next;:
s->next=temp;
}
int
pop(stack
s)
{
int
res=s->next->element;
position
temp=s->next;
s->next=temp->next;
free
temp;
}
int
top(stack
s)
{
return
s->next->element;
}
好啦,先creat()一个栈,再进行push
pop等。程序中忽略了麻烦的错误检测给出了重点,当然还可以添加其他操作。。对了,头文件也要加上。本人还是习惯用c++弄成一个类^_^

Ⅳ 请问c语言有没有像c++一样的stack库函数用来直接使用栈

C语言中包含一些标准的库函数,但是没有像C++中的STL容器那部分全面的结构和函数。
在C语言中如果想使用栈,需要自己编写代码,如果是简单的一次性应用,可以用数组模拟栈的功能,如果是在一个大项目中反复使用,可以自己写一个stack的库函数。
自定义的库函数中,至少应该包含初始化、销毁、入栈、出栈、取栈顶元素、判断栈是否为空等操作。

Ⅵ C语言中使用stack头文件

stack < item >s; //栈元素类型要定义成item
s.push(temp); //这样才可以压入结构体数据

Ⅶ 基于C语言堆栈push,pop,destroystack,isEmpty,isFull实现

以下代码是基于C语言写的堆栈的压栈,出栈,清栈,读栈指针等方法,在Visual studio 中,可直接使用,供学习者参考学习。

#include

#include

#include

#include

#include

#include

#define MAX_SIZE 100

typedef struct Stack

{

char *data;

int size;

int top;

};

void initStack(Stack *s); //init stack

void destroyStack(Stack *s);

bool push(Stack *s,char ch);

char pop(Stack *s);

char gettop(Stack *s);

bool isEmpty(Stack *s);

bool isFull(Stack *s);

void setNull(Stack *s);

#endif

void initStack(Stack *s)

{

s->data = (char*)malloc(MAX_SIZE * sizeof(char)); //分配最大内存空间

if (!s->data)

exit(OVERFLOW); //提前终止程序

s->size = MAX_SIZE;

s->top = -1;

}

void destroyStack(Stack *s)

{

free(s->data);

}

bool push(Stack *s, char ch)

{

if ((s->top + 1) != s->size)

{

s->data[++s->top] = ch;

return true;

}

else

return false;

}

char pop(Stack *s)

{

if (s->top != -1)

return s->data[s->top--];

}

char gettop(Stack *s)

{

return s->data[s->top];

}

bool isEmpty(Stack *s)

{

if (s->top == -1)

return true;

else

return false;

}

bool isFull(Stack *s)

{

if ((s->top + 1) == s->size)

return true;

else

return false;

}

void setNull(Stack *s)

{

s->top = -1;

}

int main()

{

char chd;

bool c;

Stack s1;

initStack(&s1);

c = push(&s1, 'a');

printf("Stack s1 push status is %d,s.data is %c,top value is %d ", c,s1.data[s1.top],s1.top);

c = push(&s1, 'b');

printf("Stack s1 push status is %d,s.data is %c,top value is %d ", c, s1.data[s1.top], s1.top);

chd = gettop(&s1);

printf("Stack s1->top data:%c,top value is %d ", chd, s1.top);

chd = pop(&s1);

printf("Stack 弹出 data:%c,top value is %d ", chd, s1.top);

chd = pop(&s1);

printf("Stack 弹出 data:%c,top value is %d ", chd, s1.top);

c = isEmpty(&s1);

printf("Stack s1 c bool:%d,top value is %d ", c, s1.top);

c = isFull(&s1);

printf("Stack s1 c bool:%d,top value is %d ", c, s1.top);

return 0;

}

Ⅷ C程序中如何使用堆栈

先从大家比较熟悉的栈说起,它是一种具有后进先出性质的数据结构,也就是说后存放的先取,先存放的后取。这就如同要取出放在箱子里面底下的东西(放入的比较早的物体),首先要移开压在它上面的物体(放入的比较晚的物体)。而堆就不同了,堆是一种经过排序的树形数据结构,每个结点都有一个值。通常所说的堆的数据结构,是指二叉堆。堆的特点是根结点的值最小(或最大),且根结点的两个子树也是一个堆。由于堆的这个特性,常用来实现优先队列,堆的存取是随意,这就如同在图书馆的书架上取书,虽然书的摆放是有顺序的,但是想取任意一本时不必像栈一样,先取出前面所有的书,书架这种机制不同于箱子,可以直接取出想要的书。
下面就说说C语言程序内存分配中的堆和栈,这里有必要把内存分配也提一下,一般情况下程序存放在Rom或Flash中,运行时需要拷到内存中执行,内存会分别存储不同的信息。
内存中的栈区处于相对较高的地址以地址的增长方向为上的话,栈地址是向下增长的,栈中分配局部变量空间,堆区是向上增长的用于分配程序员申请的内存空间。另外还有静态区是分配静态变量,全局变量空间的;只读区是分配常量和程序代码空间的;以及其他一些分区。来看一个网上很流行的经典例子:
main.cpp
int a = 0; 全局初始化区
char *p1; 全局未初始化区
main()
{
int b; 栈
char s[] = "abc"; 栈
char *p2; 栈
char *p3 = "123456"; 123456\0在常量区,p3在栈上。
static int c =0; 全局(静态)初始化区
p1 = (char *)malloc(10); 堆
p2 = (char *)malloc(20); 堆
}
堆和栈的第一个区别就是申请方式不同:栈(英文名称是stack)是系统自动分配空间的,例如定义一个 char a;系统会自动在栈上为其开辟空间。而堆(英文名称是heap)则是程序员根据需要自己申请的空间,例如malloc(10);开辟十个字节的空间。由于栈上的空间是自动分配自动回收的,所以栈上的数据的生存周期只是在函数的运行过程中,运行后就释放掉,不可以再访问。而堆上的数据只要程序员不释放空间,就一直可以访问到,不过缺点是一旦忘记释放会造成内存泄露。

Ⅸ 如何在C语言里应用stack

就用一个函数就可以了

voidprint(SqStackp)
{
while(top>=0)
{
printf("%d",p[top]);
top--;
}
}