Unit 4pps2
Unit 4pps2
Unit 4pps2
3. Stack:
Stack is a linear data structure which follows a particular order in which the operations are performed. The order may be LIFO(Last In
First Out) or FILO(First In Last Out). In stack, all insertion and deletion are permitted at only one end of the list.
Stack Operations:
push(): When this operation is performed, an element is inserted into the stack.
pop(): When this operation is performed, an element is removed from the top of the stack and is returned.
top(): This operation will return the last inserted element that is at the top without removing it.
size(): This operation will return the size of the stack i.e. the total number of elements present in the stack.
isEmpty(): This operation indicates whether the stack is empty or not.
Characteristics of a Stack:
Applications of Stack:
A stack is a linear data structure that implements the Last-In-First-Out (LIFO) principle. Here are some common operations performed
on stacks:
Push: Elements can be pushed onto the top of the stack, adding a new element to the top of the stack.
Pop: The top element can be removed from the stack by performing a pop operation, effectively removing the last element that was
pushed onto the stack.
Peek: The top element can be inspected without removing it from the stack using a peek operation.
IsEmpty: A check can be made to determine if the stack is empty.
Size: The number of elements in the stack can be determined using a size operation.
These are some of the most common operations performed on stacks. The specific operations and algorithms used may vary based on
the requirements of the problem and the programming language used. Stacks are commonly used in applications such as evaluating
expressions, implementing function call stacks in computer programs, and many others.
Real life example of a stack is the layer of eating plates arranged one above the other. When you remove a plate from the pile, you
can take the plate to the top of the pile. But this is exactly the plate that was added most recently to the pile. If you want the plate at
the bottom of the pile, you must remove all the plates on top of it to reach it.
Browsers use stack data structures to keep track of previously visited sites.
Call log in mobile also uses stack data structure.
4. Queue:
Like Stack, Queue is a linear structure which follows a particular order in which the operations are performed. The order is First In
First Out (FIFO). In the queue, items are inserted at one end and deleted from the other end. A good example of the queue is any queue
of consumers for a resource where the consumer that came first is served first. The difference between stacks and queues is in
removing. In a stack we remove the item the most recently added; in a queue, we remove the item the least recently added.
Queue is a linear data structure that follows a particular order in which the operations are performed. The order is First In First Out(FIFO) i.e.
the data item stored first will be accessed first. In this, entering and retrieving data is not done from only one end. An example of a queue is
any queue of consumers for a resource where the consumer that came first is served first. Different operations are performed on a Queue like
Reversing a Queue (with or without using recursion), Reversing the first K elements of a Queue, etc. A few basic operations performed In
Queue are enqueue, dequeue, front, rear, etc.
Queue Operations:
Enqueue(): Adds (or stores) an element to the end of the queue..
Dequeue(): Removal of elements from the queue.
Peek() or front(): Acquires the data element available at the front node of the queue without deleting it.
rear(): This operation returns the element at the rear end without removing it.
isFull(): Validates if the queue is full.
isNull(): Checks if the queue is empty.
Characteristics of a Queue:
The queue has various different characteristics which are as follows:
The queue is a FIFO (First In First Out) structure.
To remove the last element of the Queue, all the elements inserted before the new element in the queue must be removed.
A queue is an ordered list of elements of similar data types.
Applications of Queue:
A queue is a linear data structure that implements the First-In-First-Out (FIFO) principle. Here are some common operations performed on
queues:
Enqueue: Elements can be added to the back of the queue, adding a new element to the end of the queue.
Dequeue: The front element can be removed from the queue by performing a dequeue operation, effectively removing the first element
that was added to the queue.
Peek: The front element can be inspected without removing it from the queue using a peek operation.
IsEmpty: A check can be made to determine if the queue is empty.
Size: The number of elements in the queue can be determined using a size operation.
These are some of the most common operations performed on queues. The specific operations and algorithms used may vary based on the
requirements of the problem and the programming language used. Queues are commonly used in applications such as scheduling tasks,
managing communication between processes, and many others.
A real-world example of a queue is a single-lane one-way road, where the vehicle that enters first will exit first.
A more real-world example can be seen in the queue at the ticket windows.
A cashier line in a store is also an example of a queue.
People on an escalator