The stack
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. When an element is added to a stack, it is said that the element is pushed onto the stack and the operation is called a push operation. When we remove an item from the stack, it is said that an item is popped from the stack and this operation is called the pop operation. When performing the POP operation, there may be the chance of stack underflow i.e. t
Summary
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. When an element is added to a stack, it is said that the element is pushed onto the stack and the operation is called a push operation. When we remove an item from the stack, it is said that an item is popped from the stack and this operation is called the pop operation. When performing the POP operation, there may be the chance of stack underflow i.e. t
Things to Remember
- 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.
- A stack is a data structure that permits adding and removing elements in a specific order.
- The primitive operations on a stack are push, pop, empty, full and stacktop.
- When an element is added to a stack, it is said that the element is pushed onto the stack and the operation is called a push operation.
- When we remove an item from the stack, it is said that an item is popped from the stack and this operation is called the pop operation.
- Empty operation determines whether the stack is empty or not.
- Full operation determines whether the stack is full or not.
- Stacktop operation allows the user to know what the top item of the stack is without having to pop it.
- When performing the POP operation, there may be the chance of stack underflow i.e. the stack is empty.
- With the detection of underflow, it may not be necessary to halt the execution immediately, Instead, the pop routine could signal the occurrence of an underflow and take the corrective action.
- Suppose the stack is full and we attempt to push an item onto the stack. This results in a condition called the overflow.
- When an overflow is detected in push, an error message is printed and the execution halts immediately.
MCQs
No MCQs found.
Subjective Questions
Q1:
What are the developmental activities of an adolescent stage ?
Type: Long Difficulty: Easy
<p> </p>
<p><strong>Stages of adolescence</strong></p>
<p> </p>
<ol>
<li><strong> Early adolescence ( 11-13 years of age )</strong></li>
</ol>
<p><strong>Physical development</strong></p>
<p>_ Puberty grow body hair, increase perspiration and oil production in hair and skin.</p>
<p>_ Girls : breast and hip development onset of menstruation.</p>
<p>_ Boys : growth in testicles and penis, wet dreams, deeping voice.</p>
<p>_ Tremendous physical growth : gain height and weight</p>
<p> </p>
<p><strong>Cognitive development</strong></p>
<p>_ Growing capacity for abstract thought</p>
<p>_ mostly interest in present with limited thought to the future</p>
<p>_ Intellectual interests expand and become more important.</p>
<p>_ Deeper moral thinking.</p>
<p> </p>
<p><strong>Social-emotional development</strong>.</p>
<p>_ struggle with a sense of identity.</p>
<p>_ feel awkward about one's self and one's body , worry about being normal.</p>
<p>_ increased influence of peer group</p>
<p>_ desire for independence</p>
<p>_ tendency to return to childish behaviour particularly when stressed</p>
<p>_ moodiness</p>
<p>_ rule and limit testing</p>
<p> </p>
<ol start="2">
<li><strong> Middle adolescence ( 14-18 years of age )</strong></li>
</ol>
<p><strong>Physical development</strong></p>
<p>_ puberty is completed.</p>
<p>_ physical growth slows of girls continues for boys.</p>
<p> </p>
<p><strong>Cognitive development</strong></p>
<p>_ continued growth pf capacity for abstract thought.</p>
<p>_ greater capacity for setting goals.</p>
<p>_ interest in moral reasoning</p>
<p>_ thinking about the meaning of life.</p>
<p> </p>
<p><strong>Social-emotional development</strong></p>
<p>_ intense self - environment, changing between high expectation and poor self - concept.</p>
<p>_ continued adjustment to changing body, worries about being normal</p>
<p>_ tendency to distance selves from parents , continued the drive for independence.</p>
<p>_ drive to make friends and greater reliance on them, popularity can be an important issue.</p>
<p>_ feelings of love and passion.</p>
<p> </p>
<ol start="3">
<li><strong> Late adolescence ( 19-21 years of age )</strong></li>
</ol>
<p><strong>Physical development</strong></p>
<p>_ young women typically, are fully developed.</p>
<p>_ young men continue to gain height , weight , muscle mass and body hair.</p>
<p> </p>
<p><strong>Cognitive development</strong></p>
<p>_ ability to think ideas</p>
<p>_ ability to delay gratification</p>
<p>_ examination of inner experience</p>
<p>_ increased concern for future</p>
<p>_ continued interest in moral reasoning.</p>
<p> </p>
<p><strong>Socio-emotional development</strong></p>
<p>_ firmer sense of identity</p>
<p>_ increased emotional stability</p>
<p>_ increased concern for other</p>
<p>_ increased independence and self-reliance</p>
<p>_ peer relationship remains important</p>
<p>_ development of a more serious relationship.</p>
<p> </p>
Q2:
What are the developmental activities of an adolescent ?
Type: Short Difficulty: Easy
<p>_ Accepting one's physique.</p>
<p>_ Accepting a masculine or feminine role.</p>
<p>_ Gaining emotional independence from parents and other adults.</p>
<p>_ Establishing new relation with age mates of both sexes.</p>
<p>_ Developing intellectual skills.</p>
<p>_ Preparing for marriage and family</p>
Q3:
Define adolescent.
Type: Very_short Difficulty: Easy
<p>Adolescence describes the teenage years between 13-19 and can be considered the transitional stage from childhood to adulthood.</p>
Videos
Adolescence: Crash Course Psychology

