lecture 9 linked lists - ccvclvisionlab.engr.ccny.cuny.edu/~fhu/lecture09-linkedlists.pdf ·...
TRANSCRIPT
![Page 1: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/1.jpg)
CSC212 Data Structure
- Section FG
Lecture9LinkedLists
Instructor:FengHUDepartmentofComputerScience
CityCollegeofNewYork
![Page 2: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/2.jpg)
Motivation
• Inasequenceusinganarray,insertinganewitemneedstomoveothersback...
10 20 30 ? ?
18
![Page 3: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/3.jpg)
Motivation
• Inasequenceusinganarray,insertinganewitemneedstomoveothersback...
10 20 20 30 ?
18
![Page 4: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/4.jpg)
Motivation
• Inasequenceusinganarray,insertinganewitemneedstomoveothersback...
• SotheBig-OoftheinsertisO(n)
10 18 20 30 ?
![Page 5: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/5.jpg)
Motivation
• Howcanweinsertanewitemwithoutmovingothers?
10 20 30 ? ?
18
We need a new data structure
![Page 6: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/6.jpg)
Motivation
• Howcanweinsertanewitemwithoutmovingothers?
18
??302010
break an array into a linked chain...
![Page 7: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/7.jpg)
Motivation
• Howcanweinsertanewitemwithoutmovingothers?
18
??302010
and then put the new item into the chain
![Page 8: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/8.jpg)
Motivation
• Howcanweinsertanewitemwithoutmovingothers?
18
??302010
But the links (->) need some way to build up
![Page 9: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/9.jpg)
LinkedListsinAction
•Chapter5introducestheoften-useddatastructureoflinkedlists.
• Thispresentationshowshowtoimplementthemostcommonoperationsonlinkedlists.
CHAPTER 5Data Structures and Other Objects
![Page 10: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/10.jpg)
DeclarationsforLinkedLists
• Eachnode inthelinkedlistisaclass,asshownhere.
data
link
10
data
link
15
data
link
7
null
class node{public:
typedef int value_type;...
private:value_type data;node *link;
};
![Page 11: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/11.jpg)
data
link
7
DeclarationsforLinkedLists
• Thedataportionofeachnodeisatypecalledvalue_type,definedbyatypedef.
link
null
data
link
15
data10
class node{public:
typedef int value_type;...
private:value_type data;node *link;
};
![Page 12: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/12.jpg)
DeclarationsforLinkedLists
• Eachnodealsocontainsalinkfieldwhichisapointertoanothernode.
data15
data7
data10
link
linknull
link
class node{public:
typedef int value_type;...
private:value_type data;node *link;
};
![Page 13: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/13.jpg)
DeclarationsforLinkedLists
• Aprogramcankeeptrackofthefirstnodebyusingapointervariablesuchashead_ptr inthisexample.
• Noticethathead_ptr itselfisnotanode-- itisapointertoanode.
data
link
10
data
link
15
data
link
7
nullhead_ptr
node * head_ptr;
![Page 14: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/14.jpg)
DeclarationsforLinkedLists
• Aprogramcanalsokeeptrackofthelastnodebyusingapointervariablesuchastail_ptr inthisexample.
• Noticethattail_ptr itselfisnotanode-- itisapointertoanode.
data
link
10
data
link
15
data
link
7
nullhead_ptr
node * head_ptr;node * tail_ptr; tail_ptr
![Page 15: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/15.jpg)
DeclarationsforLinkedLists
• Aprogramcankeeptrackofthefirstandthelastnodesbyusingpointervariablessuchashead_ptr,tail_ptr.
• Noticethatneitherhead_ptrnortail_ptrisanode-- itisapointertoanode.
• Foranemptylist,null isstoredinboththeheadandthetailpointers.
head_ptrnull
tail_ptrnull
node * head_ptr;node * tail_ptr;head_ptr = NULL;tail_ptr = NULL;// NULL can be used for any pointers!
![Page 16: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/16.jpg)
TheComplete node ClassDefinition
• Thenodeclassisfundamentaltolinkedlists• Theprivatemembervariables
• data:avalue_typevariable• link:apointertothenextnode
• Thememberfunctionsinclude:• Aconstructor• Setdataandsetlink• Retrievedataandretrievelink
![Page 17: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/17.jpg)
TheComplete node ClassDefinition
• Thenodeclassisfundamentaltolinkedlists• Theprivatemembervariables
• data_field• link_field
• Thememberfunctionsinclude:• Aconstructor• Setdataandsetlink• Retrievedataandretrievelink
![Page 18: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/18.jpg)
class node{public:
// TYPEDEFtypedef double value_type;
// CONSTRUCTORnode(
const value_type& init_data = value_type( ),node* init_link = NULL
){ data = init_data; link = init_link; }
// Member functions to set the data and link fields:void set_data(const value_type& new_data) { data = new_data; }void set_link(node* new_link) { link = new_link; }
// Constant member function to retrieve the current data:value_type data( ) const { return data; }
// Two slightly different member functions to retrieve// the current link:const node* link( ) const { return link; }node* link( ) { return link;}
private:value_type data;node* link;
};
default argument given by the value_type default constructor
Why TWO? p. 213-4
![Page 19: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/19.jpg)
ASmallQuiz-
• Supposeaaprogramhasbuiltthelinkedlistasshown,andhead_ptrisapointertoanode.
• What is the data type of *head_ptr?• cout << (*head_ptr). data();• cout << head_ptr->data();
data
link
10
data
link
15
data
link
7
nullhead_ptr
![Page 20: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/20.jpg)
LinkedListToolkit
• DesignContainerClassesusingLinkedLists• Theuseofalinked listissimilartoourprevioususeofanarrayinacontainerclass
• Butstoringandretrievingneedsmoreworksincewedonothavethathandyindexing
• =>LinkedListToolbox• usingnodeclass
![Page 21: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/21.jpg)
TheWorkingsoffourfunctions
• Thislecturewillshowfourfunctions:• Computethelengthofalinkedlist(code)• Insertanewnodeatthehead(code)• Insertanodeatanylocation(pseudo-code)• Deleteanodefromthehead(pseudo-code)
•ReadSection5.2forotherfunctionsintheToolbox• willbeusedincontainerclassesbagandsequence
![Page 22: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/22.jpg)
size_t list_length(const node* head_ptr);
LengthofaLinkedList
Wesimplywanttocomputethelength ofthelinkedlist,forexampletheoneshownhere.
Notethatlist_lengthisnotamemberfunctionofthenodeclass
10
15
7
nullhead_ptr
![Page 23: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/23.jpg)
size_t list_length(const node* head_ptr);
Pseudo-codeoflist_length
1. Initializethecount tozero.2. Makecursorpointtoeachnode,
startingatthehead.Eachtimecursorpointstoanewnode,add1tocount.
3. returncount.
10
15
7
nullhead_ptr
count0
![Page 24: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/24.jpg)
size_t list_length(const node* head_ptr);
Pseudo-codeoflist_length
1. Initializethecount tozero.2. Makecursorpointtoeachnode,
startingatthehead.Eachtimecursorpointstoanewnode,add1tocount.
3. returncount.
10
15
7
nullhead_ptr
count1
cursor
![Page 25: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/25.jpg)
size_t list_length(const node* head_ptr);
Pseudo-codeoflist_length
1. Initializethecount tozero.2. Makecursorpointtoeachnode,
startingatthehead.Eachtimecursorpointstoanewnode,add1tocount.
3. returncount.
10
15
7
nullhead_ptr
count2
cursor
![Page 26: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/26.jpg)
size_t list_length(const node* head_ptr);
Pseudo-codeoflist_length
1. Initializethecount tozero.2. Makecursorpointtoeachnode,
startingatthehead.Eachtimecursorpointstoanewnode,add1tocount.
3. returncount.
10
15
7
nullhead_ptr
count3
cursor
![Page 27: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/27.jpg)
size_t list_length(const node* head_ptr);
Pseudo-codeoflist_length
1. Initializethecount tozero.2. Makecursorpointtoeachnode,
startingatthehead.Eachtimecursorpointstoanewnode,add1tocount.
3. returncount.
10
15
7
nullhead_ptr
count3
cursor
![Page 28: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/28.jpg)
size_t list_length(const node* head_ptr){
const node *cursor;size_t count = 0; // step 1for (cursor = head_ptr; cursor != NULL; cursor = cursor->link())
count++; // step 2return count; // step 3
}
Realcodeoflist_length:ListTraverse
1. Initializethecount tozero.2. Eachtimecursor pointstoanew
node,add1tocount.3. returncount.
10
15
7
nullhead_ptrcount
0
cursor
![Page 29: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/29.jpg)
size_t list_length(const node* head_ptr){
const node *cursor;size_t count = 0; // step 1for (cursor = head_ptr; cursor != NULL; cursor = cursor->link())
count++; // step 2return count; // step 3
}
Realcodeoflist_length:ListTraverse
1. Initializethecount tozero.2. Eachtimecursor pointstoanew
node,add1tocount.3. returncount.
10
15
7
nullhead_ptrcount
1
cursor
![Page 30: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/30.jpg)
size_t list_length(const node* head_ptr){
const node *cursor;size_t count = 0; // step 1for (cursor = head_ptr; cursor != NULL; cursor = cursor->link())
count++; // step 2return count; // step 3
}
Realcodeoflist_length:ListTraverse
1. Initializethecount tozero.2. Eachtimecursor pointstoanew
node,add1tocount.3. returncount.
10
15
7
nullhead_ptrcount
2cursor
![Page 31: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/31.jpg)
size_t list_length(const node* head_ptr){
const node *cursor;size_t count = 0; // step 1for (cursor = head_ptr; cursor != NULL; cursor = cursor->link())
count++; // step 2return count; // step 3
}
Realcodeoflist_length:ListTraverse
1. Initializethecount tozero.2. Eachtimecursor pointstoanew
node,add1tocount.3. returncount.
10
15
7
nullhead_ptrcount
3
cursor
![Page 32: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/32.jpg)
size_t list_length(const node* head_ptr){
const node *cursor;size_t count = 0;for (cursor = head_ptr; cursor != NULL; cursor = cursor->link())
count++;return count; // step 3
}
Big-Ooflist_length
Big-O:O(n)iflengthisn10
15
7
nullhead_ptrcount
3
cursor
![Page 33: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/33.jpg)
size_t list_length(const node* head_ptr){
const node *cursor;size_t count = 0;for (cursor = head_ptr; cursor != NULL; cursor = cursor->link())
count++;return count;
}
Islist_lengthworksforanemptylist?
cursor=head_ptr=NULLcount=0
count0
head_ptr
nullcursor
null
![Page 34: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/34.jpg)
TheWorkingsoffourfunctions
• Thislecturewillshowfourfunctions:• Computethelengthofalinkedlist(code)• Insertanewnodeatthehead(code)• Insertanodeatanylocation(pseudo-code)• Deleteanodefromthehead(pseudo-code)
•ReadSection5.2forotherfunctionsintheToolbox• willbeusedincontainerclassesbagandsequence
![Page 35: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/35.jpg)
void list_head_insert(node*& head_ptr, const node::value_type& entry);
InsertinganodeattheHead
Wewanttoaddanewentry,13,tothehead ofthelinkedlistshownhere.
Notethathead_ptrisareferencenodepointer
10
15
7
nullhead_ptr
entry13
![Page 36: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/36.jpg)
InsertingaNodeattheHead
❶ Createanewnode,pointedtobyalocalvariableinsert_ptr.
10
15
7
nullhead_ptr
entry13
insert_ptr
void list_head_insert(node*& head_ptr, const node::value_type& entry);
![Page 37: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/37.jpg)
InsertingaNodeattheHead
❶ insert_ptr = new node;
10
15
7
nullhead_ptr
entry13
insert_ptr
void list_head_insert(node*& head_ptr, const node::value_type& entry);
![Page 38: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/38.jpg)
InsertingaNodeattheHead
❶ insert_ptr = new node;❷ Placethedatainthenewnode'sdata
field.
10
15
7
nullhead_ptr
entry13
insert_ptr13
void list_head_insert(node*& head_ptr, const node::value_type& entry);
![Page 39: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/39.jpg)
InsertingaNodeattheHead
❶ insert_ptr = new node;❷ insert_ptr->data = entry;
What expression appears on the left side of the assignment statement ?
10
15
7
nullhead_ptr
entry13
insert_ptr13
?
void list_head_insert(node*& head_ptr, const node::value_type& entry);
![Page 40: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/40.jpg)
InsertingaNodeattheHead
❶ insert_ptr = new node;❷ insert_ptr->data = entry;
But data is a private variable, so cannot be accessed by a non-member function
10
15
7
nullhead_ptr
entry13
insert_ptr13
void list_head_insert(node*& head_ptr, const node::value_type& entry);
![Page 41: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/41.jpg)
InsertingaNodeattheHead
❶ insert_ptr = new node;❷ insert_ptr->data = entry;
But data is a private variable, so cannot be accessed by a non-member function
10
15
7
nullhead_ptr
entry13
insert_ptr13
void list_head_insert(node*& head_ptr, const node::value_type& entry);
X
![Page 42: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/42.jpg)
InsertingaNodeattheHead
❶ insert_ptr = new node;❷ insert_ptr->set_data(entry);
Instead, Set_data function is used since data_field is a private variable of the node class
10
15
7
nullhead_ptr
entry13
insert_ptr13
void list_head_insert(node*& head_ptr, const node::value_type& entry);
![Page 43: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/43.jpg)
InsertingaNodeattheHead
10
15
7
nullhead_ptr
entry13
insert_ptr13
❶ insert_ptr = new node;❷ insert_ptr->set_data(entry);❸ Connect the new node to the
front of the list.
void list_head_insert(node*& head_ptr, const node::value_type& entry);
![Page 44: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/44.jpg)
InsertingaNodeattheHead
What will be the parameter ?
10
15
7
nullhead_ptr
entry13
insert_ptr13
❶ insert_ptr = new node;❷ insert_ptr->set_data(entry);➌ insert_ptr->set_link(head_ptr);?
void list_head_insert(node*& head_ptr, const node::value_type& entry);
![Page 45: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/45.jpg)
InsertingaNodeattheHead
The new node is linked to the node that head_ptr is pointing to. 10
15
7
nullhead_ptr
entry13
insert_ptr13
❶ insert_ptr = new node;❷ insert_ptr->set_data(entry);➌ insert_ptr->set_link(head_ptr);
void list_head_insert(node*& head_ptr, const node::value_type& entry);
![Page 46: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/46.jpg)
InsertingaNodeattheHead
10
15
7
nullhead_ptr
entry13
insert_ptr13
❶ insert_ptr = new node;❷ insert_ptr->set_data(entry);➌ insert_ptr->set_link(head_ptr);❹ Make the head_ptr point to the new
head of the linked list.
void list_head_insert(node*& head_ptr, const node::value_type& entry);
![Page 47: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/47.jpg)
InsertingaNodeattheHead
10
15
7
nullhead_ptr
entry13
insert_ptr13
❶ insert_ptr = new node;❷ insert_ptr->set_data(entry);➌ insert_ptr->set_link(head_ptr);➍ head_ptr = insert_ptr;
void list_head_insert(node*& head_ptr, const node::value_type& entry);
![Page 48: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/48.jpg)
InsertingaNodeattheHead
❶ insert_ptr = new node;❷ insert_ptr->set_data(entry);➌ insert_ptr->set_link(head_ptr);➍ head_ptr = insert_ptr;
10
15
7
nullhead_ptr
13
When the function returns, thelinked list has a new node at thehead, containing 13.
void list_head_insert(node*& head_ptr, const node::value_type& entry);
![Page 49: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/49.jpg)
void list_head_insert(node*& head_ptr, const node::value_type& entry){
node *insert_ptr;
insert_ptr = new node;insert_ptr->set_data(entry);insert_ptr->set_link(head_ptr);head_ptr = insert_ptr;
}
InsertingaNodeattheHead
Linked List: O(1)
- cmp: Array: O(n)
What is the Big-O of
the head_insert function?
![Page 50: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/50.jpg)
void list_head_insert(node*& head_ptr, const node::value_type& entry){
node *insert_ptr;
insert_ptr = new node;insert_ptr->set_data(entry);insert_ptr->set_link(head_ptr);head_ptr = insert_ptr;
}
InsertingaNodeattheHead
Does the function workcorrectly for the empty
list ?
![Page 51: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/51.jpg)
void list_head_insert(node*& head_ptr, const node::value_type& entry){
node *insert_ptr;
insert_ptr = new node;insert_ptr->set_data(entry);insert_ptr->set_link(head_ptr);head_ptr = insert_ptr;
}
InsertingaNodeattheHead
head_ptrentry13 null
Does the function workcorrectly for the empty
list ?
![Page 52: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/52.jpg)
void list_head_insert(node*& head_ptr, const node::value_type& entry){
node *insert_ptr;
insert_ptr = new node;insert_ptr->set_data(entry);insert_ptr->set_link(head_ptr);head_ptr = insert_ptr;
}
InsertingaNodeattheFront
head_ptrentry13 null
insert_ptr13
![Page 53: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/53.jpg)
void list_head_insert(node*& head_ptr, const node::value_type& entry){
node *insert_ptr;
insert_ptr = new node;insert_ptr->set_data(entry);insert_ptr->set_link(head_ptr);head_ptr = insert_ptr;
}
InsertingaNodeattheHead
head_ptrentry13 null
insert_ptr13
null
![Page 54: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/54.jpg)
void list_head_insert(node*& head_ptr, const node::value_type& entry){
node *insert_ptr;
insert_ptr = new node;insert_ptr->set_data(entry);insert_ptr->set_link(head_ptr);head_ptr = insert_ptr;
}
InsertingaNodeattheHead
head_ptrentry13
insert_ptr13
null
![Page 55: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/55.jpg)
void list_head_insert(node*& head_ptr, const node::value_type& entry){
node *insert_ptr;
insert_ptr = new node;insert_ptr->set_data(entry);insert_ptr->set_link(head_ptr);head_ptr = insert_ptr;
}
InsertingaNodeattheHead
head_ptr
13
null
When the functionreturns, the linked list
has one node,containing 13.
![Page 56: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/56.jpg)
Caution!
•Alwaysmakesurethatyourlinkedlistfunctionsworkcorrectlywithanemptylist.
EMPTY LIST
![Page 57: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/57.jpg)
void list_head_insert(node*& head_ptr, const node::value_type& entry){
node *insert_ptr;
insert_ptr = new node;insert_ptr->set_data(entry);insert_ptr->set_link(head_ptr);head_ptr = insert_ptr;
}
InsertingaNodeattheHead
Q: Can you give an implementation with ONLY a single statement?
![Page 58: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/58.jpg)
void list_head_insert(node*& head_ptr, const node::value_type& entry){
node *insert_ptr;
insert_ptr = new node(entry, head_ptr);
head_ptr = insert_ptr;}
InsertingaNodeattheHead
YES, we can use the constructor with parameters!
![Page 59: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/59.jpg)
void list_head_insert(node*& head_ptr, const node::value_type& entry){
head_ptr = new node(entry, head_ptr);
}
InsertingaNodeattheHead
and assign the return pointer of new directly to the head pointer !
![Page 60: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/60.jpg)
• Break
![Page 61: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/61.jpg)
DeclarationsforLinkedLists
• Eachnodealsocontainsalinkfieldwhichisapointertoanothernode.
data15
data7
data10
link
linknull
link
class node{public:
typedef int value_type;...
private:value_type data;node *link;
};
![Page 62: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/62.jpg)
TheComplete node ClassDefinition
• Thenodeclassisfundamentaltolinkedlists• Theprivatemembervariables
• data_field• link_field
• Thememberfunctionsinclude:• Aconstructor• Setdataandsetlink• Retrievedataandretrievelink
![Page 63: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/63.jpg)
class node{public:
// TYPEDEFtypedef double value_type;
// CONSTRUCTORnode(
const value_type& init_data = value_type( ),node* init_link = NULL
){ data = init_data; link = init_link; }
// Member functions to set the data and link fields:void set_data(const value_type& new_data) { data = new_data; }void set_link(node* new_link) { link = new_link; }
// Constant member function to retrieve the current data:value_type data( ) const { return data; }
// Two slightly different member functions to retrieve// the current link:const node* link( ) const { return link; }node* link( ) { return link;}
private:value_type data;node* link;
};
default argument given by the value_type default constructor
Why TWO? p. 213-4
![Page 64: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/64.jpg)
size_t list_length(const node* head_ptr){
const node *cursor;size_t count = 0;for (cursor = head_ptr; cursor != NULL; cursor = cursor->link())
count++;return count; // step 3
}
Big-Ooflist_length
Big-O:O(n)iflengthisn10
15
7
nullhead_ptrcount
3
cursor
![Page 65: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/65.jpg)
TheWorkingsoffourfunctions
• Thislecturewillshowfourfunctions:• Computethelengthofalinkedlist(code)• Insertanewnodeatthehead(code)• Insertanodeatanylocation(pseudo-code)• Deleteanodefromthehead(pseudo-code)
•ReadSection5.2forotherfunctionsintheToolbox• willbeusedincontainerclassesbagandsequence
![Page 66: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/66.jpg)
PseudocodeforInsertingNodes
• Nodesareofteninsertedatplacesotherthanthefrontofalinkedlist.• Thereisageneralpseudocodethatyoucanfollowforanyinsertion
function...
![Page 67: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/67.jpg)
PseudocodeforInsertingNodes
❶Determinewhetherthenewnodewillbethefirstnodeinthelinkedlist.Ifso,thenthereisonlyonestep:
list_head_insert(head_ptr, entry);
![Page 68: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/68.jpg)
PseudocodeforInsertingNodes
➊Determinewhetherthenewnodewillbethefirstnodeinthelinkedlist.Ifso,thenthereisonlyonestep:
list_head_insert(head_ptr, entry);
![Page 69: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/69.jpg)
PseudocodeforInsertingNodes
❶Determinewhetherthenewnodewillbethefirstnodeinthelinkedlist.Ifso,thenthereisonlyonestep:
list_head_insert(head_ptr, entry);
A pointerto the
head ofthe list
![Page 70: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/70.jpg)
PseudocodeforInsertingNodes
❶Determinewhetherthenewnodewillbethefirstnodeinthelinkedlist.Ifso,thenthereisonlyonestep:
list_head_insert(head_ptr, entry);
![Page 71: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/71.jpg)
PseudocodeforInsertingNodes
❷ Otherwise (if the new node will not be first):❐ Start by setting a pointer named previous_ptr to point to the
node which is just before the new node's position.
![Page 72: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/72.jpg)
PseudocodeforInsertingNodes
15
10
7
nullhead_ptr
❷ Otherwise (if the new node will not be first):❐ Start by setting a pointer named previous_ptr to point to the
node which is just before the new node's position.
In this example, thenew node will bethe second node
previous_ptr
![Page 73: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/73.jpg)
PseudocodeforInsertingNodes
What is the name of this pointer ?
15
10
7
nullhead_ptr
❷ Otherwise (if the new node will not be first):❐ Start by setting a pointer named previous_ptr to point to the
node which is just before the new node's position
Look at the pointerwhich is in the node
*previous_ptrprevious_ptr
![Page 74: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/74.jpg)
PseudocodeforInsertingNodes
Always remember how can you access link
15
10
7
nullhead_ptr
❷ Otherwise (if the new node will not be first):❐ Start by setting a pointer named previous_ptr to point to the
node which is just before the new node's position
This pointer is calledprevious_ptr->link
previous_ptr
![Page 75: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/75.jpg)
PseudocodeforInsertingNodes
15
10
7
nullhead_ptr
➋ Otherwise (if the new node will not be first):❐ Start by setting a pointer named previous_ptr to point to the
node which is just before the new node's position
previous_ptr->linkpoints to the headof a smaller linkedlist, with 10 and 7
previous_ptr
![Page 76: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/76.jpg)
PseudocodeforInsertingNodes
Write one C++ statement which will do the insertion.
15
10
7
nullhead_ptr
❷ Otherwise (if the new node will not be first):❐ Start by setting a pointer named previous_ptr to point to the
node which is just before the new node's position.
The new node mustbe inserted at thehead of this small
linked list.
13
previous_ptr
![Page 77: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/77.jpg)
PseudocodeforInsertingNodes
Write one C++ statement which will do the insertion.
15
10
7
nullhead_ptr
❷ Otherwise (if the new node will not be first):❐ Start by setting a pointer named previous_ptr to point to the
node which is just before the new node's position. 13
previous_ptrlist_head_insert(previous_ptr->link, entry);
X
![Page 78: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/78.jpg)
PseudocodeforInsertingNodes
More precisely, you need to use member function link() , and have three lines of code
15
10
7
nullhead_ptr
❷ Otherwise (if the new node will not be first):❐ Start by setting a pointer named previous_ptr to point to the
node which is just before the new node's position.13
previous_ptr
node *sl_head_ptr;sl_head_ptr = previous_ptr->link();list_head_insert(sl_head_ptr, entry);previous_ptr->set_link(sl_head_ptr);
sl_head_ptr
![Page 79: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/79.jpg)
PseudocodeforInsertingNodes
More precisely, you need to use member function link() , and have three lines of code
15
10
7
nullhead_ptr
❷ Otherwise (if the new node will not be first):❐ Start by setting a pointer named previous_ptr to point to the
node which is just before the new node's position. 13
previous_ptr
node *sl_head_ptr;sl_head_ptr = previous_ptr->link();list_head_insert(sl_head_ptr, entry);previous_ptr->set_link(sl_head_ptr);
sl_head_ptr
![Page 80: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/80.jpg)
PseudocodeforInsertingNodes
More precisely, you need to use member function link() , and have three lines of code
15
10
7
nullhead_ptr
❷ Otherwise (if the new node will not be first):❐ Start by setting a pointer named previous_ptr to point to the
node which is just before the new node's position. 13
previous_ptr
node *sl_head_ptr;sl_head_ptr = previous_ptr->link();list_head_insert(sl_head_ptr, entry);previous_ptr->set_link(sl_head_ptr);
sl_head_ptr
![Page 81: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/81.jpg)
PseudocodeforInsertingNodes
❶Determinewhetherthenewnodewillbethefirstnodeinthelinkedlist.Ifso,thenthereisonlyonestep:
list_head_insert(head_ptr, entry);
❷ Otherwise (if the new node will not be first):❐ Set a pointer named previous_ptr to point to the node
which is just before the new node's position.❐ Do the following :
node *sl_head_ptr;sl_head_ptr = previous_ptr->link(); list_head_insert(sl_head_ptr, entry);previous_ptr->set_link(sl_head_ptr);
![Page 82: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/82.jpg)
PseudocodeforInsertingNodes
• Theprocessofaddinganewnodeinthemiddleofalist(only the step after previous_ptr has been set)canalsobeincorporatedasaseparatefunction.Thisfunctioniscalledlist_insert inthelinkedlisttoolkitofSection5.2.
• Challengeyourself:• Thetextbookactuallygivesyouadifferentimplementation(p235,4linesofcode)
• Canyouimplementlist_insert withjustonelineofcode?• Don’tuselist_head_insert• SeeSelf-TestEx16
![Page 83: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/83.jpg)
TheWorkingsoffourfunctions
• Thislecturewillshowfourfunctions:• Computethelengthofalinkedlist(code)• Insertanewnodeatthehead(code)• Insertanodeatanylocation(pseudo-code)• Deleteanodefromthehead(pseudo-code)
•ReadSection5.2forotherfunctionsintheToolbox• willbeusedincontainerclassesbagandsequence
![Page 84: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/84.jpg)
PseudocodeforRemovingNodes
• Nodesoftenneedtoberemovedfromalinkedlist.• Aswithinsertion,thereisatechniqueforremovinganodefromthe
frontofalist,andatechniqueforremovinganodefromelsewhere.• We’lllookatthepseudocodeforremovinganodefromtheheadofa
linkedlist.
![Page 85: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/85.jpg)
RemovingtheHeadNode
10 15
7
nullhead_ptr
13
❶ Start by setting up a temporary pointer named remove_ptrto the head node.
remove_ptr
![Page 86: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/86.jpg)
RemovingtheHeadNode
10 15
7
nullhead_ptr
13
❶ Set up remove_ptr.❷ head_ptr = remove_ptr->link();
remove_ptr
Draw the change that this statement will make to the linked list.
![Page 87: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/87.jpg)
RemovingtheHeadNode
10 15
7
nullhead_ptr
13
➊ Set up remove_ptr.➋ head_ptr = remove_ptr->link();
remove_ptr
![Page 88: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/88.jpg)
RemovingtheHeadNode
➊ Set up remove_ptr.➋ head_ptr = remove_ptr->link;❸ delete remove_ptr; // Return the node's memory to heap.
10 15
7
nullhead_ptr
13
remove_ptr
![Page 89: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/89.jpg)
RemovingtheHeadNode
Here’s what the linked list looks like after the removal finishes.
10 15
7
nullhead_ptr
![Page 90: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/90.jpg)
Summary
• Itiseasytoinsertanodeatthefrontofalist.• Thelinkedlisttoolkitalsoprovidesafunctionforinsertinganewnodeelsewhere
• Itiseasytoremoveanodeatthefrontofalist.• Thelinkedlisttoolkitalsoprovidesafunctionforremovinganodeelsewhere--youshouldreadaboutthisfunctionandtheotherfunctionsofthetoolkit.
![Page 91: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/91.jpg)
Keypointsyouneedtoknow
• LinkedListToolkitusesthenodeclasswhichhas• setandretrievefunctions
• ThefunctionsintheToolkitarenotmemberfunctionsofthenodeclass• length,insert(2),remove(2),search,locate,copy,...• comparetheirBig-Oswithsimilarfunctionsforanarray
• Theycanbeusedinvariouscontainerclasses,suchasbag,sequence,etc.
Toolkit Code
![Page 92: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/92.jpg)
Homework...
• Self-TestExercises(node)• 1-12
•Readafterclass• LinkedListToolKit (Section5.2)• DoSelf-TestEx13-25
•Readbeforethenextlecture• Section5.3- 5.4
![Page 93: Lecture 9 Linked Lists - CCVCLvisionlab.engr.ccny.cuny.edu/~fhu/Lecture09-LinkedLists.pdf · Lecture 9 Linked Lists Instructor: Feng HU Department of Computer Science ... points to](https://reader034.vdocument.in/reader034/viewer/2022042801/5a78ddff7f8b9a5a148ce1cc/html5/thumbnails/93.jpg)
THE END
Presentation copyright 1997, Addison Wesley Longman,For use with Data Structures and Other Objects Using C++by Michael Main and Walter Savitch.
Some artwork in the presentation is used with permission from Presentation Task Force(copyright New Vision Technologies Inc) and Corel Gallery Clipart Catalog (copyrightCorel Corporation, 3G Graphics Inc, Archive Arts, Cartesia Software, Image ClubGraphics Inc, One Mile Up Inc, TechPool Studios, Totem Graphics Inc).
Students and instructors who use Data Structures and Other Objects Using C++ are welcometo use this presentation however they see fit, so long as this copyright notice remainsintact.