如何把字符串到堆栈? [英] How to push strings into a stack?
问题描述
欲推字符串如红色,蓝色和绿色的成叠
I want to push strings like red, blue, and green into a stack
//This is my structure containing the stack and top pointer
typedef struct{
char stk[10];
int top;
}STACK;
//This is my push funtion
void push(STACK stak, char str[])
{
stak->top++;
strcpy(stak->stk[stak->top], str);
return;
}
我想形成一个栈像这样
I want to form a stack like so
red
blue
green
我这样做对吗?
推荐答案
有关的基础知识的回答你的问题是堆本身的定义。
For the basics the answer to your question is definition of stack itself.
一个堆栈是可以在逻辑上认为是由一个真实的物理栈或桩,其中,插入和项删除发生在一端称为栈顶部的结构psented线性结构重新$ P $一个基本数据结构。
A stack is a basic data structure that can be logically thought as linear structure represented by a real physical stack or pile, a structure where insertion and deletion of items takes place at one end called top of the stack.
你在做什么是创造字符堆栈,并试图推动字符串到它。
相反,你应该创建一个字符串的堆栈。
What you are doing is creating a stack of chars, and trying to push string to it. Instead you should create a stack of strings.
typedef struct{
string stk[10];
int top;
}STACK;
void push(top,string str)
{
top++;
//overflow condition here
strcpy(STACK.stk[top],str);
}
还有很多东西都在C和C ++不同,所以请首先决定你要坚持使用的语言。这将帮助您得到更好的答案。
Also a lot of things are different in C and C++, so please decide first which language you gonna stick with. That will help you get better answers.
这篇关于如何把字符串到堆栈?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!