Advertisement
nikitast

Another_RK_3

Dec 25th, 2020
971
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.68 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. struct Node {
  6.     char data;
  7.     Node* next;
  8. };
  9.  
  10. void AddNode(Node*& first, char symbol)
  11. {
  12.     if (first == NULL)
  13.     {
  14.         first = new Node;
  15.         first->data = symbol;
  16.         first->next = NULL;
  17.     }
  18.     else
  19.     {
  20.         Node* cur = first;
  21.         first = new Node;
  22.         first->data = symbol;
  23.         first->next = cur;
  24.     }
  25. }
  26.  
  27. void PrintList(Node* first)
  28. {
  29.     while (first != NULL)
  30.     {
  31.         printf("%c ", first->data);
  32.         first = first->next;
  33.     }
  34. }
  35.  
  36. void ClearMemory(Node*& first)
  37. {
  38.     while (first != NULL)
  39.     {
  40.         Node* tmp = first;
  41.         first = first->next;
  42.         delete tmp;
  43.     }
  44. }
  45.  
  46. void InsertNodeAfter(Node*& cur, char symbol)
  47. {
  48.     Node* new_node = new Node;
  49.     new_node->data = symbol;
  50.     new_node->next = cur->next;
  51.     cur->next = new_node;
  52. }
  53.  
  54. void InsertHash(Node* first)
  55. {
  56.     int cnt = 0;
  57.     while (first->next != NULL)
  58.     {
  59.         cnt++;
  60.         if (cnt % 2 != 0)
  61.         {
  62.             InsertNodeAfter(first, '#');
  63.             first = first->next;
  64.         }
  65.         first = first->next;
  66.     }
  67. }
  68.  
  69. void DeleteNodeAfter(Node*& cur)
  70. {
  71.     Node* tmp = cur->next;
  72.     cur->next = tmp->next;
  73.     delete tmp;
  74. }
  75.  
  76. void DeleteSymbols(Node* first)
  77. {
  78.     int cnt = 0;
  79.     while (first->next != NULL)
  80.     {
  81.         cnt++;
  82.         if ((cnt+1) % 3 == 0)
  83.         {
  84.             DeleteNodeAfter(first);
  85.             cnt++;
  86.         }
  87.         if (first->next != NULL)
  88.             first = first->next;
  89.     }
  90. }
  91.  
  92. int main()
  93. {
  94.     Node* first = NULL;
  95.     char str[80];
  96.     char* ptr;
  97.  
  98.     puts("Enter the word sequence without spaces:");
  99.     gets_s(str);
  100.     for (int i = 0; i < strlen(str); i++)
  101.         AddNode(first, str[i]);
  102.     puts("\nThe formed list:");
  103.     PrintList(first);
  104.     DeleteSymbols(first);
  105.     puts("\nThe formed list:");
  106.     PrintList(first);
  107.     ClearMemory(first);
  108.     puts("\n");
  109.     return 0;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement