SlideShare a Scribd company logo
COMP 2710 Software Construction
Linked List – Exercises
Dr. Xiao Qin
Department of Computer Science and
Software Engineering
Auburn University
http://www.eng.auburn.edu/~xqin
xqin@auburn.edu
Exercise 1
1-2
• Task 1: Define a structure named node, where there are two
items – (1) data whose type is int and (2) a pointer pointing
to the next node
• Task 2: Using typedef to define a new type (e.g., nodePtr)
of pointer pointing to node (i.e., node*)
• Task 3: Create a pointer (e.g., node_ptr) using the above new
data type
• Task 4: Allocate memory resource for the new pointer
• Task 5: Assign value (e.g., 10) to the node pointed by
node_ptr
Exercise 2: Print a list of nodes
struct node {
int data;
node *next;
};
typedef node* nodePtr;
void printList(nodePtr root);
What two cases should we consider?
Answer: Print a list of nodes
void printList(nodePtr root) {
nodePtr cur;
if (root == NULL)
cout << "This is an empty listn";
cur = root;
while (cur != NULL) {
cout << cur->data << endl;
cur = cur->next;
}
}
Exercise 3:
Insert a node to the head of the list
struct node {
int data;
node *next;
};
typedef node* nodePtr;
void insertNode(nodePtr& root, int info);
//Another possible prototype
void insertNode(nodePtr& root, nodePtr newNodePtr);
What is the difference between the above two prototypes?
How many cases should we consider? What are these cases?
Exercise 3 - Insert a node to the head of the list
1-6
struct node {
int data;
node *next;
};
typedef node* nodePtr;
void insertNode(nodePtr& root, int info);
root
cur_ptr
11
22 33
info
44
55
66
Exercise 3: Answer
Insert a node to the head of the list
void insertNode(nodePtr& root, int info) {
nodePtr cur_prt;
cur_ptr = new node; //Do not use this: new nodePtr
assert(cur_ptr != NULL); //Ensure that memory is allocatd
cur_ptr->data = info;
cur_ptr->next = NULL;
if (root == NULL) //For empty list, cur_ptr becomes the root
root = cur_prt;
else { //Insert cur as the root of the list
cur_ptr->next = root;
root = cur_ptr;
}
}
Exercise 4: Insert a node to the
end of the list
1-8
struct node {
int data;
node *next;
};
typedef node* nodePtr;
void appendNode(nodePtr& root, int info);
How many cases should we consider? What are these cases?
Exercise 4 - Insert a node to the end of the list
1-9
struct node {
int data;
node *next;
};
typedef node* nodePtr;
void appendNode(nodePtr& root, int info);
root
new_ptr
11
22 33
info
44
55
cur_ptr
6.16.1
6.26.2
void appendNode(nodePtr& root, int info) {
nodePtr new_ptr;
nodePtr cur_ptr;
new_ptr = new node; //Do not use this: new nodePtr
assert(new_Ptr != NULL); //Ensure that memory is allocatd
new_ptr->data = info;
new_ptr->next = NULL;
if (root == NULL) //For empty list, new_ptr becomes the root
root = new_ptr;
else { //Append the new node at the end of the list
cur_ptr = root;
while (cur_ptr->next != NULL)
cur_ptr = cur_ptr->next;
cur_ptr->next = new_ptr;
}
}
Exercise 5: Delete the head node
of the list
1-11
struct node {
int data;
node *next;
};
typedef node* nodePtr;
void deleteHead(nodePtr& root);
How many cases should we consider? What are these cases?
Exercise 5 - Delete the head node of the list
1-12
struct node {
int data;
node *next;
};
typedef node* nodePtr;
void deleteHead(nodePtr& root);
root
22
11 curPtr
33
void deleteHead(nodePtr& root) {
nodePtr cur_ptr;
if (root != NULL) {
cur_ptr = root; //Deleted node must be returned to OS
root = root->next;
delete cur_ptr;
}
else
cout << "This is an empty list. No deletion!n";
}
Review: Linked List
struct node {
int data;
node *next;
};
typedef node* nodePtr;
void insertNode(nodePtr& root, int info);
void appendNode(nodePtr& root, int info);
void deleteHead(nodePtr& root);
Exercise 6:
Delete the last node in the list
1-15
struct node {
int data;
node *next;
};
typedef node* nodePtr;
void deleteTail(nodePtr& root);
How many cases should we consider? What are these cases?
1.Implement the function
2.Write a test driver
Exercise 6 - Delete the last node in the list
1-16
struct node {
int data;
node *next;
};
typedef node* nodePtr;
void deleteTail(nodePtr& root);
root
11
22
33
pre_ptr cur_ptr
44
void deleteTail(nodePtr& root) {
nodePtr cur_prt, pre_ptr;
//There are three cases:
if (root == NULL) //Empty list
cout << "This is an empty list. No tail is deleted!n";
else {
if (root->next == NULL) { //List has one node
free(root); //or delete root;
root = NULL;
}
else { //List has more than one node
pre_ptr = root;
cur_ptr = root->next;
while (cur_ptr->next != NULL) {
pre_ptr = cur_ptr;
cur_ptr = cur_ptr->next;
}
pre_ptr->next = NULL;
delete cur_ptr; //or delete cur, pointing at the last node
}
}
}
Exercise 7:
Delete a specified node in the list
1-18
struct node {
int data;
node *next;
};
typedef node* nodePtr;
void deleteNode(nodePtr& root, int info);
How many cases should we consider? What are these cases?
1.Implement the function
2.Write a test driver
Exercise 7 - Delete a specified node in the list
1-19
void deleteNode(nodePtr& root, int info);
root
11
22
33
cur_ptr
44
pre_ptr
NULL
find
void deleteNode(nodePtr& root, int info) {
nodePtr cur_ptr, pre_ptr;
if (root == NULL) //Empty list
cout << "This is an empty list. No node is deleted!n";
else {
pre_ptr = NULL; cur_ptr = root;
while (cur_ptr != NULL) { //cur_ptr->next != NULL is Bug 1: coredump
if (cur_ptr->data != info) { //compare and does not match
pre_ptr = cur_ptr;
cur_ptr = cur_ptr->next;
}
else { //match and delete node pointed by cur
if (pre_ptr == NULL) { //cur is pointing to the first node
root = root->next;
delete cur_ptr;
cur_ptr = root;
}
else { //cur_ptr is NOT pointing to the first node
pre_ptr->next = cur_ptr->next;
delete cur_ptr;
cur_ptr = pre_ptr->next;
}
}

More Related Content

What's hot

Dynamic Memory allocation
Dynamic Memory allocationDynamic Memory allocation
Dynamic Memory allocation
Grishma Rajput
 
Linked list
Linked listLinked list
Linked list
A. S. M. Shafi
 
Linked list Output tracing
Linked list Output tracingLinked list Output tracing
Linked list Output tracing
Samsil Arefin
 
Singly & Circular Linked list
Singly & Circular Linked listSingly & Circular Linked list
Singly & Circular Linked list
Khulna University of Engineering & Tecnology
 
Dynamic Memory Allocation(DMA)
Dynamic Memory Allocation(DMA)Dynamic Memory Allocation(DMA)
Dynamic Memory Allocation(DMA)
Kamal Acharya
 
Data Structure (Dynamic Array and Linked List)
Data Structure (Dynamic Array and Linked List)Data Structure (Dynamic Array and Linked List)
Data Structure (Dynamic Array and Linked List)
Adam Mukharil Bachtiar
 
Dynamic memory Allocation in c language
Dynamic memory Allocation in c languageDynamic memory Allocation in c language
Dynamic memory Allocation in c language
kiran Patel
 
linked list
linked listlinked list
linked list
Shaista Qadir
 
Linked list
Linked listLinked list
Linked list
Trupti Agrawal
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
Naveen Gupta
 
Linked list
Linked listLinked list
Linked list
akshat360
 
Computer programming and utilization (2)
Computer programming and utilization (2)Computer programming and utilization (2)
Computer programming and utilization (2)
Digvijaysinh Gohil
 
linked list
linked listlinked list
linked list
Abbott
 
Memory allocation in c
Memory allocation in cMemory allocation in c
Memory allocation in c
Prabhu Govind
 
L3
L3L3
L3
lksoo
 
File
FileFile
File
Acad
 
Csc1100 lecture13 ch16_pt1
Csc1100 lecture13 ch16_pt1Csc1100 lecture13 ch16_pt1
Csc1100 lecture13 ch16_pt1IIUM
 
Link List
Link ListLink List
Link List
umiekalsum
 

What's hot (20)

Dynamic Memory allocation
Dynamic Memory allocationDynamic Memory allocation
Dynamic Memory allocation
 
Linked list
Linked listLinked list
Linked list
 
Team 10
Team 10Team 10
Team 10
 
Linked list Output tracing
Linked list Output tracingLinked list Output tracing
Linked list Output tracing
 
Singly & Circular Linked list
Singly & Circular Linked listSingly & Circular Linked list
Singly & Circular Linked list
 
Dynamic Memory Allocation(DMA)
Dynamic Memory Allocation(DMA)Dynamic Memory Allocation(DMA)
Dynamic Memory Allocation(DMA)
 
Data Structure (Dynamic Array and Linked List)
Data Structure (Dynamic Array and Linked List)Data Structure (Dynamic Array and Linked List)
Data Structure (Dynamic Array and Linked List)
 
Dynamic memory Allocation in c language
Dynamic memory Allocation in c languageDynamic memory Allocation in c language
Dynamic memory Allocation in c language
 
linked list
linked listlinked list
linked list
 
Linked list
Linked listLinked list
Linked list
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
 
Linked list
Linked listLinked list
Linked list
 
Computer programming and utilization (2)
Computer programming and utilization (2)Computer programming and utilization (2)
Computer programming and utilization (2)
 
linked list
linked listlinked list
linked list
 
Memory allocation in c
Memory allocation in cMemory allocation in c
Memory allocation in c
 
L3
L3L3
L3
 
File
FileFile
File
 
Lists
ListsLists
Lists
 
Csc1100 lecture13 ch16_pt1
Csc1100 lecture13 ch16_pt1Csc1100 lecture13 ch16_pt1
Csc1100 lecture13 ch16_pt1
 
Link List
Link ListLink List
Link List
 

Viewers also liked

Unidad 7
Unidad 7Unidad 7
Energy Efficient Data Storage Systems
Energy Efficient Data Storage SystemsEnergy Efficient Data Storage Systems
Energy Efficient Data Storage Systems
Xiao Qin
 
An Active and Hybrid Storage System for Data-intensive Applications
An Active and Hybrid Storage System for Data-intensive ApplicationsAn Active and Hybrid Storage System for Data-intensive Applications
An Active and Hybrid Storage System for Data-intensive Applications
Xiao Qin
 
How to do research?
How to do research?How to do research?
How to do research?
Xiao Qin
 
Reliability Analysis for an Energy-Aware RAID System
Reliability Analysis for an Energy-Aware RAID SystemReliability Analysis for an Energy-Aware RAID System
Reliability Analysis for an Energy-Aware RAID System
Xiao Qin
 
Nas'12 overview
Nas'12 overviewNas'12 overview
Nas'12 overview
Xiao Qin
 
OS/161 Overview
OS/161 OverviewOS/161 Overview
OS/161 Overview
Xiao Qin
 
COMP2710 Software Construction: header files
COMP2710 Software Construction: header filesCOMP2710 Software Construction: header files
COMP2710 Software Construction: header files
Xiao Qin
 
Project 2 how to modify OS/161
Project 2 how to modify OS/161Project 2 how to modify OS/161
Project 2 how to modify OS/161
Xiao Qin
 
IPCCC 2012 Conference Program Overview
IPCCC 2012 Conference Program OverviewIPCCC 2012 Conference Program Overview
IPCCC 2012 Conference Program Overview
Xiao Qin
 
Project 2 - how to compile os161?
Project 2 - how to compile os161?Project 2 - how to compile os161?
Project 2 - how to compile os161?
Xiao Qin
 
Common grammar mistakes
Common grammar mistakesCommon grammar mistakes
Common grammar mistakesXiao Qin
 
Thermal modeling and management of cluster storage systems xunfei jiang 2014
Thermal modeling and management of cluster storage systems xunfei jiang 2014Thermal modeling and management of cluster storage systems xunfei jiang 2014
Thermal modeling and management of cluster storage systems xunfei jiang 2014
Xiao Qin
 
Why Major in Computer Science and Software Engineering at Auburn University?
Why Major in Computer Science and Software Engineering at Auburn University?Why Major in Computer Science and Software Engineering at Auburn University?
Why Major in Computer Science and Software Engineering at Auburn University?
Xiao Qin
 
Project 2 How to modify os161: A Manual
Project 2 How to modify os161: A ManualProject 2 How to modify os161: A Manual
Project 2 How to modify os161: A Manual
Xiao Qin
 
Project 2 how to install and compile os161
Project 2 how to install and compile os161Project 2 how to install and compile os161
Project 2 how to install and compile os161
Xiao Qin
 
Surviving a group project
Surviving a group projectSurviving a group project
Surviving a group project
Xiao Qin
 
How to add system calls to OS/161
How to add system calls to OS/161How to add system calls to OS/161
How to add system calls to OS/161
Xiao Qin
 
Data center specific thermal and energy saving techniques
Data center specific thermal and energy saving techniquesData center specific thermal and energy saving techniques
Data center specific thermal and energy saving techniques
Xiao Qin
 
Understanding what our customer wants-slideshare
Understanding what our customer wants-slideshareUnderstanding what our customer wants-slideshare
Understanding what our customer wants-slideshare
Xiao Qin
 

Viewers also liked (20)

Unidad 7
Unidad 7Unidad 7
Unidad 7
 
Energy Efficient Data Storage Systems
Energy Efficient Data Storage SystemsEnergy Efficient Data Storage Systems
Energy Efficient Data Storage Systems
 
An Active and Hybrid Storage System for Data-intensive Applications
An Active and Hybrid Storage System for Data-intensive ApplicationsAn Active and Hybrid Storage System for Data-intensive Applications
An Active and Hybrid Storage System for Data-intensive Applications
 
How to do research?
How to do research?How to do research?
How to do research?
 
Reliability Analysis for an Energy-Aware RAID System
Reliability Analysis for an Energy-Aware RAID SystemReliability Analysis for an Energy-Aware RAID System
Reliability Analysis for an Energy-Aware RAID System
 
Nas'12 overview
Nas'12 overviewNas'12 overview
Nas'12 overview
 
OS/161 Overview
OS/161 OverviewOS/161 Overview
OS/161 Overview
 
COMP2710 Software Construction: header files
COMP2710 Software Construction: header filesCOMP2710 Software Construction: header files
COMP2710 Software Construction: header files
 
Project 2 how to modify OS/161
Project 2 how to modify OS/161Project 2 how to modify OS/161
Project 2 how to modify OS/161
 
IPCCC 2012 Conference Program Overview
IPCCC 2012 Conference Program OverviewIPCCC 2012 Conference Program Overview
IPCCC 2012 Conference Program Overview
 
Project 2 - how to compile os161?
Project 2 - how to compile os161?Project 2 - how to compile os161?
Project 2 - how to compile os161?
 
Common grammar mistakes
Common grammar mistakesCommon grammar mistakes
Common grammar mistakes
 
Thermal modeling and management of cluster storage systems xunfei jiang 2014
Thermal modeling and management of cluster storage systems xunfei jiang 2014Thermal modeling and management of cluster storage systems xunfei jiang 2014
Thermal modeling and management of cluster storage systems xunfei jiang 2014
 
Why Major in Computer Science and Software Engineering at Auburn University?
Why Major in Computer Science and Software Engineering at Auburn University?Why Major in Computer Science and Software Engineering at Auburn University?
Why Major in Computer Science and Software Engineering at Auburn University?
 
Project 2 How to modify os161: A Manual
Project 2 How to modify os161: A ManualProject 2 How to modify os161: A Manual
Project 2 How to modify os161: A Manual
 
Project 2 how to install and compile os161
Project 2 how to install and compile os161Project 2 how to install and compile os161
Project 2 how to install and compile os161
 
Surviving a group project
Surviving a group projectSurviving a group project
Surviving a group project
 
How to add system calls to OS/161
How to add system calls to OS/161How to add system calls to OS/161
How to add system calls to OS/161
 
Data center specific thermal and energy saving techniques
Data center specific thermal and energy saving techniquesData center specific thermal and energy saving techniques
Data center specific thermal and energy saving techniques
 
Understanding what our customer wants-slideshare
Understanding what our customer wants-slideshareUnderstanding what our customer wants-slideshare
Understanding what our customer wants-slideshare
 

Similar to COMP2710: Software Construction - Linked list exercises

Linked List.pptx
Linked List.pptxLinked List.pptx
Linked List.pptx
PoonamPatil120
 
DSA(1).pptx
DSA(1).pptxDSA(1).pptx
DSA(1).pptx
DaniyalAli81
 
This assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdfThis assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdf
EricvtJFraserr
 
Write java program using linked list to get integer from user and.docx
 Write java program using linked list to get integer from user and.docx Write java program using linked list to get integer from user and.docx
Write java program using linked list to get integer from user and.docx
ajoy21
 
linkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptxlinkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptx
MeghaKulkarni27
 
Write a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdfWrite a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdf
JUSTSTYLISH3B2MOHALI
 
Unit - 2.pdf
Unit - 2.pdfUnit - 2.pdf
Unit - 2.pdf
AravindAnand21
 
How do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdfHow do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdf
feelinggift
 
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdfTHE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
fathimahardwareelect
 
Data Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdfData Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdf
rohit219406
 
C Exam Help
C Exam Help C Exam Help
C Exam Help
Programming Exam Help
 
In C++Write a recursive function to determine whether or not a Lin.pdf
In C++Write a recursive function to determine whether or not a Lin.pdfIn C++Write a recursive function to determine whether or not a Lin.pdf
In C++Write a recursive function to determine whether or not a Lin.pdf
flashfashioncasualwe
 
Doublylinklist
DoublylinklistDoublylinklist
Doublylinklistritu1806
 
linkedlistwith animations.ppt
linkedlistwith animations.pptlinkedlistwith animations.ppt
linkedlistwith animations.ppt
MuhammadShafi89
 
Ll.pptx
Ll.pptxLl.pptx
Ll.pptx
chin463670
 
linked list.pptx
linked list.pptxlinked list.pptx
linked list.pptx
chin463670
 
Use C++class Node{public   Node ( int = 0 );       constru.pdf
Use C++class Node{public   Node ( int = 0 );        constru.pdfUse C++class Node{public   Node ( int = 0 );        constru.pdf
Use C++class Node{public   Node ( int = 0 );       constru.pdf
optokunal1
 
C Homework Help
C Homework HelpC Homework Help
C Homework Help
Programming Homework Help
 
Template LinkedList;I am using templates to make some linkedLists.pdf
Template LinkedList;I am using templates to make some linkedLists.pdfTemplate LinkedList;I am using templates to make some linkedLists.pdf
Template LinkedList;I am using templates to make some linkedLists.pdf
fatoryoutlets
 

Similar to COMP2710: Software Construction - Linked list exercises (20)

Linked List.pptx
Linked List.pptxLinked List.pptx
Linked List.pptx
 
DSA(1).pptx
DSA(1).pptxDSA(1).pptx
DSA(1).pptx
 
This assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdfThis assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdf
 
Unit7 C
Unit7 CUnit7 C
Unit7 C
 
Write java program using linked list to get integer from user and.docx
 Write java program using linked list to get integer from user and.docx Write java program using linked list to get integer from user and.docx
Write java program using linked list to get integer from user and.docx
 
linkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptxlinkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptx
 
Write a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdfWrite a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdf
 
Unit - 2.pdf
Unit - 2.pdfUnit - 2.pdf
Unit - 2.pdf
 
How do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdfHow do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdf
 
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdfTHE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
 
Data Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdfData Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdf
 
C Exam Help
C Exam Help C Exam Help
C Exam Help
 
In C++Write a recursive function to determine whether or not a Lin.pdf
In C++Write a recursive function to determine whether or not a Lin.pdfIn C++Write a recursive function to determine whether or not a Lin.pdf
In C++Write a recursive function to determine whether or not a Lin.pdf
 
Doublylinklist
DoublylinklistDoublylinklist
Doublylinklist
 
linkedlistwith animations.ppt
linkedlistwith animations.pptlinkedlistwith animations.ppt
linkedlistwith animations.ppt
 
Ll.pptx
Ll.pptxLl.pptx
Ll.pptx
 
linked list.pptx
linked list.pptxlinked list.pptx
linked list.pptx
 
Use C++class Node{public   Node ( int = 0 );       constru.pdf
Use C++class Node{public   Node ( int = 0 );        constru.pdfUse C++class Node{public   Node ( int = 0 );        constru.pdf
Use C++class Node{public   Node ( int = 0 );       constru.pdf
 
C Homework Help
C Homework HelpC Homework Help
C Homework Help
 
Template LinkedList;I am using templates to make some linkedLists.pdf
Template LinkedList;I am using templates to make some linkedLists.pdfTemplate LinkedList;I am using templates to make some linkedLists.pdf
Template LinkedList;I am using templates to make some linkedLists.pdf
 

More from Xiao Qin

How to apply for internship positions?
How to apply for internship positions?How to apply for internship positions?
How to apply for internship positions?
Xiao Qin
 
How to write research papers? Version 5.0
How to write research papers? Version 5.0How to write research papers? Version 5.0
How to write research papers? Version 5.0
Xiao Qin
 
Making a competitive nsf career proposal: Part 2 Worksheet
Making a competitive nsf career proposal: Part 2 WorksheetMaking a competitive nsf career proposal: Part 2 Worksheet
Making a competitive nsf career proposal: Part 2 Worksheet
Xiao Qin
 
Making a competitive nsf career proposal: Part 1 Tips
Making a competitive nsf career proposal: Part 1 TipsMaking a competitive nsf career proposal: Part 1 Tips
Making a competitive nsf career proposal: Part 1 Tips
Xiao Qin
 
Auburn csse faculty orientation
Auburn csse faculty orientationAuburn csse faculty orientation
Auburn csse faculty orientation
Xiao Qin
 
Auburn CSSE graduate student orientation
Auburn CSSE graduate student orientationAuburn CSSE graduate student orientation
Auburn CSSE graduate student orientation
Xiao Qin
 
CSSE Graduate Programs Committee: Progress Report
CSSE Graduate Programs Committee: Progress ReportCSSE Graduate Programs Committee: Progress Report
CSSE Graduate Programs Committee: Progress Report
Xiao Qin
 
P#1 stream of praise
P#1 stream of praiseP#1 stream of praise
P#1 stream of praise
Xiao Qin
 
HDFS-HC2: Analysis of Data Placement Strategy based on Computing Power of Nod...
HDFS-HC2: Analysis of Data Placement Strategy based on Computing Power of Nod...HDFS-HC2: Analysis of Data Placement Strategy based on Computing Power of Nod...
HDFS-HC2: Analysis of Data Placement Strategy based on Computing Power of Nod...
Xiao Qin
 
Performance Evaluation of Traditional Caching Policies on a Large System with...
Performance Evaluation of Traditional Caching Policies on a Large System with...Performance Evaluation of Traditional Caching Policies on a Large System with...
Performance Evaluation of Traditional Caching Policies on a Large System with...
Xiao Qin
 
Reliability Modeling and Analysis of Energy-Efficient Storage Systems
Reliability Modeling and Analysis of Energy-Efficient Storage SystemsReliability Modeling and Analysis of Energy-Efficient Storage Systems
Reliability Modeling and Analysis of Energy-Efficient Storage Systems
Xiao Qin
 

More from Xiao Qin (11)

How to apply for internship positions?
How to apply for internship positions?How to apply for internship positions?
How to apply for internship positions?
 
How to write research papers? Version 5.0
How to write research papers? Version 5.0How to write research papers? Version 5.0
How to write research papers? Version 5.0
 
Making a competitive nsf career proposal: Part 2 Worksheet
Making a competitive nsf career proposal: Part 2 WorksheetMaking a competitive nsf career proposal: Part 2 Worksheet
Making a competitive nsf career proposal: Part 2 Worksheet
 
Making a competitive nsf career proposal: Part 1 Tips
Making a competitive nsf career proposal: Part 1 TipsMaking a competitive nsf career proposal: Part 1 Tips
Making a competitive nsf career proposal: Part 1 Tips
 
Auburn csse faculty orientation
Auburn csse faculty orientationAuburn csse faculty orientation
Auburn csse faculty orientation
 
Auburn CSSE graduate student orientation
Auburn CSSE graduate student orientationAuburn CSSE graduate student orientation
Auburn CSSE graduate student orientation
 
CSSE Graduate Programs Committee: Progress Report
CSSE Graduate Programs Committee: Progress ReportCSSE Graduate Programs Committee: Progress Report
CSSE Graduate Programs Committee: Progress Report
 
P#1 stream of praise
P#1 stream of praiseP#1 stream of praise
P#1 stream of praise
 
HDFS-HC2: Analysis of Data Placement Strategy based on Computing Power of Nod...
HDFS-HC2: Analysis of Data Placement Strategy based on Computing Power of Nod...HDFS-HC2: Analysis of Data Placement Strategy based on Computing Power of Nod...
HDFS-HC2: Analysis of Data Placement Strategy based on Computing Power of Nod...
 
Performance Evaluation of Traditional Caching Policies on a Large System with...
Performance Evaluation of Traditional Caching Policies on a Large System with...Performance Evaluation of Traditional Caching Policies on a Large System with...
Performance Evaluation of Traditional Caching Policies on a Large System with...
 
Reliability Modeling and Analysis of Energy-Efficient Storage Systems
Reliability Modeling and Analysis of Energy-Efficient Storage SystemsReliability Modeling and Analysis of Energy-Efficient Storage Systems
Reliability Modeling and Analysis of Energy-Efficient Storage Systems
 

Recently uploaded

Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
Peter Windle
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Atul Kumar Singh
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Po-Chuan Chen
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
DhatriParmar
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
timhan337
 

Recently uploaded (20)

Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
 

COMP2710: Software Construction - Linked list exercises

  • 1. COMP 2710 Software Construction Linked List – Exercises Dr. Xiao Qin Department of Computer Science and Software Engineering Auburn University http://www.eng.auburn.edu/~xqin xqin@auburn.edu
  • 2. Exercise 1 1-2 • Task 1: Define a structure named node, where there are two items – (1) data whose type is int and (2) a pointer pointing to the next node • Task 2: Using typedef to define a new type (e.g., nodePtr) of pointer pointing to node (i.e., node*) • Task 3: Create a pointer (e.g., node_ptr) using the above new data type • Task 4: Allocate memory resource for the new pointer • Task 5: Assign value (e.g., 10) to the node pointed by node_ptr
  • 3. Exercise 2: Print a list of nodes struct node { int data; node *next; }; typedef node* nodePtr; void printList(nodePtr root); What two cases should we consider?
  • 4. Answer: Print a list of nodes void printList(nodePtr root) { nodePtr cur; if (root == NULL) cout << "This is an empty listn"; cur = root; while (cur != NULL) { cout << cur->data << endl; cur = cur->next; } }
  • 5. Exercise 3: Insert a node to the head of the list struct node { int data; node *next; }; typedef node* nodePtr; void insertNode(nodePtr& root, int info); //Another possible prototype void insertNode(nodePtr& root, nodePtr newNodePtr); What is the difference between the above two prototypes? How many cases should we consider? What are these cases?
  • 6. Exercise 3 - Insert a node to the head of the list 1-6 struct node { int data; node *next; }; typedef node* nodePtr; void insertNode(nodePtr& root, int info); root cur_ptr 11 22 33 info 44 55 66
  • 7. Exercise 3: Answer Insert a node to the head of the list void insertNode(nodePtr& root, int info) { nodePtr cur_prt; cur_ptr = new node; //Do not use this: new nodePtr assert(cur_ptr != NULL); //Ensure that memory is allocatd cur_ptr->data = info; cur_ptr->next = NULL; if (root == NULL) //For empty list, cur_ptr becomes the root root = cur_prt; else { //Insert cur as the root of the list cur_ptr->next = root; root = cur_ptr; } }
  • 8. Exercise 4: Insert a node to the end of the list 1-8 struct node { int data; node *next; }; typedef node* nodePtr; void appendNode(nodePtr& root, int info); How many cases should we consider? What are these cases?
  • 9. Exercise 4 - Insert a node to the end of the list 1-9 struct node { int data; node *next; }; typedef node* nodePtr; void appendNode(nodePtr& root, int info); root new_ptr 11 22 33 info 44 55 cur_ptr 6.16.1 6.26.2
  • 10. void appendNode(nodePtr& root, int info) { nodePtr new_ptr; nodePtr cur_ptr; new_ptr = new node; //Do not use this: new nodePtr assert(new_Ptr != NULL); //Ensure that memory is allocatd new_ptr->data = info; new_ptr->next = NULL; if (root == NULL) //For empty list, new_ptr becomes the root root = new_ptr; else { //Append the new node at the end of the list cur_ptr = root; while (cur_ptr->next != NULL) cur_ptr = cur_ptr->next; cur_ptr->next = new_ptr; } }
  • 11. Exercise 5: Delete the head node of the list 1-11 struct node { int data; node *next; }; typedef node* nodePtr; void deleteHead(nodePtr& root); How many cases should we consider? What are these cases?
  • 12. Exercise 5 - Delete the head node of the list 1-12 struct node { int data; node *next; }; typedef node* nodePtr; void deleteHead(nodePtr& root); root 22 11 curPtr 33
  • 13. void deleteHead(nodePtr& root) { nodePtr cur_ptr; if (root != NULL) { cur_ptr = root; //Deleted node must be returned to OS root = root->next; delete cur_ptr; } else cout << "This is an empty list. No deletion!n"; }
  • 14. Review: Linked List struct node { int data; node *next; }; typedef node* nodePtr; void insertNode(nodePtr& root, int info); void appendNode(nodePtr& root, int info); void deleteHead(nodePtr& root);
  • 15. Exercise 6: Delete the last node in the list 1-15 struct node { int data; node *next; }; typedef node* nodePtr; void deleteTail(nodePtr& root); How many cases should we consider? What are these cases? 1.Implement the function 2.Write a test driver
  • 16. Exercise 6 - Delete the last node in the list 1-16 struct node { int data; node *next; }; typedef node* nodePtr; void deleteTail(nodePtr& root); root 11 22 33 pre_ptr cur_ptr 44
  • 17. void deleteTail(nodePtr& root) { nodePtr cur_prt, pre_ptr; //There are three cases: if (root == NULL) //Empty list cout << "This is an empty list. No tail is deleted!n"; else { if (root->next == NULL) { //List has one node free(root); //or delete root; root = NULL; } else { //List has more than one node pre_ptr = root; cur_ptr = root->next; while (cur_ptr->next != NULL) { pre_ptr = cur_ptr; cur_ptr = cur_ptr->next; } pre_ptr->next = NULL; delete cur_ptr; //or delete cur, pointing at the last node } } }
  • 18. Exercise 7: Delete a specified node in the list 1-18 struct node { int data; node *next; }; typedef node* nodePtr; void deleteNode(nodePtr& root, int info); How many cases should we consider? What are these cases? 1.Implement the function 2.Write a test driver
  • 19. Exercise 7 - Delete a specified node in the list 1-19 void deleteNode(nodePtr& root, int info); root 11 22 33 cur_ptr 44 pre_ptr NULL find
  • 20. void deleteNode(nodePtr& root, int info) { nodePtr cur_ptr, pre_ptr; if (root == NULL) //Empty list cout << "This is an empty list. No node is deleted!n"; else { pre_ptr = NULL; cur_ptr = root; while (cur_ptr != NULL) { //cur_ptr->next != NULL is Bug 1: coredump if (cur_ptr->data != info) { //compare and does not match pre_ptr = cur_ptr; cur_ptr = cur_ptr->next; } else { //match and delete node pointed by cur if (pre_ptr == NULL) { //cur is pointing to the first node root = root->next; delete cur_ptr; cur_ptr = root; } else { //cur_ptr is NOT pointing to the first node pre_ptr->next = cur_ptr->next; delete cur_ptr; cur_ptr = pre_ptr->next; } }

Editor's Notes

  1. Programming Language: C or C++
  2. Spring’15: slides 14-16 - See also Lec08c2-Lnked List Exercise 1.ppt
  3. Empty List Non-empty list void printList(nodePtr root) { nodePtr cur; if (root == NULL) cout &amp;lt;&amp;lt; &amp;quot;This is an empty list\n&amp;quot;; cur = root; while (cur != NULL) { cout &amp;lt;&amp;lt; cur-&amp;gt;data &amp;lt;&amp;lt; endl; cur = cur-&amp;gt;next; } }
  4. Empty List Non-empty list void printList(nodePtr root) { nodePtr cur; if (root == NULL) cout &amp;lt;&amp;lt; &amp;quot;This is an empty list\n&amp;quot;; cur = root; while (cur != NULL) { cout &amp;lt;&amp;lt; cur-&amp;gt;data &amp;lt;&amp;lt; endl; cur = cur-&amp;gt;next; } }
  5. void insertNode(nodePtr&amp; root, int info) { nodePtr cur; cur = new node; //Do not use this: new nodePtr assert(cur != NULL); //Ensure that memory is allocatd cur-&amp;gt;data = info; cur-&amp;gt;next = NULL; if (root == NULL) //If the list is empty, cur becomes the root root = cur; else { //Insert cur as the root of the list cur-&amp;gt;next = root; root = cur; } }
  6. void appendNode(nodePtr&amp; root, int info) { nodePtr cur; nodePtr pre; cur = new node; //Do not use this: new nodePtr assert(cur != NULL); //Ensure that memory is allocatd cur-&amp;gt;data = info; cur-&amp;gt;next = NULL; if (root == NULL) //If the list is empty, cur becomes the root root = cur; else { //Append the new node at the end of the list pre = root; while (pre-&amp;gt;next != NULL) pre = pre-&amp;gt;next; pre-&amp;gt;next = cur; } }
  7. void insertNode(nodePtr&amp; root, int info) { nodePtr cur; cur = new node; //Do not use this: new nodePtr assert(cur != NULL); //Ensure that memory is allocatd cur-&amp;gt;data = info; cur-&amp;gt;next = NULL; if (root == NULL) //If the list is empty, cur becomes the root root = cur; else { //Insert cur as the root of the list cur-&amp;gt;next = root; root = cur; } }
  8. void appendNode(nodePtr&amp; root, int info) { nodePtr cur; nodePtr pre; cur = new node; //Do not use this: new nodePtr assert(cur != NULL); //Ensure that memory is allocatd cur-&amp;gt;data = info; cur-&amp;gt;next = NULL; if (root == NULL) //If the list is empty, cur becomes the root root = cur; else { //Append the new node at the end of the list pre = root; while (pre-&amp;gt;next != NULL) pre = pre-&amp;gt;next; pre-&amp;gt;next = cur; } }
  9. void appendNode(nodePtr&amp; root, int info) { nodePtr cur; nodePtr pre; cur = new node; //Do not use this: new nodePtr assert(cur != NULL); //Ensure that memory is allocatd cur-&amp;gt;data = info; cur-&amp;gt;next = NULL; if (root == NULL) //If the list is empty, cur becomes the root root = cur; else { //Append the new node at the end of the list pre = root; while (pre-&amp;gt;next != NULL) pre = pre-&amp;gt;next; pre-&amp;gt;next = cur; } }
  10. void deleteHead(nodePtr&amp; root); void deleteHead(nodePtr&amp; root) { nodePtr cur; if (root != NULL) { cur = root; //Deleted node must be returned to the OS root = root-&amp;gt;next; delete cur; } else cout &amp;lt;&amp;lt; &amp;quot;This is an empty list. No head is deleted!\n&amp;quot;; }
  11. void appendNode(nodePtr&amp; root, int info) { nodePtr cur; nodePtr pre; cur = new node; //Do not use this: new nodePtr assert(cur != NULL); //Ensure that memory is allocatd cur-&amp;gt;data = info; cur-&amp;gt;next = NULL; if (root == NULL) //If the list is empty, cur becomes the root root = cur; else { //Append the new node at the end of the list pre = root; while (pre-&amp;gt;next != NULL) pre = pre-&amp;gt;next; pre-&amp;gt;next = cur; } }
  12. void insertNode(nodePtr&amp; root, int info) { nodePtr cur; cur = new node; //Do not use this: new nodePtr assert(cur != NULL); //Ensure that memory is allocatd cur-&amp;gt;data = info; cur-&amp;gt;next = NULL; if (root == NULL) //If the list is empty, cur becomes the root root = cur; else { //Insert cur as the root of the list cur-&amp;gt;next = root; root = cur; } }
  13. void deleteTail(nodePtr&amp; root) { nodePtr cur, pre; if (root == NULL) //Empty list cout &amp;lt;&amp;lt; &amp;quot;This is an empty list. No tail is deleted!\n&amp;quot;; else { if (root-&amp;gt;next == NULL) { //List has one node free(root); root = NULL; } else { //List has more than one node pre = root; cur = root-&amp;gt;next; while (cur-&amp;gt;next != NULL) { pre = cur; cur = cur-&amp;gt;next; } pre-&amp;gt;next = NULL; free(cur); //delete cur, which is pointing at the last node } } }
  14. void appendNode(nodePtr&amp; root, int info) { nodePtr cur; nodePtr pre; cur = new node; //Do not use this: new nodePtr assert(cur != NULL); //Ensure that memory is allocatd cur-&amp;gt;data = info; cur-&amp;gt;next = NULL; if (root == NULL) //If the list is empty, cur becomes the root root = cur; else { //Append the new node at the end of the list pre = root; while (pre-&amp;gt;next != NULL) pre = pre-&amp;gt;next; pre-&amp;gt;next = cur; } }
  15. void deleteNode(nodePtr&amp; root, int info) { nodePtr cur, pre; if (root == NULL) //Empty list cout &amp;lt;&amp;lt; &amp;quot;This is an empty list. No node is deleted!\n&amp;quot;; else { pre = NULL; cur = root; while (cur != NULL) { //cur-&amp;gt;next != NULL is Bug 1 - core dump occur if (cur-&amp;gt;data != info) { //compare and does not match pre = cur; cur = cur-&amp;gt;next; } else { //match and delete node pointed by cur if (pre == NULL) { //cur is pointing to the first node root = root-&amp;gt;next; delete cur; cur = root; } else { //cur is NOT pointing to the first node pre-&amp;gt;next = cur-&amp;gt;next; delete cur; cur = pre-&amp;gt;next; } } } //Bug 2: With the following two statement, only when delete one-node list, can core dump occur //pre-&amp;gt;next = NULL; //adding these two statements - core dump occur //delete cur; //delete cur, which is pointing at the last node } }
  16. void appendNode(nodePtr&amp; root, int info) { nodePtr cur; nodePtr pre; cur = new node; //Do not use this: new nodePtr assert(cur != NULL); //Ensure that memory is allocatd cur-&amp;gt;data = info; cur-&amp;gt;next = NULL; if (root == NULL) //If the list is empty, cur becomes the root root = cur; else { //Append the new node at the end of the list pre = root; while (pre-&amp;gt;next != NULL) pre = pre-&amp;gt;next; pre-&amp;gt;next = cur; } }