30/09/2018, 22:24
Bài tập Stack trong C++
Giúp em sửa cho nó chạy với ạ
#include <stdio.h>
#include <string>
#include <stdlib.h>
#include <iostream>
using namespace std;
typedef struct{
int top;
int *nodes;
}Stack;
void Init(Stack *&stack){
stack = new Stack;
stack->top = -1;
}
void push(Stack *stack, int node){
int *tmpNodes = new int[stack->top + 2];
stack->top++;
for (int i = 0; i < stack->top; i++)
tmpNodes[i] = stack->nodes[i];
tmpNodes[stack->top] = node;
delete[]stack->nodes;
stack->nodes = tmpNodes;
return;
}
int pop(Stack *stack){
if (stack->top < 0){
cout << "Stack is empty!" << endl;
return 0;
}
int result = stack -> nodes[stack->top];
int *tmpNodes = new int[stack->top];
for (int i = 0; i < stack->top; i++)
tmpNodes[i] = stack->nodes[i];
stack->top--;
delete[] stack->nodes;
stack->nodes = tmpNodes;
return result;
}
void release(Stack *stack){
delete[] stack->nodes;
delete stack;
return;
}
int main(){
Stack *stack;
Init(stack);
cout << "Nhap chuoi: ";
char strIn[250];
cin >> strIn;
for (int i = 0; i <strlen(strIn); i++)
push(stack, strIn[i]);
while (stack->top >-1)
cout << pop(stack);
release(stack);
}
Bài liên quan
This post was flagged by the community and is temporarily hidden.