Advertisement
3axap_010

queue.cpp

Jun 20th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.65 KB | None | 0 0
  1. // queue2.cpp : Этот файл содержит функцию "main". Здесь начинается и заканчивается выполнение программы.
  2. // Удалить любой элемент из однонаправленной очереди
  3.  
  4. #include "pch.h"
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8.  
  9. struct queue
  10. {
  11.     char* str;
  12.     struct queue* next;
  13. };
  14.  
  15. void add_elemenet(struct queue** head, struct queue** tail, char* str)
  16. {
  17.     struct queue* temp = (struct queue*)malloc(sizeof(struct queue));
  18.     temp->str = (char*)malloc(sizeof(char) * (strlen(str) + 1));
  19.     strcpy(temp->str, str);
  20.     temp->next = NULL;
  21.  
  22.     if (*head == NULL)
  23.     {
  24.         *head = *tail = temp;
  25.         return;
  26.     }
  27.  
  28.     temp->next = *tail;
  29.     *tail = temp;
  30. }
  31.  
  32. void show_queue(struct queue* tail)
  33. {
  34.     if (!tail)
  35.     {
  36.         return;
  37.     }
  38.  
  39.     do
  40.     {
  41.         puts(tail->str);
  42.         tail = tail->next;
  43.     } while (tail);
  44.  
  45.     fprintf(stdout, "\n\n");
  46. }
  47.  
  48. void dequeue(struct queue** head, struct queue** tail) //first element remove
  49. {
  50.     if (!*head)
  51.     {
  52.         return;
  53.     }
  54.  
  55.     if (*head == *tail)
  56.     {
  57.         free((*head)->str);
  58.         free(*head);
  59.         *head = *tail = NULL;
  60.         return;
  61.     }
  62.  
  63.     struct queue* temp = *tail;
  64.  
  65.     while (temp->next != *head)
  66.     {
  67.         temp = temp->next;
  68.     }
  69.  
  70.     *head = temp;
  71.     free(temp->next->str);
  72.     free(temp->next);
  73.     temp->next = NULL;
  74. }
  75.  
  76. void remove_any(struct queue** head, struct queue** tail, char* str)
  77. {
  78.     if (!head)
  79.     {
  80.         return;
  81.     }
  82.  
  83.     if (*tail == *head)
  84.     {
  85.         if (strcmp((*head)->str, str) == 0)
  86.         {
  87.             free((*head)->str);
  88.             free(*head);
  89.             *head = *tail = NULL;
  90.         }
  91.  
  92.         return;
  93.     }
  94.  
  95.     struct queue* cur = *tail;
  96.     struct queue* prev;
  97.  
  98.     while (*tail && strcmp(cur->str, str) == 0)
  99.     {
  100.         *tail = (*tail)->next;
  101.         free(cur->str);
  102.         free(cur);
  103.         cur = *tail;
  104.     }
  105.  
  106.     cur = (*tail)->next;
  107.     prev = *tail;
  108.  
  109.     while (cur)
  110.     {
  111.         if (strcmp(cur->str, str) == 0)
  112.         {
  113.             if (cur == *head)
  114.             {
  115.                 *head = prev;
  116.                 (*head)->next = NULL;
  117.                 free(cur->str);
  118.                 free(cur);
  119.                 return;
  120.             }
  121.  
  122.             prev->next = cur->next;
  123.             free(cur->str);
  124.             free(cur);
  125.             cur = prev->next;
  126.         }
  127.         else
  128.         {
  129.             prev = cur;
  130.             cur = cur->next;
  131.         }
  132.     }
  133. }
  134.  
  135. int main()
  136. {
  137.     struct queue* head = NULL, *tail = NULL;
  138.  
  139.     char* str = NULL;
  140.  
  141.     for(;;)
  142.     {
  143.         str = (char*)malloc(sizeof(char) * 100);
  144.         gets_s(str, 100);
  145.         if (str[0] == '\0' )
  146.         {
  147.             free(str);
  148.             break;
  149.         }
  150.         add_elemenet(&head, &tail, str);
  151.         free(str);
  152.     }
  153.  
  154.     show_queue(tail);
  155.  
  156.     dequeue(&head, &tail);
  157.  
  158.     show_queue(tail);
  159.  
  160.     str = (char*)malloc(sizeof(char) * 100);
  161.     gets_s(str, 100);
  162.  
  163.     remove_any(&head, &tail, str);
  164.  
  165.     show_queue(tail);
  166.  
  167.     free(str);
  168.  
  169.     return 0;
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement