Advertisement
nikitast

RK_prep_2

Dec 16th, 2020
567
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.43 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. struct Node {
  5.     char data[80];
  6.     Node* next;
  7. };
  8.  
  9. void AddWord(Node*& first, char* word)
  10. {
  11.     if (first == NULL)
  12.     {
  13.         first = new Node;
  14.         strcpy_s(first->data, word);
  15.         first->next = NULL;
  16.     }
  17.     else
  18.     {
  19.         Node* cur = first;
  20.         while (cur->next != NULL)
  21.             cur = cur->next;
  22.         Node* new_cur = new Node;
  23.         strcpy_s(new_cur->data, word);
  24.         cur->next = new_cur;
  25.         new_cur->next = NULL;
  26.     }
  27. }
  28.  
  29. void RemoveWords(Node*& first)
  30. {
  31.     Node* tmp = first;
  32.     first = first->next;
  33.     delete tmp;
  34.     Node* cur = first;
  35.     bool flag = true;
  36.     while (flag)
  37.     {
  38.         if (cur->next->next->next == NULL)
  39.         {
  40.             tmp = cur->next;
  41.             cur->next = cur->next->next;
  42.             delete tmp;
  43.             flag = false;
  44.         }
  45.         cur = cur->next;
  46.     }
  47. }
  48.  
  49. void ListOutput(Node* first)
  50. {
  51.     Node* cur = first;
  52.     while (cur != NULL)
  53.     {
  54.         printf("%s ", cur->data);
  55.         cur = cur->next;
  56.     }
  57. }
  58.  
  59. void MemoryClear(Node*& first)
  60. {
  61.     while (first != NULL)
  62.     {
  63.         Node* tmp = first;
  64.         first = first->next;
  65.         delete tmp;
  66.     }
  67. }
  68.  
  69. int main()
  70. {
  71.     char str[160], * buffer[160];
  72.     char* ptr;
  73.     Node* first = NULL;
  74.    
  75.     puts("Enter the string:");
  76.     gets_s(str);
  77.     ptr = strtok_s(str, " ,.", buffer);
  78.     while (ptr != NULL)
  79.     {
  80.         AddWord(first, ptr);
  81.         ptr = strtok_s(NULL, " ,.", buffer);
  82.     }
  83.     puts("\nFormed list:");
  84.     ListOutput(first);
  85.     RemoveWords(first);
  86.     puts("\n\nList with removed elements:");
  87.     ListOutput(first);
  88.     MemoryClear(first);
  89.     return 0;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement