0% found this document useful (0 votes)
28 views

Stack

Uploaded by

Muqeet Jinabade
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Stack

Uploaded by

Muqeet Jinabade
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

• Stack is a data structure in which all the insertion and

deletion are performed at one end which is known as


TOP of the stack.
• The insertion operation is known as PUSH
• The deletion operation is known as POP.
• Stack follows LIFO (Last in first out) mechanism.
• Example : Bunch of plates and Stack of coin.
A stack is an Abstract Data Type (ADT), commonly used in
most programming languages. It is named stack as it behaves
like a real-world stack,
for example – a deck of cards or a pile of plates, etc.
• 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.
• The following diagram depicts a stack and its operations −

• A stack can be implemented by means of Array, Structure,
Pointer, and Linked List.
• Stack can either be a fixed size one or it may have a sense
of dynamic resizing.
• Here, we are going to implement stack using arrays, which
makes it a fixed size stack implementation.
Syntax of stack
typedef struct stack
{
int data[SIZE];
int top;
} stack;
/*size is a constant , maximum number of elements that can be
stored */
• Stack operations may involve initializing the stack, using it and then de-
initializing it.
1.Initialize() –Make a stack empty
2.push() − Pushing (storing) an element on the stack.
3.pop() − Removing (accessing) an element from the stack.
4.Full() − check if stack is full.
5.Empty() − check if stack is empty.
• At all times, we maintain a pointer (top ) to the last PUSHed data on the
stack. As this pointer always represents the top of the stack, hence
named top. The top pointer provides top value of the stack without
actually removing it.
1. Void initialize (stack *s)
{

s top = -1;
}
2. int empty (stack *s)
{
if (s top = =-1);
return(1); /*stack is empty */
return (0); /*stack empty condition is false*/
}
3. int full (stack *s)
{
if (s top = = Max-1);
return(1); /*stack is full */
return (0); /*stack full condition is false*/
}

Max – maximum number of elements stored in stack


Max=6 Size = 6
Array for stack
First index position is 0 and last index position is Max-1
3. void push (stack *s , int x)
{
s top == s top+1 ;
s data [s top]=x ;
}
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.
4. void pop (stack *s)
{ int x;
x=s data [s top] ;
s top= s top-1;
return(x);
}
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.

You might also like