Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Смирнова_2370_ЛР1

.docx
Скачиваний:
1
Добавлен:
05.12.2024
Размер:
16.26 Кб
Скачать

Дисциплина: «Программирование»

Отчет по лабораторной работе №1

Выполнила: Смирнова М. В.

Группа: №2370

Цели работы:

  1. Построение линейного односвязного списка типа «стек» с заполнением его узлов вводимыми с клавиатуры числами;

  2. Вывод на экран в обратном порядке всех положительных чисел, хранимых в стеке.

Код программы:

#include <iostream>

using namespace std;

struct node {

int data;

struct node* next; };

void push(struct node** head_ref, int data) {

struct node* node;

node = (struct node*)malloc(sizeof(struct node));

node->data = data;

node->next = (*head_ref);

(*head_ref) = node; }

void reverse(struct node** head) {

struct node* temp = NULL;

struct node* prev = NULL;

struct node* current = (*head);

while (current != NULL) {

temp = current->next;

current->next = prev;

prev = current;

current = temp;}

(*head) = prev;}

void pop(struct node* head) {

struct node* next = head;

while (head != NULL) {

if (next->data > 0) {

cout << next->data << endl;}

next = next->next;}}

int main() {

setlocale(LC_ALL, "ru");

struct node* head = NULL;

int c = 1;

while (c != 0) {

int t;

cin >> t;

c = t;

push(&head, t);}

reverse(&head);

pop(head);}