221066_ADS Assignment-1
221066_ADS Assignment-1
PRN: 22311902
Batch: SY-A3
PROBLEM STATEMENT:
Implement Stack and its operations using Linked List.
OBJECTIVES:
1. To understand the concept of the stack data structure and its properties.
2. To implement the stack using an array in Java.
3. To develop methods for basic stack operations such as push, pop, and
peek.
4. To implement error handling for underflow (popping from an empty
stack) and overflow (pushing into a full stack) conditions.
5. To create a menu-driven program that allows users to interact with the
stack, performing operations dynamically.
6. To learn how to check if the stack is empty or full using appropriate
conditions.
7. To gain practical experience in managing data using the Last-In-First-
Out (LIFO) principle.
SYSTEM REQUIREMENTS:
Minimum Hardware Requirements
MEMORY (RAM): 2GB
Hard Disk Capacity: 40GB
Processor Speed: 1.6 GHz or faster processor
Minimum Software Requirements
Operating System: Windows / Linux
Editor: Notepad/sublime/Visula Studio Code/ Atom
Browser: Chrome/ Microsoft Edge / Mozila Firefox / Safari /Opera
THEORY:
A stack is a linear data structure that operates on the principle of Last In, First
Out (LIFO). This means the last element added to the stack is the first one to be
removed. Stacks are an essential tool in programming, commonly used for
managing function calls, recursion, expression evaluation, and other operations
requiring temporary storage.
Key Concepts
1. LIFO Principle:
- In a stack, the most recently added element is removed first, adhering to the
Last In, First Out (LIFO) principle.
- For instance, consider a stack of books: the last book placed on the top will
be the first one removed.
5. Menu-Driven Program:
- A menu-driven program enables user interaction with the stack through
simple options to perform operations like push, pop, peek, and check the stack's
status.
- This interactive interface makes it easier for users to understand and
manipulate the stack's behavior.
CODE:
import java.util.Scanner;
class StackLL {
class node {
int data;
node next = null;
}
void push(int p) {
if (height == capacity) {
System.out.println("The stack is full");
} else {
node temp = new node();
temp.data = p;
temp.next = head;
head = temp;
height++;
}
}
void pop() {
if (height == 0) {
System.out.println("The stack is empty");
} else {
head = head.next;
height--;
}
}
int peek() {
return head.data;
}
void display() {
if (height == 0) {
System.out.println("The stack is empty");
} else {
node temp = head;
while (temp.next != null) {
System.out.println(temp.data);
temp = temp.next;
}
}
}
}
}
break;
case 2: {
st.pop();
}
break;
case 3: {
if (st.height == 0) {
System.out.println("The stack is empty");
} else {
System.out.println("The top most element in
stack is " + st.peek());
}
}
break;
case 4: {
st.display();
}
break;
case 5: {
System.out.println("Exiting.");
}
break;
default: {
System.out.println("Incorrect Option.");
}
break;
}
CONCLUSION:
This program implements a simple stack using an array in Java. It demonstrates
core stack operations such as push, pop, and peek while addressing important
conditions like underflow and overflow. By exploring this implementation,
users gain a deeper understanding of how the LIFO principle governs data flow
and how stacks manage memory efficiently in various applications.
Problem Statement:
Convert infix expression to postfix expression using stack.
Objective:
The goal of this assignment is to understand the application of stacks in
transforming infix expressions into postfix. This involves analyzing operator
precedence, associativity, and efficient use of stack operations. The exercise
demonstrates the importance of postfix expressions in simplifying arithmetic
evaluation and eliminating ambiguities inherent in infix notation.
SYSTEM REQUIREMENTS:
Minimum Hardware Requirements
MEMORY (RAM): 2GB
Hard Disk Capacity: 40GB
Processor Speed: 1.6 GHz or faster processor
Minimum Software Requirements
Operating System: Windows / Linux
Editor: Notepad/sublime/Visula Studio Code/ Atom
Browser: Chrome/ Microsoft Edge / Mozila Firefox / Safari /Opera
Theory:
Mathematical expressions can be represented in three notations: infix, postfix,
and prefix.
Infix Notation: Operators are placed between operands (e.g., a + b). It is
the standard human-readable format but can lead to ambiguity without
parentheses due to precedence and associativity rules.
Postfix Notation (Reverse Polish Notation): Operators are placed after
operands (e.g., a b +). It eliminates the need for parentheses and operator
precedence, making it straightforward for computers to evaluate.
The stack data structure provides an efficient mechanism for converting infix
expressions to postfix. During conversion, the stack temporarily stores operators
and parentheses, ensuring that precedence and associativity are respected.
Key Conversion Principles:
1. Operands are added directly to the result.
2. Operators are pushed to the stack, but only after popping operators from
the stack that have higher or equal precedence.
3. Parentheses explicitly dictate precedence:
o A left parenthesis (() is pushed onto the stack.
o A right parenthesis ()) causes operators to be popped and added to
the result until a left parenthesis is encountered (the left parenthesis
is discarded).
Algorithm:
1. Initialize an empty stack (`st`) and an empty result string (`result`).
2. Input the infix expression to be converted.
3. Process each character in the infix expression sequentially:
- Operand: Append directly to the result string.
- Opening Parenthesis (`(`): Push onto the stack.
- Closing Parenthesis (`)`):
- Pop and append operators from the stack to the result until a left
parenthesis (`(`) is found.
- Discard the left parenthesis.
- Operator*:
- If the stack is empty, push the operator onto the stack.
- Otherwise:
- Pop and append operators from the stack to the result while the
precedence of the top operator on the stack is higher or equal to the current
operator.
- Push the current operator onto the stack.
4. After processing all characters, pop remaining operators from the stack and
append them to the result.
5. Output the final postfix expression.
Code:
package main;
import java.util.*;
public class Main {
public static int precednce(char a) {
switch(a){
case '+':{
return 1;
}
case '-':{
return 1;
}
case '*':{
return 2;
}
case '/':{
return 2;
}
case '^':{
return 3;
}
}
return 0;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Stack<Character> st=new Stack<>();
String infix;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the infix expression: ");
infix=sc.nextLine();
StringBuilder result=new StringBuilder();
for(int i =0;i<infix.length();i++) {
char temp=infix.charAt(i);
if(Character.isLetterOrDigit(temp)) {
result.append(temp);
}else if(temp=='(') {
st.push(temp);
}else if(temp==')') {
while((!st.isEmpty())&&st.peek()!='(')
{
result.append(st.pop());
}
st.pop();
}else {
if(!st.empty()) {
if(precednce(temp)>precednce(st.peek())) {
st.push(temp);
}else {
while((!st.empty())&&(!
(precednce(temp)>precednce(st.peek())))) {
result.append(st.pop());
}
st.push(temp);
}
}else {
st.push(temp);
}
System.out.println("Contents of stack:
");
System.out.println(st.toString());
System.out.println("Postfix expression:
");
System.out.println(result.toString());
}
}
while(!st.empty()) {
result.append(st.pop());
}
System.out.println("Final postfix expression: ");
System.out.println(result.toString());
}
Conclusion:
In this assignment, we successfully implemented the conversion of an infix
expression to a postfix expression using a stack. The algorithm efficiently
handles operators, operands, and parentheses by following the rules of operator
precedence and associativity. By using the stack to temporarily hold operators,
the program ensures that the postfix expression is generated without the need
for parentheses, making it easier to evaluate computationally. This approach
demonstrates the practical application of stack data structures in parsing and
expression evaluation.