The stack
Stack
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 (Langsam, Augenstein, & Tenenbaum, 2013). A stack is a data structure that permits adding and removing elements in a specific order. Each time an element is added, it goes on the top of the stack; the only element that can be removed is the element at the top of the stack. It follows the Last-In-First-Out (LIFO) principle. The first element added to a stack will be the last element removed from a stack.
Primitive Operations
The primitive operations on stack are as follows:
- PUSH operation
- POP operation
- Empty operation
- Full operation
- Stacktop operation
Push Operation
It is used to add or insert a new element in a stack. When an element is added to a stack, it is said that the element is pushed onto the stack and the operation is called a push operation. The new elements are always pushed to the top of the stack. The syntax for PUSH operation is
PUSH(stack, element)
POP Operation
It is used to remove an item from the stack. When we remove an item from the stack, it is said that an item is popped from the stack and this operation is called the pop operation. The element is popped from the top of the stack. The syntax for POP operation is
POP(stack)
Empty Operation
It determines whether the stack is empty or not. Its syntax is
empty(stack)
If empty(stack) returns the value TRUE, then the stack is empty, else it is not empty.
Full Operation
It determines whether the stack is full or not. Its syntax is
full(stack)
If full(stack) returns the value TRUE, then the stack is full, else it is not full.
Stacktop Operation
This operation allows the user to know what the top item of the stack is without having to pop it. Its syntax is
stacktop(stack)
This operation can be decomposed into PUSH and POP operation as
i=POP(stack);
PUSH(stack, i);

Stack as an ADT
Consider a stack ‘‘s’’ of data type T with finite number of elements, then we can perform the following operations:
- CreateEmptyStack(s): creates an empty stack.
- PUSH(s, x): insert x at the top of s.
- POP(s): removes the top element of s.
- IsFull(s): if 1 then s is full, else not full.
- IsEmpty(s): if 1 then empty, else not empty.
Since all these operations can be performed in a stack, we can consider stack as an ADT.
Implementing the POP Operation
When performing the POP operation, there may be the chance of stack underflow i.e. the stack is empty. In this case, the user must be informed about the underflow condition. The POP operation performs the following actions:
- If the stack is empty, print a warning message and halt execution.
- Remove the top element from the stack.
- Return this element into the calling program.
Algorithm for POP operation
- Check whether the stack is empty. If (TOP=-1), stack is empty and operation is not possible else
Data= stack[top];
Set top=top=1;
- Display the popped item.
- Exit.
int pop (struct *ps)
{
if (empty(ps))
{
printf(“Stack underflow”);
exit(1)
}/*end if*/
return (ps->items[ps->top--];
}/*end pop*/
Testing for Exceptional Conditions
With the detection of underflow, it may not be necessary to halt the execution immediately, Instead, the pop routine could signal the occurrence of an underflow and take the corrective action. Let ’popandrest’ be a procedure that pops the stack and returns a signal whether the underflow has occurred or not.
Void popandtest (struct stack *ps, int *px, int*pund)
{
If (empty(ps))
{
*pund = TRUE;
return;
} /*end if*/
*pund = FALSE;
*px = ps->items[ps->top--];
return;
}/*end popandtest */
In the calling program, a programmer would write
popandtest (&s, &x, &und)
if (und)
/*take corrective action*/
Else
/*use value of x*/
Implementing the PUSH Operation
A first attempt at a PUSH procedure will be
void push (struct stact *ps, int x)
{
ps->items[++(ps->top)] = x;
return;
}/*end push*/
Suppose the stack is full and we attempt to push an item onto the stack. This results in a condition called the overflow. When an overflow condition occurs, we cannot add any more items and an error message needs to be produced to inform the user.
The push procedure therefore will be revised as follows:
void push (struct stack *ps, int x)
{
if (ps->top==STACKSIZE-1)
{
printf (“Stack overflow”);
exit(1);
}
else
ps->items[++(ps->top)] = x;
return;
}/*end push*/
When an overflow is detected in push, an error message is printed and the execution halts immediately. In some cases, it might make more sense for the calling routine to invoke the push operation with the instructions.
pushandtest (&s, x, &overflow);
if (overflow)
/*overflow has been detected, x was not pushed to stack, take remedial action */
else
/* x was successfully pushed onto the stack, continue processing */
References
Langsam, Y., Augenstein, M. j., & Tenenbaum, A. M. (2013). Data Structures Using C and C++. Delhi: PHI Learning Private Limited.
Lesson
The Stack
Subject
Data Structure and Algorithm
Grade
IT
Recent Notes
No recent notes.
Related Notes
No related notes.