Advertisement
nicx321

Linked List Text Store

Dec 21st, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.45 KB | None | 0 0
  1. // ConsoleApplication1.cpp : Tento soubor obsahuje funkci main. Provádění programu se tam zahajuje a ukončuje.
  2. //
  3.  
  4. #include "pch.h"
  5. #include <iostream>
  6.  
  7. using namespace std;
  8.  
  9. #define SizeArr 40
  10.  
  11. class Link{
  12. public:
  13.     char Text[SizeArr];
  14.     Link *Next;
  15.     Link() {
  16.         for (int i = 0; i < SizeArr; i++) {
  17.             Text[i] = 0;
  18.         }
  19.     }
  20. };
  21.  
  22. void PrintThemAll(Link **LinkN) {
  23.     while (*LinkN != 0) {
  24.         cout << (*LinkN)->Text << endl;
  25.         *LinkN = (*LinkN)->Next;
  26.     }
  27. }
  28.  
  29. void Clear(Link **LinkN) {;
  30.     Link *Next;
  31.     while (*LinkN != 0) {
  32.         Next = (*LinkN)->Next;
  33.         delete *LinkN;
  34.         *LinkN = Next;
  35.     }
  36.     *LinkN = NULL;
  37. }
  38.  
  39. void Add(Link **StartP) {
  40.     uint8_t End = 0;
  41.     char input[SizeArr];
  42.     while (End == 0) {
  43.         cin.getline(input, SizeArr);
  44.         if (input[0] == '-') {
  45.             End = 1;
  46.         }
  47.         else {
  48.             if (*StartP == 0) {
  49.                 *StartP = new Link;
  50.                 strcpy_s((*StartP)->Text, input);
  51.                 (*StartP)->Next = 0;
  52.             }
  53.             else {
  54.                 Link *Prev = *StartP;
  55.                 Link *WorkStart = *StartP;
  56.                 while (WorkStart != 0) {
  57.                     Prev = WorkStart;
  58.                     WorkStart = WorkStart->Next;
  59.                 }
  60.                 WorkStart = Prev;
  61.                 Link *NewStart = new Link;
  62.                 WorkStart->Next = NewStart;
  63.                 WorkStart = WorkStart->Next;
  64.                 strcpy_s(NewStart->Text, input);
  65.                 NewStart->Next = 0;
  66.             }
  67.         }
  68.     }
  69. }
  70.  
  71. int main()
  72. {
  73.     cout << "Inser text line by line, end with inserting -"<< endl << endl;
  74.     Link *First = NULL;
  75.     Add(&First);
  76.     PrintThemAll(&First);
  77.     Clear(&First);
  78.     return 0;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement