Lab 05 Stack Implementation with Applications, Exams of Data Structures and Algorithms

A lab on stack implementation with applications. It covers the implementation of stacks using linked lists, stacks of varying data types using linked lists, and solving different problems using stacks. The lab includes an in-lab task on reversing an array of numbers and a post-lab task on infix to postfix conversion. The document also provides code examples and critical analysis of the stack data structure and its applications.

Typology: Exams

2022/2023

Available from 05/17/2023

souban-javed
souban-javed 🇵🇰

9 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Lab 05 Stack Implementation with Applications
Learning Outcomes
After completing the lab students will be able to:
Implement stacks using linked lists.
Implement stacks of varying data types using linked lists
Solve different problems using stacks
In-lab Task 1: Reversing an array of numbers.
You have to write a function to reverse the order of integers in an array. Call this function
from your main function to demonstrate that this functions successfully reverses (in-place)
the contents of the input array. You should declare and use a stack within this function. The
functions for stack implementation are already given to you. The prototype for the function
is given below;
void reverse_num_array(int * num_array);
Code:
main.c file:
/** Program to demonstrate that different data types can be pushed onto the
stack using
* structures. Each element contains an extra field 'd_type' to tell which
data type
* is being pushed onto the stack.
*/
#include <stdio.h>
#include <stdlib.h>
#include "node.h"
#include "stack_functions.h"
#define MAX_SIZE 20
int main()
{
int num_array[MAX_SIZE] = {1, 2, 3, 4, 5,6,7,8,9,10 ,'\0'};
printf("Original array: ");
for (int i = 0; num_array[i] != '\0'; i++) {
printf("%d ", num_array[i]);
}
reverse_num_array(num_array);
printf("\nReversed array: ");
for (int i = 0; num_array[i] != '\0'; i++) {
printf("%d ", num_array[i]);
}
struct node * top = NULL; /// This is the top of the stack
struct element d1, d2, d3;
d1.d = 10;
d1.d_type = 0;
d2.ch = 'A';
d2.d_type = 1;
pf3
pf4
pf5
pf8

Partial preview of the text

Download Lab 05 Stack Implementation with Applications and more Exams Data Structures and Algorithms in PDF only on Docsity!

Lab 05 Stack Implementation with Applications

Learning Outcomes

After completing the lab students will be able to:

• Implement stacks using linked lists.

• Implement stacks of varying data types using linked lists

• Solve different problems using stacks

In-lab Task 1: Reversing an array of numbers.

You have to write a function to reverse the order of integers in an array. Call this function

from your main function to demonstrate that this functions successfully reverses (in-place)

the contents of the input array. You should declare and use a stack within this function. The

functions for stack implementation are already given to you. The prototype for the function

is given below;

void reverse_num_array(int * num_array);

Code:

main.c file:

/** Program to demonstrate that different data types can be pushed onto the stack using

  • structures. Each element contains an extra field 'd_type' to tell which data type
  • is being pushed onto the stack. */ #include <stdio.h> #include <stdlib.h> #include "node.h" #include "stack_functions.h" #define MAX_SIZE 20 int main() { int num_array[MAX_SIZE] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ,'\0'}; printf("Original array: "); for (int i = 0 ; num_array[i] != '\0'; i++) { printf("%d ", num_array[i]); } reverse_num_array(num_array); printf("\nReversed array: "); for (int i = 0 ; num_array[i] != '\0'; i++) { printf("%d ", num_array[i]); } struct node * top = NULL; /// This is the top of the stack struct element d1, d2, d3; d1.d = 10 ; d1.d_type = 0 ; d2.ch = 'A'; d2.d_type = 1 ;

d3.d = 45 ; d3.d_type = 0 ; push(&top, d1); push(&top, d3); push(&top, d2); for(int i = 0 ; i< 3 ; i++) { struct element temp; temp = pop(&top); if(temp.d_type == 0 ) printf("\nThe data popped is %d", temp.d); else printf("\nThe data popped is %c", temp.ch); } return 0 ; }

stackfunction.c file:

