Stack as ADT
size() – Returns the
size of the stack
top() – Returns a reference to the top most element of the stack
push(g) – Adds the element ‘g’ at the top of the stack
pop() – Deletes the top most element of the stack
top() – Returns a reference to the top most element of the stack
push(g) – Adds the element ‘g’ at the top of the stack
pop() – Deletes the top most element of the stack
Example:
#include <iostream>
#include <stack>
using namespace std;
void showstack(stack <int> s)
{
while
(!s.empty())
{
cout << '\t'
<< s.top();
s.pop();
}
cout << '\n';
}
int main
()
{
stack <int> s;
s.push(10);
s.push(30);
s.push(20);
s.push(5);
s.push(1);
cout << "The
stack is : ";
showstack(s);
cout << "\ns.size()
: " << s.size();
cout << "\ns.top()
: " << s.top();
cout << "\ns.pop()
: ";
s.pop();
showstack(s);
return 0;
}
Output:
The stack is : 1 5 20 30 10
s.size() : 5
s.top() : 1
s.pop() : 5 20 30 10
Queue As ADT
The functions supported by queue are :
1. empty() –
Returns whether the queue is empty.
2. size() –
Returns the size of the queue.
3. queue::swap() in C++ STL: Exchange the contents of two queues
but the queues must be of same type, although sizes may differ.
4. queue::emplace() in C++ STL: Insert a new element into the
queue container, the new element is added to the end of the queue.
5. queue::front() and
queue::back() in C++ STL– front() function
returns a reference to the first element of the queue. back() function returns a reference to the last
element of the queue.
6. push(g) and pop() – push() function
adds the element ‘g’ at the end of the queue. pop() function
deletes the first element of the queue.
#include <iostream>
#include <queue>
using namespace
std;
void showq(queue
<int> q1)
{
while
(!q1.empty())
{
cout << '\t'
<< q1.front();
q1.pop();
}
cout << '\n';
}
int main()
{
queue
<int> q;
q.push(10);
q.push(20);
q.push(30);
cout << "The
queue is : ";
showq(q);
cout << "\nsize()
: " << q.size();
cout << "\nfront()
: " << q.front();
cout << "\nback()
: " << q.back();
q.pop();
cout << "\nThe
queue after pop() : ";
showq(q);
return 0;
}
Output:
The queue is : 10 20 30
size() : 3
front() : 10
back() : 30
The queue after pop() : 20 30
No comments:
Post a Comment