STACK "Array-Based Implementation" In C++
Im going to show you the implementation of of "Stack" "First In Last Out" In Array-Based Implementation in C++ Language
You will find the code splitted to three parts at the first part stack.h we are going to define function's header and at the second part stack.cpp we are going to implement this function and in the last part main.cpp we are going call this function
Implementation Of Stack Array-Based
Stack .h
1 #define MAXSTACK 5 2 typedef int INFO; 3 4 typedef struct stack 5 { 6 INFO Entry[MAXSTACK]; 7 int top; 8 }STACK; 9 10 void CreateStack(STACK *); 11 int StackEmpty(STACK *); 12 int StackFULL(STACK *); 13 void Push(STACK *,INFO *); 14 void Pop(STACK *,INFO *); 15 void CopyStack(STACK *,STACK *); 16 int SizeStack(STACK *); 17 void TraverseStack(STACK *,void (*pt)(INFO ));
1 #include "Stack.h" 2 3 void CreateStack(STACK *s) 4 { 5 s->top=0; 6 } 7 8 int StackEmpty(STACK *s) 9 { 10 return(s->top==0); 11 } 12 13 int StackFULL(STACK *s) 14 { 15 return(s->top==MAXSTACK); 16 } 17 18 void Push(STACK *s,INFO *item) 19 { 20 s->Entry[s->top]=*item; 21 s->top++; 22 } 23 24 void Pop(STACK *s,INFO *item) 25 { 26 s->top--; 27 *item=s->Entry[s->top]; 28 } 29 30 void CopyStack(STACK *s,STACK *s1) 31 { 32 int i; 33 for(i=0;i<s->top;i++) 34 { 35 s1->Entry[i]=s->Entry[i]; 36 } 37 s1->top = s->top; 38 } 39 40 int SizeStack(STACK *s) 41 { 42 return s->top; 43 } 44 45 void TraverseStack(STACK *s,void (*pt)(INFO )) 46 { 47 int i; 48 for(i=s->top-1;i>=0;i--) 49 { 50 (*pt)(s->Entry[i]); 51 } 52 }
Main.cpp
1 #include "Stack.h" 2 #include <stdio.h> 3 #include <stddef.h> 4 5 void Display(INFO e) 6 { 7 printf("%d\n",e); 8 } 9 10 11 int main() 12 { 13 int ch; 14 INFO e; 15 STACK s,s1; 16 CreateStack(&s); 17 CreateStack(&s1); 18 printf("\nChoose 1 To Push into stack\nChoose 2 To Pop from stack\nChoose 3 To Copy FROM Stack To Another\nChoose 4 To Copy Stack Number 2\nChoose 5 To Size Of Stack\nChoose 6 To TraversStack\n"); 19 scanf("%d",&ch); 20 while(ch!=7) 21 { 22 if(ch==1) 23 { 24 printf("Enter Data To Push it into stack\n"); 25 scanf("%d",&e); 26 if(!StackFULL(&s)) 27 { 28 Push(&s,&e); 29 } 30 else 31 printf("Stack Full .. Sorry !,Icant Push IT\n"); 32 } 33 else if(ch==2) 34 { 35 if(!StackEmpty(&s)) 36 { 37 Pop(&s,&e); 38 printf("\nThe Item is : %d\n",e); 39 } 40 else 41 printf("Stack Empty .. Sorry !\n"); 42 43 } 44 else if(ch==3) 45 { 46 /*Implementation Level*/ 47 CopyStack(&s,&s1); 48 printf("Copied Successfully\n"); 49 } 50 else if(ch==4) 51 { 52 if(!StackEmpty(&s1)) 53 { 54 Pop(&s1,&e); 55 printf("\nThe Item is : %d\n",e); 56 } 57 else 58 printf("Stack Empty .. Sorry !\n"); 59 } 60 61 else if(ch==5) 62 { 63 printf("\nSize Of Stack : %d\n",SizeStack(&s)); 64 } 65 else if(ch==6) 66 { 67 printf("You Are Going To Traverse This Stack\n"); 68 TraverseStack(&s,&Display); 69 printf("Successfully Traversed\n"); 70 } 71 printf("\nChoose 1 To Push into stack\nChoose 2 To Pop from stack\nChoose 3 To Copy FROM Stack To Another\nChoose 4 To Copy Stack Number 2\nChoose 5 To Size Of Stack\nChoose 6 To TraversStack\n"); 72 scanf("%d",&ch); 73 } 74 75 76 }
You Can Download It From Here : http://goo.gl/lZ3yd0

Leave a Comment