0% found this document useful (0 votes)
68 views49 pages

DSA Fall2021-Stack

Here are the steps to evaluate the postfix expression 6 5 2 3 + 8 * + 3 + *: 1. Push the operands 6, 5, 2, 3 onto the stack 2. Pop 3 and 2, add them and push the result 5 onto the stack 3. Pop 5 and 8, multiply them and push the result 40 onto the stack 4. Pop 40 and 3, add them and push the result 43 onto the stack 5. Pop 43 and 6, multiply them and push the result 258 onto the stack The final result is 258.

Uploaded by

Shayan Ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views49 pages

DSA Fall2021-Stack

Here are the steps to evaluate the postfix expression 6 5 2 3 + 8 * + 3 + *: 1. Push the operands 6, 5, 2, 3 onto the stack 2. Pop 3 and 2, add them and push the result 5 onto the stack 3. Pop 5 and 8, multiply them and push the result 40 onto the stack 4. Pop 40 and 3, add them and push the result 43 onto the stack 5. Pop 43 and 6, multiply them and push the result 258 onto the stack The final result is 258.

Uploaded by

Shayan Ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 49

Data Structures &

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 /

void list::insertEnd(int a){


Node *temp=new Node();
temp->data=a; 18 /
if(first==NULL)
first=last=temp;
else{
last->next=temp;
last=temp;
}
}
4
Display List
First Last

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

prev element next


Doubly Linked List

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….

UCP FAISALABAD CAMPUS 12


Stack

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

Data can be added or removed from only the top

Stack is LIFO(Last In, First Out)

UCP FAISALABAD CAMPUS 14


Stack Operations
Push: Pushes an item onto the stack
Pop: Removes an item from the stack
Top: Returns the value of the item at the top of the
stack

IsEmpty: Returns true if the stack is empty and false


if it is not

IsFull: Returns true if the stack is full and false if it is


not
15
Stack Operations

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();

cout << "\nPoping..\n";


stk.Pop();
stk.Display();
system("pause");
} 28
Stack Applications
Infix to postfix conversion Infix to prefix conversion
Evaluation of prefix and postfix expressions
Decimal to Binary Conversion
Checking Balanced Symbols

UCP FAISALABAD CAMPUS 29


Basic Operators
Five binary operators are:
◦ Addition
◦ Subtraction
◦ Multiplication
◦ Division
◦ Exponentiation

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/+*23+

47
Evaluating Postfix Using Stack
Evaluate 6 2 3 + - 3 8 2 / + * 2  3 +

Input Op1Op2Value Stack


66
2 6,2
3 6,2,3
+ 2 3 5 6,5
- 6511
3 6 5 1 1,3
8 6 5 1 1,3,8
2 6 5 1 1,3,8,2
/ 8 2 4 1,3,4
+ 3 4 7 1,7
*1777
2 1 7 7 7,2
 7 2 49 49
3 7 2 49 49,3
48
+ 49 3 52 52

You might also like