UNIT – 2 LINEAR DATA STRUCTURE PREPARED BY Ms. SUBI. S Assistant Professor, RMKCET THIRUVALLUR, INDIA LINEAR DATA STRUCTURES – STACKS, QUEUES Stack ADT – Stack Model - Implementations: Array and Linked list - Applications - Balancing symbols - Evaluating arithmetic expressions - Conversion of Infix to postfix expression Queue ADT – Queue Model - Implementations: Array and Linked list - applications of queues - Priority Queues – Binary Heap – Applications of Priority Queues. List of Exercise/Experiments: 1. Applications of Stack–Infix to postfix conversion and expression evaluation. 2. Implementation of Heaps using Priority Queues. STACK A stack is a linear list in which all additions and deletions are restricted to one end, called the top. Stacks are known as the last in–first out (LIFO) data structure. STACK ADT LAB EXERCISE IMPLEMENT THE FUNCTIONS isEmpty() pop() peek() push() When an underflow occurs the program must print “Underflow” and return 0. When Overflow, print “Overflow” and don ot add value to the satck. SIZE OF THE STACK=5. APPLICATIONS Examples of Infix, Prefix & Postfix Expression EVALUATING ARITHMETIC EXPRESSIONS Postfix Ecpresion: 24+46+* Conversion of Infix to Postfix Rules for conversion 1. Print the operand as they arrive. 2. If the stack is empty or contains a left parenthesis on top, push the incoming operator on to the stack. 3. If the incoming symbol is '(', push it on to the stack. 4. If the incoming symbol is ')', pop the stack and print the operators until the left parenthesis is found. 5. If the incoming symbol has higher precedence than the top of the stack, push it on the stack. Rules for conversion Infix to Postfix (contd.) 6. If the incoming symbol has lower precedence than the top of the stack, pop and print the top of the stack. Then test the incoming operator against the new top of the stack. 7. If the incoming operator has the same precedence with the top of the stack then use the associativity rules. If the associativity is from left to right then pop and print the top of the stack then push the incoming operator. If the associativity is from right to left then push the incoming operator. 8. At the end of the expression, pop and print all the operators of the stack. LINEAR DS - QUEUE QUEUE DATA STRUCTURE A Queue is defined as a linear data structure that is open at both ends and the operations are performed in First In First Out (FIFO) order. QUEUE MODEL Queue ADT 1. 2. 3. 4. 5. The most fundamental operations in the queue ADT include: enqueue() : to insert elements into the stack. dequeue() : to remove elements from the stack. peek() : to retrieve the frontmost element in the queue, without deleting it. isFull() : verifies whether the stack is full. isEmpty() : verifies whether the stack is empty. This operation is used to check the status of the stack with the help of top pointer. Algorithm for Enqueue 1 − START 2 – Check if the queue is full. 3 − If the queue is full, produce overflow error and exit. 4 − If the queue is not full, increment rear pointer to point the next empty space. 5 − Add data element to the queue location, where the rear is pointing. 6 − return success. 7 – END Algorithm for DEqueue 1 – START 2 − Check if the queue is empty. 3 − If the queue is empty, produce underflow error and exit. 4 − If the queue is not empty, access the data where front is pointing. 5 − Increment front pointer to point to the next available data element. 6 − Return success. 7 – END Array Implementation of Queue CIRCULAR QUEUE https://www.tutorialspoint.com/cplusplus-programto-implement-queue-using-array https://www.tutorialspoint.com/cplusplus-programto-implement-queue-using-linked-list APPLICATIONS OF QUEUE Task Scheduling: Queues can be used to schedule tasks based on priority or the order in which they were received. Resource Allocation: Queues can be used to manage and allocate resources, such as printers or CPU processing time. Batch Processing: Queues can be used to handle batch processing jobs, such as data analysis or image rendering. Message Buffering: Queues can be used to buffer messages in communication systems, such as message queues in messaging systems or buffers in computer networks. Event Handling: Queues can be used to handle events in event-driven systems, such as GUI applications or simulation systems. Traffic Management: Queues can be used to manage traffic flow in transportation systems, such as airport control systems or road networks. Applications (contd.) Operating systems: Operating systems often use queues to manage processes and resources. For example, a process scheduler might use a queue to manage the order in which processes are executed. Network protocols: Network protocols like TCP and UDP use queues to manage packets that are transmitted over the network. Queues can help to ensure that packets are delivered in the correct order and at the appropriate rate. Printer queues :In printing systems, queues are used to manage the order in which print jobs are processed. Jobs are added to the queue as they are submitted, and the printer processes them in the order they were received. Web servers: Web servers use queues to manage incoming requests from clients. Requests are added to the queue as they are received, and they are processed by the server in the order they were received. Breadth-first search algorithm: The breadth-first search algorithm uses a queue to explore nodes in a graph level-by-level. The algorithm starts at a given node, adds its neighbors to the queue, and then processes each neighbor in turn. Priority Queue A priority queue is a special type of queue in which each element is associated with a priority value. And, elements are served on the basis of their priority. That is, higher-priority elements are served first. Implementation of Priority Queue • Priority queue can be implemented using an array, a linked list, a heap data structure, or a binary search tree. • Among these data structures, heap data structure efficiently implements priority queues. Heap is a special tree-based data structure. A binary tree is said to follow a heap data structure if •it is a complete binary tree. •All nodes in the tree follow the property that they are greater than their children i.e. the largest element is at the root and both its children and smaller than the root and so on. Such a heap is called a max-heap. If instead, all nodes are smaller than their children, it is called a min-heap. BINARY HEAP A Binary Heap is a Complete Binary tree that is used to store data efficiently to get the max or min element based on its structure. A complete binary tree is a special type of binary tree where all the levels of the tree are filled completely except the lowest level nodes which are filled from as left as possible Heap Operations • • • • • Heapify Insert element to heap Delete element from heap Peek (find max/min) Extract-Max/Min https://www.tutorialspoint.co m/array-representation-ofbinary-heap https://www.tutorialspoint.co m/cplusplus-program-toimplement-binary-heap https://www.programiz.com/dsa/h eap-data-structure https://www.javatpoint.com/heapdata-structure Applications of Priority Queue •Heap is used while implementing a priority queue. •Medical Systems •Operating System •Artificial Intelligence •Optimization problems- Huffman coding, Kruskal’s Algorithm and Prim’s AlgorithmDijkstra's Algorithm •Robotics •Heap Sort •https://www.programiz.com/dsa/heap-sort#heap THANK YOU