1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

CHP-5 LinkedList.
§3 The Stack ADT 1. ADT A stack is a Last-In-First-Out (LIFO) list, that is, an ordered list in which insertions and deletions are.
Introduction to Linked Lists In your previous programming course, you saw how data is organized and processed sequentially using an array. You probably.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
Module R2 Overview. Process queues As processes enter the system and transition from state to state, they are stored queues. There may be many different.
Module R2 CS450. Next Week R1 is due next Friday ▫Bring manuals in a binder - make sure to have a cover page with group number, module, and date. You.
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
Data Structure (Part I) Stacks and Queues. Introduction to Stack An stack is a ordered list in which insertion and deletions are made at one end. –The.
Today’s Agenda  Stacks  Queues  Priority Queues CS2336: Computer Science II.
 Abstract Data Type Abstract Data Type  What is the difference? What is the difference?  Stacks Stacks  Stack operations Stack operations  Parsing.
 Balancing Symbols 3. Applications
Data Structures: A Pseudocode Approach with C 1 Chapter 5 Contd... Objectives Explain the design, use, and operation of a linear list Implement a linear.
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Unit : Overview of Queues.
Queue using an array. .head.tail Pointers head and tail always point to the first empty slot before or after elements in the list. Thus, initially they.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
Lecture 4 Abstract Data Types (ADT). Usual Data Types Integer, Double, Boolean You do not know the implementation of these. There are some operations.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
Hashing General idea: Get a large array
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
Stacks, Queues, and Deques
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
Stacks, Queues, and Deques. A stack is a last in, first out (LIFO) data structure –Items are removed from a stack in the reverse order from the way they.
Stacks, Queues, and Deques
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.
1 CSE 1342 Programming Concepts Lists. 2 Basic Terminology A list is a finite sequence of zero or more elements. –For example, (1,3,5,7) is a list of.
A first look an ADTs Solving a problem involves processing data, and an important part of the solution is the careful organization of the data In order.
Data Structures Types of Data Structure Data Structure Operations Examples Choosing Data Structures Data Structures in Alice.
3 Data. Software And Data Data Data element – a single, meaningful unit of data. Name Social Security Number Data structure – a set of related data elements.
Kovács Zita 2014/2015. II. félév DATA STRUCTURES AND ALGORITHMS 26 February 2015, Linked list.
Subject Name : Data Structure Using C Title : Linked Lists
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
Queues. Like Stacks, Queues are a special type of List for storing collections of entities. Stacks are Lists where insertions (pushes) and deletions (pops)
1. Circular Linked List In a circular linked list, the last node contains a pointer to the first node of the list. In a circular linked list,
Review Sorting algorithms Selection Sort Insertion Sort Bubble Sort Merge Sort Quick Sort.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
Definition: A stack is an ordered collection of elements in which insertions(Push) and deletions(Pop) are restricted to one end. LIFO(Last In First Out)
Linear Data Structures
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
Data Structures AZHAR MAQSOOD NUST Institute of Information Technology (NIIT) Lecture 6: Linked Lists Linked List Basics.
Linked list: a list of items (nodes), in which the order of the nodes is determined by the address, called the link, stored in each node C++ Programming:
CHAPTER 51 LINKED LISTS. Introduction link list is a linear array collection of data elements called nodes, where the linear order is given by means of.
UNIT-II Topics to be covered Singly linked list Circular linked list
LINKED LISTS.
List Structures What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element.
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
 In general, Queue is line of person waiting for their turn at some service counter like ticket window at cinema hall, at bus stand or at railway station.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
STACKS & QUEUES for CLASS XII ( C++).
Review Array Array Elements Accessing array elements
Data Structures Using C, 2e
Queues.
Program based on queue & their operations for an application
Data Structure Interview Question and Answers
Objectives In this lesson, you will learn to: Define stacks
Lectures Queues Chapter 8 of textbook 1. Concepts of queue
Stacks and Queues.
Queues Queues Queues.
Stacks Stack: restricted variant of list
Pointers and Linked Lists
Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
QUEUE Visit for more Learning Resources Free Powerpoint Templates.
Dynamic allocation (continued)
CSCS-200 Data Structure and Algorithms
Presentation transcript:

1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays

2 Learning Objectives Describe algorithms for the insertion and reading of items in stacks and queues stored in linked-lists or arrays. Describe algorithms for the insertion, searching, amendment and deletion of data items stored in a linked list of records.

3 Queues & Stacks Structures used to list data to be used / jobs to be executed (see AS presentation Stacks and Queues Presentation). Stacks and Queues Presentation Stacks and Queues Presentation They can be held in linked lists or arrays.

4 Queues / Stacks in Linked Lists

