Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Header File Part STACK.h
- #ifndef stack_h_
- #define stack_h_
- void push (int item);
- void pop();
- void isFULL();
- void isEmpty();
- int getTop();
- #endif //
- //STACK APPLICATION
- #include <stdio.h>
- #include <stdlib.h>
- #include "stack.h"
- void main (){
- int value, choice, topvalue;
- while(1){
- printf("------MENU------\n");
- printf("(1) Push\n");
- printf("(2) Pop\n");
- printf("(3) Is Full\n");
- printf("(4) Is Empty\n");
- printf("(5) Get top\n");
- printf("(6) Exit\n");
- printf(" Enter your choice \n");
- scanf("%d", &choice);
- switch(choice) {
- case 1:
- printf("Enter an number: ");
- scanf("%d", &value);
- push(value);
- printf("Value pushed\n");
- break;
- case 2:
- pop();
- printf("Value poped\n");
- break;
- case 3:
- isFULL();
- break;
- case 4:
- isEmpty();
- break;
- case 5:
- topvalue = getTop();
- printf("The top element is: %d\n", topvalue );
- break;
- case 6:
- exit(0);
- default:
- printf("Wrong input\n");
- }
- }
- }
- // Stack Implementation
- #include <stdio.h>
- #include "stack.h"
- const int maxItem = 5;
- int top = -1;
- int items[maxItem];
- int item;
- void push (int item){
- if (top + 1 > maxItem){
- printf("Stack Overflow\n");
- }
- else {
- items[top+1] = item;
- top = top +1;
- //top = top++;
- }
- }
- void pop (){
- if (top==-1){
- printf("Stack underflow\n");
- }
- else {
- item = items[top];
- printf("Value is : %d\n", item);
- items[top] = 0;
- top = top -1;
- }
- }
- void isFULL(){
- if (top + 1 == maxItem){
- printf("Stack is full\n");
- }
- else {
- printf("Stack is full\n");
- }
- }
- void isEmpty(){
- if (top == -1 ){
- printf("Stack Empty\n");
- }
- else {
- printf("Stack not empty\n");
- }
- }
- int getTop(){
- if (top != -1){
- return items[top];
- }
- else{
- return -1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement