D.Y.PATIL TECHNICAL CAMPUS, TALSANDE FACULTY OF ENGINEERING & FACULTY OF MANAGEMENT (Polytechnic) A Microproject Report On “Information About Stack Data Structure” Submitted By Enrollement No. Name Of Student 2212200044 Nikita Prakash Khot 2212200045 Supriya Sudarshan Bedkale 2212200046 Shruti Ashok Patil Guided By Ms. Gurav.P.S D.Y.PATIL TECHNICAL CAMPUS, TALSANDE FACULTY OF ENGINEERING & FACULTY OF MANAGEMENT (Polytechnic) DEPARTMENT OF COMPUTER ENGINEERING SEMESTER III CERTIFICATE This is Certify that student of Computer Engineering has successfully completed the project term work “Report on Stack Data Structure.” in partial fulfillment of the Diploma of Engineering in Computer as laid down during academic year 2023-24 Roll No. Name of Student 2219 Khot Nikita Prakash 2220 Bedkale Supriya Sudarshan 2221 Patil Shruti Ashok Ms. Gurav.P.S Project Guide Mr. Kumbhar R. S. HoD Dr. S. R. Pawaskar Principal Date – Exam Seat No. / – Talsande /202 Place INDEX Sr. No. 1 Title Introduction Page No. 4 2 Information about Stack 5-7 3 Real Time Example of Stack Data Structure 7 4 5 Application of Stack Operation on Stack 8-9 10-14 6 Stack implementation using ‘C’ Program 15-16 7 Output of code 17-18 8 Conclusion 19 9 References 20 Information About Stack Data structure. Introduction A Stack is a linear data structure that follows the LIFO (Last-In-First-Out) principle. Stack has one end, whereas the Queue has two ends (front and rear). It contains only one pointer top pointer pointing to the topmost element of the stack. Whenever an element is added in the stack, it is added on the top of the stack, and the element can be deleted only from the stack. In other words, a stack can be defined as a container in which insertion and deletion can be done from the one end known as the top of the stack. Stack works on the LIFO pattern. As we can observe in the below figure there are five memory blocks in the stack; therefore, the size of the stack is 5. Suppose we want to store the elements in a stack and let's assume that stack is empty. We have taken the stack of size 5 as shown below in which we are pushing the elements one by one until the stack becomes full. Since our stack is full as the size of the stack is 5. In the above cases, we can observe that it goes from the top to the bottom when we were entering the new element in the stack. The stack gets filled up from the bottom to the top. D.Y.Patil Technical Campus Talsande Faculty of Emgineering And Faculty of Management (Polytechnic) Page 5 Information About Stack Data structure. A Stack is a linear data structure that follows the LIFO (Last-In-First-Out) principle. Stack has one end, whereas the Queue has two ends (front and rear). It contains only one pointer top pointer pointing to the topmost element of the stack. Whenever an element is added in the stack, it is added on the top of the stack, and the element can be deleted only from the stack. In other words, a stack can be defined as a container in which insertion and deletion can be done from the one end known as the top of the stack. Some key points related to stack : o It is called as stack because it behaves like a real-world stack, piles of books, etc. o A Stack is an abstract data type with a pre-defined capacity, which means that it can store the elements of a limited size. o It is a data structure that follows some order to insert and delete the elements, and that order can be LIFO or FILO. D.Y.Patil Technical Campus Talsande Faculty of Emgineering And Faculty of Management (Polytechnic) Page 6 Information About Stack Data structure. Working of Stack Stack works on the LIFO pattern. As we can observe in the below figure there are five memory blocks in the stack; therefore, the size of the stack is 5. Suppose we want to store the elements in a stack and let's assume that stack is empty. We have taken the stack of size 5 as shown below in which we are pushing the elements one by one until the stack becomes full. Since our stack is full as the size of the stack is 5. In the above cases, we can observe that it goes from the top to the bottom when we were entering the new element in the stack. The stack gets filled up from the bottom to the top. When we perform the delete operation on the stack, there is only one way for entry and exit as the other end is closed. It follows the LIFO pattern, which means that the value entered first will be removed last. In the above case, the value 5 is entered first, so it will be removed only after the deletion of all the other elements. Standard Stack Operations: The following are some common operations implemented on the stack: o push(): When we insert an element in a stack then the operation is known as a push. If the stack is full then the overflow condition occurs. o pop(): When we delete an element from the stack, the operation is known as a pop. If the stack is empty means that no element exists in the stack, this state is known as an underflow state. o isEmpty(): It determines whether the stack is empty or not. o isFull(): It determines whether the stack is full or not.' o peek(): It returns the element at the given position. o count(): It returns the total number of elements available in a stack. o change(): It changes the element at the given position. o display(): It prints all the elements available in the stack. D.Y.Patil Technical Campus Talsande Faculty of Emgineering And Faculty of Management (Polytechnic) Page 7 Information About Stack Data structure. Real Time Examples of Stack Data Structure Stack is a linear data structure which follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). There are many real-life examples of a stack. Consider an example of plates stacked over one another in the canteen. The plate which is at the top is the first one to be removed, i.e. the plate which has been placed at the bottommost position remains in the stack for the longest period of time. So, it can be simply seen to follow LIFO(Last In First Out)/FILO(First In Last Out) order. 1) 2) D.Y.Patil Technical Campus Talsande Faculty of Emgineering And Faculty of Management (Polytechnic) Page 8 Information About Stack Data structure. Applications of Stack The following are the applications of the stack: o Balancing of symbols: Stack is used for balancing a symbol. For example, we have the following program: int main() { cout<<"Hello"; cout<<"javaTpoint"; } As we know, each program has an opening and closing braces; when the opening braces come, we push the braces in a stack, and when the closing braces appear, we pop the opening braces from the stack. Therefore, the net value comes out to be zero. If any symbol is left in the stack, it means that some syntax occurs in a program. o String reversal: Stack is also used for reversing a string. For example, we want to reverse a "javaTpoint" string, so we can achieve this with the help of a stack. First, we push all the characters of the string in a stack until we reach the null character. After pushing all the characters, we start taking out the character one by one until we reach the bottom of the stack. o UNDO/REDO: It can also be used for performing UNDO/REDO operations. For example, we have an editor in which we write 'a', then 'b', and then 'c'; therefore, the text written in an editor is abc. So, there are three states, a, ab, and abc, which are stored in a stack. There would be two stacks in which one stack shows UNDO state, and the other shows REDO state. If we want to perform UNDO operation, and want to achieve 'ab' state, then we implement pop operation. o Recursion: The recursion means that the function is calling itself again. To maintain the previous states, the compiler creates a system stack in which all the previous records of the function are maintained. o DFS(Depth First Search): This search is implemented on a Graph, and Graph uses the stack data structure. D.Y.Patil Technical Campus Talsande Faculty of Emgineering And Faculty of Management (Polytechnic) Page 9 Information About Stack Data structure. o Backtracking: Suppose we have to create a path to solve a maze problem. If we are moving in a particular path, and we realize that we come on the wrong way. In order to come at the beginning of the path to create a new path, we have to use the stack data structure. o Expression conversion: Stack can also be used for expression conversion. This is one of the most important applications of stack. The list of the expression conversion is given below: 1. Infix to prefix 2. Infix to postfix 3. Prefix to infix 4. Prefix to postfix 5. Postfix to infix o Memory management: The stack manages the memory. The memory is assigned in the contiguous memory blocks. The memory is known as stack memory as all the variables are assigned in a function call stack memory. The memory size assigned to the program is known to the compiler. When the function is created, all its variables are assigned in the stack memory. When the function completed its execution, all the variables assigned in the stack are released. D.Y.Patil Technical Campus Talsande Faculty of Emgineering And Faculty of Management (Polytechnic) Page 10 Information About Stack Data structure. Operations on Stack Stack operations may involve initializing the stack, using it and then de-initializing it. Apart from these basic stuffs, a stack is used for the following two primary operations − • push() − Pushing (storing) an element on the stack. • pop() − Removing (accessing) an element from the stack. When data is PUSHed onto stack. To use a stack efficiently, we need to check the status of stack as well. For the same purpose, the following functionality is added to stacks − • peek() − get the top data element of the stack, without removing it. • isFull() − check if stack is full. • isEmpty() − check if stack is empty. D.Y.Patil Technical Campus Talsande Faculty of Emgineering And Faculty of Management (Polytechnic) Page 11 Information About Stack Data structure. 1) isfull(): Algorithm of isfull() function − begin procedure isfull if top equals to MAXSIZE return true else return false endif end procedure; Implementation of isfull() function in C programming language − Example int isfull() { if(top == MAXSIZE) return 1; else return 0; } 2) isempty(): Algorithm of isempty() function − begin procedure isempty if top less than 1 else false return true return endif end procedure Implementation of isempty() function in C programming language is slightly different. We initialize top at -1, as the index in array starts from 0. So we check if the top is below zero or -1 to determine if the stack is empty. Here's the code − D.Y.Patil Technical Campus Talsande Faculty of Emgineering And Faculty of Management (Polytechnic) Page 12 Information About Stack Data structure. Example bool isempty() { if(top == -1) return true; else return false; } 3) Push(): The process of putting a new data element onto stack is known as a Push Operation. Push operation involves a series of steps − • Step 1 − Checks if the stack is full. • Step 2 − If the stack is full, produces an error and exit. • Step 3 − If the stack is not full, increments top to point next empty space. • Step 4 − Adds data element to the stack location, where top is pointing. • Step 5 − Returns success. If the linked list is used to implement the stack, then in step 3, we need to allocate space dynamically. Algorithm for PUSH Operation A simple algorithm for Push operation can be derived as follows − begin procedure push: stack, data if stack is full return null endif top ← top + 1 D.Y.Patil Technical Campus Talsande Faculty of Emgineering And Faculty of Management (Polytechnic) Page 13 Information About Stack Data structure. stack[top] ← data end procedure Implementation of this algorithm in C, is very easy. See the following code − Example void push(int data) { if(!isFull () { top = top + 1; stack[top] = data; } else { printf("Could not insert data, Stack is full.\n"); } } 4) Pop Operation Accessing the content while removing it from the stack, is known as a Pop Operation. In an array implementation of pop() operation, the data element is not actually removed, instead top is decremented to a lower position in the stack to point to the next value. But in linked-list implementation, pop() actually removes data element and deallocates memory space. A Pop operation may involve the following steps − • Step 1 − Checks if the stack is empty. • Step 2 − If the stack is empty, produces an error and exit. • Step 3 − If the stack is not empty, accesses the data element at which top is pointing. • Step 4 − Decreases the value of top by 1. • Step 5 − Returns success. D.Y.Patil Technical Campus Talsande Faculty of Emgineering And Faculty of Management (Polytechnic) Page 14 Information About Stack Data structure. Algorithm for Pop Operation A simple algorithm for Pop operation can be derived as follows − begin procedure pop: stack if stack is empty return null endif data ← stack[top] top ← top - 1 return data end procedure Implementation of this algorithm in C, is as follows − Example int pop(int data) { if(!isempty()) { data = stack[top]; top - 1; top = return data; } else { printf("Could not retrieve data, Stack is empty.\n"); } } D.Y.Patil Technical Campus Talsande Faculty of Emgineering And Faculty of Management (Polytechnic) Page 15 Information About Stack Data structure. Stack implementation using ‘C’ Program #include<stdio.h> break; #include<process.h> case 4: empty(); #include<stdlib.h> break; #define MAX 5 //Maximum number case 5: full(); of elements that can be stored break; int top=-1,stack[MAX]; case 6:exit(0); void push(); default: printf("\nWrong Choice!!"); void pop(); } void display(); } void empty(); } void full(); void push() void main() { { int val; int ch; if(top==MAX-1) clrscr(); { while(1) //infinite loop, will end when printf("\nStack is full!!"); choice will be 6 } { else { printf("\n*** Stack printf("\nEnter element to push:"); Menu***"); scanf("%d",&val); printf("\n\n1.Push\n2.Pop\n3.Display\n4.Chec top=top+1; k Stack is empty or not?\n5.Check Stack is full stack[top]=val; or not?\n6.Exit"); printf("\n\nEnter your } choice(1-6):"); scanf("%d",&ch); switch(ch) } { Case1:push void pop() { break; if(top == -1) case 2:pop(); { break; printf("\nStack is empty!!"); } Case 3:display() D.Y.Patil Technical Campus Talsande Faculty of Emgineering And Faculty of Management (Polytechnic) Page 16 Information About Stack Data structure. else { { printf("\nDeleted element printf("\nStack is full!!"); is %d",stack[top]); } top=top-1; else } { } printf("\nStack is not Full!!"); Void display } { } int i; if(top==-1) { printf("\nStack is empty!!"); } Else { printf("\nStack is\n"); for(i=top;i>=0;--i) printf("%d\n",stack[i]); } } void empty() { if(top==-1) { printf("\nStack is empty!!"); } Else { printf("\nstack is not Empty!!"); } } void full() { if(top==MAX-1) D.Y.Patil Technical Campus Talsande Faculty of Emgineering And Faculty of Management (Polytechnic) Page 17 Information About Stack Data structure. Output of code D.Y.Patil Technical Campus Talsande Faculty of Emgineering And Faculty of Management (Polytechnic) Page 18 Information About Stack Data structure. D.Y.Patil Technical Campus Talsande Faculty of Emgineering And Faculty of Management (Polytechnic) Page 19 Information About Stack Data structure. Conclusion A real-world stack allows operations at one end only. For example, we can place or remove a card or plate from the top of the stack only. Likewise, Stack ADT allows all data operations at one end only. At any given time, we can only access the top element of a stack. This feature makes it LIFO data structure. LIFO stands for Last-in-first-out. Here, the element which is placed (inserted or added) last, is accessed first. In stack terminology, insertion operation is called PUSH operation and removal operation is called POP operation. D.Y.Patil Technical Campus Talsande Faculty of Emgineering And Faculty of Management (Polytechnic) Page 20 Information About Stack Data structure. References 1. https://www.google.com/ 2. https://www.simplilearn.com/tutorials/data-structuretutorial/stacksindatastructures#:~:text=Stacks%20in%20Data%20Structures%20is, data %20structure%2C%20that%20is%20top. 3. https://www.geeksforgeeks.org/stack-data-structure/ 4. https://www.javatpoint.com/data-structure-stack 5. https://www.w3schools.in/datastructures/stack#:~:text=A%20stack %20is%20a%20linear,Infix 6. Data structure using c, Tech-max publication. D.Y.Patil Technical Campus Talsande Faculty of Emgineering And Faculty of Management (Polytechnic) Page 21