Advertisement
Guest User

Untitled

a guest
Apr 30th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.98 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include<iostream>
  3. using namespace std;
  4.  
  5.  struct stack
  6.  {
  7.  int inf;
  8.  stack* a;
  9.  };
  10.  
  11. stack* Create(stack*&sp);
  12. stack* Add(stack*&sp,int inf);
  13. void Find(stack*&);
  14. //void Execusion(stack*&);
  15. void Delete(stack *&, stack *&);
  16. int menu();
  17. void Read(stack *);
  18.  
  19. int main()
  20. {
  21. stack *sp=new stack;
  22.     stack*now;
  23.    now = NULL;
  24.     sp=NULL;
  25.     while(true)
  26.     {
  27.     switch (menu())
  28.     {
  29.     case 1:sp=Create(sp);
  30.     case 2: Find(sp);
  31.     case 3: Read(sp);
  32.     }
  33.    
  34.     }
  35. }
  36. int menu()
  37. {
  38. int i;
  39. cin>>i;
  40. return i;
  41. }
  42.  
  43. stack* Create(stack*&sp)
  44.  
  45. {
  46.     stack*yub;
  47. int n, j;
  48.     cout << "skolko znachenii vnesti v stek?" << endl;
  49.     cin >> n;
  50.     for (int i = 0;i < n;i++)
  51.     {
  52.         cout << "vvedite" << i << "ellement" << endl;
  53.         cin >> j;
  54.         yub=Add(sp, j);
  55.     }
  56. return yub;
  57. }
  58.  
  59.  
  60.  
  61. stack* Add(stack*&sp,int inf)
  62. {  
  63.    
  64.         stack* spt = new stack;
  65.         spt->a = sp;
  66.         spt->inf = inf;
  67.         sp = spt;
  68.  
  69.     return spt;
  70. }
  71.  
  72. void Find(stack*&sp)
  73. {
  74.     stack *temp1 = new stack;
  75.     temp1=sp;
  76.     stack *temp2 = new stack;
  77.     temp2=sp;
  78.     stack *temp3 = new stack;
  79.     temp3=sp;
  80.    
  81.     while(temp1->inf > 0 && temp1->a!=NULL)
  82.     {
  83.     temp1 = temp1->a;
  84.     }
  85.     if(temp1>0&&temp1->a==NULL){cout<<"There is no negative elements in stack"<<endl;return;}
  86.     temp3=NULL;
  87.     temp2=temp1->a;
  88.    
  89.     while(temp2 != NULL)
  90.     {
  91.         if(temp2->inf<0){temp3=temp2;};
  92.         temp2 = temp2->a;
  93.     }
  94.     if(temp3==NULL) { cout<<" There is no 2 negative elements in stack"; return;}
  95.     Delete (temp1,temp3);
  96. }
  97.  
  98. void Delete(stack *&temp1, stack *&temp3)
  99. {
  100. stack *remove = new stack;
  101. stack *y = new stack;
  102. stack *z = new stack;
  103. y=temp1;
  104. y=y->a;
  105.  temp1->a=temp3;
  106. while(y!=temp3)
  107. {
  108.     z=y->a;
  109.     remove = y;
  110.     delete remove;
  111.     y = z;
  112. }
  113. }
  114.  
  115.  
  116. void Read(stack *spt)
  117.  {
  118.      while(spt !=NULL)
  119.      {
  120.      cout<<spt->inf<<"\t";
  121.      spt=spt->a;
  122.      }
  123.      return;
  124.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement