Linked Lists. Contents. Steven J. Zeil. July 31, Linked Lists: the Basics 4
|
|
- Randell Montgomery
- 5 years ago
- Views:
Transcription
1 Steven J. Zeil July 31, 2013 Contents 1 Linked Lists: the Basics 4 1
2 2 Coding for Linked Lists Traversing a Linked List Searching a Linked List Adding to a Linked List addinorder addafter Removing from a Linked List remove Copying and Clean-up Variations: Headers with First and Last First-Last Header Algorithms Variations: Doubly-Linked Lists 106 CS333 2
3 4.1 Doubliy-Linked List Algorithms CS333 3
4 1 Linked Lists: the Basics Sequences: arrays Consider the abstraction: a sequence of elements CS333 4
5 Main/Subpr Memory Heap Arrays (and vectors) work by storing elements of a sequence contiguously in memory Easy to access elements by number array Allen Baker Jones Smith Inserting things into the middle is slow and awkward Sequences: Linked Lists CS333 5
6 Main/Subpr Memory Heap Head Current Prev Baker Allen Jones Smith Linked lists store each element in a distinct node. Nodes are linked by pointers. Accessing elements by number is slow and awkward Easy to insert things into the middle Linked List Nodes CS333 6
7 A linked list node: struct LinkedListNode DataType data ; LinkedListNode * next ; ; LinkedListNode * firstnode ; A linked list consists of a number of nodes. Each node provides a data field and a next pointer Basic List Operations CS333 7
8 We traverse move from node to node by tracing the pointers. We insert new nodes by working from the node prior to the insertion point. We remove nodes by moving the previous pointer "around" the unwanted node Coding for Linked Lists The Data Types We need two data types to build a linked list: The nodes CS333 8
9 struct LListNode Data data ; LListNode<Data>* next ; LListNode ( ) next = 0 ; LListNode ( const Data& d, LListNode<Data>* nxt = 0) : data (d ), next ( nxt ) ; I m doing this as a template so it can be easily re-used CS333 9
10 The actual kind of data stored is not particularly important And the header struct LListHeader LListNode<Data>* f i r s t ; LListHeader ( ) ;. ; provides the starting point for the list CS333 10
11 I am treating these as utilities, not full-fledged ADTs We would use these to build "proper" ADTs like BidCollection So I m not going to be as picky about encapsulation and providing operators as I would usually be Example: Using the LListHeader c l ass BidderCollection int s i z e ; LListHeader <Bidder> l i s t ; public : CS333 11
12 typedef LListNode<Bidder >* Position ; / * * * Create a c o l l e c t i o n capable of holding any number of items * / BidderCollection ( ) ; In the sections that follow, we will look both at how we would use the linked list (in BidderCollection and BidCollection) and how we would implement it. 2.1 Traversing a Linked List Traversing a Linked List CS333 12
13 c l ass BidderCollection. / / Print the c o l l e c t i o n void print ( std : : ostream& out ) const ; / / Comparison operators bool operator== ( const BidderCollection &) const ; bool operator< ( const BidderCollection &) const ; ; All of these require us to walk the list, one node at a time. We use a pointer to a node to denote our current position We advance to the next position by getting the link field out of that node CS333 13
14 Printing a collection - while loop / / Print the c o l l e c t i o n void BidderCollection : : print ( std : : ostream& out ) const out << s i z e << " " ; Position current = l i s t. f i r s t ; while ( current! = NULL) out << " " ; current >data. print ( out ) ; out << "\n" ; current = current >next ; out << " " ; CS333 14
15 Printing a collection - for loop / / Print the c o l l e c t i o n void BidderCollection : : print ( std : : ostream& out ) const out << s i z e << " " ; for ( Position current = l i s t. f i r s t ; current! = NULL; current = current >next ) out << " " ; current >data. print ( out ) ; out << "\n" ; CS333 15
16 out << " " ; Searching a Linked List Searching a Linked List To support: c l ass BidderCollection. Position findbidder ( std : : s t r i n g name) const ; / / Returns the position where a bidder matching / / the given name can be found, or null i f no CS333 16
17 / / bidder with that name e x i s t s. we do a traversal but stop early if we find what we are looking for findbidder / * * * Find the index of the bidder with the given name. I f no such bidder e x i s t s, * return null. * / BidderCollection : : Position BidderCollection : : findbidder ( std : : s t r i n g name) const for ( Position current = l i s t. f i r s t ; current!= NULL; current = current >next ) CS333 17
18 i f (name == current >data. getname ( ) ) return current ; return NULL; Searching Linked Lists As a general utility, we might try to provide: struct LListHeader LListNode<Data>* f i r s t ; CS333 18
19 . / / Search f o r a value. Returns null i f not found LListNode<Data>* find ( const Data& value ) const ; / / Search an ordered l i s t f o r a value. Returns null i f not found LListNode<Data>* findordered ( const Data& value ) const ; Searching Unordered Linked Lists / / Search f o r a value. Returns null i f not found LListNode<Data>* LListHeader <Data > : : find ( const Data& value ) const CS333 19
20 LListNode<Data>* current = f i r s t ; while ( current! = NULL && value! = current >data ) current = current >l i n k ; return current ; Searching Ordered Linked Lists / / Search an ordered l i s t f o r a value. Returns null i f not found LListNode<Data>* LListHeader <Data > : : findordered ( const Data& value ) const CS333 20
21 LListNode<Data>* current = f i r s t ; while ( current! = NULL && value > current >data ) current = current >l i n k ; i f ( current! = NULL && value == current >data ) return current ; else return NULL; findbidder (alternate) / * * * Find the index of the bidder with the given name. I f no such bidder e x i s t s, * return null. CS333 21
22 * / BidderCollection : : Position BidderCollection : : findbidder ( std : : s t r i n g name) const Bidder searchfor (name, 0. 0 ) ; return l i s t. find ( searchfor ) ; (works only because bool Bidder : : operator== ( const Bidder& b) const return name == b. name; CS333 22
23 bool Bidder : : operator< ( const Bidder& b) const i f (name < b.name) return true ; else return f a l s e ; compare only by name). Walking Two Lists at Once Although not really a search, the relational operator code is similar, in that we do a traversal with a possible early exit. But now we have to walk two lists: // Comparison operators CS333 23
24 bool BidderCollection::operator== (const BidderCollection& bc) const if (size == bc.size) Position current = list.first; Position bcurrent = bc.list.first; while (current!= NULL) if (!(current->data == bcurrent->data)) return false; current = current->next; bcurrent = bcurrent->next; return true; CS333 24
25 else return false; Adding to a Linked List Adding to a Linked List void BidderCollection : : add ( const Bidder& value ) could be implemented as void BidderCollection : : add ( const Bidder& value ) / / Adds t h i s bidder CS333 25
26 / / Pre : getsize ( ) < getmaxsize ( ) l i s t. addtoend ( value ) ; ++ s i z e ; add functions struct LListHeader LListNode<Data>* f i r s t ; CS333 26
27 LListHeader ( ) ; void addtofront ( const Data& value ) ; void addtoend ( const Data& value ) ; / / Add value in sorted order. / / Pre : a l l e x i s t i n g values are already ordered void addinorder ( const Data& value ) ;. Let s look at how to do addtoend first addtoend CS333 27
28 void LListHeader<Data>::addToEnd (const Data& value) LListNode<Data>* newnode = new LListNode<Data>(value, NULL); if (first == NULL) first = newnode; else // Move to last node LListNode<Data>* current = first; while (current->next!= NULL) current = current->next; CS333 28
29 // Link after that node current->next = newnode; addtoend Example CS333 29
30 list: first: E.g. Suppose we have a list containing "Adams", "Baker", & "Davis", and we are going to add "Chen". We create a new node. Adams Baker Davis newnode: Chen void LListHeader <Data > : : addtoend ( const Data& value ) LListNode<Data>* newnode = new LListNode<Data >( value, NULL) ; CS333 30
31 If the list is not empty, we will have to find a pointer to the last node currently in the list.. i f ( f i r s t == NULL) f i r s t = newnode; else / / Move to l a s t node. LListNode<Data>* current = f i r s t ; list: Adams current: newnode: first: Baker Davis Chen CS333 31
32 list: first:. / / Move to l a s t node LListNode<Data>* current = f i r s t ; while ( current >next! = NULL) current = current >next ;. Eventually we find it. Adams current: newnode: Baker Davis Chen CS333 32
33 and then link in the new node.. while ( current >next! = NULL) current = current >next ; list: Adams first: Davis / / Link a f t e r that node current >next = newnode; current: newnode: Baker Chen addinorder addinorder CS333 33
34 void LListHeader<Data>::addInOrder (const Data& value) if (first == NULL) first = new LListNode<Data>(value, NULL); else LListNode<Data>* current = first; LListNode<Data>* prev = NULL; while (current!= NULL && value > current->data) prev = current; current = current->next; // Add between prev and current CS333 34
35 if (prev == NULL) addtofront (value); else addafter (prev, value); addinorder Example Again, suppose we have a list containing "Adams", "Baker", & "Davis", and we are going to add "Chen". void LListHeader <Data > : : addinorder ( const Data& value ) CS333 35
36 i f ( f i r s t == NULL) f i r s t = new LListNode<Data >( value, NULL) ; else LListNode<Data>* current = f i r s t ; LListNode<Data>* prev = NULL;. CS333 36
37 list: first: Start by setting up two position pointers. Adams Davis prev will point one position in front of current. current: Baker prev: CS333 37
38 list: first: Adams Baker Davis Move forward till prev and current bracket the place where we want to insert the new data. current: prev:. while ( current! = NULL && value > current >data ) prev = current ; current = current >next ; CS333 38
39 CS333 39
40 Found it! list: first: Adams current: Baker Davis. // Add between prev and current if (prev == NULL) addtofront (value); else addafter (prev, value); prev: Now we just add the new data after the prev position. And that s one of the functions we need to implement anyway. CS333 40
41 addafter addafter list: first: Starting from here Adams Davis Baker afterthis: void LListHeader <Data > : : addafter ( LListNode<Data>* afterthis, const Data& value CS333 41
42 LListNode<Data>* newnode = new LListNode<Data >( value, afterthis >next ) ; afterthis >next = newnode; addafter 2 CS333 42
43 list: first: Create the new node: Adams Davis Baker afterthis: newnode: Chen void LListHeader <Data > : : addafter ( LListNode<Data>* afterthis, const Data& value LListNode<Data>* newnode = new LListNode<Data >( value, afterthis >next ) ; afterthis >next = newnode; CS333 43
44 Notice that it is created with its link field already pointing to the correct place. That s not magic we just passed the appropriate value to the node constructor addafter 3 void LListHeader <Data > : : addafter ( LListNode<Data>* afterthis, const Data& value LListNode<Data>* newnode = new LListNode<Data >( value, afterthis >next ) ; CS333 44
45 list: first: Then change the link from the afterthis node. Adams Davis Baker And we re done! afterthis: newnode: Chen 2.4 Removing from a Linked List Removing from a Linked List CS333 45
46 void LListHeader <Data > : : removeafter ( LListNode<Data>* afterthis ) LListNode<Data>* toremove = afterthis >next ; afterthis >next = toremove >next ; delete toremove ; list: first: Starting here. Adams Davis Baker afterthis: CS333 46
47 list: first: Get a pointer to the node we actually want to remove. Adams Baker Davis afterthis: toremove: CS333 47
48 list: first: Make the earlier node point around the one we want to remove. Adams Baker Davis afterthis: toremove: CS333 48
49 list: first: Then delete the unwanted node. Adams Baker Davis afterthis: toremove: remove remove CS333 49
50 void LListHeader <Data > : : remove ( LListNode<Data>* here ) i f ( here == f i r s t ) LListNode<Data>* a f t e r = f i r s t >next ; delete f i r s t ; f i r s t = a f t e r ; else LListNode<Data>* prev = f i r s t ; while ( prev >next! = here ) prev = prev >next ; prev >next = here >next ; CS333 50
51 delete here ; Basically removeafter preceded by a traversal Copying and Clean-up Copying BidderCollection : : BidderCollection ( const BidderCollection& bc ) : s i z e ( bc. s i z e ) l i s t. append( bc. l i s t ) ; CS333 51
52 BidderCollection& BidderCollection : : operator= ( const BidderCollection& bc ) i f ( t h i s!= &bc ) l i s t. clear ( ) ; s i z e = bc. s i z e ; l i s t. append( bc. l i s t ) ; return * this ; CS333 52
53 append // Add all values from another list onto the end of this one void LListHeader<Data>::append (const LListHeader<Data>& list) // Move to last node LListNode<Data>* last = first; while (last->next!= NULL) last = last->next; // Append new nodes onto end of list const LListNode<Data>* current = list.first; while (current!= NULL) CS333 53
54 LListNode<Data>* newnode = new LListNode<Data>(current->data, NULL); if (last!= NULL) last->next = newnode; last = newnode; current = current->next; void LListHeader<Data>::append (const LListHeader<Data>& list) // Move to last node LListNode<Data>* last = first; if (last!= NULL) CS333 54
55 while (last->next!= NULL) last = last->next; // Append new nodes onto end of list const LListNode<Data>* current = list.first; while (current!= NULL) LListNode<Data>* newnode = new LListNode<Data>(current->data, NULL); if (last!= NULL) last->next = newnode; else first = newnode; last = newnode; current = current->next; CS333 55
56 A traversal to the end of the current list, followed by repeated "addtoend" equivalents Collection Destructor BidderCollection : : ~ BidderCollection ( ) l i s t. clear ( ) ; CS333 56
57 clear void LListHeader <Data > : : clear ( ) LListNode<Data>* current = f i r s t ; LListNode<Data>* nxt = NULL; while ( current! = NULL) nxt = current >next ; delete current ; current = nxt ; f i r s t = NULL; CS333 57
58 Be careful when deleting Only "trick" here is that we cant do the usual current = current >next ; at the end of the loop, because we will have already deleted the node that contains next Example: The List-based Collection #ifndef BIDDERCOLLECTION_H #define BIDDERCOLLECTION_H #include "bidders.h" #include <iostream> CS333 58
59 #include "sllistutils.h" // // Bidders Registered for auction // class BidderCollection int size; LListHeader<Bidder> list; public: CS333 59
60 typedef LListNode<Bidder>* Position; /** * Create a collection capable of holding any number of items */ BidderCollection (); // Big 3 BidderCollection (const BidderCollection&); BidderCollection& operator= (const BidderCollection&); ~BidderCollection (); CS333 60
61 // Access to attributes int getsize() const return size; // Access to individual elements const Bidder& get(position index) const; Bidder& get(position index); Position getfirst() const; bool more (Position afterthis) const; Position getnext(position afterthis) const; CS333 61
62 // Collection operations void add (const Bidder& value); // Adds this bidder to the collection at an unspecified position. //Pre: getsize() < getmaxsize() void remove (Position); // Remove the item at the indicated position CS333 62
63 Position findbidder (std::string name) const; // Returns the position where a bidde mathcing the given // name can be found, or null if no bidder with that name exists. /** * Read all items from the indicated file */ void readbidders (std::string filename); // Print the collection void print (std::ostream& out) const; CS333 63
64 // Comparison operators bool operator== (const BidderCollection&) const; bool operator< (const BidderCollection&) const; ; inline std::ostream& operator<< (std::ostream& out, const BidderCollection& b) b.print(out); return out; CS333 64
65 #endif The above code makes use of some utility functions for singly-linked lists: #ifndef SLLISTUTILS_H #define SLLISTUTILS_H #include <cstdlib> // for NULL /** CS333 65
66 * Utility operations for singly linked lists * */ struct LListNode Data data; LListNode<Data>* next; LListNode() next = 0; CS333 66
67 LListNode (const Data& d, LListNode<Data>* nxt = 0) : data(d), next(nxt) ; struct LListHeader LListNode<Data>* first; LListHeader(); void addtofront (const Data& value); CS333 67
68 void addtoend (const Data& value); // Add value after the indicated position void addafter (LListNode<Data>* afterthis, const Data& value); // Add value before the indicated position void addbefore (LListNode<Data>* beforethis, const Data& value); // Add value in sorted order. //Pre: all existing values are already ordered void addinorder (const Data& value); // Remove value at the indicated position void remove (LListNode<Data>* here); CS333 68
69 // Add value after the indicated position void removeafter (LListNode<Data>* afterthis); // Search for a value. Returns null if not found LListNode<Data>* find (const Data& value) const; // Search an ordered list for a value. Returns null if not found LListNode<Data>* findordered (const Data& value) const; // Empty the list void clear(); CS333 69
70 ; LListHeader<Data>::LListHeader() : first(null) void LListHeader<Data>::addToFront (const Data& value) LListNode<Data>* newnode = new LListNode<Data>(value, first); CS333 70
71 first = newnode; void LListHeader<Data>::addToEnd (const Data& value) LListNode<Data>* newnode = new LListNode<Data>(value, NULL); if (first == NULL) first = newnode; else // Move to last node CS333 71
72 LListNode<Data>* current = first; while (current->next!= NULL) current = current->next; // Link after that node current->next = newnode; // Add value after the indicated position void LListHeader<Data>::addAfter (LListNode<Data>* afterthis, const Data& v LListNode<Data>* newnode = new LListNode<Data>(value, afterthis->next); CS333 72
73 afterthis->next = newnode; // Add value before the indicated position void LListHeader<Data>::addBefore (LListNode<Data>* beforethis, const Data& if (beforethis == first) addtofront (value); else // Move to front of beforethis LListNode<Data>* current = first; while (current->next!= beforethis) CS333 73
74 current = current->next; // Link after that node addafter (current, value); // Add value in sorted order. //Pre: all existing values are already ordered void LListHeader<Data>::addInOrder (const Data& value) if (first == NULL) first = new LListNode<Data>(value, NULL); CS333 74
75 else LListNode<Data>* current = first; LListNode<Data>* prev = NULL; while (current!= NULL && value < current->data) prev = current; current = current->next; // Add between prev and current if (prev == NULL) addtofront (value); else addafter (prev, value); CS333 75
76 // Add all values from another list onto the end of this one void LListHeader<Data>::append (const LListHeader<Data>& list) // Move to last node LListNode<Data>* last = first; while (last->next!= NULL) last = last->next; // Append new nodes onto end of list CS333 76
77 const LListNode<Data>* current = list.first; while (current!= NULL) LListNode<Data>* newnode = new LListNode<Data>(current->data, NULL); if (last!= NULL) last->next = newnode; last = newnode; // Remove value at the indicated position void LListHeader<Data>::remove (LListNode<Data>* here) CS333 77
78 if (here == first) LListNode<Data>* after = first->next; delete first; first = after; else LListNode<Data>* prev = first; while (prev->next!= here) prev = prev->next; prev->next = here->next; delete here; CS333 78
79 // Remove value after the indicated position void LListHeader<Data>::removeAfter (LListNode<Data>* afterthis) LListNode<Data>* toremove = afterthis->next; afterthis->next = toremove->next; delete toremove; CS333 79
80 // Search for a value. Returns null if not found LListNode<Data>* LListHeader<Data>::find (const Data& value) const LListNode<Data>* current = first; while (current!= NULL && value!= current->data) current = current->next; return current; // Search an ordered list for a value. Returns null if not found LListNode<Data>* LListHeader<Data>::findOrdered (const Data& value) const CS333 80
81 LListNode<Data>* current = first; while (current!= NULL && value < current->data) current = current->next; if (current!= NULL && value == current->data) return current; else return NULL; void LListHeader<Data>::clear() LListNode<Data>* current = first; LListNode<Data>* nxt = NULL; CS333 81
82 while (current!= NULL) nxt = current->next; delete current; current = nxt; first = NULL; #endif CS333 82
83 3 Variations: Headers with First and Last Adding on Either End Adding to (either) end of a list is very common, but compare the amount of work required: void LListHeader <Data > : : addtofront ( const Data& value ) LListNode<Data>* newnode = new LListNode<Data >( value, f i r s t ) ; f i r s t = newnode; void LListHeader <Data > : : addtoend ( const Data& value ) CS333 83
84 LListNode<Data>* newnode = new LListNode<Data >( value, NULL) ; i f ( f i r s t == NULL) f i r s t = newnode; else / / Move to l a s t node LListNode<Data>* current = f i r s t ; while ( current >next! = NULL) current = current >next ; / / Link a f t e r that node current >next = newnode; CS333 84
85 Adding a Last Pointer We can simplify by adding a second pointer in the header: CS333 85
86 struct LListHeader list: first: last: LListNode<Data>* first; LListNode<Data>* last; Adams Davis LListHeader(); Baker Look at the Change void LListHeader <Data > : : addtofront ( const Data& value ) F i r s t Last Header Algorith CS333 86
87 LListNode<Data>* newnode = new LListNode<Data >( value, f i r s t ) ; f i r s t = newnode; i f ( l a s t == NULL) l a s t = f i r s t ; void LListHeader <Data > : : addtoend ( const Data& value ) LListNode<Data>* newnode = new LListNode<Data >( value, NULL) ; i f ( l a s t == NULL) f i r s t = l a s t = newnode; CS333 87
88 else l a s t >next = newnode; l a s t = newnode; First-Last Header Algorithms #ifndef SLLISTUTILS_H #define SLLISTUTILS_H #include <cstdlib> // for NULL CS333 88
89 /** * Utility operations for singly linked lists * * Variant: header has first and last pointers * */ struct LListNode CS333 89
90 Data data; LListNode<Data>* next; LListNode() next = 0; LListNode (const Data& d, LListNode<Data>* nxt = 0) : data(d), next(nxt) ; struct LListHeader CS333 90
91 LListNode<Data>* first; LListNode<Data>* last; LListHeader(); void addtofront (const Data& value); void addtoend (const Data& value); // Add value after the indicated position void addafter (LListNode<Data>* afterthis, const Data& value); // Add value before the indicated position void addbefore (LListNode<Data>* beforethis, const Data& value); CS333 91
92 // Add value in sorted order. //Pre: all existing values are already ordered void addinorder (const Data& value); // Append all nodes from another list onto the end of this one void append (const LListHeader<Data>& list); // Remove value at the indicated position void remove (LListNode<Data>* here); // Add value after the indicated position void removeafter (LListNode<Data>* afterthis); CS333 92
93 // Search for a value. Returns null if not found LListNode<Data>* find (const Data& value) const; // Search an ordered list for a value. Returns null if not found LListNode<Data>* findordered (const Data& value) const; // Empty the list void clear(); ; CS333 93
94 LListHeader<Data>::LListHeader() : first(null), last(null) void LListHeader<Data>::addToFront (const Data& value) LListNode<Data>* newnode = new LListNode<Data>(value, first); first = newnode; if (last == NULL) last = first; CS333 94
95 void LListHeader<Data>::addToEnd (const Data& value) LListNode<Data>* newnode = new LListNode<Data>(value, NULL); if (last == NULL) first = last = newnode; else last->next = newnode; last = newnode; CS333 95
96 // Add value after the indicated position void LListHeader<Data>::addAfter (LListNode<Data>* afterthis, const Data& value) LListNode<Data>* newnode = new LListNode<Data>(value, afterthis->nex afterthis->next = newnode; if (afterthis == last) last = newnode; // Add value before the indicated position CS333 96
97 void LListHeader<Data>::addBefore (LListNode<Data>* beforethis, const Data& value) if (beforethis == first) addtofront (value); else // Move to front of beforethis LListNode<Data>* current = first; while (current->next!= beforethis) current = current->next; // Link after that node addafter (current, value); CS333 97
98 // Add value in sorted order. //Pre: all existing values are already ordered void LListHeader<Data>::addInOrder (const Data& value) if (first == NULL) first = last = new LListNode<Data>(value, NULL); else LListNode<Data>* current = first; LListNode<Data>* prev = NULL; CS333 98
99 while (current!= NULL && value > current->data) prev = current; current = current->next; // Add between prev and current if (prev == NULL) addtofront (value); else addafter (prev, value); // Add all values from another list onto the end of this one CS333 99
100 void LListHeader<Data>::append (const LListHeader<Data>& list) const LListNode<Data>* current = list.first; while (current!= NULL) addtoend (current->data); current = current->next; // Remove value at the indicated position CS
101 void LListHeader<Data>::remove (LListNode<Data>* here) if (here == first) LListNode<Data>* after = first->next; delete first; first = after; else LListNode<Data>* prev = first; while (prev->next!= here) prev = prev->next; prev->next = here->next; CS
102 if (here == last) last = prev; delete here; // Add value after the indicated position void LListHeader<Data>::removeAfter (LListNode<Data>* afterthis) LListNode<Data>* toremove = afterthis->next; afterthis->next = toremove->next; CS
103 if (toremove == last) last = afterthis; delete toremove; // Search for a value. Returns null if not found LListNode<Data>* LListHeader<Data>::find (const Data& value) const LListNode<Data>* current = first; while (current!= NULL && value!= current->data) current = current->next; return current; CS
104 // Search an ordered list for a value. Returns null if not found LListNode<Data>* LListHeader<Data>::findOrdered (const Data& value) co LListNode<Data>* current = first; while (current!= NULL && value > current->data) current = current->next; if (current!= NULL && value == current->data) return current; else return NULL; CS
105 void LListHeader<Data>::clear() LListNode<Data>* current = first; LListNode<Data>* nxt = NULL; while (current!= NULL) nxt = current->next; delete current; current = nxt; first = last = NULL; CS
106 #endif 4 Variations: Doubly-Linked Lists Doubly-Linked Lists CS
107 By modifying the node structure: struct DListNode Data data; DListNode<Data>* prev; DListNode<Data>* next; list: Adams first: last: Davis DListNode() next = prev = 0; DListNode (const Data& d, DListNode<Data>* prv = 0, DListNode<Data>* n : data(d), next(nxt), prev(prv) ; CS Baker
108 we can Move backwards as well as forward in the list by following theprev pointers Easily add in front of a node addbefore: Singly Linked CS
109 void LListHeader <Data > : : addbefore ( LListNode<Data>* beforethis, const Data& value ) i f ( beforethis == f i r s t ) addtofront ( value ) ; list: first: else last: / / Move to f ro nt of beforethis Adams LListNode<Data>* current = f i r s t ; while ( current >next! = beforethis ) Baker current = current >next ; Davis / / Link a f t e r that node addafter ( current, value ) ; CS
110 addbefore: Doubly Linked CS
111 void DListHeader<Data > : : addbefore ( DListNode<Data>* beforethis, const Data& value ) list: first: i f ( beforethis == f i r s t ) addtofront ( value ) ; last: else Adams / / Move to f ro nt of beforethis DListNode<Data>* current = beforethis >prev ; Baker / / Link a f t e r that node addafter ( current, value ) ; Davis CS
112 4.1 Doubliy-Linked List Algorithms #ifndef DLLISTUTILS_H #define DLLISTUTILS_H #include <cstdlib> // for NULL /** * Utility operations for doubly linked lists * * Variant: header has first and last pointers * */ CS
113 struct DListNode Data data; DListNode<Data>* prev; DListNode<Data>* next; DListNode() next = prev = 0; DListNode (const Data& d, DListNode<Data>* prv = 0, DListNode<Data>* : data(d), next(nxt), prev(prv) CS
114 ; struct DListHeader DListNode<Data>* first; DListNode<Data>* last; DListHeader(); void addtofront (const Data& value); void addtoend (const Data& value); CS
115 // Add value after the indicated position void addafter (DListNode<Data>* afterthis, const Data& value); // Add value before the indicated position void addbefore (DListNode<Data>* beforethis, const Data& value); // Add value in sorted order. //Pre: all existing values are already ordered void addinorder (const Data& value); // Remove value at the indicated position void remove (DListNode<Data>* here); CS
116 // Add value after the indicated position void removeafter (DListNode<Data>* afterthis); // Search for a value. Returns null if not found DListNode<Data>* find (const Data& value) const; // Search an ordered list for a value. Returns null if not found DListNode<Data>* findordered (const Data& value) const; // Empty the list void clear(); CS
117 ; DListHeader<Data>::DListHeader() : first(null), last(null) void DListHeader<Data>::addToFront (const Data& value) DListNode<Data>* newnode = new DListNode<Data>(value, NULL, first); if (first == NULL) CS
118 first = last = newnode; else first->prev = newnode; first = newnode; void DListHeader<Data>::addToEnd (const Data& value) DListNode<Data>* newnode = new DListNode<Data>(value, last, NULL); if (last == NULL) CS
119 first = last = newnode; else last->next = newnode; last = newnode; // Add value after the indicated position void DListHeader<Data>::addAfter (DListNode<Data>* afterthis, const Data& value) CS
120 DListNode<Data>* newnode = new DListNode<Data>(value, afterthis, afterthis->next); if (afterthis == last) last = newnode; else afterthis->next->prev = newnode; afterthis->next = newnode; // Add value before the indicated position CS
121 void DListHeader<Data>::addBefore (DListNode<Data>* beforethis, const Data& value) if (beforethis == first) addtofront (value); else // Move to front of beforethis DListNode<Data>* current = beforethis->prev; // Link after that node addafter (current, value); CS
122 // Add value in sorted order. //Pre: all existing values are already ordered void DListHeader<Data>::addInOrder (const Data& value) if (first == NULL) first = last = new DListNode<Data>(value, NULL, NULL); else DListNode<Data>* current = first; DListNode<Data>* prev = NULL; while (current!= NULL && value < current->data) CS
123 prev = current; current = current->next; // Add between prev and current if (prev == NULL) addtofront (value); else addafter (prev, value); // Remove value at the indicated position void DListHeader<Data>::remove (DListNode<Data>* here) CS
124 if (here == first) DListNode<Data>* after = first->next; delete first; first = after; else DListNode<Data>* prev = here->prev; prev->next = here->next; here->prev = prev; if (here == last) last = prev; CS
125 delete here; // Add value after the indicated position void DListHeader<Data>::removeAfter (DListNode<Data>* afterthis) DListNode<Data>* toremove = afterthis->next; afterthis->next = toremove->next; if (afterthis->next!= NULL) afterthis->next->prev = afterthis; CS
126 if (toremove == last) last = afterthis; delete toremove; // Search for a value. Returns null if not found DListNode<Data>* DListHeader<Data>::find (const Data& value) const DListNode<Data>* current = first; while (current!= NULL && value!= current->data) current = current->next; return current; CS
127 // Search an ordered list for a value. Returns null if not found DListNode<Data>* DListHeader<Data>::findOrdered (const Data& value) co DListNode<Data>* current = first; while (current!= NULL && value < current->data) current = current->next; if (current!= NULL && value == current->data) return current; else return NULL; CS
128 void DListHeader<Data>::clear() DListNode<Data>* current = first; DListNode<Data>* nxt = NULL; while (current!= NULL) nxt = current->next; delete current; current = nxt; first = last = NULL; CS
129 #endif CS
Linked Lists. Contents. Steven J. Zeil. July 31, Linked Lists: the Basics 3
Steven J. Zeil July 31, 2013 Contents 1 Linked Lists: the Basics 3 2 Coding for Linked Lists 7 2.1 Traversing a Linked List........................... 10 2.2 Searching a Linked List............................
More informationCopying Data. Contents. Steven J. Zeil. November 13, Destructors 2
Steven J. Zeil November 13, 2013 Contents 1 Destructors 2 2 Copy Constructors 11 2.1 Where Do We Use a Copy Constructor? 12 2.2 Compiler-Generated Copy Constructors............................................
More informationOperator Overloading
Steven Zeil November 4, 2013 Contents 1 Operators 2 1.1 Operators are Functions...................................................... 2 2 I/O Operators 4 3 Comparison Operators 38 3.1 Equality Operators.........................................................
More informationOperator Overloading
Steven Zeil November 4, 2013 Contents 1 Operators 2 1.1 Operators are Functions.... 2 2 I/O Operators 4 3 Comparison Operators 6 3.1 Equality Operators....... 11 3.2 Less-Than Operators...... 13 1 1 Operators
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 informationEncapsulation. Contents. Steven Zeil. July 17, Encapsulation Encapsulation in C Classes 4. 3 Hiding Attributes 8
Steven Zeil July 17, 2013 Contents 1 Encapsulation 2 1.1 Encapsulation in C++........................................................ 2 2 Classes 4 3 Hiding Attributes 8 4 Inline Functions 11 4.1 How Functions
More informationA C++ Class Designer s Checklist
Steven J. Zeil August 28, 2013 Contents 1 The Checklist 2 2 Discussion and Explanation 3 2.1 Is the interface complete?........ 3 2.2 Are there redundant functions or functions that can be generalized?............................
More informationDefensive Programming
Steven Zeil July 22, 2013 Contents 1 Common Assumptions 2 2 Documenting Assumptions 2 3 Guarding Assumptions 5 3.1 Guarding Assumptions with Assertions............................... 8 1 Defensive Programming
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 informationUnit Testing. Contents. Steven Zeil. July 22, Types of Testing 2. 2 Unit Testing Scaffolding Drivers Stubs...
Steven Zeil July 22, 2013 Contents 1 Types of Testing 2 2 6 2.1 Scaffolding................. 7 2.1.1 Drivers............... 7 2.1.2 Stubs................ 13 3 Integration Testing 17 1 1 Types of Testing
More informationUnit Testing. Steven Zeil. July 22, Types of Testing 2. 2 Unit Testing Scaffolding Drivers Stubs...
Steven Zeil July 22, 2013 Contents 1 Types of Testing 2 2 Unit Testing 4 2.1 Scaffolding............ 4 2.1.1 Drivers.......... 4 2.1.2 Stubs........... 9 3 Integration Testing 12 1 1 Types of Testing Testing
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 information2. List Implementations (a) Class Templates (b) Contiguous (c) Simply Linked (d) Simply Linked with Position Pointer (e) Doubly Linked
Chapter 6 LISTS AND STRINGS 1. List Specifications 2. List Implementations (a) Class Templates (b) Contiguous (c) Simply Linked (d) Simply Linked with Position Pointer (e) Doubly Linked 3. Strings 4. Application:
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 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 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 informationLab 2: ADT Design & Implementation
Lab 2: ADT Design & Implementation By Dr. Yingwu Zhu, Seattle University 1. Goals In this lab, you are required to use a dynamic array to design and implement an ADT SortedList that maintains a sorted
More informationJava - First Impressions for a C++ Programmer
Steven Zeil October 25, 2013 Contents 1 Program Structure 3 2 Program Structure == File Structure 6 3 Swimming in Pointers 31 4 Exceptions 34 5 Corresponding Data Structures 38 1 Moving from C++ to Java
More informationTraversing Trees with Iterators
Steven J. Zeil June 25, 2013 Contents 1 Iterating over Trees 4 1.1 begin()..................................... 6 1.2 operator++................................... 7 2 Iterators using Parent Pointers 11
More informationTraversing Trees with Iterators
Steven J. Zeil June 25, 2013 Contents 1 Iterating over Trees 3 1.1 begin()................................................................ 5 1.2 operator++..............................................................
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 informationCommon Modifications of Class Members
Steven Zeil July 13, 2013 Contents 1 Static 3 1.1 Static Data Members. 3 1.2 Static Function Members.......................... 18 2 const 22 2.1 const Pointers and References....................... 24
More informationEECE.3220: Data Structures Spring 2017
EECE.3220: Data Structures Spring 2017 Lecture 14: Key Questions February 24, 2017 1. Describe the characteristics of an ADT to store a list. 2. What data members would be necessary for a static array-based
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 informationCommon Modifications of Class Members
Steven Zeil July 13, 2013 Contents 1 Static 2 1.1 Static Data Members...... 2 1.2 Static Function Members... 9 2 const 10 2.1 const Pointers and References 11 2.2 Is your class const-correct?.. 12 1 1
More informationCommon Modifications of Class Members
Steven Zeil July 13, 2013 Contents 1 Static 2 1.1 Static Data Members........... 2 1.2 Static Function Members........ 10 2 const 12 2.1 const Pointers and References..... 13 2.2 Is your class const-correct?.......
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 informationCSE 143. Linked Lists. Linked Lists. Manipulating Nodes (1) Creating Nodes. Manipulating Nodes (3) Manipulating Nodes (2) CSE 143 1
CSE 143 Linked Lists [Chapter 4; Chapter 6, pp. 265-271] Linked Lists A linked list is a collection of dynamically allocated nodes Each node contains at least one member (field) that points to another
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 informationSharing Pointers and Garbage Collection
Steven J Zeil July 31, 2013 Contents 1 Shared Structures 5 1.1 Singly Linked Lists............ 5 1.2 Doubly Linked Lists............ 10 1.3 Airline Connections........... 14 2 Garbage Collection 21 2.1
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 informationCS32 Discussion Sec.on 1B Week 2. TA: Zhou Ren
CS32 Discussion Sec.on 1B Week 2 TA: Zhou Ren Agenda Copy Constructor Assignment Operator Overloading Linked Lists Copy Constructors - Motivation class School { public: }; School(const string &name); string
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 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 informationCS24 Week 4 Lecture 1
CS24 Week 4 Lecture 1 Kyle Dewey Overview Additional use of const in C++ List ADT Array Lists Linked Lists Additional Use of const We ve seen const already in two positions: What is pointed to is constant
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 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 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 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 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 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 informationSet Implementation Version 1
Introduction to System Programming 234122 Set Implementation Version 1 Masha Nikolski, CS Department, Technion 1 // Version 1.0 2 // Header file for set class. 3 // In this implementation set is a container
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 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 informationAssignment 4: SkipList
Assignment 4: SkipList Goals : Working with dynamic arrays, pointers, doubly linked lists For this assignment, you will write a Skip List data structure to store integers. When searching a Skip List, items
More informationPart of the Picture: Simulation
Part of the Picture: Simulation Random Number Generators The RandomInt Class The Part of the Picture: Simulation section in Chapter 5 referred to a class RandomInt, which can be used to conveniently generate
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 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 informationChapter 20: Binary Trees
Chapter 20: Binary Trees 20.1 Definition and Application of Binary Trees Definition and Application of Binary Trees Binary tree: a nonlinear linked list in which each node may point to 0, 1, or two other
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 informationData Structure Series
Data Structure Series This series is actually something I started back when I was part of the Sweet.Oblivion staff, but then some things happened and I was no longer able to complete it. So now, after
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 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 informationLAB 5, THE HIDDEN DELIGHTS OF LINKED LISTS
LAB 5, THE HIDDEN DELIGHTS OF LINKED LISTS Questions are based on the Main and Savitch review questions for chapter 5 in the Exam Preparation section of the webct course page. In case you haven t observed
More informationUNIT IV 4 LINKED LIST
4 UNIT IV LINKED LIST LINKED LIST SYLLABUS: 4.1 Pointers Revision 4.2 Revision of Structure 4.3 Revision of structure using pointers 4.4 Dynamic Memory Allocation 4.5 Linked list Presentation 4.6 Types
More informationclass Polynomial { public: Polynomial(const string& N = "no name", const vector<int>& C = vector<int>());... };
Default Arguments 1 When declaring a C++ function, you may optionally specify a default value for function parameters by listing initializations for them in the declaration: class Polynomial { public:
More informationCS 215 Fundamentals of Programming II Spring 2018 Project 5
CS 215 Fundamentals of Programming II Spring 2018 Project 5 30 points Out: March 23, 2018 Due: April 9, 2018 (Monday) As explained in Project 3, a line editor is a text editor that operates on lines of
More informationIntroduction to Programming in C Department of Computer Science and Engineering
Introduction to Programming in C Department of Computer Science and Engineering In this lecture, we will see slightly more advanced data type, then a singly link list. We will briefly go over one or two
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 informationLinear Structures. Linear Structure. Implementations. Array details. List details. Operations 2/10/2013
Linear Structure Linear Structures Chapter 4 CPTR 318 Every non-empty linear structure has A unique element called first A unique element called last Every element except last has a unique successor Every
More informationShort Notes of CS201
#includes: Short Notes of CS201 The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with < and > if the file is a system
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 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 informationCS201 - Introduction to Programming Glossary By
CS201 - Introduction to Programming Glossary By #include : The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with
More informationIII. Classes (Chap. 3)
III. Classes III-1 III. Classes (Chap. 3) As we have seen, C++ data types can be classified as: Fundamental (or simple or scalar): A data object of one of these types is a single object. int, double, char,
More informationLinked Lists. Gaddis Ch. 17. CS 2308 :: Spring 2016 Molly O'Neil
Linked Lists Gaddis Ch. 17 CS 2308 :: Spring 2016 Molly O'Neil List ADT A list is an abstract data type representing an ordered sequence of values For example, both MP3 Player assignments have used lists:
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 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 information+ Abstract Data Types
Linked Lists Abstract Data Types An Abstract Data Type (ADT) is: a set of values a set of operations Sounds familiar, right? I gave a similar definition for a data structure. Abstract Data Types Abstract
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 informationA collision is said to occur when two or more keys hash to the same index location.
Problem Set #8 Solutions Problem 1-9 points Given the tree below, traverse the tree a. Inorder : a/b c * d*e / f + g b. Preorder: * - / a b c / * d e + f g c. Postorder a b / c d e * f g + / * * - / /
More informationMotivation for Templates
Motivation for You want both: a list of Location objects a list of MazeMonster objects 1 How can you accomplish this by writing one LinkedList class? state all the ways you can think of doing this state
More informationLinked List in Data Structure. By Prof. B J Gorad, BECSE, M.Tech CST, PHD(CSE)* Assistant Professor, CSE, SITCOE, Ichalkaranji,Kolhapur, Maharashtra
Linked List in Data Structure By Prof. B J Gorad, BECSE, M.Tech CST, PHD(CSE)* Assistant Professor, CSE, SITCOE, Ichalkaranji,Kolhapur, Maharashtra Linked List Like arrays, Linked List is a linear data
More informationThe Memory Manager Project
The Memory Manager Project Objectives The goal of your next project is to simulate the C heap manager A runtime module used to allocate and de-allocate dynamic memory. The "heap" is a large "pool" of memory
More informationHomework Assignment #1
CISC 2200 Data Structure Spring, 2016 Homework Assignment #1 1 Short practices on linked list, see Textbook Page 205, Problem 9-12 2 Pointers: operations (deference, address-of), and syntax errors (a)
More informationIV. Stacks. A. Introduction 1. Consider the 4 problems on pp (1) Model the discard pile in a card game. (2) Model a railroad switching yard
IV. Stacks 1 A. Introduction 1. Consider the problems on pp. 170-1 (1) Model the discard pile in a card game (2) Model a railroad switching yard (3) Parentheses checker () Calculate and display base-two
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 informationLinked Lists and other Dynamic Data Structures
Linked Lists and other Dynamic Data Structures Arrays Fixed in size Allocated in advance within a contiguous memory block Look-up is fast Resizing and Deleting is hard (reallocate and copy) Dynamic Data
More informationThe time and space are the two measure for efficiency of an algorithm.
There are basically six operations: 5. Sorting: Arranging the elements of list in an order (either ascending or descending). 6. Merging: combining the two list into one list. Algorithm: The time and space
More informationThe Structure of a C++ Program
Steven Zeil May 25, 2013 Contents 1 Separate Compilation 3 1.1 Separate Compilation.......... 4 2 Pre-processing 7 2.1 #include.................. 9 2.2 Other Pre-processing Commands... 14 3 Declarations
More informationLinear Structures. Linear Structure. Implementations. Array details. List details. Operations 4/18/2013
Linear Structure Linear Structures Chapter 4 CPTR 318 Every non-empty linear structure has A unique element called first A unique element called last Every element except last has a unique successor Every
More informationLists, Stacks, and Queues. (Lists, Stacks, and Queues ) Data Structures and Programming Spring / 50
Lists, Stacks, and Queues (Lists, Stacks, and Queues ) Data Structures and Programming Spring 2016 1 / 50 Abstract Data Types (ADT) Data type a set of objects + a set of operations Example: integer set
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 informationECE 2035 Programming HW/SW Systems Spring problems, 5 pages Exam Three 8 April Your Name (please print clearly)
Your Name (please print clearly) This exam will be conducted according to the Georgia Tech Honor Code. I pledge to neither give nor receive unauthorized assistance on this exam and to abide by all provisions
More informationDue Date: See Blackboard
Source File: ~/2315/45/lab45.(C CPP cpp c++ cc cxx cp) Input: under control of main function Output: under control of main function Value: 4 Integer data is usually represented in a single word on a computer.
More informationCS350: Data Structures. Doubly Linked Lists
Doubly Linked Lists James Moscola Department of Physical Sciences York College of Pennsylvania James Moscola Doubly Linked Lists Adds an additional pointer to a the list nodes that points to the previous
More informationData Structures & Algorithms
A 1. What are the basic components of a linked list? A. Head and tail are the only important components B. Data members for the information to be stored and a link to the next item C. Generic class because
More informationProgramming II (CS300)
1 Programming II (CS300) Chapter 07: Linked Lists and Iterators MOUNA KACEM mouna@cs.wisc.edu Fall 2018 Linked Lists 2 Introduction Linked List Abstract Data Type General Implementation of the ListADT
More informationImplementing an abstract datatype. Linked lists and queues
Computer Programming Implementing an abstract datatype. Linked lists and queues Marius Minea marius@cs.upt.ro 19 December 2016 Review: compilation basics Briefly: Compiler translates source code to executable
More information03/31/03 Lab 7. Linked Lists
03/31/03 Lab 7 Lists are a collection of items in which each item has a specific position. The specification for positioning the items provides some rules of order so this data structure is called an ordered
More information#ifndef DOUBLE_LIST /* this string SHOULD NOT previously exist */ #define DOUBLE_LIST
/* This is a personal header file. Call it double_list.h * Its name should be reflected into the macro definition (#define). * For instance, here I should say something like: * #define DOUBLE_LIST #ifndef
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 informationP.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above
P.G.TRB - COMPUTER SCIENCE Total Marks : 50 Time : 30 Minutes 1. C was primarily developed as a a)systems programming language b) general purpose language c) data processing language d) none of the above
More informationLargest Online Community of VU Students
WWW.VUPages.com http://forum.vupages.com WWW.VUTUBE.EDU.PK Largest Online Community of VU Students MIDTERM EXAMINATION SEMESTER FALL 2003 CS301-DATA STRUCTURE Total Marks:86 Duration: 60min Instructions
More informationCS 106X, Lecture 16 More Linked Lists
CS 106X, Lecture 16 More Linked Lists reading: Programming Abstractions in C++, Chapters 11-12, 14.1-14.2 This document is copyright (C) Stanford Computer Science and Nick Troccoli, licensed under Creative
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 informationClass and Function Templates
Class and Function 1 Motivation for One Way to Look at... Example: Queue of some type Foo C++ What can a parameter be used for? Instantiating a Template Usage of Compiler view of templates... Implementing
More informationMotivation for Templates. Class and Function Templates. One Way to Look at Templates...
Class and Function 1 Motivation for 2 Motivation for One Way to Look at... Example: Queue of some type Foo C++ What can a parameter be used for? Instantiating a Template Usage of Compiler view of templates...
More informationDue Date: See Blackboard
Source File: ~/2305/lab06.(C CPP cpp c++ cc cxx cp) Input: Under control of main function Output: Under control of main function Value: 2 Extend the IntegerSet class from Lab 04 to provide the following
More information