#include <stdio.h> #include <stdlib.h> #include "node.h" #include "stack_functions.h" struct element pop(struct node ** top) { struct element temp = (top)->data; /// I copy the data at the top node into a temporary variable struct node * ptr_temp = (top)->next; free(*top); *top = ptr_temp; return(temp); } void push(struct node ** top, struct element new_data) { struct node * new_node = (struct node *) malloc(sizeof(struct node)); new_node->data = new_data; /// I can assign one struct to another if the type is the same new_node->next = * top;

  • top = new_node; } void reverse_num_array(int * num_array) { // Create an empty stack to store the integers struct node * top = NULL; // Push each integer in the array onto the stack for (int i = 0 ; num_array[i] != '\0'; i++) { struct element temp;

st.top = - 1 ; } int isFull() { if(st.top >= MAXSIZE - 1 ) return TRUE; else return FALSE; } int isEmpty() { if(st.top == - 1 ) return TRUE; else return FALSE; } void push(int num) { if (isFull()) printf("Stack is Full...\n"); else { st.array[st.top + 1 ] = num; st.top++; } } int pop() { if (isEmpty()) printf("Stack is Empty...\n"); else { st.top = st.top - 1 ; return st.array[st.top+ 1 ]; } } int main() { char inputString[ 100 ], ch; int i, len; init(); printf("Enter a string of parentheses\n"); gets(inputString); len = strlen(inputString); for(i = 0 ; i < len; i++){ if(inputString[i] == '(') push(inputString[i]); else if(inputString[i] == ')') pop(); else { printf("Error : Invalid Character !! \n"); return 0 ; } } if(isEmpty()) printf("Valid Parentheses Expression\n"); else printf("Invalid Parentheses Expression\n"); return 0 ; }

Output

Post-Lab Task: Infix to postfix conversion

Write a function that converts a mathematical expression containing parentheses from infix

form to postfix form. The function should take pointers to both source infix array and

destination postfix array as arguments. You may assume that the maximum array size is

fixed. The infix expression will contain multi digit positive integers only. The function must

first check if the brackets in the expression are balanced. The function prototype is given

below:

void infixToPostfix(char * src, char * dst);

Code:

#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_SIZE 100 int isOperator(char c) { if (c == '+' || c == '-' || c == '' || c == '/') return 1 ; return 0 ; } int precedence(char c) { if (c == '' || c == '/') return 2 ; if (c == '+' || c == '-') return 1 ; return 0 ; } int isOpeningBracket(char c) { if (c == '(' || c == '{' || c == '[')

dst[j++] = stack[top--]; } top--; // remove the opening bracket from the stack } } while (top != - 1 ) { if (isOpeningBracket(stack[top])) { printf("Error: unbalanced brackets\n"); return; } dst[j++] = stack[top--]; } dst[j] = '\0'; } int main() { char infix[MAX_SIZE], postfix[MAX_SIZE]; printf("Enter infix expression: "); scanf("%s", infix); infixToPostfix(infix, postfix); printf("Postfix expression: %s\n", postfix); return 0 ; }

Output:

Critical Analysis and Conclusion:

In conclusion, the stack data structure is a powerful tool for implementing a wide range of

applications, from simple ones like evaluating expressions in postfix notation, to more complex ones

like implementing a web browser's back button or undo functionality in a text editor.

We have seen how the stack can be implemented using an array or a linked list, each with

their own advantages and disadvantages depending on the application requirements. We have

also seen how to implement stack operations such as push, pop, and peek, as well as checking

if the stack is empty or full.

Some of the main applications of the stack data structure include:

1. Evaluating expressions in postfix notation

2. Undo/Redo functionality in text editors

3. Web browser back button

4. Call stack in programming languages

5. Recursive function calls

In addition, the stack is a fundamental concept in computer science and is used in many other

data structures and algorithms, such as the queue, tree traversal algorithms, and depth-first

search.

Overall, the stack is a versatile and powerful tool that has many practical applications in

computer science and beyond. A solid understanding of the stack data structure and its

implementation is essential for any programmer or computer scientist.