Skip to content

Instantly share code, notes, and snippets.

@htoann
Last active June 12, 2022 07:02
Show Gist options
  • Save htoann/833ff754059c50a1c1cfe06b21438d92 to your computer and use it in GitHub Desktop.
Save htoann/833ff754059c50a1c1cfe06b21438d92 to your computer and use it in GitHub Desktop.
Stack
• Các phần tử cùng kiểu
• Số phần tử không cố định
• Các phần tử được đưa vào và lấy ra ở đỉnh ngăn xếp
• Ngăn xếp hoạt động theo cơ chế LIFO (Last In First Out)
Ví dụ:
- Chồng đĩa khi rửa + Chồng đĩa khi đưa ra phục vụ
class Stack {
struct Node {
int data;
Node * next;
};
Node * top;
public:
CreateS() {
top = NULL;
}
bool EmptyS() {
return top == NULL;
}
int PopS() {
int x = 0;
if (top != NULL) {
Node * t = top;
x = top -> data;
top = top -> next;
delete t;
} else cout << "\n Ngan xep rong!!!";
return x;
}
void PushS(int x) {
Node * t = new Node;
t -> data = x;
t -> next = top;
top = t;
}
};
void them() {
S = new Stack < > ();
for (int i = 1; i <= 10; i++) S.push(" Phan tu thu " + i);
System.out.println("\n Noi dung Stack: \n");
while (!S.empty()) {
String x = S.pop();
System.out.println(" --" + x);
}
System.out.println("\n Noi dung Stack: " + S);
}
bool kiemtra(string t) {
stack < char > S;
for (int i = 0; i < t.length(); i++)
if (t[i] == '(') S.push(t[i]);
else if (t[i] == ')')
if (!S.empty()) S.pop();
else return false;
else {
cout << "\n chuoi chua ky tu khong hop le!!!";
return false;
}
if (S.empty()) return true;
else return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment