From ce3e6cbad2d2eb7e68ef43598276e5f80ea592eb Mon Sep 17 00:00:00 2001
From: sujalgera01 <sujalgera01@gmail.com>
Date: Thu, 1 Oct 2020 20:17:30 +0530
Subject: [PATCH] added Stack

---
 002_STACK/display_items_of_Stack.c | 35 +++++++++++++++++
 002_STACK/peek_items_in_stack.c    | 16 ++++++++
 002_STACK/pop_items_from_stack.c   | 30 ++++++++++++++
 002_STACK/push_into_stack.c        | 27 +++++++++++++
 002_STACK/stack_Setup.c            | 63 ++++++++++++++++++++++++++++++
 5 files changed, 171 insertions(+)
 create mode 100644 002_STACK/display_items_of_Stack.c
 create mode 100644 002_STACK/peek_items_in_stack.c
 create mode 100644 002_STACK/pop_items_from_stack.c
 create mode 100644 002_STACK/push_into_stack.c
 create mode 100644 002_STACK/stack_Setup.c

diff --git a/002_STACK/display_items_of_Stack.c b/002_STACK/display_items_of_Stack.c
new file mode 100644
index 0000000..ae4e086
--- /dev/null
+++ b/002_STACK/display_items_of_Stack.c
@@ -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]);
+		}
+	}
+}
diff --git a/002_STACK/peek_items_in_stack.c b/002_STACK/peek_items_in_stack.c
new file mode 100644
index 0000000..64d6748
--- /dev/null
+++ b/002_STACK/peek_items_in_stack.c
@@ -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];
+	}
+}
\ No newline at end of file
diff --git a/002_STACK/pop_items_from_stack.c b/002_STACK/pop_items_from_stack.c
new file mode 100644
index 0000000..6f92529
--- /dev/null
+++ b/002_STACK/pop_items_from_stack.c
@@ -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;
+	}
+}
\ No newline at end of file
diff --git a/002_STACK/push_into_stack.c b/002_STACK/push_into_stack.c
new file mode 100644
index 0000000..5bd8e80
--- /dev/null
+++ b/002_STACK/push_into_stack.c
@@ -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;
+	}
+}
\ No newline at end of file
diff --git a/002_STACK/stack_Setup.c b/002_STACK/stack_Setup.c
new file mode 100644
index 0000000..1bb0721
--- /dev/null
+++ b/002_STACK/stack_Setup.c
@@ -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.
+	}
+}