DSA Fall2021-Stack
DSA Fall2021-Stack
Algorithms
SIR. HABIB UR RAHMAN
Properties of Link List
•Dynamic Memory Allocation
•Easy data Insertion and Deletion
•No Direct Access
Array 12 13 14 15 16
0 1 2 3 4
Link List 12 13 14 15 16 /
2
Implementation of Linked List
12 13 14 15 16 /
class Node{
private:
int data;
Node Data Ptr Node* ptr;
}
3
Inserting Nodes at the end
First Last
12 13 14 15 16 /
12 13 14 15 16 /
void display()
{
node *tmp;
tmp = First;
while (tmp != NULL)
{
cout << tmp->data << endl;
tmp = tmp->next;
}
}
5
Operations on List
•Finding Minimum
•Finding Maximum
•Searching
•Inserting Item at desired location
•Sorting the list
•Swapping two elements in the linked list
•Inserting an element in sorted list
Analysis of Linked List
Add
• we simply insert the new node after the current node. So add is a one-step
operation.
Remove
remove is also a one-step operation
Find
worst-case: may have to search the entire list
Back
moving the current pointer back one node requires traversing the list from
the start until the node whose next pointer points to current node.
Problems in Link List
•Moving forward in a singly-linked list is easy
•Moving backwards is not so easy.
•Wastage of Memory
•No Random Access
Doubly-linked List
•Moving backward in a singly-linked list is not so easy
•To avoid this we can use two pointers in a node: one to point to next
node and another to point to the previous node
Next
P Data N Data
P Data N
Previous
10
Circular Linked List
To link the last node with the first node in the list to create a
circularly-linked list
current
head 2 6 8 7 1 size=5
current
6
8
size=5
head 2
11
Time to Think …..
APPLICATION/USES OF LINK LIST IN APPLICATION….
13
Stack
A stack is a homogeneous collection of items of any
one type, arranged linearly with access at one end
only, called the top
16
Stacks Operations
17
Stack Class
int STACKSIZE = 5;
class Stack
{
private:
int StackArray[STACKSIZE];
int Top;
18
Stack Class
public:
Stack();
bool IsEmpty();
bool IsFull();
int Size();
void Push(int);
void Pop();
void Display();
};
19
Constructor
Stack::Stack()
{
Top = 0;
}
20
IsEmpty Function
bool Stack::IsEmpty()
{
if(Top == 0)
return true;
else
return false;
}
21
IsFull Function
bool Stack::IsFull()
{
if(Top == STACKSIZE)
return true;
else
return false;
}
22
Push Function
void Stack::Push(int element)
{
if(IsFull() == true)
{
cout << "Can't insert Element because the Stack is Full\n";
}
else
{
StackArray[Top++] = element;
}
}
23
Pop Function
void Stack::Pop()
{
if(IsEmpty() == true)
{
cout << "Can't Pop becaue the Stack is Empty\n";
}
else
{
Top--;
}
}
24
Size Function
int Stack::Size()
{
return Top;
}
25
Display Function
void Stack::Display()
{
if(Top == 0)
{
cout << "No element in the Stack";
return;
}
else
{
for(int i = Size()-1; i>=0; i--)
{
cout << StackArray[i] << endl;
}
}
} 26
Display
int main()
Function
{
Stack stk;
stk.Push(100);
stk.Push(200);
stk.Push(300);
stk.Push(400);
stk.Push(500);
stk.Display();
system("pause");
}
27
Display Function
int main()
{
Stack stk;
stk.Push(100);
stk.Push(200);
stk.Push(300);
stk.Push(400);
stk.Push(500);
stk.Display();
30
Evaluating Postfix Using Stack
6 5 2 3 + 8 * + 3 + *
31
Evaluating Postfix Using Stack
6 5 2 3 + 8 * + 3 + *
5
6
32
Evaluating Postfix Using Stack
6 5 2 3 + 8 * + 3 + *
2
5
6
33
Evaluating Postfix Using Stack
6 5 2 3 + 8 * + 3 + *
3
2
5
6
34
Evaluating Postfix Using Stack
6 5 2 3 + 8 * + 3 + *
3
2
op2=3 5
op1=2 6
push(op1+op2);
35
Evaluating Postfix Using Stack
6 5 2 3 + 8 * + 3 + *
5
5
6
36
Evaluating Postfix Using Stack
6 5 2 3 + 8 * + 3 + *
8
5
5
6
37
Evaluating Postfix Using Stack
6 5 2 3 + 8 * + 3 + *
8
5
Op2=8 5
Op1=5 6
push(op1*op2);
38
Evaluating Postfix Using Stack
6 5 2 3 + 8 * + 3 + *
40
5
6
39
Evaluating Postfix Using Stack
6 5 2 3 + 8 * + 3 + *
40
5
Op2=40 6
Op1=5
push(op1+op2);
40
Evaluating Postfix Using Stack
6 5 2 3 + 8 * + 3 + *
45
6
41
Evaluating Postfix Using Stack
6 5 2 3 + 8 * + 3 + *
3
45
6
42
Evaluating Postfix Using Stack
6 5 2 3 + 8 * + 3 + *
3
45
Op2=3 6
Op1=45
push(op1+op2);
43
Evaluating Postfix Using Stack
6 5 2 3 + 8 * + 3 + *
48
6
44
Evaluating Postfix Using Stack
6 5 2 3 + 8 * + 3 + *
48
6
Op2=48
Op1=6
push(op1*op2);
45
Evaluating Postfix Using Stack
6 5 2 3 + 8 * + 3 + *
288
46
Evaluating
Solve it yourself
Postfix Using Stack
623+-382/+*23+
47
Evaluating Postfix Using Stack
Evaluate 6 2 3 + - 3 8 2 / + * 2 3 +