Review of Lists, Stacks, and Queues CS 400/600 – Data Structures.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Lists A list is a finite, ordered sequence of data items. Important concept: List elements have a position. Notation: What operations should we implement?
Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Templates in C++ Template function in C++ makes it easier to reuse classes and functions. A template can be viewed as a variable that can be instantiated.
Review of Stacks and Queues Dr. Yingwu Zhu. Our Focus Only link-list based implementation of Stack class Won’t talk about different implementations of.
1 Linked List Position (1). 2 Linked List Position (2)
COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
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.
E.G.M. Petrakislists, stacks, queues1 Stacks Stack: restricted variant of list –Elements may by inserted or deleted from only one end  LIFO lists –Top:
Queues CS-212 Dick Steflik. Queues First In, First Out operation – FIFO As items are added they are chronologically ordered, items are removed in their.
What is a Queue? A queue is a FIFO “first in, first out” structure.
ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.
LINKED QUEUES P LINKED QUEUE OBJECT Introduction Introduction again, the problem with the previous example of queues is that we are working.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 18 Stacks.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Deux applications. Freelists Les appels système new et delete sont lents: // Singly-linked list node with freelist template class Link { private: static.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
E.G.M. Petrakisstacks, queues1 Stacks  Stack: restricted variant of list  elements may by inserted or deleted from only one end : LIFO lists  top: the.
Linked Lists1 Part-B3 Linked Lists. Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data structure consisting of a sequence.
E.G.M. Petrakislists1 Lists  List: finite sequence of data elements  all elements have the same data type  The operations depend on the type of the.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 18: Stacks and.
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.
Implementing a Queue as a Linked Structure CS 308 – Data Structures.
E.G.M. Petrakislists, stacks, queues1 Lists List: finite sequence of data elements –Ordered: each element has a position in list –All elements has the.
Stacks, Queues, and Deques
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
1 An Introduction to Data Structures and Abstract Data Types.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
Linked Lists list elements are stored, in memory, in an arbitrary order explicit information (called a link) is used to go from one element to the next.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R1. Elementary Data Structures.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Exam Review 5/28/15. Software Development (Review)  Make it work…then make it pretty can result in dangerous code  Better to take a unified, consistent.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
Stacks And Queues Chapter 18.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Review of Stacks and Queues Dr. Yingwu Zhu. How does a Stack Work? Last-in-First-out (LIFO) data structure Adding an item Push operation Removing an item.
The Abstract Data Type Queue A queue New items enter at the back, or rear, of the queue Items leave from the front of the queue First-in, first-out (FIFO)
Abstract Data Types and a review of C++ programming concepts CS 400/600 – Data Structures.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 18: Stacks and Queues.
Coursenotes CS3114: Data Structures and Algorithms Clifford A. Shaffer Yang Cao Department of Computer Science Virginia Tech Copyright ©
Computer Science Department Data Structures and Algorithms Queues Lecture 5.
Lecture 20 Stacks and Queues. Stacks, Queues and Priority Queues Stacks – first-in-last-out structure – used for function evaluation (run-time stack)
UNCA CSCI September, 2001 These notes were prepared by the text’s author Clifford A. Shaffer Department of Computer Science Virginia Tech Copyright.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
1 Midterm 1 on Friday February 12 Closed book, closed notes No computer can be used 50 minutes 4 questions Write a function Write program fragment Explain.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues.
Cpt S 122 – Data Structures Abstract Data Types
CS505 Data Structures and Algorithms
More on Linked Lists, Stacks, and Queues
Chapter 4 The easy stuff.
Sequences 8/2/ :13 AM Linked Lists Linked Lists.
Stack and Queue APURBO DATTA.
Stacks Stack: restricted variant of list
CST230: Applied Data Structures
CMSC 341 Lecture 5 Stacks, Queues
Queues.
Linked List (Part I) Data structure.
Basics of Algorithm Analysis
Lists List: finite sequence of data elements
CSCI 333 Data Structures Chapter 4 13 and 16 September 2002.
Sequences 12/8/2018 3:02 AM Linked Lists Linked Lists.
Queues: Implemented using Arrays
CS210- Lecture 6 Jun 13, 2005 Announcements
Stacks, Queues, and Deques
Data Structures & Programming
Presentation transcript:

Review of Lists, Stacks, and Queues CS 400/600 – Data Structures

Lists, Stacks, Queues2 The LIST ADT  Fence – a pointer to the current position in the list. Example:  Operations insert/remove/getValue – start of the right partition Append – end of list setStart, setEnd, prev, next, setPos – move the fence leftLength, rightLength clear, print

Lists, Stacks, Queues3 Implementation  The LIST abstract data type can be implemented in a variety of ways, including: Array Linked list

Lists, Stacks, Queues4 An Array-based LIST class AList : public List { private: int maxSize; int listSize; int fence; Elem* listArray; … }; maxSize: listSize: fence: 8 6 2

Lists, Stacks, Queues5 Array-based implementation  Delete and insert take  (n) time: // Insert at front of right partition template bool AList ::insert(const Elem& item) { if (listSize == maxSize) return false; for(int i=listSize; i>fence; i--) // Shift Elems up to make room listArray[i] = listArray[i-1]; listArray[fence] = item; listSize++; // Increment list size return true; } New value

Lists, Stacks, Queues6 Single-linked node // Singly-linked list node template class Link { public: Elem element;// Value for this node Link *next;// Pointer to next node Link(const Elem& elmval, Link* nextval = NULL) { element = elemval; next = nextval; } Link(Link* nextval = NULL) {next = nextval;} };

