Files
hello-algo/ru/codes/zig/chapter_stack_and_queue/stack.zig
Yudong Jin 772183705e Add ru version (#1865)
* Add Russian docs site baseline

* Add Russian localized codebase

* Polish Russian code wording

* Update ru code translation.

* Update code translation and chapter covers.

* Fix pythontutor extraction.

* Add README and landing page.

* placeholder of profiles

* Use figures of English version

* Remove chapter paperbook
2026-03-28 04:24:07 +08:00

44 lines
1.6 KiB
Zig
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// File: stack.zig
// Created Time: 2023-01-08
// Author: codingonion (coderonion@gmail.com)
const std = @import("std");
const inc = @import("include");
// Driver Code
pub fn main() !void {
// Инициализировать стек
// В Zig рекомендуется использовать ArrayList как стек
var stack = std.ArrayList(i32).init(std.heap.page_allocator);
// Отложенное освобождение памяти
defer stack.deinit();
// Помещение элемента в стек
try stack.append(1);
try stack.append(3);
try stack.append(2);
try stack.append(5);
try stack.append(4);
std.debug.print("Стек stack = ", .{});
inc.PrintUtil.printList(i32, stack);
// Доступ к верхнему элементу стека
var peek = stack.items[stack.items.len - 1];
std.debug.print("\nВерхний элемент стека peek = {}", .{peek});
// Извлечение элемента из стека
var pop = stack.pop();
std.debug.print("\nИзвлечен элемент pop = {}, стек после извлечения stack = ", .{pop});
inc.PrintUtil.printList(i32, stack);
// Получение длины стека
var size = stack.items.len;
std.debug.print("\nДлина стека size = {}", .{size});
// Проверка, пуст ли стек
var is_empty = if (stack.items.len == 0) true else false;
std.debug.print("\nПуст ли стек = {}", .{is_empty});
_ = try std.io.getStdIn().reader().readByte();
}