5 Stacks & Queues Stacks Insert and read at the back end of stack - beginning of the linked list. Insert and read at the back end of stack - beginning of the linked list Insert / Read Queues Insert at back of queue - beginning of linked list. Insert at back of queue - beginning of linked list. However, if the list holds a priority queue then higher priority items (jobs) may enter at other positions. Read from front queue - end of linked list. Read from front queue - end of linked list Insert Read

6 Queue / Stack in a linked List - Insertion Suppose we wish to insert an element / cell / item / node into a queue or stack held in a linked list.

7 Free List A linked list of unallocated (not used) regions of memory.

8 Queue / Stack in a linked List - Insertion 1.Check that the free list is not empty (no free space). If so report an error and stop. Free Null pointer element / cell / item / node

9 Queue / Stack in a linked List - Insertion 2. Call first free cell NEW. FREE Ex Data NEW Pointer

10 Queue / Stack in a linked List - Insertion 3. Insert data into NEW (first free space). FREE Ex Data New data Ex Data NEW

11 Queue / Stack in a linked List - Insertion 4. Remove the node from the stack by setting FREE to pointer in NEW. FREE Ex Data New data Ex Data NEW

12 Queue / Stack in the linked list is empty 5.Check if the list had previously no items of data by seeing if HEAD (of the list in which the NEW node is to be inserted) is NULL and if so: Head of the list to which the new data is being added to.

13 Queue / Stack in the linked list is empty 5.Check if the list had previously no items of data by seeing if HEAD (of the list in which the NEW node is to be inserted) is NULL and if so: a.Set pointer in NEW to NULL b.Set HEAD to NEW and stop. Head FREE Ex Data New data Ex Data NEW a. b.

14 Not empty linked list holding a stack / non- priority queue / priority queue where the NEW priority is the lowest 6.If the linked list is not empty and either holds a stack or a non-priority queue or a priority queue where NEW’s priority is lowest then NEW is placed at the beginning of the list (back) : a.Set pointer in NEW to (previous) HEAD. b.Set HEAD to NEW and stop. HEAD Data FREE Ex Data Data Ex DataData a. b.

15 Not empty linked list holding a stack / non- priority queue / priority queue where the NEW priority is the lowest 7.If the linked list is not empty and holds a priority queue where NEW’s priority is highest then NEW is placed at the end of the list (front) : a.Follow pointers to null pointer. b.Call this cell Previous. c.Set pointer in NEW to NULL. d.Make the pointer in PREVIOUS equal to NEW and stop. HEAD Data FREE Ex Data New Data Ex Data NEW PREVIOUS

16 Not empty linked list holding a priority queue where the NEW priority is higher than some but lower than others 8. Search list sequentially until the cell immediately before the cell with higher priority than NEW is found. Call this cell PREVIOUS. HEAD Data FREE Ex Data New Data Ex Data PREVIOUS NEW e.g.

17 Not empty linked list holding a priority queue where the NEW priority is higher than some but lower than others 9. Copy the pointer in PREVIOUS into TEMP. HEAD Data FREE PREVIOUS = TEMP Ex Data New Data Ex Data NEW

18 Not empty linked list holding a priority queue where the NEW priority is higher than some but lower than others 10. Make the pointer in PREVIOUS equal to NEW HEAD Data FREE Ex Data New Data NEW ex PREVIOUS

19 Not empty linked list holding a priority queue where the NEW priority is higher than some but lower than others 10.Make the pointer in NEW equal to TEMP and stop. HEAD Data FREE Ex DataData Ex Data = TEMP ( = ex pointer in ex PREVIOUS) ex PREVIOUS

20 Algorithm for Queue / Stack in a linked List - Insertion 1.Check that the free list is not empty. If it is empty report an error and stop. 2.Call first free cell NEW. 3.Insert data into NEW (first free space). 4.Remove the node from the stack by setting FREE to pointer in NEW.

21 Algorithm for Queue / Stack in a linked List - Insertion 5.Check if the list had previously no items of data by seeing if HEAD is NULL and if so: a.Set pointer in NEW to NULL b.Set HEAD to NEW and stop. 6.If the linked list is not empty and either holds a stack or a non-priority queue or a priority queue where NEW’s priority is lowest then NEW is placed at the beginning of the list (end if queue) : a.Set pointer in NEW to (previous) HEAD. b.Set HEAD to NEW and stop.

22 Algorithm for Queue / Stack in a linked List - Insertion 7.If the linked list is not empty and holds a priority queue where NEW’s priority is highest then NEW is placed at the end of the list (front) : a.Follow pointers to null pointer. b.Call this cell Previous. c.Set pointer in NEW to NULL. d.Make the pointer in PREVIOUS equal to NEW and stop. 8.Search list sequentially until the cell immediately before the cell with higher priority than NEW is found. Call this cell PREVIOUS. 9.Copy the pointer in PREVIOUS into TEMP. 10.Make the pointer in PREVIOUS equal to NEW 11.Make the pointer in NEW equal to TEMP and stop. Records in a Linked List

23 Queue / Stack in a linked List - Read Suppose we wish to read an item from a stack / queue held in a linked list. Note when an item is read from a queue it is copied to another location. So it can be used / executed next. So it can be used / executed next. and then deleted from the stack/queue. Once an item is read it is no longer in the queue / stack as the next item is the one to be read after this one. Once an item is read it is no longer in the queue / stack as the next item is the one to be read after this one. We humans think of “deleting” once an item is used but computers do so before using.

24 Stack in a linked List - Read Stack Last - In – First - Out (LIFO) data structure so only one end is used. Last - In – First - Out (LIFO) data structure so only one end is used. Read and insert from the beginning of the list (back).

25 Stack in a linked List - Read 1.Check that the list is not empty (no items to read). 2.If the list is empty report an error and stop. HEAD

26 Stack in a linked List - Read 3.Copy data in first cell. 4.Set TEMP to HEAD. Head Data Free Ex Data = TEMP Copy data

27 Stack in a linked List - Read 5.Set HEAD equal to pointer in first cell. Head Data Ex DataData Free Ex Data

28 Stack in a linked List - Read 6.Set pointer in first cell equal to (previous) FREE. Head Data Ex DataData Free Ex Data

29 Stack in a linked List - Read 7.Set FREE equal to TEMP and stop. Head Data Ex DataData Free Ex Data = TEMP (= ex HEAD)

30 Stack in a linked List - Read 1.Check that the list is not empty. 2.If the list is empty report an error and stop. 3.Copy data in first cell. 4.Set TEMP to HEAD. 5.Set HEAD equal to pointer in first cell. 6.Set pointer in first cell equal to (previous) FREE. 7.Set FREE equal to TEMP and stop.

31 Queue in a linked List - Read Queue First-In-First-Out (FIFO) data structure so both ends are used: First-In-First-Out (FIFO) data structure so both ends are used: Read from the end of the list (front). Insert at the beginning of the list (back).

32 Queue in a linked List - Read 1.Check that the list is not empty (no items to read). 2.If the list is empty report an error and stop. HEAD

33 Queue in a linked List - Read 3.Follow pointers to null pointer. 4.Copy data in this cell. Head Data Copy data Free Ex Data

34 Queue in a linked List - Read 5.Call this cell READ. Head Data READ Free Ex Data

35 Queue in a linked List - Read 6.Move null pointer to previous node. Head Data Ex Data READ Free Ex Data

36 Queue in a linked List - Read 7.Set pointer in READ equal to (previous) FREE. Head Data Free Data Ex Data Data READ Ex Data

37 Queue in a linked List - Read 8.Set FREE to READ and stop. Head Data Free READ Ex Data

38 Queue in a linked List - Read 1.Check that the list is not empty. 2.If the list is empty report an error and stop. 3.Follow pointers to null pointer. 4.Copy data in this cell. 5.Call this cell READ. 6.Move null pointer to previous node. 7.Set pointer in READ equal to (previous) FREE. 8.Set FREE to READ and stop.

39 Stacks in arrays

40 Stacks in arrays Insert and read at the back end of stack - end of the array Insert / Read

41 To insert an item into a stack in an array 1.Check to see if stack is full. 2.If the stack is full report an error and stop. 3.Increment the stack pointer. 4.Insert new data item into cell pointed to by the stack pointer and stop. Assume that cells are numbered from 1 upwards and that, when the stack is empty, the pointer is zero. Pointer to Top of stack Data

42 Stacks in an array – Read 1.Check to see if the stack is empty. 2.If the stack is empty report an error and stop. 3.Copy data item in cell pointed to by the stack pointer (so it can be used or executed next). 4.Decrement the stack pointer and stop. When an item is deleted from a stack, the item's value is copied and the stack pointer is moved down one cell. The data itself is not deleted. The data itself is not deleted.

43 Queues in arrays

44 Queues in an array Insert at back of queue - end of array. However, if the list holds a priority queue then higher priority items (jobs) may enter at other positions. However, if the list holds a priority queue then higher priority items (jobs) may enter at other positions. Read from front queue - beginning of array. Insert Read

45 Queues in an array - Insertion 1.Check to see if queue is full. 2.If the queue is full report an error and stop. 3.Insert new data item into cell pointed to by the head pointer. 4.Increment the head pointer and stop. Assume that the cells are numbered from 1 upwards and that, when the queue is empty, the two pointers point to the same cell. Data Head Pointer Tail Pointer

46 Queues in an array – Read & Delete 1.Check to see if the queue is empty (when the head and tail pointers point to the same cell). 2.If the queue is empty report error and stop. 3.Copy data item in cell pointed to by the tail pointer (so it can be used or executed next). 4.Increment tail pointer and stop.

