Arrays + Stack Notes
Arrays + Stack Notes
Array Representation: -
One-Dimensional Array:
Two-Dimensional Array:
int arrayName[row][column][depth]
Basic Operations: -
STACK
A stack is a data structure consisting of a set of homogeneous
elements and is based on the principle of last in first out
(LIFO). It is a commonly used abstract data type with two
major operations, namely push and pop. Push and pop are
carried out on the topmost element, which is the item most
recently added to the stack. The push operation adds an
element to the stack while the pop operation removes an
element from the top position. The stack concept is used in
programming and memory organization in computers.
Basic Operations: -
Examples:
AB+ , CD- , EF* , GH/
We translate, step by step, the following infix expression into postfix
natation using brackets [ ] to indicate a partial translation:
(A + B) * C = [AB+]*C = AB+C*
A + (B * C) = A + [BC*] = ABC*+
(A + B) / (C – D) = [AB+] / [CD-] = AB+CD-/
(A+B) * (C-D) = [AB+]*[CD-] = AB + CD -*
(A + B) * C = [+AB]*C = *+ABC
A + (B * C) = A + [*BC] = +A*BC
(A + B) / (C – D) = [+AB] / [-CD] = /+AB -CD
(A+B) * (C-D) = [+AB]*[-CD] = *+AB-CD
Examples:
Expression : (A+B) * (C-D)
Postfix : AB+CD-*
Prefix : *+AB-CD
Expression : ((A-(B/C))*((A/K)-L))
Postfix : ABC/-AK/L-*
Prefix : *-A/BC-/AKL
Example of expression evaluation using stack: -
Applications of array: -
Used in mathematical problems like matrices etc.
They are used in the implementation of other data
structures like linked lists etc.
Database records are usually implemented as arrays.
Arrays are also used to implement CPU Scheduling
Algorithms.
Applications of STACK: -
Expression Handling −
Infix to Postfix or Infix to Prefix Conversion: -
The stack can be used to convert some infix expression into its
postfix equivalent, or prefix equivalent. These postfix or prefix
notations are used in computers to express some expressions.
These expressions are not so much familiar to the infix
expression, but they have some great advantages also. We do
not need to maintain operator ordering, and parenthesis.
Backtracking Procedure :-
Backtracking is one of the algorithms designing technique. For
that purpose, we dive into some way, if that way is not
efficient, we come back to the previous state and go into some
other paths. To get back from current state, we need to store
the previous state. For that purpose, we need stack. Some
examples of backtracking are finding the solution for Knight
Tour problem or N-Queen Problem etc.
push(int value)
if(top>(size-1))
cout<<"STACK OVERFLOW"<<endl;
else
top=top+1;
stack[top]=value;
pop()
if(top<0)
else
top=top-1;
display()
for(i=0;i<=top;i++)
cout<<stack[i]<<endl;
int main()
do
cin>>choice;
switch(choice)
case 1:
cin>>value;
push(value);
break;
case 2:
pop();
break;
case 3:
{
display();
break;
case 4:
cout<<"Exit"<<endl;
break;
}//end of switch
} while(choice!=4);
return 0;