2 CONTOH PROGRAM STACK BESERTA FLOWCHARTNYA PADA C++

2 CONTOH PROGRAM STACK BESERTA FLOWCHARTNYA PADA C++




2 CONTOH PROGRAM STACK BESERTA FLOWCHARTNYA PADA C++
1. PROGRAM STACK POINTER
PROGRAM :

#include <iostream>
#include <conio.h>
#include <stdio.h>
#include <malloc.h>

using namespace std;
typedef struct elmt *alamatelmt;
typedef struct elmt {
int isi;
alamatelmt next;
} Tstack;

typedef Tstack *stack;

void buatStack(stack *S){
(*S) = NULL;
}

void push (stack *S, int IB){
Tstack *NB;
NB = (Tstack *) malloc (sizeof (Tstack));
NB ->isi= IB;
NB->next = NULL;
if((*S) == NULL){
(*S)=NB;
}
else {
NB->next=(*S);
(*S)=NB;
}
}

void pop (stack *S,int &X){
Tstack *p;
X = (*S)->isi;
p = (*S);
if ((*S)!=NULL){
(*S)=(*S)->next;
}
free (p);
}

void cetak (stack S){
Tstack *p;
p=S;
while (p!=NULL){
cout << p->isi<<" ";
p=p->next;
}
}

main (){
int bil;
stack ST;
buatStack(&ST);
push(&ST, 26);
push(&ST, 12);
push(&ST, 21);
cout << "isi Stack mula mula : "<< endl;
cetak(ST);
cout<<endl;
for (int i=0;i<2;i++){
pop(&ST, bil);
cout << "yang diambil = "<<bil;
cout<<endl;
cout<<"Sisanya : ";
cetak(ST);
cout << endl;}

cout <<"\nIsi Stack sekarang : "<<endl;
cetak(ST);
/*cout<<"masukkan bilanga bulat : ";
cin >> bil;
while (bil !=0){
push (&ST, bil %2);
bil = bil/2;
}
cout <<"Bilangan Biner = ";
cetak (ST);*/
return 0;
}


FLOWCHAT:






2. PROGRAM PUSH STACK
PROGRAM :

#include <iostream>
#include <stdio.h>
#include <conio.h>
#define size 5
using namespace std;
struct stack {
int a [size];
int top;
};

typedef struct stack STACK;
void push (STACK *p,int value)/*Push operation */
{
if (p->top==size-1)
cout<<"STACK IS FULL";
else
p->a[++p->top]=value;}

int pop (STACK *p)/*POP OPERATION*/
{
if (p->top==-1){
cout <<"STACK IS EMPTY";
return -1;
}

else
return p->a[p->top--];}

void display (STACK *p) /* DISPLAY OPERATION */
{
int i;
if (p->top==-1)
cout <<"\n STACK IS EMPTY\n";
else
cout << "\n STACK IS\n";
for (i=p->top;i>=0;i--)
cout <<p->a[i]<<"\n";
}

int main (){
STACK s;
int x,c,i;
s.top=-1;
do {
cout << "\n1 : To PUSH \n";
cout << "2 : To POP \n";
cout << "3 : To DISPLAY \n";
cout << "4 : To DESTROY \n";
cout << "5 : To EXIT \n";
cout << "\n\n EnterChoice \n";
cin>>c;
switch (c){
case 1 :
cout << "\nEnter element";
cin >>x;
push (&s,x);
break;
case 2 :
x=pop(&s);
if (x!=-1)
cout << "\n Element deleted ="<<x;
break;
case 3 :
display(&s);
break;
case 4 :
if(s.top==-1)
printf("STACK is destroying\n");
for (i=s.top;i>=0;--i)
printf("element destroyed are %d\n",pop(&s));
s.top=-1;
}printf ("STACK DESTROYED\n");
}while (c!=5);}


FLOWCHART:




Related Posts

Previous
Next Post »