Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include "stack_character.h"
- using namespace std;
- void createStack(stack &S){
- top(S) = 0;
- }
- bool isEmpty(stack S){
- if(top(S) == 0){
- return true;
- }
- return false;
- }
- bool isFull(stack S){
- if(top(S)== MAXSTACK){
- return true;
- }
- return false;
- }
- void push(stack &s,infotype x){
- if(!isFull(s)){
- top(s) += 1;
- info(s)[top(s)] = x;
- }else{
- cout<<"Stack penuh"<<endl;
- }
- }
- void pop(stack &s,infotype &x){
- if(!isEmpty(s)){
- x = info(s)[top(s)];
- top(s)-=1;
- }else{
- cout<<"Stack Kosong"<<endl;
- }
- }
- void printInfo(stack S){
- if(!isEmpty(S)){
- for(int x = top(S);x>0;x--){
- cout << info(S)[x] <<" ";
- }
- cout<<endl;
- }else{
- cout<<"Stack Kosong"<<endl;
- }
- }
- void ascending(stack &s){
- int n = s.top-1;
- infotype tempArray[MAXSTACK];
- for (int i = 1; i <= n+1; i++) {
- tempArray[i] = info(s)[i];
- }
- for (int i = 1; i <= n; i++) {
- for (int j = 1; j <= n; j++) {
- if (tempArray[j] < tempArray[j + 1]) {
- infotype temp = tempArray[j];
- tempArray[j] = tempArray[j + 1];
- tempArray[j + 1] = temp;
- }
- }
- }
- for (int i = 1; i <= n+1; i++) {
- info(s)[i] = tempArray[i];
- }
- }
- void descending(stack &s){
- int n = s.top-1;
- infotype tempArray[MAXSTACK];
- for (int i = 1; i <= n+1; i++) {
- tempArray[i] = info(s)[i];
- }
- for (int i = 1; i <= n; i++) {
- for (int j = 1; j <= n; j++) {
- if (tempArray[j] > tempArray[j + 1]) {
- infotype temp = tempArray[j];
- tempArray[j] = tempArray[j + 1];
- tempArray[j + 1] = temp;
- }
- }
- }
- for (int i = 1; i <= n+1; i++) {
- info(s)[i] = tempArray[i];
- }
- }
- void stringToStack(stack &S,const string &str){
- for (int i = 0; i < str.size(); i++){
- if (!isFull(S)) {
- push(S, str[i]);
- } else {
- break;
- }
- }
- }
- void reverseStack(stack &s){
- int n = top(s);
- for (int i = 1; i <= n / 2; i++) {
- infotype temp = s.info[i];
- s.info[i] = s.info[n - i+1];
- s.info[n - i+1] = temp;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement