Share c++ stack source code

phidiepvo

New member
## C ++ mã nguồn ngăn xếp

Một ngăn xếp là một cấu trúc dữ liệu tuyến tính tuân theo nguyên tắc của LIFO (cuối cùng, ra ngoài).Điều này có nghĩa là phần tử cuối cùng được thêm vào ngăn xếp là phần tử đầu tiên được loại bỏ.Các ngăn xếp thường được sử dụng để thực hiện các chức năng như hoàn tác và làm lại, cũng như để theo dõi thứ tự hoạt động trong một chương trình.

Sau đây là mã nguồn cho ngăn xếp được triển khai trong C ++:

`` `C ++
#include <Istream>

sử dụng không gian tên STD;

// Một ngăn xếp là một mảng các yếu tố được phân bổ động.
Stack Stack {
dữ liệu int*;
kích thước int;
int top;
};

// Tạo một ngăn xếp mới với kích thước ban đầu được chỉ định.
Stack* createdestack (int size) {
Ngăn xếp* stack = new stack ();
Stack-> data = new int [size];
Stack-> kích thước = kích thước;
Stack-> top = -1;
trả lại ngăn xếp;
}

// đẩy một phần tử lên ngăn xếp.
void Push (Stack* Stack, Int Element) {
if (stack-> top == stack-> size-1) {
// ngăn xếp đã đầy, vì vậy chúng ta cần thay đổi kích thước nó.
int * newData = new int [stack-> size * 2];
for (int i = 0; i <stack-> size; i ++) {
newData = stack-> data ;
}
xóa [] stack-> dữ liệu;
Stack-> data = newData;
Stack-> kích thước *= 2;
}

// Thêm phần tử vào ngăn xếp.
Stack-> data [++ stack-> top] = phần tử;
}

// bật một phần tử ra khỏi ngăn xếp.
int pop (stack* stack) {
if (stack-> top == -1) {
// Ngăn xếp trống, vì vậy chúng tôi không thể bật một yếu tố.
trả lại -1;
}

// Xóa phần tử khỏi ngăn xếp.
int phần tử = stack-> data [stack-> top--];

// Nếu ngăn xếp bây giờ trống một nửa, chúng ta có thể thay đổi kích thước nó để lưu bộ nhớ.
if (stack-> top <= stack-> size / 2) {
int* newData = new int [stack-> size / 2];
for (int i = 0; i <stack-> top+1; i ++) {
newData = stack-> data ;
}
xóa [] stack-> dữ liệu;
Stack-> data = newData;
Stack-> kích thước /= 2;
}

trở lại phần tử;
}

// Nhận phần tử hàng đầu của ngăn xếp.
int top (stack* stack) {
if (stack-> top == -1) {
// Ngăn xếp trống, vì vậy không có phần tử hàng đầu.
trả lại -1;
}

// Trả về phần tử hàng đầu của ngăn xếp.
trả về ngăn xếp-> data [stack-> top];
}

// Kiểm tra xem ngăn xếp có trống không.
bool isempty (stack* stack) {
trả về ngăn xếp-> top == -1;
}

// Phá hủy một ngăn xếp.
void hủy diệt (ngăn xếp* ngăn xếp) {
xóa [] stack-> dữ liệu;
xóa ngăn xếp;
}

int main () {
// Tạo một ngăn xếp với kích thước 10.
Ngăn xếp* stack = createStack (10);

// Đẩy một số yếu tố lên ngăn xếp.
Đẩy (ngăn xếp, 1);
Đẩy (ngăn xếp, 2);
Đẩy (ngăn xếp, 3);

// bật một phần tử ra khỏi ngăn xếp.
phần tử int = pop (stack);
cout << "Phần tử popped:" << phần tử << endl;

// Kiểm tra xem ngăn xếp có trống không.
bool isempty = isempty (stack);
cout << "là chồng trống?" << isempty << endl;

// Phá hủy ngăn xếp.
Kẻ hủy diệt (Stack);
=======================================
## C++ Stack Source Code

A stack is a linear data structure that follows the principle of LIFO (last in, first out). This means that the last element added to the stack is the first element to be removed. Stacks are often used to implement functions such as undo and redo, as well as to keep track of the order of operations in a program.

The following is the source code for a stack implemented in C++:

```c++
#include <iostream>

using namespace std;

// A stack is a dynamically allocated array of elements.
struct Stack {
int* data;
int size;
int top;
};

// Create a new stack with the specified initial size.
Stack* createStack(int size) {
Stack* stack = new Stack();
stack->data = new int[size];
stack->size = size;
stack->top = -1;
return stack;
}

// Push an element onto the stack.
void push(Stack* stack, int element) {
if (stack->top == stack->size - 1) {
// The stack is full, so we need to resize it.
int* newData = new int[stack->size * 2];
for (int i = 0; i < stack->size; i++) {
newData = stack->data;
}
delete[] stack->data;
stack->data = newData;
stack->size *= 2;
}

// Add the element to the stack.
stack->data[++stack->top] = element;
}

// Pop an element off the stack.
int pop(Stack* stack) {
if (stack->top == -1) {
// The stack is empty, so we can't pop an element.
return -1;
}

// Remove the element from the stack.
int element = stack->data[stack->top--];

// If the stack is now half empty, we can resize it to save memory.
if (stack->top <= stack->size / 2) {
int* newData = new int[stack->size / 2];
for (int i = 0; i < stack->top + 1; i++) {
newData = stack->data;
}
delete[] stack->data;
stack->data = newData;
stack->size /= 2;
}

return element;
}

// Get the top element of the stack.
int top(Stack* stack) {
if (stack->top == -1) {
// The stack is empty, so there is no top element.
return -1;
}

// Return the top element of the stack.
return stack->data[stack->top];
}

// Check if the stack is empty.
bool isEmpty(Stack* stack) {
return stack->top == -1;
}

// Destroy a stack.
void destroyStack(Stack* stack) {
delete[] stack->data;
delete stack;
}

int main() {
// Create a stack with a size of 10.
Stack* stack = createStack(10);

// Push some elements onto the stack.
push(stack, 1);
push(stack, 2);
push(stack, 3);

// Pop an element off the stack.
int element = pop(stack);
cout << "Popped element: " << element << endl;

// Check if the stack is empty.
bool isEmpty = isEmpty(stack);
cout << "Is stack empty? " << isEmpty << endl;

// Destroy the stack.
destroyStack(stack);
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top