Advertisement
nikitast

Another_RK_2

Dec 25th, 2020
1,042
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.35 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. int main()
  70. {
  71.     Node* first = NULL;
  72.     char str[80];
  73.     char* ptr;
  74.  
  75.     puts("Enter the word sequence without spaces:");
  76.     gets_s(str);
  77.     for (int i = 0; i < strlen(str); i++)
  78.         AddNode(first, str[i]);
  79.     puts("\nThe formed list:");
  80.     PrintList(first);
  81.     InsertHash(first);
  82.     puts("\nThe formed list:");
  83.     PrintList(first);
  84.     ClearMemory(first);
  85.     puts("\n");
  86.     return 0;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement