Module-1
Module-1
Structure
&
Stacks and Queues
MODULE-1
Overview of Data Structures
Data structure is a storage that is used to store and organize data.
It is a way of arranging data on a computer so that it can be accessed and
updated efficiently.
Depending on your requirement and project, it is important to choose the
right data structure for your project.
For example, if you want to store data sequentially in the memory, then you
can go for the Array data structure.
However, when the complexity of the program increases, the linear data structures
might not be the best choice because of operational complexities.
Stack
queue
Linked list
• Binary Tree
• Binary Search Tree
• AVL Tree
• B-Tree
• B+ Tree
• Red-Black Tree
The Hash table data structure stores elements in key-value pairs where
Example:
Inserting an element at the beginning of the list is faster in Linked list than
Array.
f(n)=n
5. For n=2,
The equation will be 1(2) ≤ 2(2)+3 ≤ 5(2) ⇒ 2 ≤ 7 ≤ 10 which is
again true.
6. Hence, we can say that the equation holds for all the values of n
given that c1=1 and c2=5.
7. Therefore, we can say that for the above example, f(n) is equal to
θ(g(n)). And it gives average-case complexity for f(n).
Sorting techniques:
int square(int a)
{
How many bytes of memory required to store variable ’a’ ?
return a*a;
}
If any algorithm requires a fixed amount of space for all input values then that
space complexity is said to be Constant Space Complexity.
If the amount of space required by an algorithm is increased with the increase of
input value, then that space complexity is said to be Linear Space Complexity.
• Since deletion is done from the same end, the last element inserted is the first
element to be deleted.
• Apart from initialization, the common operations associated with a stack are push
and pop.
• Items are added and removed from only one designated end called the top of the
stack.
• Two important preconditions associated with the push and pop operations are
overflow and underflow, respectively.
Implementation of Stack
Array index
1
push(1)
(-1+1=0)
Data to be inserted
push(1)
push(1)
Static final int MAX=10;
int arr[]=new int[MAX];
Int top;
Stack()
{
Top=-1;
}
Boolean push(int x)
{
If(top>=Max-1)
{
sop(overflow);
}
Else
{
Arr[++top]=x;
sop(x +” pushed into the stack”);
Return true;}
}
int pop()
{
if(top <0)
sop(“Stack is Underflow”);
Return 0;
else {
Int x=arr[top--];
return x;
}
}
int Display()
int peek() {
{
for(int i:stack)
if(top <0)
sop(“Stack is Underflow”); { sop(i+” “);
Return 0; }
else { }
Int x=arr[top];
return x;
}
}
Pop an element: pop()
5-1=4
# import java.util.*; int pop()
class Stack {
{ if (top < 0)
static final int MAX=1000; {
int top; System.out.println("Stack Underflow");
int a[] = new int[MAX]; return 0;
boolean isEmpty() }
{ return (top < 0); else
} {
Stack() int x = a[top--];
{ return x;
top = -1; }
} }
boolean push(int x) int peek()
{ {
if (top >= (MAX - 1)) if (top < 0)
{ {
System.out.println("Stack Overflow"); System.out.println("Stack Underflow");
return false; return 0;
} }
else else
{ {
a[++top] = x; int x = a[top];
System.out.println(x + " pushed into stack"); return x;
return true; }
} }
}
void print()
{
for(int i = top;i>-1;i--)
{
System.out.print(a[i]+ " ");
}
}
public static void main(String args[])
{
Stack s = new Stack();
s.push(10);
s.push(20);
s.push(30);
System.out.println(s.pop() + " Popped from stack");
System.out.println("Top element is :" + s.peek());
System.out.print("Elements present in stack :");
s.print();
}
}
Applications of Stacks
Stacks can be used for:
• Expression evaluation
• To check parenthesis matching in an expression.
• Expression Conversion
Infix to Postfix
Infix to Prefix
Postfix to Infix
Prefix to Infix
• Memory Management.
Notations for Arithmetic
Expression
Infix Notation
The infix notation is a convenient way of writing an expression in which each operator is
placed between the operands. Infix expressions can be parenthesized or unparenthesized
depending upon the problem requirement.
A + B, (C - D)
Prefix Notation
The prefix notation places the operator before the operands.
+ A B, -CD
Postfix Notation
The postfix notation places the operator after the operands.
AB +, CD+
Precedence of operators
The precedence of operators determines which operator is executed first if
there is more than one operator in an expression.
Let us consider an example:
int x = 5 - 17* 6;
Associativity of Operators
The associativity of operators determines the direction in which an expression
is evaluated. For example,
b = a;
Find the result of following infix expression:
3+5*7 – 4 ^ 2
Ans: 22
500+5^3 – (10+56) * 9
Ans: 31
365*52*7+10/10
3+5*(7-4)^2
8*(5^4+2)-6^2/(9*3)
A+B / C*D – E/ (F+G)
Infix to Postfix A+B / C*D – E/ (F+G)
A+BC\ *D – E/ (FG+)
A+BC\D* – E/ (FG+)
A+BC\D* – EFG+/
ABC\D*+ – EFG+/
ABC\D*+EFG+/-
(A+B)*(C-D)
Ans: AB+CD-*
D=A+B*C
Ans: DABC*+=
• Read all the symbols one by one from left to right in the given Infix Expression.
• If the reading symbol is operand, then directly print it to the result (Output).
• If the reading symbol is left parenthesis '(', then Push it on to the Stack.
• If the reading symbol is right parenthesis ')', then Pop all the contents of stack until
respective left parenthesis is poped and print each poped symbol to the result.
• If the reading symbol is operator (+ , - , * , / etc.,), then Push it on to the Stack.
However, first pop the operators which are already on the stack that have higher or equal
precedence than current operator and print them to the result.
(A+B)*(C-D)
AB+CD-*
let us consider the following infix expression 2 * (4+3) - 5.
rear
front 10
class Queue {
private static final int maxsize = 5;
int[] queue = new int[maxsize];
int front = -1;
int rear = -1;
void main () {
Scanner scanner = new Scanner(System.in);
Queue q = new Queue();
int choice;
do {
Sop("\n1.insert an element\n2.Delete an element\n3.Display the queue\n4.Exit\n");
Sop("\nEnter your choice ?");
choice = scanner.nextInt();
switch (choice) {
case 1: Sop("Enter the element to insert: ");
int data = scanner.nextInt();
q.insert(data);
break;
case 2:
q.delete();
break;
case 3:
q.display();
break;
case 4:
System.exit(0);
break;
default:
System.out.println("Enter a valid choice!");
}
} while (choice != 4);
scanner.close();
}
}
void insert() {
if(rear == maxsize-1) {
Sop("\nOVERFLOW\n");
return;
}
if(front == -1 && rear == -1)
{
front = 0;
rear = 0;
} else {
rear = rear+1;
}
queue[rear] = item;
Sop("\nValue inserted ");
}
void delete() {