Advertisement
FokaKefir

Untitled

May 16th, 2025
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.84 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. typedef struct linked_list {
  6.     int value{};
  7.     linked_list* next = nullptr;
  8. } linked_list;
  9.  
  10. void insert_last(linked_list* &head, int new_value) {
  11.     if (head==nullptr) {
  12.         linked_list *k = new linked_list();
  13.         k->value=new_value;
  14.         head=k;
  15.     } else {
  16.         linked_list *act = head;
  17.         while (act->next != nullptr) {
  18.             act=act->next;
  19.         }
  20.         linked_list *elem = new linked_list();
  21.         elem->value=new_value;
  22.         act->next = elem;
  23.     }
  24.  
  25. }
  26.  
  27. void print_ll (linked_list *head) {
  28.     while (head!=nullptr) {
  29.         cout<<head->value<<" ";
  30.         head=head->next;
  31.     }
  32. }
  33.  
  34. int main() {
  35.     linked_list *head = nullptr;
  36.     insert_last(head, 1);
  37.     insert_last(head, 2);
  38.     insert_last(head, 3);
  39.     print_ll(head);
  40.    
  41. }
  42.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement