Advertisement
Proff_Ust

list_delete

Nov 10th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.95 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4. struct TList
  5. {
  6.     int value;
  7.     TList* next;
  8. };
  9.  
  10. bool isEmpty(TList* lst)
  11. {
  12.     return lst==NULL;
  13. }
  14.  
  15. int addElem(TList** lst, int newValue)
  16. {
  17.     TList * temp = new TList;
  18.     temp->value = newValue;
  19.     temp->next = NULL;
  20.     if(isEmpty((*lst)))
  21.         *lst = temp;
  22.     else
  23.     {
  24.         TList* temp1 = *lst;
  25.         while(temp1->next!=NULL)
  26.             temp1 = temp1->next;
  27.         temp1->next = temp;
  28.     }
  29.     return 0;
  30. }
  31.  
  32. int printList(TList* lst)
  33. {
  34.     while(lst!=NULL)
  35.     {
  36.         cout<<lst->value<<" ";
  37.         lst = lst->next;
  38.     }
  39.     return 0;
  40. }
  41.  
  42. int delElem(TList** lst, int delValue)
  43. {
  44.     if(*lst!=NULL)
  45.     {
  46.         TList* temp = *lst;
  47.         while((*lst!=NULL)&&((*lst)->value==delValue))
  48.         {
  49.             temp = (*lst)->next;
  50.             delete *lst;
  51.             (*lst) = temp;
  52.         }
  53.         if(*lst!=NULL)
  54.         {
  55.             temp = *lst;
  56.             TList* temp1 = *lst;
  57.             while(temp!=NULL)
  58.             {
  59.                 while((temp!=NULL) && (temp->value!=delValue))
  60.                 {
  61.                     temp1 = temp;
  62.                     temp = temp->next;
  63.                 }
  64.                 if((temp!=NULL)&&(temp->value==delValue))
  65.                 {
  66.                     temp1->next = temp->next;
  67.                     delete temp;
  68.                     temp = temp1;
  69.                 }
  70.                 if(temp!=NULL)
  71.                     temp = temp->next;
  72.             }
  73.         }
  74.         return 0;
  75.     }
  76.     return -1;
  77. }
  78.  
  79. int main()
  80. {
  81.     TList* List = NULL;
  82.     int t;
  83.     ifstream inputFile("input.txt");
  84.     if(inputFile)
  85.     {
  86.         while(!inputFile.eof())
  87.         {
  88.             inputFile>>t;
  89.             addElem(&List,t);
  90.         }
  91.  
  92.         printList(List);
  93.         delElem(&List,2);
  94.         cout<<endl;
  95.         printList(List);
  96.         return 0;
  97.     }
  98.     else
  99.         return -1;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement