Student Of Fortune

Stack With C++ language

Share on :
Stack itself is a concept to store and retrieve the algorithm LIFO ("Last In First Out") in which incoming data first to be issued first, realnya example when we insert the ball into a tube, then the ball is entered first will be taken last time,, well that's the view stack concept.

Operations on the stack include
  1. push that is used to reserve data
  2. pop to retrieve data
  3. isEmpty to determine stack empty
  4. isFull to determine the stack in a full state
  5. clear for deleting stack
See the Code I :
// constructing stacks
#include <iostream>
#include <vector>
#include <deque>
#include <stack>
using namespace std;

int main ()
{
  deque<int> mydeque (3,100);     // deque with 3 elements
  vector<int> myvector (2,200);   // vector with 2 elements

  stack<int> first;               // empty stack
  stack<int> second (mydeque);    // stack initialized to copy of deque

  stack<int,vector<int> > third;  // empty stack using vector
  stack<int,vector<int> > fourth (myvector);

  cout << "size of first: " << (int) first.size() << endl;
  cout << "size of second: " << (int) second.size() << endl;
  cout << "size of third: " << (int) third.size() << endl;
  cout << "size of fourth: " << (int) fourth.size() << endl;

  return 0;
}

And see The code II :
# Include <stdio.h>
     # Include <conio.h>
     # Include <iostream>
     # Define MAXSTACK 100
     typedef int itemType; typedef struct {int items [MAXSTACK];
        int Amount; Stack};
     void init (Stack * s) {
      s-> Amount = 0;}

     int empty (Stack * s) {
      return (s-> Amount == 0);}
     int full (Stack * s) {return (s-> Amount == MAXSTACK);}
     void content (itemType x, Stack * s) {if (full (s))
         printf ("\ nMaaf data is full \ n");
        else {
        s-> item [s-> Amount] = x;
           + + (S-> Amount);
        }}

     void grab (Stack * s, itemType * x) {if (empty (s))
        printf ("\ nSory data is empty \ n");
        else
        {
         - (S-> Amount);
           * X = s-> item [s-> Amount];
           s-> item [s-> Amount] = 0;
           printf ("\ nData% i successfully retrieved \ n", * x);
        }}
     void show (Stack * s) {if (empty (s))
        printf ("\ nSory Data is empty \ n");
        else
 
    printf ("\ n");  
for (int i = s-> Amount-1; i> = 0; i -) {
printf ("Data:% d \ n", s-> items [i]);
}
}

void delete (Stack * s) {
s-> Amount = 0;
printf ("\ nAll data has been deleted \ n");
}

void main () {
int pill;
Stack stack;
itemType data;
init (& stack);

do {
printf ("\ nMENU: \ n 1. Contents \ n 2. Take \ n 3. See \ n 4. Delete \ n 5. Exit \ n");
printf ("Enter choice:"); scanf ("% i", & pills);

switch (pill) {
case 1:
printf ("\ nEnter Data:"); scanf ("% i", & data);;
content (data, & stack);
break;
case 2:
capture (& stack, & data);
break;
case 3:
perform (& stack);
break;
case 4:
delete (& stack);
break;
}
} while (pil! = 5);

getch ();
}
 
 reference :
http://www.cplusplus.com/reference/stl/stack/stack/

0 comments on Stack With C++ language :

Post a Comment and Don't Spam!

Dont Spam please

 
Recommended Post Slide Out For Blogger

Recent Comments

My Rank