STACK
If the sequence of operations - push(1), push(2), pop(), push(1), push(2), pop(), pop(), push(2) are performed on a stack then size of stack after these operations is:
а) 2 б) 3 в) 5 г) 7
Едноставен пример
#include <iostream>
#include <stack>
using namespace std;
int main ()
{
stack <string> cards; /* stek od stringovi */
cards.push("King of Hearts"); /* dodavame karta */
cards.push("King of Clubs"); /* dodavame karta */
cards.push("King of Diamonds");
cards.push("King of Spades");
cout << "Vo spilot imame " << cards.size () << " karti " << endl;
cout << "Kartata na vrvot e " << cards.top() << endl;
/* ke se prikaze King of Spades, bidejki e posleden dodaden */
cards.pop();
cout << "Kartata na vrvot e " << cards.top() << endl;
cout << cards.size();
return 0;
}
Пример програма за креирање на стек и примена на основните операции со стек. Што ќе се прикаже?Објасни ги функциите !
#include <iostream>
#include <stack>
using namespace std;
int main()
{
stack<int> nStack;
cout<<"sega imame stek !!!"<<endl;
cout<<endl;
cout<<"goleminata e="<<nStack.size()<<endl;
if (nStack.empty()==true) cout<<"stekot e prazen ";
else cout<<" stekot ne e prazen ";
cout<<endl;
cout<<"se vnesuvaat tri elementi vo stekot!!!"<<endl;
nStack.push(1);
nStack.push(2);
nStack.push(3);
cout<<"goleminata e ="<<nStack.size()<<endl;
if (nStack.empty()==true) cout<<"stekot e prazen "<<endl;
else cout<<"stekot ne e prazen "<<endl;
cout<<endl;
cout<<"vo stekot se vnesuva uste eden element "<<endl;
nStack.push(4);
cout<<"goleminata na stekot e="<<nStack.size()<<endl;
int nElement =nStack.top();
if (nStack.empty()==true) cout<<"stekot e prazen "<<endl;
else cout<<"stekot ne e prazen "<<endl;
cout<<endl;
cout<<"se vadi gorniot element!!!"<<endl;
nStack.pop();
cout<<"goleminata e="<<nStack.size()<<endl;
if (nStack.empty()==true) cout<<"stekot e prazen "<<endl;
else cout<<"stekot ne e prazen "<<endl;
return 0;
}
Што ќе се прикаже
#include <iostream>
#include <stack>
using namespace std;
void display(stack <int> ds)
{
stack <int> dis = ds;
while (!dis.empty())
{
cout << dis.top() << " ";
dis.pop();
}
}
int main()
{
stack <int> getstack;
getstack.push(80);
getstack.push(60);
getstack.push(50);
getstack.push(30);
cout << "The stack element from top to bottom are : ";
display(getstack);
cout << "\nThe value of getstack.size() : " << getstack.size();
cout << "\nThe value of getstack.top() : " << getstack.top();
cout << "\nThe value of getstack.pop() : ";
getstack.pop();
display(getstack);
return 0;
}
#include <iostream>
#include <stack>
using namespace std;
int main()
{
int sum = 0,n,x,i;
stack<int> mstack;
cout<<"vnesi broj na elementi vo stekot"<<endl;
cin>>n;
for(i=1;i<=n;i++)
{
cout<<"vnesi broj"<<endl;
cin>>x;
mstack.push(x);
}
while (!mstack.empty()) {
sum+= mstack.top();
mstack.pop();
}
cout << "zbirot na elementite e "<<sum;
return 0;
}
QUEUE
#include <iostream>
#include <queue>
using namespace std;
int main ()
{
queue<int> myqueue; //inicijalizacija na prazen red (queue) koj ke sodrzi celi broevi
int element;
cout << "Please enter some integers (enter 0 to end):\n";
do
{
cin >> element;
myqueue.push(element); //vneseniot broj so funkcijata push() se smestuva vo myqueue
} while(element); //koga ke se vnese 0 zavrsuva do-while
cout << "myqueue contains: ";
while (!myqueue.empty()) //se dodeka ne se isprazni redot myqueue
{
cout<<myqueue.front()<<" "; //se prikazuva prviot element na redot myqueue
myqueue.pop(); //se isfrla prviot element na redot myqueue
}
cout<<'\n';
return 0;
}
Програма со која се внесуваат n елементи во ред, се вади првиот елемент и се печатат останатите елементи на редот
#include <iostream>
#include <queue>
using namespace std;
int main()
{
queue <int> q;
int n,i,x;
cout << "vnesi broj na elementi " ;
cin>>n;
for(i=1;i<=n;i++)
{
cin>>x;
q.push(x);
}
q.pop(); // елементите се печатат од вториот елемент во редот
while(!q.empty())
{
cout<<q.front()<<" ";
q.pop();
}
return 0;
}
Програма со која се полни queue со n елементи цели броеви и се пресметува нивниот збир
#include <iostream>
#include <queue>
using namespace std;
int main ()
{ int s=0, i, n;
queue<int> myqueue; //inicijalizacija na prazen red (queue) koj ke sodrzi celi broevi
int x;
cout << "Vnesi element cel broj (enter 0 to end):\n";
do
{
cin >> x;
myqueue.push(x); //vneseniot broj so funkcijata push() se smestuva vo myqueue
} while(x); //koga ke se vnese 0 zavrsuva do-while
cout << "myqueue contains: ";
while (!myqueue.empty()) //se dodeka ne se isprazni redot myqueue
{ s+=myqueue.front();
myqueue.pop(); //se isfrla prviot element na redot myqueue
}
cout<<"zbirot na elementite e "<<s;
return 0;
}
Задачи
1. Напиши програма со која стек / ред се полни со n цели броеви и пресметај ја сумата на непарните броеви
2. Напиши програма со која стек/ ред се полни со n цели броеви и се пресметува збир на позитивните вредности на внесените броеви.
3. Напиши програма со која стек/ред се полни со n цели броеви и пресметај ја аритметичка средина на елементите кои се непарни броеви
Sort a stack using a temporary stack
/* Delete array elements which are smaller than next or become smaller
Given an array arr[] and a number k. The task is to delete k elements which are smaller than next element (i.e., we delete arr[i] if arr[i] < arr[i+1]) or become smaller than next because next element is deleted.
Examples:
Input : arr[] = { 3, 100, 1 }
k = 1
Output : 100, 1
Explanation : arr[0] < arr[1] means 3 is less than
100, so delete 3
Input : arr[] = {20, 10, 25, 30, 40}
k = 2
Output : 25 30 40
Explanation : First we delete 10 because it follows
arr[i] < arr[i+1]. Then we delete 20
because 25 is moved next to it and it
also starts following the condition.
Input : arr[] = { 23, 45, 11, 77, 18}
k = 3
Output : 77, 18
Explanation : We delete 23, 45 and 11 as they follow
the condition arr[i] < arr[i+1]
Approach: Stack is used to solving this problem. First we push arr[0] in stack S and then initialize count as 0, then after traverse a loop from 1 to n and then we check that s.top() < arr[i] if condition is true then we pop the element from stack and increase the count if count == k then we stop the loop and then store the value of stack in another array and then print that array.
*/
// C++ program to delete elements from array.
#include <bits/stdc++.h>
using namespace std;
// Function for deleting k elements
void deleteElements(int arr[], int n, int k)
{
// Create a stack and push arr[0]
stack<int> s;
s.push(arr[0]);
int count = 0;
// traversing a loop from i = 1 to n
for (int i=1; i<n; i++) {
// condition for deleting an element
while (!s.empty() && s.top() < arr[i]
&& count < k) {
s.pop();
count++;
}
s.push(arr[i]);
}
// Putting elements of stack in a vector
// from end to begin.
int m = s.size();
vector<int> v(m); // Size of vector is m
while (!s.empty()) {
// push element from stack to vector v
v[--m] = s.top();
s.pop();
}
// printing result
for (auto x : v)
cout << x << " ";
cout << endl;
}
// Driver code
int main()
{
int n = 5, k = 2;
cout<<"Vnesi gi elementite ";
int arr[] = {20, 10, 25, 30, 40};
deleteElements(arr, n, k);
return 0;
}
No comments:
Post a Comment