ESP-IDF:字符串中字符转为链表结点入栈(使用STL stack),然后打印栈中内容
/字符串中字符转为链表结点入栈(使用STL stack),然后打印栈中内容/
#include
typedef struct LINKNODE20 {
struct LINKNODE20 * next;
}linknode20;
typedef struct MYCHAR20 {
linknode20 node;
char * address;
int index;
}MyChar20;
MyChar20 * createMyChar(char * p, int index) {
MyChar20 * pp = (MyChar20 *)malloc(sizeof(MyChar20));
// pp->node.next = NULL;
pp->address = p;
pp->index = index;
return pp;
}
void test20() {
stack<linknode20 *> stack20;
char * s1=“(”;
char * s2=“)”;
stack20.push((linknode20 *)createMyChar(s1,0)); //把MyChar20入栈
stack20.push((linknode20 *)createMyChar(s2,0)); //把MyChar20入栈
cout<<“--------使用栈检查括号是否匹配测试--------”<<endl;
MyChar20 *testMychar1 = (MyChar20 *)stack20.top();//查看栈顶元素
cout<address<<endl;
stack20.pop();
MyChar20 *testMychar2 = (MyChar20 *)stack20.top();//查看栈顶元素
cout<address<<endl;
stack20.pop();
// char str[] = "1+6-6*(0+9)/9(8-7";
// char * p = str;
string str = "1+6-6*(0+9)/9(8-7";
int index = 0;
// while (*p != '\0'){
// stack20.push((linknode20 *)createMyChar(p,index));
// p++;
// index++;
// }
while (str[index] != '\0'){
cout<<str[index]<<endl;
// char a = str[index];
stack20.push((linknode20 *)createMyChar(&str[index],index));
index++;
}
for(int i = 0; i<index; i++) {
MyChar20 *testMychar = (MyChar20 *)stack20.top();//查看栈顶元素
//char c = *(testMychar->address);//一定要用字符去接它
cout<<*(testMychar->address)<<" index is:" <<testMychar->index<<endl;
cout<<testMychar->address<<" index is:" <<testMychar->index<<endl;
stack20.pop();
}
}
extern “C” void app_main(void)
{
test20();
}
结果:
注意cout时地址取*.否则打印的是一串字符,如下图: