Advertisement
Grossos

DynamicStack

Apr 3rd, 2017
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.62 KB | None | 0 0
  1. #include<iostream>
  2. #include<windows.h>
  3. #include<conio.h>
  4. using namespace std;
  5.     int br=0;
  6. struct elem
  7. {
  8.       int key;
  9.       elem *next;
  10. }*s1=NULL, *p, *s2=NULL;
  11.  
  12. void push(int n, elem *&start);
  13. int pop(int &n,elem *&start);
  14. void insert();
  15.  
  16.  
  17. void insert()
  18. {      SetConsoleCP(1251);SetConsoleOutputCP(1251);
  19.     int n=1,size,i;
  20.      size=1;
  21.                           cout<<"Въведете размер:\n";
  22.                             cin >> size;
  23.  for (int i = 0; i < size; i++)
  24.    
  25.        
  26.     {
  27.         cout<<"Въведете число:\n";
  28.         cin>>n;
  29.         if (n!=0) push(n,s1);
  30.     }
  31. }
  32.  
  33. void push(int n, elem *&start)
  34. {
  35.    
  36.       p=start;
  37.       start=new elem;
  38.       start->key=n;
  39.       start->next=p;
  40.       br++;
  41. }
  42. int pop(int &n,elem *&start)
  43. {
  44.       if(start)
  45.       {
  46.             n=start->key;
  47.             p=start;
  48.             start=start->next;
  49.             delete p;
  50.             return 1;
  51.       }
  52.       else
  53.       {
  54.             return 0;
  55.       }
  56. }
  57.  
  58.  
  59. void main()
  60. {
  61.     insert();
  62.       int m, a;
  63.       SetConsoleCP(1251);SetConsoleOutputCP(1251);
  64.       do
  65.       {
  66.             cout<<"\n Въведи число между 1 и "<<br<<" !"<<endl;
  67.             cout<<" M= ";
  68.             cin>>m;
  69.  
  70.       }while(m<1 && m>br);
  71.  
  72.  
  73.  
  74.         for(int i=1;i<m;i++)
  75.         {
  76.             pop(a,s1);
  77.             push(a,s2);
  78.         }
  79.         cout<<"\nВъведи нова стойност "<<endl;
  80.       cout<<" A=\t";
  81.       cin>>a;
  82.         s1->key=a;
  83.         while(pop(a,s2))
  84.             push(a,s1);
  85.    
  86.         while(pop(a,s1))
  87.         cout<<" "<<a<<"\t";
  88.       system("pause");
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement