1 / 20

Day 14.

Day 14. 0. Questions on Assignment 4? 1. Linked list concept. 2. Linked implementation of a Queue. 3. What does .NET provide?. 0. Questions on assignment 4?. My advice: Develop your solution incrementally. First make sure you have all of the data members and member methods you need.

Download Presentation

Day 14.

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Day 14. 0. Questions on Assignment 4? 1. Linked list concept. 2. Linked implementation of a Queue. 3. What does .NET provide?

  2. 0. Questions on assignment 4? My advice: Develop your solution incrementally. First make sure you have all of the data members and member methods you need. Add to TempQueue.cs Then devise a simulation algorithm. Modify Queue_Driver.cs

  3. 1. Linked List concept. • So far we have focused on the use of an array as the container for a data structure. • Arrays are easy to use, but have some disadvantages. • Discuss. • 1) How big? • 2) The problem of contiguous storage.

  4. Dynamic nodes. • Rather than use contiguous storage like an array, it is possible to store each element in its own memory location. The elements might happen to be contiguous, but don’t have to be. • See diagram.

  5. A problem. If we simply allocate elements, non-contiguously, what difficulty do we face? This is particularly bad for stacks and queues, because in both cases, the order matters. On a stack, data is arranged from newest (Top) to oldest (bottom). Likewise in a queue, the Front is oldest and the Rear is newest.

  6. We need a system to maintain the order of the data. We need at least one way into the list from the outside (an external pointer). The nodes of the list should be “intelligent.” They will contain not only data (“Info”) but also a pointer to the next node in the list (“Next”). See diagram.

  7. The null concept. We also need to know if a linked list is empty, or if we have reached the end of a list. For this purpose, we can use the keyword “null,” which means “no valid address.” It is important to avoid doing something to a pointer set to null. Why?

  8. Uses of linked lists. Linked lists are just as versatile as arrays. Obviously, they can be used to implement a standard list. But also: We can use them to define a stack, and then do Push and Pop dynamically (i.e. the stack literally shrinks and grows). See diagram.

  9. Uses of linked lists. We can also use a linked list to define a queue, and then do Enq and Deq dynamically. For this, notice that we will need 2 external pointers to the linked list, Front and Rear. See diagram.

  10. 2. Linked implementation of a queue. See LinkedQueue.cs First we need to define a Node class. Within the class we have Info to hold data and Next as a pointer. What is bizarre about the definition of “Next”? There is also a constructor used to fill in a node’s Info and Next when the node is created by Enq.

  11. Data members. The Linked_Queue class has 2 external pointers: Front and Rear, both of type Node. NumberElements keeps track of the number of elements in the queue. It is initialized to zero by the constructor, and then is updated…how?

  12. Member methods include. (a) a constructor. Why set both Front and Rear to null? (b) Enq: Creates a new node. If the queue was empty, special treatment. Why? else what does the code do?

  13. Member methods. (c) EmptyQueue. Why? (d) Count. (e) Deq. Needs to return data from the old front node, update the Front pointer, and deallocate the old front. Note exception handling.

  14. How does it work? (1) Enq into empty queue. (2) Enq at Rear of existing queue. (3) Deq from Front. (4) Deq last element. See diagrams.

  15. How to use the code. See LinkedQueueDriver.CS. This code: (1) instantiates the Linked_Queue class as a queue of integers. (2) Uses Enq, Deq and Count. What will it output?

  16. 3. What does .NET provide? System.Collections provides non-generic data structures where each element is stored as an object, including: ArrayList Stack Queue SortedList

  17. Example. using System.Collections; Queue Q = new Queue(); Q.Enqueue (3); Q.Enqueue (2); Q.Enqueue (1); foreach (int value in Q) Console.WriteLine (value);

  18. Generic collections. System.Collections.Generic provides generic data structures, including: Stack<T> Queue<T> LinkedList<T> The latter is actually maintained as a doubly-linked list.

  19. LinkedList<T> offers the following: AddAfter (node, newNode). AddBefore (newNode, node). AddFirst (node). [ “Push”] AddLast (node). [“Enq”]. RemoveFirst () [“Pop” or “Deq”]. RemoveLast ().

  20. Using LinkedList<T> to model a queue: See DynamicQDriver.CS. Advantages of generic collections: (a) Quick development time. (b) Control abstraction. (c) Standardized to facilitate coordination between independent programmers on a team.

More Related