Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
13 views8 pages

Stack

A stack is a linear data structure that operates on a Last In First Out (LIFO) principle, allowing elements to be inserted and deleted from the top. Key operations include push (to insert), pop (to delete), top (to view the last element), size (to check the number of elements), isEmpty, and isFull. The document also provides algorithms and code snippets for implementing push and pop operations, including error handling for stack overflow and underflow conditions.

Uploaded by

badimela1508
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views8 pages

Stack

A stack is a linear data structure that operates on a Last In First Out (LIFO) principle, allowing elements to be inserted and deleted from the top. Key operations include push (to insert), pop (to delete), top (to view the last element), size (to check the number of elements), isEmpty, and isFull. The document also provides algorithms and code snippets for implementing push and pop operations, including error handling for stack overflow and underflow conditions.

Uploaded by

badimela1508
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

STACKS

Stack: Stack is a linear data structure where elements are inserted and
deleted at one end, called the top of the stack.
• Top is incremented while pushing (inserting) an element into the stack
and decremented while popping (deleting) an element from the stack
• Stack follows Last In First Out mechanism (LIFO).
Primary Stack Operations:
• Push( ) ---- inserts the data onto stack.
• Pop( ) ---- deletes the last inserted data element from the stack.
• Top( )----returns the last inserted element without removing it.
• Size( )---returns the size or the no.of the elements in the stack.
• Isempty( )--- returns TRUE if the stack is empty, else returns FALSE.
• Isfull( )---returns TRUE if the stack is full, else returns FALSE.
Push( )
• The procedure of inserting a new element to the top of the stack is
known as push operation.
• After every push operation, the value of “top” is incremented by one.
• However before inserting the value we have to check whether there is
some space in the stack or not.
• If an attempt is made to insert a value in a stack that is already full
then stack overflow error occurs.
• Stack overflow: Attempt to insert an element when the stack is full
i.e., top= STACK_SIZE-1.
Algorithm for push operation:

Step 1: if top== STACK_SIZE-1 then print “Stack is full”


Step 2: top=top+1
Step 3: stack[top]=element
void push ( )
{
int element;
printf(“enter the element to be pushed\n”);
scanf(“%d”,&element);
if (top = = (STACK_SIZE-1))
{
printf("Stack is full");
}
else
{
top=top+1;
stack[top]=element;
Pop( )

• The procedure of removing element from the top of the stack is called
pop operation.
• In pop operation the topmost element from the stack will be deleted.
• After every pop operation, the value of “top” is decremented by one.
• Therefore before deleting an element from the stack we have to check
the underflow condition.
(Stack underflow : Attempt to delete an element when the stack is
empty).
Algorithm for pop operation:
• Step 1 : if top==-1 then print “stack is empty”
• Step 2: print stack[top]
• Step 3: top--.
Code for pop operation(using global variables):

void pop( )
{
if(top = = -1)
printf("stack is empty\n");
else
{
printf("%d is popped from stack\n",stack[top]);
top--;
}
}

You might also like