Lists, Stacks, Queues7 Single-linked implementation  Leftcnt and rightcnt only for efficiency  Pointers only // Linked list implementation template class LList: public List { private: Link * head; // Point to list header Link * tail; // Pointer to last Elem Link * fence;// Last element on left int leftcnt; // Size of left int rightcnt; // Size of right

Lists, Stacks, Queues8 Single-linked implementation  Now insert and delete are  (1) operations  What about setpos(i)?  What about prev()? // Linked list implementation template class LList: public List { private: Link * head; // Point to list header Link * tail; // Pointer to last Elem Link * fence;// Last element on left int leftcnt; // Size of left int rightcnt; // Size of right 23 head

Lists, Stacks, Queues9 Insertion // Insert at front of right partition template bool LList ::insert(const Elem& item) { fence->next = new Link (item, fence->next); if (tail == fence) tail = fence->next; rightcnt++; return true; }

Lists, Stacks, Queues10 Deletion // Remove and return first Elem in right partition template bool LList ::remove(Elem& it) { if (fence->next == NULL) return false; it = fence->next->element; // Remember value // Remember link node Link * ltemp = fence->next; fence->next = ltemp->next; // Remove if (tail == ltemp) tail = fence; // Reset tail delete ltemp; // Reclaim space rightcnt--; return true; }

Lists, Stacks, Queues11 Doubly-linked list  How can we make setpos() and prev() more efficient in terms of time? By sacrificing space (space/time tradeoff) Each node points to both the next and prev nodes

Lists, Stacks, Queues12 Doubly-linked insert // Insert at front of right partition template bool LList ::insert(const Elem& item) { fence->next = new Link (item, fence, fence->next); if (fence->next->next != NULL) fence->next->next->prev = fence->next; if (tail == fence) // Appending new Elem tail = fence->next; // so set tail rightcnt++; // Added to right return true; }

Lists, Stacks, Queues13 Comparison of Implementations Array-Based Lists:  Insertion and deletion are  (n).  Prev and direct access are  (1).  Array must be allocated in advance.  No overhead if all array positions are full. Linked Lists:  Insertion and deletion are  (1).  Prev and direct access are  (n).  Space grows with number of elements.  Every element requires overhead.

Lists, Stacks, Queues14 Space Comparison “Break-even” point: DE = n(P + E); n = DE P + E E: Space for data value. P: Space for pointer. D: Number of elements in array. n: Number of elements in list.

Lists, Stacks, Queues15 Managing Memory  C++ new() and delete() operations are very expensive in terms of time  We can use a freelist to manage our memory and speed up our implementation  Simple singly-linked list of nodes  Easier than general-purpose memory manager: All nodes are the same size Always emove from front of list, add to front of list

Lists, Stacks, Queues16 Freelists // Singly-linked list node with freelist template class Link { private: static Link * freelist; // Head public: Elem element; // Value for this node Link* next; // Point to next node Link(const Elem& elemval, Link* nextval =NULL) { element = elemval; next = nextval; } Link(Link* nextval =NULL) {next=nextval;} void* operator new(size_t); // Overload void operator delete(void*); // Overload };

Lists, Stacks, Queues17 Freelists (2) template Link * Link ::freelist = NULL; template // Overload for new void* Link ::operator new(size_t) { if (freelist == NULL) return ::new Link; Link * temp = freelist; // Reuse freelist = freelist->next; return temp; // Return the link } template // Overload delete void Link ::operator delete(void* ptr){ ((Link *)ptr)->next = freelist; freelist = (Link *)ptr; }

Lists, Stacks, Queues18 Stacks  LIFO – Last in, first out Stack S1; S1.push(10); S1.push(12); S1.push(15) cout << S1.pop() << S1.pop() << S1.pop();

Lists, Stacks, Queues19 Array-Based Stack // Array-based stack implementation template class AStack: public Stack { private: int size; // Maximum size of stack int top; // Index for top element Elem *listArray; // Array holding elements public: AStack(int sz =DefaultListSize) { size = sz; top = 0; listArray = new Elem[sz]; } ~AStack() { delete [] listArray; } void clear() { top = 0; }

Lists, Stacks, Queues20 Array-Based Stack (2) bool push(const Elem& item) { if (top == size) return false; // full else { listArray[top++] = item; return true; } } bool pop(Elem& it) { // Pop top element if (top == 0) return false; else {it = listArray[--top]; return true;} } // Return top element bool topValue(Elem& it) const { if (top == 0) return false; else {it = listArray[top-1]; return true;} } int length() const { return top; }

Lists, Stacks, Queues21 Linked Stack (1) template class LStack: public Stack { private: Link * top; // Pointer to first elem int size; // Count number of elems public: LStack(int sz =DefaultListSize) { top = NULL; size = 0; } bool push(const Elem& item) { top = new Link (item, top); size++; return true; }

Lists, Stacks, Queues22 Linked Stack (2) bool pop(Elem& it) { if (size == 0) return false; it = top->element; Link * ltemp = top->next; delete top; top = ltemp; size--; return true; } bool topValue(Elem& it) const { if (size == 0) return false; it = top->element; return true; }

Lists, Stacks, Queues23 Queues FIFO: First in, First Out Restricted form of list: Insert at one end, remove from the other. Notation:  Insert: Enqueue  Delete: Dequeue  First element: Front  Last element: Rear

Lists, Stacks, Queues24 Queue Implementation (1) Insert and delete are  (1), but the queue drifts towards the end of the array…

Lists, Stacks, Queues25 Queue Implementation (2) How can we tell if the queue is full? Pigeonhole principle n-1 elements, or an explicit count member variable