




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 8
This page cannot be seen from the preview
Don't miss anything!





/** Program to demonstrate that different data types can be pushed onto the stack using
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 ; }
#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;
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 ; }
#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 ; }