Skip to content

added Stack #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions 002_STACK/display_items_of_Stack.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
now comes the display code...
whatever we will do whether push,pop or finding peek value we have to display it so that we or the user can see it.

So look at the code below:

*/

int display(){
if(isEmpty()){
printf("Stack is Empty!")
}
else{
for(int i=0;i<=top;i++){
printf("%d\n",stack[i]);
}
}
}


/*
you can also display the elements in the reverse position.
Just change the loop.
*/

int display(){
if(isEmpty()){
printf("Stack is Empty!")
}
else{
for(int i=top;i>0;i--){
printf("%d\n",stack[i]);
}
}
}
16 changes: 16 additions & 0 deletions 002_STACK/peek_items_in_stack.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
Here now we have the code for finding "peek" value of the stack. "Peek" value means
the top-most element present in the Stack.


We have to check for the condition if the stack is empty or not.
*/

int peek(){
if(isEmpty()){
printf("Stack is Empty!");
}
else{
return stack[top];
}
}
30 changes: 30 additions & 0 deletions 002_STACK/pop_items_from_stack.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
The "pop" function is used to delete the elements from the stack.
To delete it we have to check all the possible ways like if it is already empty or not. So,
here is the code to pop the elements from a stack.

Write this code below the setup code and also don't forget to define the isEmpty() fn
outside the main function.

*/

int pop(){
int element;
if(isEmpty()){
printf("Stack is Empty");
}
else{
element = stack[top];
top--;
}
return top;
}

int isEmpty(){
if(top == -1){
return 1;
}
else{
return 0;
}
}
27 changes: 27 additions & 0 deletions 002_STACK/push_into_stack.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*The Code written below is to push the element in the Stack.

Write this code below the setup code and also don't forget to define the isFull() fn
outside the main function.

Remember that the element that we will push first will be at the last of the stack.We will see that
when we will write display function to display alll the elements of Stack.
*/

void push(int element){
if(isFull()){
printf("Stack is full!");
}
else{
top++;
stack[top] = element;
}
}

int isFull(){
if(top == max-1){
return 1;
}
else{
return 0;
}
}
63 changes: 63 additions & 0 deletions 002_STACK/stack_Setup.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
STACK CREATION USING STATIC MEMORY ALLOCATION.
---------------------------------------------
Stack works on the rule LIFO i.e. (Last In First Out). It means the element inserted at last will be
poped out first and will be displayed first.
It is similiar to the "Plates Stack" in marriages.
---------------------------------------------

The beolw code is the set-up of Stack creation.
*/

#include<stdio.h>
#include<stdlib.h>
//Defining the size of Stack by using "#define" so that you can pass the value or write "max" in array called Stack.
#define max 20
int stack[max];
// Initial value of top as -1 because array starts from 0 and we will increase its value at time of insertion.
int top = -1;

//You have to define all the functions outside the main function.

void push(int element);
int pop();
int display();
int peek();



void main(){
int element;
int choice;
// Apply while loop because it makes easy for us to know what function we want to do repeatedly.
//This makes understanding easy and our life comfortable :)
while(1){
printf("1.Push\n");
printf("2.Pop\n");
printf("3.Peek\n");
printf("4.Display\n");
printf("5.Quit\n");
printf("Enter your Choice");
scanf("%d",&choice);

// Use switch for the choice you want to perform.
switch(choice){
case 1: printf("Enter the element");
scanf("%d",&element);
push(element);
break;
case 2: pop();
break;
case 3: peek();
break;
case 4: display();
break;
case 5: exit(0);
break;
default: printf("Wrong Choice");
}
//Now We have to define all these functions to perform it on the Stack.
//Remember it is the easy way to perform functions in a simplified way.
//So,we will be writing the above code same in all the operations.
}
}