Linked Lists, Stacks, Queues, Deques. It s time for a chainge!
Full text
(2) Learning Goals After this unit, you should be able to... • Differentiate an abstraction from an implementation. • Define and give examples of problems that can be solved using the. abstract data types stacks, queues and deques. • Compare and contrast the implementations of these abstract data types using linked lists and circular arrays in C++. • Demonstrate how dynamic memory management is handled in C++ (e.g., allocation, deallocation, memory heap, run-time stack). • Gain experience with pointers in C++ and their tradeoffs and risks (dangling pointers, memory leaks). • Explain the difference between the complexity of a problem (sorting) and the complexity of a particular algorithm for solving that problem. • Manipulate data in stacks, queues, and deques (irrespective of any implementation)..
(3) Linked Lists: an alternative to arrays. •. Can you think of any limitations with the array data structure?.
(4) Another note on abstract Data Types. • •. (Public) interface specifications Language independent.
(5) Linked Lists: an alternative to arrays A Linked List is an abstract data type for representing lists as collections of linked items Instead of having an overall representation of the list, the ordering of the list is represented locally –. That is, the information about what element comes next in a list is stored as a pointer within the element object.. –. No list object (element) knows about any other elements in the list, just the ones to which it is adjacent.
(6) Linked Lists: an abstraction This is a pictorial representation of a singly linked list. Note that it is not necessarily represented contiguously in memory, nor in the order the elements occur in the list itself.
(7) Linked Lists: implementation. •. Let’s take a look at how this is represented in C++.
(8) struct hockey_player { int jersey_number; string name; hockey_player* next; }; ... hockey_player * head = NULL; ... void insert(int num, string name) { hockey_player * temp; temp = new hockey_player; temp->jersey_number = num; temp->name = name;. }. temp->next = head; head = temp; }.
(9) Comparison of Worst Case Complexities (for n entries): Assume we have a head pointer (only) for the linked list below. (Later: would your answers change if we also had a tail pointer?) Operation. Array (unordered). Array (ordered). Linked List (unordered). Linked List (ordered). Insert at start. O(1). O(n). O(1). O(1). Insert at end. O(1). O(1). O(1). O(1). Insert after current position. O(1). O(n). O(1). O(1). O(n). O(n)/O(lgn). O(n). O(n). O(1). O(n). O(1). O(1). Find (search for) a value Delete at current position.
(10) Comparison of Worst Case Complexities (for n entries): Assume we have a head pointer (only) for the linked list below. (Later: would your answers change if we also had a tail pointer?) Operation. Array (unordered). Array (ordered). Linked List (unordered). Linked List (ordered). Insert at start. O(1). O(n). O(1). O(1). Insert at end. O(1). O(1). O(1). O(1). Insert after current position. O(1). O(n). O(1). O(1). O(n). O(n)/O(lgn). O(n). O(n). O(1). O(n). O(1). O(1). Find (search for) a value Delete at current position.
(11) Comparison of Worst Case Complexities (for n entries): Assume we have a head pointer (only) for the linked list below. (Later: would your answers change if we also had a tail pointer?) Operation. Array (unordered). Array (ordered). Linked List (unordered). Linked List (ordered). Insert at start. O(1). O(n). O(1). O(1). Insert at end. O(1). O(1). O(1). O(1). Insert after current position. O(1). O(n). O(1). O(1). O(n). O(n)/O(lgn). O(n). O(n). O(1). O(n). O(1). O(1). Find (search for) a value Delete at current position.
(12) Comparison of Worst Case Complexities (for n entries): Assume we have a head pointer (only) for the linked list below. (Later: would your answers change if we also had a tail pointer?) Operation. Array (unordered). Array (ordered). Linked List (unordered). Linked List (ordered). Insert at start. O(1). O(n). O(1). O(1). Insert at end. O(1). O(1). O(1). O(1). Insert after current position. O(1). O(n). O(1). O(1). O(n). O(n)/O(lgn). O(n). O(n). O(1). O(n). O(1). O(1). Find (search for) a value Delete at current position.
(13) Comparison of Worst Case Complexities (for n entries): Assume we have a head pointer (only) for the linked list below. (Later: would your answers change if we also had a tail pointer?) Operation. Array (unordered). Array (ordered). Linked List (unordered). Linked List (ordered). Insert at start. O(1). O(n). O(1). O(1). Insert at end. O(1). O(1). O(1). O(1). Insert after current position. O(1). O(n). O(1). O(1). O(n). O(n)/O(lgn). O(n). O(n). O(1). O(n). O(1). O(1). Find (search for) a value Delete at current position.
(14) Stacks A stack operates on the LIFO principle: Last In, First Out. Push and pop operations have real-life analogies: a “stack” of trays or plates in a cafeteria, Pez dispenser, etc. Software examples: supporting a Web browser’s “Back” button, passing variables to functions, keeping track of function calls (e.g., recursion), storing local variables, etc. Some stack operations and their complexities:. - push(item) _______________ (add to top) - pop(). _______________ (take off top). - returnTop() _______________ (without removing). - empty(). _______________ (is stack empty?). - getSize(). _______________ (how many in stack?).
(15) A stack can be implemented using either an array or a linked list:.
(16) Queues A queue operates on the FIFO principle: First In, First Out Real-life analogies: line-up at a bank or supermarket Software examples: Web browser requests to a server Some queue operations and their complexities: - enqueue(item) _______________ - dequeue(). _______________. - getFirst(item). _______________ (without removing). - empty() . _______________ (is it empty?). - getSize() . _______________ (how many?).
(17) A queue can be implemented using either a circular array or a linked list:.
(18) Doubly-Linked Lists In what ways does a doubly-linked list differ from a singly-linked list, besides the presence of an extra pointer?. What are the advantages and disadvantages?.
(19) Deques A deque (pronounced “deck”) is a double-ended queue. It acts like a queue, but we can insert at either end, and remove from either end. Like a queue, a deque can be implemented using either an array or a linked list.. It is really an abstraction of the queue and stack..
(20) Learning Goals After this unit, you should be able to... • Differentiate an abstraction from an implementation. • Define and give examples of problems that can be solved using the. abstract data types stacks, queues and deques. • Compare and contrast the implementations of these abstract data types using linked lists and circular arrays in C++. • Demonstrate how dynamic memory management is handled in C++ (e.g., allocation, deallocation, memory heap, run-time stack). • Gain experience with pointers in C++ and their tradeoffs and risks (dangling pointers, memory leaks). • Explain the difference between the complexity of a problem (sorting) and the complexity of a particular algorithm for solving that problem. • Manipulate data in stacks, queues, and deques (irrespective of any implementation)..
(21)
Related documents
■ Consider adding a full-time Cabinet member--one who can bring the faculty together to craft processes for developing course-level learning outcomes, tie these into the
I/We understand that Kingdom Community Development Corporation provides information on a broad range of housing programs and products and that the housing counseling received from
– Structure that contains a pointer to a structure of the same type – Can be linked together to form useful data structures such as.. lists, queues, stacks
• As well as directly involving children in decision-making, Health and Well-being Boards, local authorities, clinical commissioning groups and local Healthwatch should draw
Notice of Dishonor - notice given by the holder or his agent to a party or parties secondarily liable that the instrument was dishonored by non-acceptance by the drawee of a bill,
Quick sort: fast in practice, O(n 2 ) worst case?. Merge sort: good worst case, great for linked lists,
the world population, as we now know it, but with one important difference; whereas now we have large con- gregations of the people in various sections of
public int size(); // return the number of // elements in the stack public boolean isEmpty(); // see if the stack.. //