1 KEEPING POINTERS OR REFERENCES UNDER CONTROL A COMPONENT BASED APPROACH TO LIST BASED DATA STRUCTURES.

Slides:



Advertisements
Similar presentations
COMP171 Fall 2005 Lists.
Advertisements

Stacks, Queues, and Linked Lists
Stack and Queues using Linked Structures Kruse and Ryba Ch 4.
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.
Stack & Queues COP 3502.
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Chapter 6 Structures By C. Shing ITEC Dept Radford University.
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.
ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.
Data Structure Dr. Mohamed Khafagy.
1 CSC 211 Data Structures Lecture 22 Dr. Iftikhar Azim Niaz 1.
Queue RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
CS Data Structures II Review COSC 2006 April 14, 2017
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures.
Stack and Queue Dr. Bernard Chen Ph.D. University of Central Arkansas.
CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.
1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 12: Stacks and Queues.
CS 106 Introduction to Computer Science I 12 / 11 / 2006 Instructor: Michael Eckmann.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
CS 106 Introduction to Computer Science I 12 / 13 / 2006 Instructor: Michael Eckmann.
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.
 Chapter 8 introduces the queue data type.  Several example applications of queues are given in that chapter.  This presentation describes the queue.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 15: Linked data structures.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
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.
Stacks and Queues Introduction to Computing Science and Programming I.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
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,
COMP 103 Linked Lists. 2 RECAP-TODAY RECAP  Linked Structures: LinkedNode  Iterating and printing Linked Nodes  Inserting and removing Linked Nodes.
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
Chapter 8 Queue I CS Data Structures I COSC2006 April 24, 2017
Queues CS 302 – Data Structures Sections 5.3 and 5.4.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
EC-211 DATA STRUCTURES LECTURE 9. Queue Data Structure An ordered group of homogeneous items or elements. Queues have two ends: – Elements are added at.
Data Structures & Algorithms
Computer Science Department Data Structures and Algorithms Queues Lecture 5.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 7: Queues Data Abstraction & Problem Solving with C++
Week 2 - Friday.  What did we talk about last time?  Computing Big Oh.
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 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Linked Lists A formal data structure. Linked Lists Collections of data items “lined up in a row” Inserts and deletes can be done anywhere in the list.
Review Array Array Elements Accessing array elements
Popping Items Off a Stack Using a Function Lesson xx
Queues Chapter 4.
Chapter 12 – Data Structures
Dr. Bernard Chen Ph.D. University of Central Arkansas
Queues Queues Queues.
Stack and Queue APURBO DATTA.
Queues Chapter 4.
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
Chapter 16-2 Linked Structures
Stacks, Queues, and Deques
Data structures in C++.
Popping Items Off a Stack Lesson xx
Stack and Queues Stack implementation using Array
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
Visit for more Learning Resources
Lists.
Using a Queue Chapter 8 introduces the queue data type.
Using a Queue Chapter 8 introduces the queue data type.
17CS1102 DATA STRUCTURES © 2016 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS.
CSCS-200 Data Structure and Algorithms
Presentation transcript:

1 KEEPING POINTERS OR REFERENCES UNDER CONTROL A COMPONENT BASED APPROACH TO LIST BASED DATA STRUCTURES

2 Appropriate Level of Abstraction Conceptual (What) Implementation (How)

3 Stack Example String of Entries Push Pop Is_Empty Length_of

4 Some Pushes and Pops Initially S =  Push( d, S) Push( b, S) Push( c, S) Pop(result, S) Push( a, S) Length_of(S)  D bd cbd bd abd 3

5 Possible Implementations Array: For small stacks Dynamic structure: For unpredictable stacks with possibly large numbers of entries

6 One Way List Conceptually a pair of strings Advance Reset Length_of_Remainder Insert Remove Advance_to_End Swap_Remainders Clear_List

7 Some operation applications Initially L = ,  Insert( t, L) Insert(q, L) Insert(b, L) Advance(L) Remove(res, L) Insert(a, L) ,  , t , qt , bqt b, qt bq, t bq,  bq, a

8 public: List(); // Create empty List void Advance(); /*Requires |Rem|>0 = x + Rem Prec + x*/ T Peek(); /*Requires List is not empty Ensures Peek = Rem.c*/ int Length_of_Rem(); /*Requires True Ensures Length_of_Rem = |Rem| */ void Reset(); /*Requires True Ensures Prec is empty, Rem …

