Linked Lists. Gaddis Ch. 17. CS 2308 :: Spring 2016 Molly O'Neil
|
|
- Shona Fisher
- 6 years ago
- Views:
Transcription
1 Linked Lists Gaddis Ch. 17 CS 2308 :: Spring 2016 Molly O'Neil
2 List ADT A list is an abstract data type representing an ordered sequence of values For example, both MP3 Player assignments have used lists: sequences of Songs, ordered by title and artist So far, when we've needed a list, we've used an array as the data structure implementing the list There are some disadvantages to using arrays for lists: Difficult to grow in size (allocate new array, then copy over elements) Allocated memory often exceeds actual # of elements in list When an element has to be added or removed to/from the middle of the list, other elements have to be moved (either shifted over, or swapped and re-sorted) 2
3 Linked List A linked list is another data structure that represents a list A series of nodes chained together in sequence Each node in the list points to the next node in the list A separate pointer points to the first node in the list (the head of the list) The last node points to nothing () head 3
4 Linked Lists The nodes are dynamically allocated The list grows and shrinks as nodes are added/deleted Easy to insert a node between other nodes head Easy to delete a node from between other nodes head 4
5 Linked Lists The nodes are dynamically allocated The list grows and shrinks as nodes are added/deleted Easy to insert a node between other nodes head Easy to delete a node from between other nodes head 5
6 Nodes The node is usually implemented as a nested struct or a class (defined inside the linked list definition) Each node contains: A data field This can be an int, a double, a struct, an object, etc. A pointer The link to the next node in the list data pointer 6
7 Empty List An empty list is a list that contains zero nodes The list head points to (address 0) head (All linked lists start with a head pointer and end in ; in an empty list, there are no nodes between the two) 7
8 Some Reminders About Equivalent to address zero In C++11, you can use nullptr is a #define for 0, defined in the <cstddef> header: #include <cstddef> To test a pointer for non- value: while(ptr) {... } // equivalent to: while(p!= ) To test a pointer for : while(!ptr) {... } // equivalent to: while(p == ) 8
9 Defining a Linked List Data Type We will define a class to represent a linked list data type that can store values of type double (But we could design a linked list that stores ints, or Student structs, or Time objects...) When we define a new abstract data type, we describe the values it can store (in this case, doubles) and the operations over those values Before defining the list values, we must: Define a (nested) data type for the nodes Define a head pointer variable to point to the first node in the list Then we define the operations/actions the list supports 9
10 Defining the Node Data Type We could use a class or a struct for the node type It's not obvious that the node type will need actions (functions), but let's use a class to keep our options open... class ListNode { public: double value; ListNode *next; }; value next (This just defines the data type; no variable declared) Why public? Lets us access value and next without getter/setter next holds the address of another ListNode (or ) This is a self-referential data structure 10
11 Defining the List Head Pointer Define a pointer for the head of the list: ListNode *head; It must be initialized to to signify that the list is empty (Initialized in the Linked List class constructor) This is the only member variable of our Linked List class As we add nodes, we retain just a pointer to the first node. Then we can follow pointers to each successive next node to access the others. 11
12 Linked List Operations Operations for our linked list to store a list of doubles: construct a new, empty list append a number (a new node) to the end of the list insert a number (a new node) within the list (in its ordered position) remove a number from the list display the linked list delete/destroy the list 12
13 Linked List Class Declaration using namespace std; class NumberList { private: class ListNode { public: double value; ListNode *next; }; NumberList.h ListNode *head; public: NumberList(); ~NumberList(); }; void append(double); void insert(double); void remove(double); void display(); ListNode is a private class internal to NumberList: cannot refer to ListNode or instantiate a ListNode object outside of NumberList. This is why we can get away with declaring ListNode's member variables as public; they're still entirely hidden from outside NumberList! 13
14 Operation: Construct the Empty List The NumberList constructor initializes the list to empty (points the head pointer at ) #include <cstddef> // for #include "NumberList.h" using namespace std; NumberList.cpp NumberList::NumberList() { head = ; } 14
15 Operation: Append Number to List Append: add a new number (a new node) to the end of the list Algorithm: Create a new node and store the data in it Set the new node's next pointer to point to If the list is empty: Make the head pointer point to the new node Else: Make the last node's next pointer point to the new node head 15
16 Operation: Append Number to List Append: add a new number (a new node) to the end of the list Algorithm: Create a new node and store the data in it Set the new node's next pointer to point to If the list is empty: Make the head pointer point to the new node Else: Make the last node's next pointer point to the new node head 16
17 Append: Find Last Element To append a new node after the last node, we have to find the last node. How? (We only have a pointer to the first/ head node) Key insight: Starting at the head node, we can always find other nodes by traversing the list (by following next pointers) The last node is the node whose next pointer points to Algorithm: Make a pointer curr and point it at the first node Check the next pointer of the node curr points to, and while it's not... Move curr to the next node by assigning curr pointer the value of (the node it points to)'s next pointer ListNode *curr = head; while(curr->next) // loop terminates when curr = last node curr = curr->next; 17
18 Traversing to Last Node curr head Set curr to head Is curr node's next pointer ==? Move curr to next node: curr = (curr node's next pointer) [ curr = curr->next ] 18
19 Traversing to Last Node curr head Set curr to head Is curr node's next pointer ==? Move curr to next node: curr = (curr node's next pointer) [ curr = curr->next ] Is curr node's next pointer ==? Move curr to next node 19
20 Traversing to Last Node curr head Set curr to head Is curr node's next pointer ==? Move curr to next node: curr = (curr node's next pointer) [ curr = curr->next ] Is curr node's next pointer ==? Move curr to next node Is curr node's next pointer ==? 20
21 append() in C++ void NumberList::append(double num) { // create the new node, store the data in it ListNode *newnode = new ListNode; newnode->value = num; newnode->next = ; NumberList.cpp // list is empty, make head point to new node if(head == ) head = newnode; else { ListNode *curr = head; // find the last node while(curr->next) curr = curr->next; moves curr to point to last node } } // set last node's next pointer to new node curr->next = newnode; 21
22 Traversing a Linked List Visit each node in a linked list to... Display the contents, sum data, delete each node, etc. Basic algorithm: Make a pointer and point it at first node (where head currently points) While pointer is not : Process data of node currently pointed at Advance to the next node by setting the pointer to the current node's next pointer 22
23 Operation: Display the List Displaying each node in succession requires traversing the list void NumberList::display() { NumberList.cpp ListNode *curr; // ptr to traverse the list curr = head; // start curr at head of list // until we encounter the at the end of the list... while(curr) { // print the value in the current node cout << curr->value << " "; } // move to the next node curr = curr->next; } cout << endl; 23
24 Demo Driver for NumberList #include "NumberList.h" using namespace std; ListDriver.cpp int main() { // Declare an instance (object) of the list class NumberList list; // Append some values (in ascending order) list.append(2.5); list.append(5.1); list.append(7.9); } // Display the values in the list list.display(); Output:
25 Operation: Delete (Destroy) the List The destructor must delete (de-allocate) all the nodes allocated for the list Use list traversal to visit each node, call delete on it Careful... What's wrong with the below definition? NumberList::~NumberList() { // BROKEN!!! ListNode *curr = head; // start traversal ptr at list head } while(curr) { delete curr; curr = curr->next; } 25
26 Destructor You have to save a copy of the pointer to the next node (curr->next) before you delete the node NumberList::~NumberList() { ListNode *curr; // traversal pointer ListNode *next; // pointer to next node NumberList.cpp curr = head; } while(curr) { next = curr->next; // save pointer to next delete curr; // delete current node curr = next; // advance pointer } 26
27 Operation: Remove Number from List Find number to remove, un-link the node from the list, and delete (de-allocate) the removed node Requires two pointers: One [curr] to find and point to the node to be removed One [prev] to point to the node before the node to be removed prev curr head Remove 6.4 from list 27
28 Removing a Node To re-link the list around the node we're removing, we change the next pointer of the previous node to point to the node after the one we're removing prev->next = curr->next; prev curr head Now delete the curr node 28
29 Removing a Node To re-link the list around the node we're removing, we change the next pointer of the previous node to point to the node after the one we're removing prev->next = curr->next; head Now delete the curr node 29
30 Finding the Node to Remove Remove function takes a number to remove, not a node (the existence of the ListNode class is hidden/ private) Must first find the associated node Traverse the list looking for a match prev curr prev = curr; curr = curr->next; head Remove 6.4 from list 30
31 Finding the Node to Remove Remove function takes a number to remove, not a node (the existence of the ListNode class is hidden/ private) Must first find the associated node Traverse the list looking for a match prev curr prev = curr; curr = curr->next; head Remove 6.4 from list 31
32 Finding the Node to Remove Remove function takes a number to remove, not a node (the existence of the ListNode class is hidden/ private) Must first find the associated node Traverse the list looking for a match prev curr prev = curr; curr = curr->next; head What if the remove target is 8.2? Remove 6.4 from list 32
33 remove() in C++ void NumberList::remove(double num) { ListNode *curr = head; // traversal pointer ListNode *prev = ; // trailing pointer NumberList.cpp // traverse the list to find the node to remove while(curr && curr->value!= num) { prev = curr; curr = curr->next; } } // if curr is, num was not in the list: nothing to do if(curr) { // if we're removing the first node... if(curr == head) // or: if(!prev) } head = head->next; // advance head pointer // otherwise, remove from list middle or end else prev->next = curr->next; // re-link around node delete curr; // delete the node What if the remove target is the last node? 33
34 remove() Variations if(curr) { if(curr == head) head = head->next; else prev->next = curr->next; delete curr; } if(curr) { if(curr == head) head = curr->next; else prev->next = curr->next; delete curr; } if(curr) { if(prev) prev->next = curr->next; else head = head->next; delete curr; } if(curr) { if(curr == head) { head = head->next; delete curr; } else { prev->next = curr->next; delete curr; } } Lots of minor variations in how we can code linked list operations All of them involve the same cases: removing the head node, or removing a middle/last node. Takeaway: Learn the algorithm; you can always re-derive the code! 34
35 Demo Driver for NumberList #include "NumberList.h" using namespace std; ListDriver.cpp int main() { // Set up the list NumberList list; list.append(2.5); list.append(5.1); list.append(7.9); list.display(); cout << endl << "remove 5.1:" << endl; list.remove(5.1); list.display(); cout << endl << "remove 8.2:" << endl; list.remove(8.2); list.display(); Output: remove 5.1: remove 8.2: remove 2.5: 7.9 } cout << endl << "remove 2.5:" << endl; list.remove(2.5); list.display(); 35
36 Operation: Insert Number Into List Create a new node with the number to insert, find its position in the list, link the node into the list Requires two pointers: One [curr] to find and point to the node after the insertion point (This one is optional - why?) One [prev] to point to the node before the insertion point prev curr head newnode
37 Determining the Insertion Point Depending on our particular linked list implementation, we can determine the insertion point... By position: insert() could take a second argument specifying an index to insert after (or before) By cursor: the NumberList class could have a member variable that is a pointer to the "current node", always insert new value after (or before) the current node By maintaining a sorted list: the insertion point is always immediately before the first node in the list with a value greater than the value being inserted If this one, we may want to re-consider having an append() function as well, since it allows building an out-of-order list We assume the list is sorted! 37
38 Insert Node Into List First step: Move prev/curr to either side of insertion point. (With curr, skip all nodes less than new node's value) prev curr head newnode
39 Insert Node Into List First step: Move prev/curr to either side of insertion point. (With curr, skip all nodes less than new node's value) prev curr head newnode
40 Insert Node Into List First step: Move prev/curr to either side of insertion point. (With curr, skip all nodes less than new node's value) prev curr head newnode
41 Insert Node Into List First step: Move prev/curr to either side of insertion point. (With curr, skip all nodes less than new node's value) Then: link new node into list between prev and curr prev curr head newnode
42 Insert Node Into List First step: Move prev/curr to either side of insertion point. (With curr, skip all nodes less than new node's value) Then: link new node into list between prev and curr prev->next = newnode; newnode->next = curr; prev curr head newnode
43 Insert Node Into List First step: Move prev/curr to either side of insertion point. (With curr, skip all nodes less than new node's value) Then: link new node into list between prev and curr prev->next = newnode; newnode->next = curr; prev curr head newnode
44 Insert Node Into List First step: Move prev/curr to either side of insertion point. (With curr, skip all nodes less than new node's value) Then: link new node into list between prev and curr prev->next = newnode; newnode->next = curr; prev curr head newnode
45 Insert Node Into List First step: Move prev/curr to either side of insertion point. (With curr, skip all nodes less than new node's value) Then: link new node into list between prev and curr prev->next = newnode; newnode->next = curr; prev curr head
46 insert() in C++ void NumberList::insert(double num) { ListNode *curr = head; // traversal pointer ListNode *prev = ; // trailing pointer // create new node ListNode *newnode = new ListNode; newnode->value = num; // find the insertion point (curr points at node after) while(curr && curr->value < num) { prev = curr; curr = curr->next; } } // new node should be new head of list if(curr == head) head = newnode; // new node in middle/end of list else prev->next = newnode; newnode->next = curr; What if the new node should be the new last node? 46
47 insert() Variations if(curr == head) head = newnode; else prev->next = newnode; newnode->next = curr; if(!prev) head = newnode; else prev->next = newnode; newnode->next = curr; if(curr == head) { newnode->next = curr; head = newnode; } else { newnode->next = curr; prev->next = newnode; } newnode->next = curr; if(curr == head) head = newnode; else prev->next = newnode; Once again, there are lots of minor variations in how we can code this. We could also get rid of the curr pointer entirely, because we can always get to the node after prev using prev->next Learn the algorithm; you can always re-derive the code! 47
48 Demo Driver for NumberList #include "NumberList.h" using namespace std; ListDriver.cpp int main() { // Set up the list NumberList list; list.append(2.5); list.append(5.1); list.append(7.9); list.display(); list.insert(3.7); list.display(); list.insert(1.2); list.display(); Output: } list.insert(9.6); list.display(); 48
49 Why An Array Instead of a Linked List? Arrays allow random access to elements, i.e. array[i] Linked lists allow only sequential access to elements (you must traverse the list to get to the i'th element) Arrays do not require extra storage for the next link Linked lists are impractical for lists of chars or bools (pointer value is bigger than the data value!) 49
50 Why a Linked List Instead of An Array? A linked list can easily grow or shrink in size Nodes are created in memory as they are needed The programmer doesn't have to know or guess up front how many elements will ultimately be in the list The amount of memory to store a linked list is always proportional to the number of elements in the list For arrays, the amount of memory allocated is often much more than required by the actual count of elements in the list (partial arrays) When a node is inserted into or deleted from a linked list, none of the other nodes have to be moved in memory 50
51 Linked List Variations Circular Linked List The last node in the list points to the head node rather than to head Doubly Linked List Each node keeps a pointer to the previous node in the list as well as to the next node head Double-Ended Linked List Two pointers, head and tail, allow constant-time insertion to both ends of list 51
52 Ideas for More Linked List Practice Similar to NumberList... Create a linked list class for Student structs (sorted by ID) Create a linked list class for Time objects (sorted by time) Add to NumberList... int count() Return the number of nodes in the list double findmaximum() or double findminimum() Return the maximum/minimum value in the list double findnumberatindex(int index) Return the value in the index'th node in the list All of these are fair game void prepend(double num) Add num to the front of the list for an exam, double popfront() and they tend to show up Remove the first node from the list and return its value in coding interviews, too! double popback() Remove the last node from the list and return its value void insertatindex(double num, int index) Insert num into the index'th position in the list void reverse() Reverse the order of the list, in place void removeduplicates() Remove all duplicate values from the list (without re-ordering the remaining elements of the list) NumberList mergewithsortedlist(numberlist other) Return a new list containing the values in the list the function is called on (*this) and the other list, both of which are assumed to be sorted, and preserving the sorted order of the output list 52
! A data structure representing a list. ! A series of nodes chained together in sequence. ! A separate pointer (the head) points to the first
Ch. 17: Linked Lists 17.1 Introduction to Linked Lists! A data structure representing a list! A series of nodes chained together in sequence CS 2308 Spring 2013 Jill Seaman - Each node points to one other
More information! A data structure representing a list. ! A series of dynamically allocated nodes. ! A separate pointer (the head) points to the first
Linked Lists Introduction to Linked Lists A data structure representing a Week 8 Gaddis: Chapter 17 CS 5301 Spring 2014 Jill Seaman A series of dynamically allocated nodes chained together in sequence
More informationCh. 17: Linked Lists. Introduction to Linked Lists
Ch. 17: Linked Lists Part 1 CS 2308 Fall 2011 Jill Seaman Lecture 16 Using content from textbook slides: Starting Out with C++, Gaddis, Pearson/Addison-Wesley 1 Introduction to Linked Lists A data structure
More informationLinked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list
Linked Lists Introduction : Data can be organized and processed sequentially using an array, called a sequential list Problems with an array Array size is fixed Unsorted array: searching for an item is
More informationLinked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list
Linked Lists Introduction : Data can be organized and processed sequentially using an array, called a sequential list Problems with an array Array size is fixed Unsorted array: searching for an item is
More information! A data structure representing a list. ! A series of nodes chained together in sequence. ! A separate pointer (the head) points to the first
Ch. 17: Linked Lists 17.1 Introduction to Linked Lists! A data structure reresenting a list! A series of nodes chained together in sequence CS 2308 Sring 2015 Jill Seaman - Each node oints to one other
More informationChapter 17: Linked Lists
Chapter 17: Linked Lists 17.1 Introduction to the Linked List ADT Introduction to the Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures list head
More informationChapter 17: Linked Lists
Chapter 17: Linked Lists Copyright 2009 Pearson Education, Inc. Copyright Publishing as Pearson 2009 Pearson Addison-Wesley Education, Inc. Publishing as Pearson Addison-Wesley 17.1 Introduction to the
More information! A data type for which: ! An ADT may be implemented using various. ! Examples:
List ADT: Linked lists vs. Arrays CS 2308 Fall 2018 Jill Seaman Abstract Data Type! A data type for which: - only the properties of the data and the operations to be performed on the data are specific,
More informationLinked List using a Sentinel
Linked List using a Sentinel Linked List.h / Linked List.h Using a sentinel for search Created by Enoch Hwang on 2/1/10. Copyright 2010 La Sierra University. All rights reserved. / #include
More informationDynamic Data Structures
Dynamic Data Structures We have seen that the STL containers vector, deque, list, set and map can grow and shrink dynamically. We now examine how some of these containers can be implemented in C++. To
More informationl Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)
Final Exam Exercises Chapters 1-7 + 11 Write C++ code to: l Determine if a number is odd or even CS 2308 Fall 2016 Jill Seaman l Determine if a number/character is in a range - 1 to 10 (inclusive) - between
More informationCSCE 110 PROGRAMMING FUNDAMENTALS
CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 16. Linked Lists Prof. amr Goneid, AUC 1 Linked Lists Prof. amr Goneid, AUC 2 Linked Lists The Linked List Structure Some Linked List
More information1 Short Answer (7 Points Each)
1 Short Answer (7 Points Each) 1. Given the following function, what operations will need to be overloaded in the class T for this code to compile? template T square(t n) { return n * n; } The
More information! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)
Final Exam Exercises Chapters 1-7 + 11 Write C++ code to:! Determine if a number is odd or even CS 2308 Fall 2018 Jill Seaman! Determine if a number/character is in a range - 1 to 10 (inclusive) - between
More information! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)
Final Exam Exercises CS 2308 Spring 2014 Jill Seaman Chapters 1-7 + 11 Write C++ code to: Determine if a number is odd or even Determine if a number/character is in a range - 1 to 10 (inclusive) - between
More informationC++ is Fun Part 14 at Turbine/Warner Bros.! Russell Hanson
C++ is Fun Part 14 at Turbine/Warner Bros.! Russell Hanson Syllabus 1) First program and introduction to data types and control structures with applications for games learning how to use the programming
More informationCS32 - Week 2. Umut Oztok. July 1, Umut Oztok CS32 - Week 2
CS32 - Week 2 Umut Oztok July 1, 2016 Arrays A basic data structure (commonly used). Organize data in a sequential way. Arrays A basic data structure (commonly used). Organize data in a sequential way.
More informationLists. linking nodes. constructors. chasing pointers. MCS 360 Lecture 11 Introduction to Data Structures Jan Verschelde, 17 September 2010.
1 2 3 MCS 360 Lecture 11 Introduction to Data Structures Jan Verschelde, 17 September 2010 1 2 3 efficient updates with lists At http://www.sgi.com/tech/stl/ is the Standard Template Library Programmer
More information! Determine if a number is odd or even. ! Determine if a number/character is in a range. ! Assign a category based on ranges (wind speed)
Final Exam Exercises Chapters 1-7 + 11 Write C++ code to:! Determine if a number is odd or even CS 2308 Spring 2013 Jill Seaman! Determine if a number/character is in a range - 1 to 10 (inclusive) - between
More informationCMSC 341 Lecture 7 Lists
CMSC 341 Lecture 7 Lists Today s Topics Linked Lists vs Arrays Nodes Using Linked Lists Supporting Actors (member variables) Overview Creation Traversal Deletion UMBC CMSC 341 Lists 2 Linked Lists vs Arrays
More informationCSCE 110 PROGRAMMING FUNDAMENTALS
CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class Prof. amr Goneid, AUC 1 Dictionaries(1): A Key Table Class Prof. Amr Goneid, AUC 2 A Key Table
More informationData Structures and Algorithms
Data Structures and Algorithms First Semester 2017/2018 Linked Lists Eng. Anis Nazer Linked List ADT Is a list of nodes Each node has: data (can be any thing, int, char, Person, Point, day,...) link to
More informationAshish Gupta, Data JUET, Guna
Categories of data structures Data structures are categories in two classes 1. Linear data structure: - organized into sequential fashion - elements are attached one after another - easy to implement because
More information(More) Fun with Pointers and Linked Lists! CS 16: Solving Problems with Computers I Lecture #17
(More) Fun with Pointers and Linked Lists! CS 16: Solving Problems with Computers I Lecture #17 Ziad Matni Dept. of Computer Science, UCSB Administrative Homework situation: Labs: NO MORE HOMEWORK! J Lab10
More informationStudent Name and ID CS 32, WINTER 2015, PRACTICE MIDTERM I.
UCLA Computer Science Department TA: Kung-Hua Chang Student Name and ID CS 32, WINTER 2015, PRACTICE MIDTERM I. Problem # Maximal Possible Points Received 1.1 3 1.2 5 1.3 5 1.4 5 1.5 5 2 3 3.1 4 3.2 5
More informationPointers: 2 Operators:
Pointers: 2 Operators: & (address of operator) : returns the memory address of the first byte in memory of a variable * (dereference operator) : dereferences a variable (what is in or value pointed to
More informationElementary Data Structures: Part 1: Arrays, Lists. CSE 2320 Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington
Elementary Data Structures: Part 1: Arrays, Lists CSE 2320 Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1 Basic Types Types like integers, real numbers, characters.
More informationSpring 2008 Data Structures (CS301) LAB
Spring 2008 Data Structures (CS301) LAB Objectives The objectives of this LAB are, o Enabling students to implement Singly Linked List practically using c++ and adding more functionality in it. o Enabling
More informationThe combination of pointers, structs, and dynamic memory allocation allow for creation of data structures
Data Structures in C C Programming and Software Tools N.C. State Department of Computer Science Data Structures in C The combination of pointers, structs, and dynamic memory allocation allow for creation
More informationData Structures (CS301) LAB
Data Structures (CS301) LAB Objectives The objectives of this LAB are, o Enabling students to implement Doubly Linked List practically using c++ and adding more functionality in it. Introduction to Singly
More informationElementary Data Structures: Lists
Elementary Data Structures: Lists CSE 2320 Algorithms and Data Structures Based on presentation by Vassilis Athitsos University of Texas at Arlington 1 Pointers What to Review Pointers and Memory : http://cslibrary.stanford.edu/102/pointersandmemory.pdf
More informationDoubly-Linked Lists
Doubly-Linked Lists 4-02-2013 Doubly-linked list Implementation of List ListIterator Reading: Maciel, Chapter 13 HW#4 due: Wednesday, 4/03 (new due date) Quiz on Thursday, 4/04, on nodes & pointers Review
More informationCS 103 Unit 11. Linked Lists. Mark Redekopp
1 CS 103 Unit 11 Linked Lists Mark Redekopp 2 NULL Pointer Just like there was a null character in ASCII = '\0' whose ue was 0 There is a NULL pointer whose ue is 0 NULL is "keyword" you can use in C/C++
More informationCS 103 Unit 15. Doubly-Linked Lists and Deques. Mark Redekopp
1 CS 103 Unit 15 Doubly-Linked Lists and Deques Mark Redekopp 2 Singly-Linked List Review Used structures/classes and pointers to make linked data structures Singly-Linked Lists dynamically allocates each
More informationUniversity of Illinois at Urbana-Champaign Department of Computer Science. First Examination
University of Illinois at Urbana-Champaign Department of Computer Science First Examination CS 225 Data Structures and Software Principles Spring 2007 7p-9p, Thursday, March 1 Name: NetID: Lab Section
More informationUniversity of Illinois at Urbana-Champaign Department of Computer Science. First Examination
University of Illinois at Urbana-Champaign Department of Computer Science First Examination CS 225 Data Structures and Software Principles Spring 2007 7p-9p, Thursday, March 1 Name: NetID: Lab Section
More informationA linked list. Subtopic: Linked lists. List-node structure type. Array implementation of a linked list. Inserting into a sorted collection
Subtopic: Linked lists A linear structure, like an array Composed of self-referential nodes Operations: insert, delete, traverse Array implementation Dynamic-allocation implementation David Keil 1/03 1
More informationCS 61B, Spring 1996 Midterm #1 Professor M. Clancy
CS 61B, Spring 1996 Midterm #1 Professor M. Clancy Problem 0 (1 point, 1 minute) Put your login name on each page. Also make sure you have provided the information requested on the first page. Problem
More informationMore About Classes. Gaddis Ch. 14, CS 2308 :: Fall 2015 Molly O'Neil
More About Classes Gaddis Ch. 14, 13.3 CS 2308 :: Fall 2015 Molly O'Neil Pointers to Objects Just like pointers to structures, we can define pointers to objects Time t1(12, 20, true); Time *tptr; tptr
More informationCSCE 2014 Final Exam Spring Version A
CSCE 2014 Final Exam Spring 2017 Version A Student Name: Student UAID: Instructions: This is a two-hour exam. Students are allowed one 8.5 by 11 page of study notes. Calculators, cell phones and computers
More informationLinked Lists. Chapter 4
Linked Lists Chapter 4 1 Linked List : Definition Linked List: A collection of data items of the same type that are stored in separate objects referred to as "nodes". Each node contains, in addition to
More informationProgramming. Lists, Stacks, Queues
Programming Lists, Stacks, Queues Summary Linked lists Create and insert elements Iterate over all elements of the list Remove elements Doubly Linked Lists Circular Linked Lists Stacks Operations and implementation
More informationSpecial Member Functions. Compiler-Generated Destructor. Compiler-Generated Default Constructor. Special Member Functions
Special Member Functions CS 247: Software Engineering Principles Special Member Functions Readings: Eckel, Vol. 1 Ch. 11 References and the Copy Constructor Ch. 12 Operator Overloading ( operator= ) C++
More informationCS 103 Unit 11. Linked Lists. Mark Redekopp
1 CS 103 Unit 11 Linked Lists Mark Redekopp 2 NULL Pointer Just like there was a null character in ASCII = '\0' whose ue was 0 There is a NULL pointer whose ue is 0 NULL is "keyword" you can use in C/C++
More informationCMPT 225. Lecture 6 linked list
CMPT 225 Lecture 6 linked list 1 Last Lecture Class documentation Linked lists and its operations 2 Learning Outcomes At the end of this lecture, a student will be able to: define one of the concrete data
More informationReview Questions for Final Exam
CS 102 / ECE 206 Spring 11 Review Questions for Final Exam The following review questions are similar to the kinds of questions you will be expected to answer on the Final Exam, which will cover LCR, chs.
More informationJordan University of Science & Technology Department of Computer Science CS 211 Exam #1 (23/10/2010) -- Form A
Jordan University of Science & Technology Department of Computer Science CS 211 Exam #1 (23/10/2010) -- Form A Name: ID#: Section #: Day & Time: Instructor: Answer all questions as indicated. Closed book/closed
More informationWhat is recursion? Recursion. Recursive message() modified. How can a function call itself? contains a reference to itself. Week 10. Gaddis:
Recursion What is recursion? Week 10 Gaddis:19.1-19.5 CS 5301 Spring 2017 Jill Seaman 1 l Generally, when something contains a reference to itself l Math: defining a function in terms of itself l Computer
More informationCA341 - Comparative Programming Languages
CA341 - Comparative Programming Languages David Sinclair Dynamic Data Structures Generally we do not know how much data a program will have to process. There are 2 ways to handle this: Create a fixed data
More information! A data type for which: ! An ADT may be implemented using various. ! Examples:
Stacks and Queues Unit 6 Chapter 19.1-2,4-5 CS 2308 Fall 2018 Jill Seaman 1 Abstract Data Type A data type for which: - only the properties of the data and the operations to be performed on the data are
More informationComS 228 Exam 1. September 27, 2004
ComS 228 Exam 1 September 27, 2004 Name: University ID: Section: (10 percent penalty if incorrect) This is a one-hour, closed-book, closed-notes, closed-calculator exam. The exam consists of 9 pages (including
More informationCpSc212 Goddard Notes Chapter 10. Linked Lists
CpSc212 Goddard Notes Chapter 10 Linked Lists 10.1 Links and Pointers The linked list is not an ADT in its own right; rather it is a way of implementing many data structures. It is designed to replace
More informationLab 2: Pointers. //declare a pointer variable ptr1 pointing to x. //change the value of x to 10 through ptr1
Lab 2: Pointers 1. Goals Further understanding of pointer variables Passing parameters to functions by address (pointers) and by references Creating and using dynamic arrays Combing pointers, structures
More informationLinked Lists CS 16: Solving Problems with Computers I Lecture #16
Linked Lists CS 16: Solving Problems with Computers I Lecture #16 Ziad Matni Dept. of Computer Science, UCSB Material: Everything we ve done Homework, Labs, Lectures, Textbook Tuesday, 12/12 in this classroom
More informationMidterm Exam #2 Review. CS 2308 :: Spring 2016 Molly O'Neil
Midterm Exam #2 Review CS 2308 :: Spring 2016 Molly O'Neil Midterm Exam #2 Wednesday, April 13 In class, pencil & paper exam Closed book, closed notes, no cell phones or calculators, clean desk 20% of
More informationCS 61B, Spring 1996 Midterm #1 Professor M. Clancy
CS 61B, Spring 1996 Midterm #1 Professor M. Clancy Problem 0 (1 point, 1 minute) Put your login name on each page. Also make sure you have provided the information requested on the first page. Problem
More informationIntroduction to Computer Science Midterm 3 Fall, Points
Introduction to Computer Science Fall, 2001 100 Points Notes 1. Tear off this sheet and use it to keep your answers covered at all times. 2. Turn the exam over and write your name next to the staple. Do
More informationlecture09: Linked Lists
lecture09: Largely based on slides by Cinda Heeren CS 225 UIUC 24th June, 2013 Announcements mp2 due tonight mt1 tomorrow night! mt1 review instead of lab tomorrow morning mp3 released tonight, mp3.1 extra
More informationSpecial Member Functions
CS 247: Software Engineering Principles Special Member Functions Readings: Eckel, Vol. 1 Ch. 11 References and the Copy Constructor Ch. 12 Operator Overloading ( operator= ) U Waterloo CS247 (Spring 2017)
More informationIterators. node UML diagram implementing a double linked list the need for a deep copy. nested classes for iterator function objects
Iterators 1 Double Linked and Circular Lists node UML diagram implementing a double linked list the need for a deep copy 2 Iterators on List nested classes for iterator function objects MCS 360 Lecture
More informationFirst Examination. CS 225 Data Structures and Software Principles Spring p-9p, Tuesday, February 19
Department of Computer Science First Examination CS 225 Data Structures and Software Principles Spring 2008 7p-9p, Tuesday, February 19 Name: NetID: Lab Section (Day/Time): This is a closed book and closed
More information2. It is possible for a structure variable to be a member of another structure variable.
FORM 1(put name, form, and section number on scantron!!!) CS 162 Exam I True (A) / False (B) (2 pts) 1. What value will the function eof return if there are more characters to be read in the input stream?
More informationStudy Guide for Test 2
Study Guide for Test 2 Topics: decisions, loops, arrays, c-strings, linux Material Selected from: Chapters 4, 5, 6, 7, 10.1, 10.2, 10.3, 10.4 Examples 14 33 Assignments 4 8 Any syntax errors are unintentional
More informationCS32 Discussion Week 3
CS32 Discussion Week 3 Muhao Chen muhaochen@ucla.edu http://yellowstone.cs.ucla.edu/~muhao/ 1 Outline Doubly Linked List Sorted Linked List Reverse a Linked List 2 Doubly Linked List A linked list where
More informationFORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):
FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each): 1. If a function has default arguments, they can be located anywhere
More informationLists (Section 5) Lists, linked lists Implementation of lists in C Other list structures List implementation of stacks, queues, priority queues
(Section 5) Lists, linked lists Implementation of lists in C Other list structures List implementation of stacks, queues, priority queues By: Pramod Parajuli, Department of Computer Science, St. Xavier
More informationLecture Notes CPSC 122 (Fall 2014) Today Quiz 7 Doubly Linked Lists (Unsorted) List ADT Assignments Program 8 and Reading 6 out S.
Today Quiz 7 Doubly Linked Lists (Unsorted) List ADT Assignments Program 8 and Reading 6 out S. Bowers 1 of 11 Doubly Linked Lists Each node has both a next and a prev pointer head \ v1 v2 v3 \ tail struct
More informationSTL: C++ Standard Library
STL: C++ Standard Library Encapsulates complex data structures and algorithms CSC 330 OO Software Design 1 We ve emphasized the importance of software reuse. Recognizing that many data structures and algorithms
More informationUniversity of Illinois at Urbana-Champaign Department of Computer Science. First Examination
University of Illinois at Urbana-Champaign Department of Computer Science First Examination CS 225 Data Structures and Software Principles Sample Exam 2 75 minutes permitted Print your name, netid, and
More informationArrays and Linked Lists
Arrays and Linked Lists Abstract Data Types Stacks Queues Priority Queues and Deques John Edgar 2 And Stacks Reverse Polish Notation (RPN) Also known as postfix notation A mathematical notation Where every
More informationEE 355 Unit 11b. Doubly-Linked Lists and Deques. Mark Redekopp
1 EE 355 Unit 11b Doubly-Linked Lists and Deques Mark Redekopp 2 Singly-Linked List Review Used structures/classes and pointers to make linked data structures Singly-Linked Lists dynamically allocates
More informationLECTURE 03 LINKED LIST
DATA STRUCTURES AND ALGORITHMS LECTURE 03 LINKED LIST IMRAN IHSAN ASSISTANT PROFESSOR AIR UNIVERSITY, ISLAMABAD LINKED LISTS DEFINITION A linked list is a data structure where each object is stored in
More informationAbstract Data Types (ADTs) 1. Legal Values. Client Code for Rational ADT. ADT Design. CS 247: Software Engineering Principles
Abstract Data Types (ADTs) CS 247: Software Engineering Principles ADT Design An abstract data type (ADT) is a user-defined type that bundles together: the range of values that variables of that type can
More informationAdvanced Linked Lists. Doubly Linked Lists Circular Linked Lists Multi- Linked Lists
Advanced Linked Lists Doubly Linked Lists Circular Linked Lists Multi- Linked Lists Review The singly linked list: consists of nodes linked in a single direction. access and traversals begin with the first
More informationImplementation. Learn how to implement the List interface Understand the efficiency trade-offs between the ArrayList and LinkedList implementations
Readings List Implementations Chapter 20.2 Objectives Learn how to implement the List interface Understand the efficiency trade-offs between the ArrayList and LinkedList implementations Additional references:
More informationWhat is recursion? Recursion. How can a function call itself? Recursive message() modified. Week 10. contains a reference to itself.
Recursion What is recursion? Week 10 Generally, when something contains a reference to itself Gaddis:19.1-19.5 CS 5301 Spring 2014 Jill Seaman 1 Math: defining a function in terms of itself Computer science:
More informationCS 247: Software Engineering Principles. ADT Design
CS 247: Software Engineering Principles ADT Design Readings: Eckel, Vol. 1 Ch. 7 Function Overloading & Default Arguments Ch. 12 Operator Overloading U Waterloo CS247 (Spring 2017) p.1/17 Abstract Data
More information! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge
Trees (& Heaps) Week 12 Gaddis: 20 Weiss: 21.1-3 CS 5301 Spring 2015 Jill Seaman 1 Tree: non-recursive definition! Tree: set of nodes and directed edges - root: one node is distinguished as the root -
More informationLecture 7. Log into Linux New documents posted to course webpage
Lecture 7 Log into Linux New documents posted to course webpage Coding style guideline; part of project grade is following this Homework 4, due on Monday; this is a written assignment Project 1, due next
More informationCS Data Structure Spring Answer Key- Assignment #3
CS300-201 Data Structure Spring 2012 2013 Answer Key- Assignment #3 Due Sunday, Mar 3 rd. Q1): Find Big-O for binary search algorithm, show your steps. Solution 1- The task is to search for a given value
More informationData Structure (CS301)
WWW.VUPages.com http://forum.vupages.com WWW.VUTUBE.EDU.PK Largest Online Community of VU Students Virtual University Government of Pakistan Midterm Examination Spring 2003 Data Structure (CS301) StudentID/LoginID
More informationBinary Search Trees. Contents. Steven J. Zeil. July 11, Definition: Binary Search Trees The Binary Search Tree ADT...
Steven J. Zeil July 11, 2013 Contents 1 Definition: Binary Search Trees 2 1.1 The Binary Search Tree ADT.................................................... 3 2 Implementing Binary Search Trees 7 2.1 Searching
More informationl Operators such as =, +, <, can be defined to l The function names are operator followed by the l Otherwise they are like normal member functions:
Operator Overloading & Templates Week 6 Gaddis: 14.5, 16.2-16.4 CS 5301 Spring 2018 Jill Seaman Operator Overloading l Operators such as =, +,
More informationPointers. Addresses in Memory. Exam 1 on July 18, :00-11:40am
Exam 1 on July 18, 2005 10:00-11:40am Pointers Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location. This is the
More informationC++ - Lesson 2 This is a function prototype. a' is a function that takes an integer array argument and returns an integer pointer.
C++ - Lesson 2 1. Explain the following declarations: a) int *a(int a[]); This is a function prototype. 'a' is a function that takes an integer array argument and returns an integer pointer. b) const char
More informationPurpose of Review. Review some basic C++ Familiarize us with Weiss s style Introduce specific constructs useful for implementing data structures
C++ Review 1 Purpose of Review Review some basic C++ Familiarize us with Weiss s style Introduce specific constructs useful for implementing data structures 2 Class The Class defines the data structure
More informationData Structures. Alice E. Fischer. Lecture 4, Fall Alice E. Fischer Data Structures L4... 1/19 Lecture 4, Fall / 19
Data Structures Alice E. Fischer Lecture 4, Fall 2018 Alice E. Fischer Data Structures L4... 1/19 Lecture 4, Fall 2018 1 / 19 Outline 1 Ordered Lists 2 Sorted Lists Tail Pointers 3 Doubly Linked Lists
More informationdouble d0, d1, d2, d3; double * dp = new double[4]; double da[4];
All multiple choice questions are equally weighted. You can generally assume that code shown in the questions is intended to be syntactically correct, unless something in the question or one of the answers
More informationPatterns: Working with Arrays
Steven Zeil October 14, 2013 Outline 1 Static & Dynamic Allocation Static Allocation Dynamic Allocation 2 Partially Filled Arrays Adding Elements Searching for Elements Removing Elements 3 Arrays and Templates
More informationSolution for Data Structure
Solution for Data Structure May 2016 INDEX Q1 a 2-3 b 4 c. 4-6 d 7 Q2- a 8-12 b 12-14 Q3 a 15-18 b 18-22 Q4- a 22-35 B..N.A Q5 a 36-38 b N.A Q6- a 39-42 b 43 1 www.brainheaters.in Q1) Ans: (a) Define ADT
More informationMULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR
STUDENT IDENTIFICATION NO MULTIMEDIA COLLEGE JALAN GURNEY KIRI 54100 KUALA LUMPUR FIFTH SEMESTER FINAL EXAMINATION, 2014/2015 SESSION PSD2023 ALGORITHM & DATA STRUCTURE DSEW-E-F-2/13 25 MAY 2015 9.00 AM
More informationQueue Implementations
Queue Implementations 1 Circular Queues buffer of fixed capacity improvements and cost estimates 2 Deques the double ended queue queue as double linked circular list MCS 360 Lecture 17 Introduction to
More informationObject Oriented Programming COP3330 / CGS5409
Object Oriented Programming COP3330 / CGS5409 Intro to Data Structures Vectors Linked Lists Queues Stacks C++ has some built-in methods of storing compound data in useful ways, like arrays and structs.
More information04-19 Discussion Notes
04-19 Discussion Notes PIC 10B Spring 2018 1 Constructors and Destructors 1.1 Copy Constructor The copy constructor should copy data. However, it s not this simple, and we need to make a distinction here
More informationCS 106X Sample Final Exam #2
CS 106X Sample Final Exam #2 This sample exam is intended to demonstrate an example of some of the kinds of problems that will be asked on the actual final exam. We do not guarantee that the number of
More informationDo not write in this area A.1 A.2 A.3 A.4 A.5 A.6 B.1 B.2 B.3 TOTAL. Maximum possible points:
Name: Student ID: Instructor: Borja Sotomayor Do not write in this area A.1 A.2 A.3 A.4 A.5 A.6 B.1 B.2 B.3 TOTAL Maximum possible points: 75 + 10 This homework has two parts: Part A (Polynomial ADT) and
More informationProgramming II (CS300)
1 Programming II (CS300) Chapter 07: Linked Lists MOUNA KACEM mouna@cs.wisc.edu Spring 2019 Linked Lists 2 Introduction Linked List Abstract Data Type SinglyLinkedList ArrayList Keep in Mind Introduction:
More informationCPSC 260 Data Structures and Algorithms for Computer Engineers Linked Lists!
CPSC 260 Data Structures and Algorithms for Computer Engineers Linked Lists! Winter 2013 Instructor: Hassan Khosravi Problems with Arrays and Vectors With arrays and vectors you are allocated a large space.
More informationSection Handout #4: Classes, Pointers, and Dynamic Memory
Nick Troccoli Section #4 CS 106X Week 5 Section Handout #4: Classes, Pointers, and Dynamic Memory Based on handouts by various current and past CS106B/X instructors and TAs. Extra practice problems: CodeStepByStep
More information