
Смирнова_2370_ЛР1
.docxДисциплина: «Программирование»
Отчет по лабораторной работе №1
Выполнила: Смирнова М. В.
Группа: №2370
Цели работы:
Построение линейного односвязного списка типа «стек» с заполнением его узлов вводимыми с клавиатуры числами;
Вывод на экран в обратном порядке всех положительных чисел, хранимых в стеке.
Код программы:
#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);}