Advertisement
nikitast

Another_RK

Dec 25th, 2020
1,184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.29 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[80];
  7.     Node* next;
  8. };
  9.  
  10. void AddNode(Node*& first, char* str)
  11. {
  12.     if (first == NULL)
  13.     {
  14.         first = new Node;
  15.         strcpy(first->data, str);
  16.         first->next = NULL;
  17.     }
  18.     else
  19.     {
  20.         Node* cur = first;
  21.         first = new Node;
  22.         strcpy(first->data, str);
  23.         first->next = cur;
  24.     }
  25. }
  26.  
  27. void PrintList(Node* first)
  28. {
  29.     while (first != NULL)
  30.     {
  31.         printf("%s ", first->data);
  32.         first = first->next;
  33.     }
  34. }
  35.  
  36. int FindWords(Node* first)
  37. {
  38.     int cnt = 0;
  39.     while (first != NULL)
  40.     {
  41.         if (first->data[0] == first->data[strlen(first->data) - 1])
  42.         {
  43.             printf("%s ", first->data);
  44.             cnt++;
  45.         }
  46.         first = first->next;
  47.     }
  48.     return cnt;
  49. }
  50.  
  51. void ClearMemory(Node*& first)
  52. {
  53.     while (first != NULL)
  54.     {
  55.         Node* tmp = first;
  56.         first = first->next;
  57.         delete tmp;
  58.     }
  59. }
  60.  
  61. int main()
  62. {
  63.     Node* first = NULL;
  64.     char str[80];
  65.     char* ptr;
  66.  
  67.     puts("Enter the word sequence:");
  68.     gets_s(str);
  69.     ptr = strtok(str, " ,.");
  70.     while (ptr != NULL)
  71.     {
  72.         AddNode(first, ptr);
  73.         ptr = strtok(NULL, " ,.");
  74.     }
  75.     puts("\nThe formed list:");
  76.     PrintList(first);
  77.     puts("\n\nWords with the same beginning/ending:");
  78.     printf("\nNumber of words: %d", FindWords(first));
  79.     ClearMemory(first);
  80.     puts("\n");
  81.     return 0;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement