Maniruzzaman Akash's Blog

Maniruzzaman Akash, A programmer, A web programmer, a helpful person to share knowledge and everything..

  • Home
  • Programming
    • C Programming
    • Java Programming
    • C++ Programming
    • C# Programming
    • Python Programming
    • Data Structure
  • Web
    • HTML
    • CSS
    • Javascript
    • PHP
    • AJAX
    • XML
  • FrameWork
    • Bootstrap(CSS)
    • Laravel 5(PHP)
  • Database
    • MySQL
    • Oracle
    • SQL Server
  • Android
    • Android Development
  • Mathematics
    • Numerical Methods
  • Others
    • My Articles
    • Download Ebooks
    • Mathematics
    • Difference Between
  • Contact
C Programming Data Structure

Stack Complete C code simply learn

Friday, February 3, 2017 By Maniruzzaman Akash 0 Comments
Stack Complete C code simply learn

/**

@author: Maniruzzaman Akash

@time: June 2016

**/



#include<stdio.h>

#include<stdlib.h>

#define MAX 100



struct stack{



    int data[MAX];

    int top;

};



typedef struct stack Stack;

Stack s;



//Functions

void push(int value);

int pop();

void display();

int getCount();



//main() method starts here

int main( ){

 int chose, pushValue;

 s.top = -1;

 printf("\n\t\t\tStack\t\t\t\n\t\t---------------------\t\t\t");



 printf("\n\t\tPress 1 for Push a value\n\t\tPress 2 for pop a value\n\t\tPress 3 for displaying total values\n\t\tPress 4 for total size\n\t\tPress 0 to exit");

 printf("\n-----------------------------------------------------\n");

 while(1){

  printf("Enter your option : ");

  scanf("%d", &chose);

  switch(chose){

   case 1:

    printf("Enter a value to push in  the stack : ");

    scanf("%d", &pushValue);

    push(pushValue);

    break;

   case 2:

    pop();

    break;

   case 3:

    display();

    break;



   case 4:

    printf("The total element  in the stack is : ");

    getCount();

    break;

   case 0:

    printf("Program exited successfully\n\n");

    return 0 ;

    break;

   default:

    printf("Please chose a value from given option\n");



  }

 }

}

//Functions

void push(int value){



  if(s.top == (MAX -1)){

   printf("Sorry stack is full\n");

  }else{

   s.top++;

   s.data[s.top] = value;

   printf("\tValue inserted successfully\n");

  }





 }

 int pop(){

  int popValue, i;

  if(s.top == -1){

   printf("Their is no value to pop\n");

  }else{

   popValue = s.data[s.top];

     printf("The element %d is popped from the stack successfully\n", popValue);

     s.top--;

     return popValue;

  }

 }

 void display(){

  int printvalue, i;

  if(s.top == -1){

   printf("Sorry stack is empty\n");

  }else{

   printf("The total stack is : ");

   for(i = 0; i <= s.top; i++){

    printvalue = s.data[i];

    printf("%d\t", printvalue);

   }

  }

  printf("\n");

 }







 int getCount(){

  int count = 0, i;

  for(i = s.top; i >=0; i--){

    s.data[i];

    count++;

  }

  printf("%d\n", count);

  return count;

 }



Code demonstration:

To start to stack please read about stack first and then come here to understand the code.
Learn about Stack fully.

Stack is mainly the push and pop function.

In push function:

if(s.top == (MAX -1)){

   printf("Sorry stack is full\n");

  }else{

   s.top++;

   s.data[s.top] = value;

   printf("\tValue inserted successfully\n");

  }
If top value of stack is over the stack size than give an alert that stack is full. Otherwise just
   s.top++;

   s.data[s.top] = value;
Make top++ and then set the top data as our push's value. This is the simple push function.

In pop function:

  int popValue, i;

  if(s.top == -1){

   printf("Their is no value to pop\n");

  }else{

   popValue = s.data[s.top];

     printf("The element %d is popped from the stack successfully\n", popValue);

     s.top--;

     return popValue;

  }
If top value postition is not minimum 0 then make an alert than stack is fully empty no value can be popped from the stack.
Otherwise, make
s.top--;
Top--, that means remove the top value from the stack. and this function will return the pop value.

In display function:

  int printvalue, i;

  if(s.top == -1){

   printf("Sorry stack is empty\n");

  }else{

   printf("The total stack is : ");

   for(i = 0; i <= s.top; i++){

    printvalue = s.data[i];

    printf("%d\t", printvalue);

   }

  }

  printf("\n");
If top value position is not minimum 0 then give an alert that stack is empty otherwise, run a for loop upto the top position from beginnings and prin the data of that position.
printvalue = s.data[i];

Count stack size function:

 int getCount(){

  int count = 0, i;

  for(i = s.top; i >=0; i--){

    s.data[i];

    count++;

  }

  printf("%d\n", count);

  return count;

 }

This is same as display function and for this if enter into loop then add  increment the count variable and finally return the count variable.

[Note: This is all about my code, Don't copy pest these codes to other sites but you can use it in your academic life.]
You face any problem further in stack then comment please.


C Programming
Share:

Maniruzzaman Akash
Maniruzzaman Akash, an enthusiastic programmer, a web developer

Related Articles


0 comments:

Post a Comment

Newer Post Older Post Home
Subscribe to: Post Comments ( Atom )

Popular Posts

  • Numerical Methods 20 Multiple Choice Questions and Answers
  • Consider a hypothetical 32-bit microprocessor having 32-bit instructions: Solutions
  • List and briefly define two approaches to dealing with multiple interrupts
  • The hypothetical machine has two I/O instructions: 0011= Load AC fro I/O 0111= Store AC to I/O Solutions
  • What are the characteristics of Digital IC's?-Solution
  • Mid Square Method Code implementation in C and MatLab
  • List and briefly define the possible states that define an instruction execution
  • BFS, DFS, DLS in Tree implementation in C
  • Download Laravel Offline Documentation as HTML
  • Simpson's 1/3 Code in Matlab

Category

Advanced PHP Android Developement Articles Artificial Intelligenece Blogger Tips Blogging Career Bootstrap Offline Documentation Bootstrap Templates C Programming Computer Architecture Data Structure Difference Between Download Documentation Download Ebook Download Free Blog Template Earning Money Electrical Electronics Guest Posts HTML Java Programming Laravel Laravel Bangla Tutorial MatLab Code My Videos MySQL Database Numerical Methods Offline Documentation Recent Topics Simulation and Modeling Unity Game Development Web Design Web Development

LIKE ME ON FACEBOOK

TAGS

  • Advanced PHP (3)
  • Android Developement (5)
  • Articles (6)
  • Artificial Intelligenece (3)
  • Blogger Tips (5)
  • Blogging Career (1)
  • Bootstrap Offline Documentation (1)
  • Bootstrap Templates (1)
  • C Programming (14)
  • Computer Architecture (5)
  • Data Structure (11)
  • Difference Between (1)
  • Download Documentation (2)
  • Download Ebook (3)
  • Download Free Blog Template (2)
  • Earning Money (1)
  • Electrical Electronics (1)
  • Guest Posts (1)
  • HTML (4)
  • Java Programming (2)
  • Laravel (3)
  • Laravel Bangla Tutorial (1)
  • MatLab Code (2)
  • My Videos (3)
  • MySQL Database (7)
  • Numerical Methods (9)
  • Offline Documentation (1)
  • Recent Topics (1)
  • Simulation and Modeling (2)
  • Unity Game Development (3)
  • Web Design (3)
  • Web Development (1)

Join Google+

Maniruzzaman Akash
View my complete profile

Join With Me

Site Visitors

Best Sites For a programmer

  • URI Online Judge Solution By Me
  • StackOverFlow
  • W3 School
  • Git Hub - Store your Every Document

Popular Posts

  • What are the characteristics of Digital IC's?-Solution
  • Numerical Methods 20 Multiple Choice Questions and Answers
  • The hypothetical machine has two I/O instructions: 0011= Load AC fro I/O 0111= Store AC to I/O Solutions
  • Consider a hypothetical 32-bit microprocessor having 32-bit instructions: Solutions
  • List and briefly define two approaches to dealing with multiple interrupts
  • Mid Square Method Code implementation in C and MatLab
  • How to import Excel,CSV file in Laravel And insert data in database
  • What is Blue Whale Game ? Why people suicide?
  • Numerical method Codes simple MatLab implementation
  • Depth First Search DFS code using Binary Tree in C language

Earn Money From Your site

Translate To your language

Categories

Advanced PHP (3) Android Developement (5) Articles (6) Artificial Intelligenece (3) Blogger Tips (5) Blogging Career (1) Bootstrap Offline Documentation (1) Bootstrap Templates (1) C Programming (14) Computer Architecture (5) Data Structure (11) Difference Between (1) Download Documentation (2) Download Ebook (3) Download Free Blog Template (2) Earning Money (1) Electrical Electronics (1) Guest Posts (1) HTML (4) Java Programming (2) Laravel (3) Laravel Bangla Tutorial (1) MatLab Code (2) My Videos (3) MySQL Database (7) Numerical Methods (9) Offline Documentation (1) Recent Topics (1) Simulation and Modeling (2) Unity Game Development (3) Web Design (3) Web Development (1)

Subscribe To This Site To Get Latest Article On Programming or web

Posts
Atom
Posts
Comments
Atom
Comments
© 2016 Maniruzzaman Akash's Blog | All rights reserved
Developed By Maniruzzaman Akash Created By Responsive Blogger Templates | Distributed By Gooyaabi Templates