47 Records in a Linked List Insertion: Exactly the same as demonstrated earlier: Exactly the same as demonstrated earlier: Slides 8 – Deletion: Almost the same as reading from a linked list (demonstrated earlier) but without the copying part (just the deletion part). Almost the same as reading from a linked list (demonstrated earlier) but without the copying part (just the deletion part). The deletion part is demonstrated again on the next 8 slides. Search: Almost the same as priority based insertion into a linked list (where the new priority is higher than some but lower than others - demonstrated earlier) but instead of searching for the cell immediately before the cell with higher priority, the search would be for a cell with data that matches what is being searched for; and also without the copying and deletion part. Almost the same as priority based insertion into a linked list (where the new priority is higher than some but lower than others - demonstrated earlier) but instead of searching for the cell immediately before the cell with higher priority, the search would be for a cell with data that matches what is being searched for; and also without the copying and deletion part. Discussed in more detail on slide Amend: The same as searching above but with the extra action of amendment. The same as searching above but with the extra action of amendment. Discussed in more detail on slide

48 Linked List – Deletion of a Record 1.Check that the list is not empty (no items to read). 2.If the list is empty report an error and stop. HEAD

49 Linked List – Deletion of a Record 3.Search list to find the data to be deleted. 4.If the data is not in the list, report an error and stop. Head Data

50 Linked List – Deletion of a Record 5.Call the cell immediately before the cell to be deleted PREVIOUS. Head Data Free Data PREVIOUS e.g. Cell to be deleted

51 Linked List – Deletion of a Record 6.Set TEMP to pointer in PREVIOUS. Head Data Free Data PREVIOUS Cell to be deleted = TEMP

52 Linked List – Deletion of a Record 7.Set pointer in PREVIOUS equal to pointer in cell to be deleted. Head Data Free Data ex PREVIOUS Cell to be deleted

53 Linked List – Deletion of a Record 8.Set pointer in cell to be deleted equal to (previous) FREE. Head Data Free Data Cell to be deleted ex PREVIOUS

54 ex PREVIOUS Linked List – Deletion of a Record 9.Set FREE equal to TEMP and stop. Head Data Free Data Deleted Cell = TEMP (= ex pointer in ex Previous)

55 Algorithm for Linked List – Deleting Records 1.Check that the list is not empty. 2.If the list is empty report an error and stop. 3.Search list to find the cell to be deleted. 4.If the cell is not in the list, report an error and stop. 5.Call the cell immediately before the cell to be deleted PREVIOUS. 6.Set TEMP to pointer in PREVIOUS. 7.Set pointer in PREVIOUS equal to pointer in cell to be deleted. 8.Set pointer in cell to be deleted equal to FREE. 9.Set FREE equal to TEMP and stop.

56 Algorithm for linked list – Search for a record Assume: Ascending order of some key value and order is required to be maintained. Ascending order of some key value and order is required to be maintained. List is not empty List is not empty Pointer[Current] references the pointer value for any node. Pointer[Current] references the pointer value for any node. 1.Set CURRENT equal to HEAD 2.REPEAT Set CURRENT to POINTER in CURRENT cell by Current = Pointer[Current] Set CURRENT to POINTER in CURRENT cell by Current = Pointer[Current] 3.UNTIL SearchData = Data in CURRENT cell OR Pointer[Current] = Null

57 Algorithm for a linked list – Amend a Record 1.Check that the list is not empty. 2.If the list is empty report an error and stop. 3.Search the list to find the cell to be amended. 4.Amend the data but do not change the key (the one field that is unique and would be used to order the list). 5.Stop.

58 Plenary Jobs that require printing, by a network printer, are stored until the printer is ready. Their addresses are placed in a queue to await their turn for printing. Addresses of new jobs are placed at one end of the queue. These job addresses are taken from the other end when the printer is ready. If the queue is held in a linked list, describe an algorithm for: a)inserting an address into the queue, a)inserting an address into the queue, b)reading an address from the queue. b)reading an address from the queue.

59 Plenary Find print Q in head of list table a)Insert data into free space H of L points to new node new node points to old first value mention of insertion of high priority jobs into queue b)Check to ensure list not empty - follow pointers to null pointer - read address of print job - move null pointer to previous node - return node to free space

60 Plenary How can a data item be inserted into and read / deleted from a queue held in an array?

61 Plenary Inserted into: Error check, Q full Error check, Q full Insert data at ARRAY (Head pointer) Insert data at ARRAY (Head pointer) Increment Head pointer Increment Head pointer

62 Plenary Read / Deleted from: Error check, Q empty Error check, Q empty Read data at ARRAY (Tail pointer) Read data at ARRAY (Tail pointer) Increment Tail pointer Increment Tail pointer