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

Unit-2_1

The document provides an overview of stacks as a data structure, detailing its operations (push and pop) and implementations using both static (arrays) and dynamic (linked lists) methods. It includes code examples for stack operations in C, along with applications of stacks in expression evaluation and conversion. Additionally, it outlines the steps for converting infix expressions to postfix and evaluating postfix expressions.

Uploaded by

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

Unit-2_1

The document provides an overview of stacks as a data structure, detailing its operations (push and pop) and implementations using both static (arrays) and dynamic (linked lists) methods. It includes code examples for stack operations in C, along with applications of stacks in expression evaluation and conversion. Additionally, it outlines the steps for converting infix expressions to postfix and evaluating postfix expressions.

Uploaded by

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

DS Notes Class: B.Tech.

CSE 4th Sem

STACK
Stack is a special type of data structure where elements are inserted from one end and elements are
deleted from the same end. The position from where elements a are inserted and from where
elements are deleted is termed as top of the stack.

Thus stack is a homogeneous collection of elements of any one type, arranged linearly with access
at one end only. Stack is also called Last In First Out (LIFO) data structure.

OPERATION ON STACK:
• Push
• Pop

1. Push Operation: The procedure of inserting a new element to the top of the stack is known
as push operation. A pointer TOP is used to point the top element in the stack, which gets
incremented by 1 after every insertion.

If the stack is full and does not contain enough space to accept the given element or data, the
stack is then considered to be in an overflow state.

2. Pop Operation: The procedure of removing element from the top of the stack is called pop
operation. Only one element can be deleted at a time and element has to be deleted only
from the top of the stack. After every deletion, TOP gets decremented by 1.

When elements are being deleted, there is a possibility of stack being empty. When stack is
empty, it is not possible to delete any element. Trying to delete an element from an empty
stack results in stack underflow.

Provided By: Shipra Swati, PSCET, Bihar


DS Notes Class: B.Tech. CSE 4th Sem

Implementation of stack:

1. Static Implementation (Using Arrays) : Though array implementation is a simple


technique, it provides less flexibility and is not very efficient with respect to memory
organization. This is because once a size of an array is declared, its size cannot be modified
during program execution. If the number of elements to be stored in a stack is less than the
allocated memory, then memory is wasted and if we want to store more elements than
declared, array cannot be expanded. It is suitable only when we exactly know the number of
elements to be stored.

2. Dynamic Implementation (Using Linked List) : The limitations of static implementation


can be removed using dynamic implementation. The memory is efficiently utilized with
pointers. Memory is allocated only after element is inserted to the stack. The stack can grow
or shrink as the program demands it to. However, if a small and/or fixed amount of data is
being dealt with, it is often simpler to implement the stack as an array.

Implementation of stack using array:

Push Operation:

Description: Here STACK is an array with MAX locations. TOP points to the top most
element and ITEM is the value to be inserted.

1. If (TOP == MAX) Then //[Check for overflow]


2. Print: Overflow
3. Else
4. Set TOP = TOP + 1 //[Increment TOP by 1]
5. Set STACK[TOP] = ITEM //[Assign ITEM to top of STACK]
6. Print: ITEM inserted //[End of If]
7. Exit

Pop Operation:

Description: Here STACK is an array with MAX locations. TOP points to the top most
element.

1. If (TOP == 0) Then //[Check for underflow]


2. Print: Underflow
3. Else
4. Set ITEM = STACK[TOP] //[Assign top of STACK to ITEM]
5. Set TOP = TOP - 1 //[Decrement TOP by 1]
6. Print: ITEM deleted //[End of If]
7. Exit

Provided By: Shipra Swati, PSCET, Bihar


DS Notes Class: B.Tech. CSE 4th Sem

Implementation of array using Linked List:

To implement stack using linked list, we need to set the following things before implementing
actual operations.
• Define a 'Node' structure with two members data and next.
• Define a Node pointer 'top' and set it to NULL.

Push Operation:

Step 1: Create a 'newNode' with given value.


Step 2: If (top == NULL)
Step 3: Print 'stack is Empty'
Step 4: set newNode → next = NULL.
Step 5: End of If
Step 6: Else
Step 7: set newNode → next = top.
Step 8: set top = newNode.
Step 9: End of Else

Pop Operation:

Step 1: If (top == NULL)


Step 2: Print 'stack is Empty, Deletion is not possible'
Step 3: End of If
Step 4: Else
Step 5: Define a Node pointer 'temp' and set it to 'top'
temp = top
Step 6: top = top → next
Step 7: Delete temp using free (temp)
Step 8: End of Else

Provided By: Shipra Swati, PSCET, Bihar


DS Notes Class: B.Tech. CSE 4th Sem

Display Operation:

Step 1: If (top == NULL)


Step 2: Print 'stack is Empty'
Step 3: End of If
Step 4: Else
Step 5: Define a Node pointer 'temp' and set it to 'top'
temp = top
Step 6: while(temp → next != NULL)
Step 7: Display temp → data
Step 8: temp = temp → next
Step 9: End of while
Step 10: End of Else

PROGRAM OF LINKED STACK:


#include<stdio.h>

// Node Structure
struct Node
{
int data;
struct Node *next;
}*top = NULL; // Initially top is NULL

//Function declaration
void push(int);
void pop();
void display();

void main()
{
int choice, value;

printf("\n:: Stack using Linked List ::\n");


while(1){
printf("\n****** MENU ******\n");
printf("1. Push\n2. Pop\n3. Display\n4. Exit\n");

Provided By: Shipra Swati, PSCET, Bihar


DS Notes Class: B.Tech. CSE 4th Sem

printf("Enter your choice: ");


scanf("%d",&choice);
switch(choice){
case 1: printf("Enter the value to be insert: ");
scanf("%d", &value);
push(value);
break;
case 2: pop(); break;
case 3: display(); break;
case 4: exit(0);
default: printf("\nWrong selection!!! Please try
again!!!\n");
}
}
}

/* Push operation */
void push(int value)
{
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode­>data = value;
if(top == NULL)
newNode­>next = NULL;
else
newNode­>next = top;
top = newNode;
printf("\nInsertion is Success!!!\n");
display();
} // End of Push

/* Pop operation */
void pop()
{
if(top == NULL)
printf("\nStack is Empty!!!\n");
else{
struct Node *temp = top;
printf("\nDeleted element: %d\n", temp­>data);
top = temp­>next;
free(temp);
}
display();
} // End of Pop

Provided By: Shipra Swati, PSCET, Bihar


DS Notes Class: B.Tech. CSE 4th Sem

/* Display */
void display()
{
if(top == NULL)
printf("\nStack is Empty!!!\n");
else{
struct Node *temp = top;
while(temp­>next != NULL){
printf("%d­­­>",temp­>data);
temp = temp ­> next;
}
printf("%d­­­>NULL \n",temp­>data);
}
} // End of display

Output

:: Stack using Linked List ::

****** MENU ******


1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 1
Enter the value to be insert: 10

Insertion is Success!!!
10­­­>NULL

****** MENU ******


1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 1
Enter the value to be insert: 30

Insertion is Success!!!
30­­­>10­­­>NULL

Provided By: Shipra Swati, PSCET, Bihar


DS Notes Class: B.Tech. CSE 4th Sem

****** MENU ******


1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 1
Enter the value to be insert: 60

Insertion is Success!!!
60­­­>30­­­>10­­­>NULL

****** MENU ******


1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 1
Enter the value to be insert: 20

Insertion is Success!!!
20­­­>60­­­>30­­­>10­­­>NULL

****** MENU ******


1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 1
Enter the value to be insert: 50

Insertion is Success!!!
50­­­>20­­­>60­­­>30­­­>10­­­>NULL

****** MENU ******


1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 2

Deleted element: 50
20­­­>60­­­>30­­­>10­­­>NULL

****** MENU ******


1. Push
2. Pop

Provided By: Shipra Swati, PSCET, Bihar


DS Notes Class: B.Tech. CSE 4th Sem

3. Display
4. Exit
Enter your choice: 2

Deleted element: 20
60­­­>30­­­>10­­­>NULL

****** MENU ******


1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 2

Deleted element: 60
30­­­>10­­­>NULL

****** MENU ******


1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 2

Deleted element: 30
10­­­>NULL

****** MENU ******


1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 2

Deleted element: 10

Stack is Empty!!!

****** MENU ******


1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 2

Stack is Empty!!!

Provided By: Shipra Swati, PSCET, Bihar


DS Notes Class: B.Tech. CSE 4th Sem

Stack is Empty!!!

****** MENU ******


1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 4

APPLICATION OF STACK:

1. Stack is used by compilers to check for balancing of parentheses, brackets and braces.
2. Stack is used to evaluate a postfix expression.
3. Stack is used to convert an infix expression into postfix/prefix form.
4. In recursion, all intermediate arguments and return values are stored on the processor’s
stack.
5. During a function call the return address and arguments are pushed onto a stack and on
return they are popped off.

Converting expressions using Stack:

It can be done as follows:


• Infix to postfix
• Infix to prefix
• Postfix to infix
• Postfix to prefix
• Prefix to infix
• Prefix to postfix

Infix to postfix:

1. Print operands as they arrive.


2. If the incoming symbol is a left parenthesis, push it on the stack.
3. If the stack is empty or contains a left parenthesis on top, push the incoming operator
onto the stack.
4. If the incoming symbol is a right parenthesis, pop the stack and print the operators
until you see a left parenthesis. Discard the pair of parentheses.

Provided By: Shipra Swati, PSCET, Bihar


DS Notes Class: B.Tech. CSE 4th Sem

5. If the incoming symbol has higher precedence than the top of the stack, push it on
the stack.
6. If the incoming symbol has lower precedence than the symbol on the top of the
stack, pop the stack and print the top operator. Then test the incoming operator
against the new top of stack.
7. At the end of the expression, pop and print all operators on the stack. (No
parentheses should remain.)

Infix to postfix:

1. Scan the postfix expression from left to right.


2. If the scanned symbol is an operand, then push it onto the stack.
3. If the scanned symbol is an operator, pop two symbols from the stack and create it as a
string by placing the operator in between the operands and push it onto the stack.
4. Repeat steps 2 and 3 till the end of the expression.

Evaluation of Postfix Expression:

Step 1 − scan the expression from left to right


Step 2 − if it is an operand push it to stack
Step 3 − if it is an operator pull operand from stack and perform operation
Step 4 − store the output of step 3, back to stack
Step 5 − scan the expression until all operands are consumed
Step 6 − pop the stack and perform operation

Provided By: Shipra Swati, PSCET, Bihar

You might also like