9 private: struct node { T c; node * next; }; node * prec; node * prerem; node * rem;

10 template List ::List() //Constructor for empty list { prec = new(node); prec->next = NULL; prerem = prec; rem = NULL; } template void List ::Advance() //Advance to next Node in list... //i.e. move first element of rem to end of prec { if (rem != NULL) //if rem is not empty { prerem = prerem->next; //advance rem pointers rem = rem->next; }

11 Implement a Stack Type Define a stack to be a list_position. A client who is looking for reusable components would see the specifications for a list position and choose it to act as the basis for creating the type stack. To Push an entry, say E, call S.Reset, followed by S.Insert(E), where S has been declared to be a stack,. A Pop is achieved by first doing a Reset and then a Remove. To check whether the stack is empty, one can do a Reset and then call Length_of_Rem. What is equally important is that now a client of the stack will not need to know anything about lists or pointers, because the client will simply declare a stack and make appropriate calls to Push and Pop, leaving the implementation details to the writer of the stack component. This approach results in complete adherence to the principles of information hiding and separation of concerns.

12 Queues To do an enqueue, call Reset followed by Insert. To dequeue, call Advamce_to_End followed by Insert. All of the pointer work was done in the list_position, leaving the queue writer to reason at the appropriate level of abstraction, rather than needing to worry about setting up nodes or links. A client of the queue component will simply call enqueues and dequeues, doing all thinking and reasoning at that level without any need to worry about low level details.

13 Insert Sort Consider what is sometimes called “an ordered list,” and treated as a different component from a list. We see that such a distinction is not necessary at all, since one can produce an ordered list without ever using a pointer or reference. We can simply use the list_position as defined, and as new items are inserted, we advance along the list until we find the position we want to satisfy the ordering requirement and then call Insert.

14 suppose we have the following characters we want to put into a sorted list: t, a, c, x, e, w. Call Insert on t, creating a list of one entry that conceptually looks like ( , t). Next, to insert the a, we use a loop to Advance until we find an entry the is alphabetically past the a. In this case, that happens right away. As soon as we see the t, we stop the advancing loop and do an Insert, resulting in ( , at). Next we Reset and then Advance until we find an element past c. This happens at the position, (a, t). Calling Insert results in (a, ct). Now Reset and continue with the x. This causes us to advance until we reach the end of the list where we do an insert, getting (act, x). We continue in this way until all of the elements have been entered.

15 Insert Sort for Characters cout << "Enter a string of characters terminated with '0'." << // endl; List L;//Declare a list of characters named L char c; cin >> c; L.Insert(c);//Puts first value in L cin >> c; while (c != '0') //Waits for 0 input to stop { if (c < L.Peek()) L.Reset();//if c is too small, reset //and start from the beginning while (L.Length_of_Rem() && (c > L.Peek())) L.Advance(); //advance to the correct spot L.Insert(c); //now that we are at the correct postition, //insert the character cin >> c;//and take the next character } L.Reset();//Reset the list so we can output from the beginning cout << "The sorted data is as follows:" << endl; while (L.Length_of_Rem())//while Rem is not empty { cout << L.Peek() << ' '; //output each element of the list L.Advance(); } cout << endl; ////////////////////////////

16 Insert Sort for Integers //Insert sort for integers// //////////////////////////// cout << "Enter integers, terminated by 0." <<endl; List B;//declare a list of integers named B int d; cin >> d; B.Insert(d);//Puts first value in B cin >> d; while (d != 0) //Waits for 0 input to stop { if (d < B.Peek()) B.Reset(); while (B.Length_of_Rem() && (d > B.Peek())) B.Advance(); B.Insert(d); cin >> d; } B.Reset(); cout << "The sorted data is as follows:" << endl; while (B.Length_of_Rem()) { cout << B.Peek() << ' '; B.Advance(); } cout << endl; } //end

17 Concerns What about reinforcing pointer or reference ideas? Give lots of pointer assignments Point out to students that the purpose is to reinforce these ideas, but in real practice, they would not program this way

18 Summary Reasoning at the right level of abstraction Information Hiding Cohesion Reuse of certified software Great feedback from employers