Unit 4
Unit 4
⚫ What is a Stack?
⚫ Stack is a linear data structure in which the
insertion and deletion operations are
performed at only one end.
⚫ In a stack, adding and removing of elements
are performed at a single position which is
known as "top". That means, a new element is
added at top of the stack and an element is
removed from the top of the stack.
⚫ In stack, the insertion and deletion operations
are performed based on LIFO (Last In First
Out) principle.
Stack
⚫ A stack data structure can be defined as
follows...
⚫ Stack is a linear data structure in which the
operations are performed based on LIFO
principle.
⚫ Stack can also be defined as
⚫ "A Collection of similar data items in which
both insertion and deletion operations are
performed based on LIFO principle".
LIFO Principle of Stack
⚫ In programming terms, putting an item on
top of the stack is called push and removing
an item is called pop.
Push and Pop Operations in Stack
⚫ In a stack, the insertion operation is
performed using a function called "push" and
deletion operation is performed using a
function called "pop".
⚫ In the figure, PUSH and POP operations are
performed at a top position in the stack. That
means, both the insertion and deletion
operations are performed at one end (i.e., at
Top)
Stack using array
.
Basic operations on stack
⚫ In order to make manipulations in stack, there are certain
operations
⚫ push() to insert an element into the stack
⚫ pop() to remove an element from the stack
⚫ top() Returns the top element of the stack.
⚫ isEmpty() returns true is stack is empty else false
⚫ peek(): Return the top element
Example
.
Stack Using Link List
Drawbacks of stack using array
.
Stack operation using link list
⚫ void push(in x)
⚫ {
⚫ ListNode *newNode;
⚫ newNode=new ListNode;
⚫ newNode->info=x;
⚫ newNode->link=head;
⚫ head=newNode;
⚫ }
Stack operation using Link list
⚫ Pop()
⚫ int pop()
⚫ {
⚫ int x=head->info;
⚫ head=head->link;
⚫ return x;
⚫ }
Advantages of stack using Link list
.
Expression
⚫ An expression is a collection of operators and
operands that represents a specific value.
⚫ In above definition, operator is a symbol
which performs a particular task like
arithmetic operation or logical operation or
conditional operation etc.,
Operands are the values on which the
operators can perform the task.
⚫ Expression Types
⚫ Based on the operator position, expressions
are divided into THREE types. They are as
follows...
⚫ Infix Expression
⚫ Postfix Expression
⚫ Prefix Expression
Infix Expression
.
Example:Convert infix expression into postfix
(A+B)*(C-D)
Example:Convert infix expression into postfix
(A+B/C+(D+E)-F
.
Example
.
Example:Convert infix expression into postfix
A+(B*C-(D/E-F)*G)*H
Evaluation of Postfix Expression
.
Evaluation of Postfix expressions
Evaluate P:5,6,2,+,*,12,4,/,-
Queue data structure
.
Queue using array
.
Deletion in queue
.
Examples:
.
Queue
⚫ A queue is a linear data structure that
follows the First in, First out
principle(FIFO). Queue supports
operations like enqueue and dequeue.
Queue using Link List
Queue using an array - drawback
⚫ If we implement the queue using an array, we
need to specify the array size at the beginning(at
compile time).
⚫ We can't change the size of an array at runtime. So,
the queue will only work for a fixed number of
elements.
⚫ Solution
⚫ We can implement the queue data structure using
the linked list.
⚫ In the linked list, we can change its size at runtime.
Node structure for Queue
.
Enqueue function
.
Enqueue function
.
Dequeue function
⚫ Algorithm
⚫ Step 1: Allocate the space for the new node
PTR
⚫ Step 2: SET PTR -> DATA = VAL
⚫ Step 3: IF FRONT = NULL
SET FRONT = REAR = PTR
SET FRONT -> NEXT = REAR -> NEXT =
NULL
ELSE
SET REAR -> NEXT = PTR
SET REAR = PTR
SET REAR -> NEXT = NULL
[END OF IF]
⚫ Step 4: END
Deletion
⚫ Deletion operation removes the element
that is first inserted among all the queue
elements. Firstly, we need to check either
the list is empty or not
Deletion
⚫ Algorithm
⚫ Step 1: IF FRONT = NULL
Write " Underflow "
Go to Step 5
[END OF IF]
⚫ Step 2: SET PTR = FRONT
⚫ Step 3: SET FRONT = FRONT -> NEXT
⚫ Step 4: FREE PTR
⚫ Step 5: END
Operation on Linked Queue
⚫ Each node of a linked queue consists of two
fields: data and next(storing address of next
node). The data field of each node contains the
assigned value, and the next points to the node
containing the next item in the queue.
⚫ A linked queue consists of two pointers, i.e., the
front pointer and the rear pointer. The front
pointer stores the address of the first element of
the queue, and the rear pointer stores the address
of the last element of the queue.
⚫ Insertion is performed at the rear end, whereas
deletion is performed at the front end of the
queue. If front and rear both points to NULL, it
signifies that the queue is empty.
⚫ The two main operations performed on the
linked queue are:
⚫ Insertion
Insert operation
1. Enqueue Function
⚫ Enqueue function adds an element to the end of the
queue. It takes O(1) time. The last element can be
tracked using the rear pointer.
⚫ First, build a new node with given data.
⚫ Check if the queue is empty or not.
⚫ If a queue is empty then, a new node is assigned to the
front and rear.
⚫ Else make next of rear as new node and rear as a new
node.
2. Dequeue Function
⚫ The dequeue function always removes the first
element of the queue. It takes O(1) time. For dequeue,
the queue must contain at least one element, else
underflow conditions will occur.
⚫ Check if queue is empty or not.
⚫ If the queue is empty, then dequeue is not possible.
⚫ Else store front in temp
Note:
⚫ Queue is a linear data structure that follows
the First in, First Out Principle (FIFO).
⚫ Queue can be represented using nodes of a
linked list.
⚫ Queue supports operations such as
enqueue, dequeue and print().
⚫ Elements can be enqueued from one end
and dequeued from the other one end.
⚫ Enqueue and Dequeue operations
take O(1) time.
Circular queue
A circular queue is a linear data structure in which
the operations are performed based on FIFO (First
In First Out) principle and the last position is
connected back to the first position to make a
circle. It is also called ring buffer
Circular queue
.
Circular queue
Enqueue operation