Skip to main content

Stacks

Amu_Ke_Fundye

Stacks


A stack is an ordered collection of items into which new items may be inserted and from which items may be deleted at one end, called the TOP of the stack. It is a LIFO (Last In First Out) kind of data structure.

Operations on Stack
  • Push: Adds an item onto the stack. PUSH (s, i); Adds the item i to the top of stack.
  • Pop: Removes the most-recently-pushed item from the stack. POP (s); Removes the top element and returns it as a function value.
  • size(): It returns the number of elements in the queue.
  • isEmpty(): It returns true if queue is empty.

Implementation of Stack
A stack can be implemented in two ways: Using Array and Using Linked list.
But since array sized is defined at compile time, it can’t grow dynamically. Therefore, an attempt to insert/push an element into stack (which is implemented through array) can cause a stack overflow situation, if it is already full.
Go, to avoid the above mentioned problem we need to use linked list to implement a stack, because linked list can grow dynamically and shrink at runtime.

1. Push and Pop Implementation Using Array:
void push( ) {
if(top==max)
printf(“\nOverflow”);
else {
int element;
printf(“\nEnter Element:”);
scanf(“%d”,&element);
printf(“\nElement(%d) has been pushed at %d”, element, top);
stack[top++]=element;
}
}
void pop( ) {
if(top==-1)
printf(“\nUnderflow”);
else
{
top–;
printf(“\nELement has been popped out!”);
}
}

2. Push and Pop Implementation Using Linked List:
struct node {
int data;
struct node *prev;
}*top=NULL, *temp=NULL;
void push( ) {
temp = (struct node*)malloc(sizeof(struct node*));
printf(“\nEnter Data:”);
scanf(“%d”,&temp->data);
temp->prev=NULL;
if(top==NULL) {
top=temp;
}
else {
temp->prev=top;
top=temp;
}
}
void pop( ) {
temp=top->prev;
top=temp;
printf(“\nDeleted: %d”,top->prev);
}

Applications of Stack
  • Backtracking: This is a process when you need to access the most recent data element in a series of elements.
  • Depth first Search can be implemented.
  • The function call mechanism.
  • Simulation of Recursive calls: The compiler uses one such data structure called stack for implementing normal as well as recursive function calls.
  • Parsing: Syntax analysis of compiler uses stack in parsing the program.
  • Web browsers store the addresses of recently visited sites on a stack.
  • The undo-mechanism in an editor.
  • Expression Evaluation: How a stack can be used for checking on syntax of an expression.
    • Infix expression: It is the one, where the binary operator comes between the operands. 
      e. g., A + B * C.
    • Postfix expression: Here, the binary operator comes after the operands.
      e.g., ABC * +
    • Prefix expression: Here, the binary operator proceeds the operands. 
      e.g.,+ A * BC
    • This prefix expression is equivalent to A + (B * C) infix expression. Prefix notation is also known as Polish notation. Postfix notation is also known as suffix or Reverse Polish notation.
  • Reversing a List: First push all the elements of string in stack and then pop elements.
  • Expression conversion: Infix to Postfix, Infix to Prefix, Postfix to Infix, and Prefix to Infix
  • Implementation of Towers of Hanoi
  • Computation of a cycle in the graph

Example-1: Implementation of Towers of Hanoi
Let A, B, and C be three stacks. Initially, B and C are empty, but A is not.
Job is to move the contents of A onto B without ever putting any object x on top of another object that was above x in the initial setup for A.
void TOH (int n, Stack A, Stack B, Stack C) {
if (n == 1) B.push (A.pop());
else {
TOH (n – 1, A, C, B); // n-1 go from A onto C
B.push (A.pop());
TOH (n – 1, C, B, A); // n-1 go from C onto B
}
}

Example-2: Evaluate the following postfix notation of expression: 15 3 2 + / 7 + 2 *
stack
 Regards 
Amrut Jagdish Gupta

Comments

Popular posts from this blog

Undecidability

Amu_Ke_Fundye Undecidability Decidable Problem If there is a Turing machine that decides the problem, called as Decidable problem. A decision problem that can be solved by an algorithm that halts on all inputs in a finite number of steps. A problem is decidable, if there is an algorithm that can answer either yes or no. A language for which membership can be decided by an algorithm that halts on all inputs in a finite number of steps. Decidable problem is also called as totally decidable problem, algorithmically solvable, recursively solvable. Undecidable Problem A problem that cannot be solved for all cases by any algorithm whatsoever. Equivalent Language cannot be recognized by a Turing machine that halts for all inputs. The following problems are undecidable problems: Halting Problem:  A halting problem is undecidable problem. There is no general method or algorithm which can solve the halting problem for all possible inputs. Emptyness Problem:  Wh

Funny Shortcut to Remember Periodic Table

Amu_Ke_Fundye Funny Shortcut to Remember Periodic Table THE PERIODIC TABLE The periodic table is a tabular display of the chemical elements, organized on the basis of their atomic number's , electron configurations, and chemical properties.In order to appreciate the general trends in chemistry and to explain the deviations of some from these general trends we need to to have good knowledge about the position of those elements in periodic table. This knowledge is also extremely important for competitive exams. So, here I present a way to memorize this using some funny easy shortcut sentences. THE FIRST 18 ELEMENTS (H He Li Be B C N O F Ne Na Mg Al Si P S Cl Ar)  H ie  He   Li es  Be cause  B oron  C an N ot  O xide  Fl orine.. N e w  Na tions  M ight  Al so  Si ng  P eaceful  S ong  Cl early  A gain  THE S-BLOCK ELEMENTS (LEARN-ALONG THE GROUP) Group 1 (Li Na K Ru Cs Fr)  Sentence:  LiNa   K i  Ru by  C e  Fr iendship          

ALU and Data Path

Amu_Ke_Fundye ALU and Data Path The CPU can be divided into a data section and a control section. The   data section , which is also called the   datapath. Datapath The registers, the ALU, and the interconnecting bus are collectively referred to as the datapath.  Each bit in datapath is functionally identical.  The datapath is capable of performing certain operations on data items. The  control section  is basically the  control unit ,  which issues control signals to the datapath . Bus : A Bus is a collection of wires or distinct lines meant to carry data, address and control information. Data Bus : it is used for transmission of data. The number of data lines corresponds to the number of bits in a word. Address Bus : it carries the address of the main memory location from where the data can be accessed. Control Bus : it is used to indicate the direction of data transfer and to coordinate the timing of events during the transfer. PC (Pro