Skip to main content

Introduction to Stack

As there are different types of books available on the rack or in the library, the moment they become handful, we start arranging them neatly. Stack of books or stack of dishes is the examples of stack in our daily life. Stacks are used in compilers in passing an expression by recursion; in memory management in operating system; etc.

Stack is a linear data structure and very much useful in various applications of computer science. Implementation of majority of the systems programs are simplified using this data structure.


DEFINITION

A stack is an ordered, collection of homogeneous data element where the insertion and deletion operations take place at one end only i.e., data is stored and retrieved in a Last In First Out (LIFO) order.




FIGURE 3.1 Schematic diagram of a stack

Like array and linked list, stack is also a linear data structure but the only difference is that in case of former two, insertion and deletion operations can take at place at any position. The insertion and deletion operations in case of stack are specially termed as PUSH and POP respectively and the position of the stack where these operations are performed is known as TOP of the stack. An element in a stack is termed as ITEM. The maximum number elements that a stack can accommodate are termed as SIZE.



OPERATIONS ON STACK

1. Algorithm for PUSH operation

Input: The new ITEM to be pushed onto it.
Output: A stack with newly published ITEM at the TOP position
Data structure: An array A with TOP as the pointer.

Steps:

1. [Check for stack overflow]
                     1. If top = size -1 then
                     2. Print “ Stack is full”
                     3. Exit
2. [Increment top pointer by one]
                     1. Top = Top + 1
3. [Perform Insertion]
                     1. Stack [Top] = value
4. Stop

2. Algorithm for POP operation

Input: A stack with elements.
Output: Removes an ITEM from the top of the stack if it is not empty.
Data structure: An array A with TOP as the pointer.

Steps:

1. [Check for stack underflow]
                     If top = -1
                     Print “Stack is empty”
                     Exit

2. [Remove the top information]
                     Value = stack[top]
                     Top = top – 1
3. Stop

3. Algorithm for STATUS operation

Input: A stack with elements.
Output: States whether it is empty or full, available free space and item at TOP.
Data structure: An array A with TOP as the pointer.

Steps:

1. if(top = -1) then
                     1. Print “ Stack is empty”
2. else if(top = size -1) then
                     1. Print “ stack is full”
3. else
                     1. Freespace = size – 1 – top
                     2. Print “ Available space “, freespace
4. endif
5. stop



Compiled By: Chaudhary Amit V.

Comments

Popular posts from this blog

Characteristics of a Good Programming Language

Till now there are many high level languages which are very popular, and there are others, which could not become so popular in-spite of being very powerful. There might be many reasons for the success of a language, but one obvious reason is the characteristics of the language. Several characteristics believed to be important with respect to making a programming language good are briefly discussed below. Simplicity A good programming language must be simple and easy to learn and use. For example, BASIC is liked by many programmers only because of its simplicity. Thus, a good programming language should provide a programmer with a clear, simple and unified set of concepts which can be easily grasped. It is also easy to develop and implement a compiler or an interpreter for a programming language that is simple. However, the power needed for the language should not be sacrificed for simplicity. The overall simplicity of a programming language strongly affects the readability of the pr...

Angular 4 and Firebase Authentication: Setup

If you have come to this article, it means you are keen to learn new technology and that too Firebase. I love Firebase because it provides all basic but important features of any web application. Take for example, authentication, which is cumbersome and risky if not implemented with utmost care. And when we have the integration of Angular and Firebase, it becomes a lot easier for a developer to build such crucial modules in less time with minimal efforts. In this article we will create a simple Angular application using Firebase. I am going ahead with the understanding that you know the benefits of Firebase and have little knowledge about it. There are plenty of posts out on the web if you are behind. Step 1: Install Angular CLI (if not installed). npm install -g angular-cli Step 2: Create new Angular 4 project. By default now angular CLI will create Angular 4 project so you need not fret. ng new firebase-authentication Step 3: Check whether the ne...

Switch-case, simple calculator . . .

11.      Write a program to perform some mathematic functions with the help of “switch-case” statement. #include<stdio.h> #include<conio.h> void main() { int a,b,ans; char c; clrscr(); printf("\ Enter a and b:"); scanf("%d%d",&a,&b); printf("\n Enter operator:"); fflush(stdin); scanf("%c",&c); switch(c) { case'+':ans=a+b;       break; case'-':ans=a-b;       break; case'*':ans=a*b;       break; case'/':ans=a/b;       break; } printf("\n ans %d",ans); getch(); } 12.      Print the Fibonacci series #include<stdio.h> #include<conio.h> void main() { int a=1,b=1,s=3,c,ch; clrscr(); printf("how many steps="); scanf("%d",&ch); printf("%5d%5d",a,b); for(s=3;s<=ch;s++) {       c=a+b;       printf("%5d",c);       a=b;    ...