SlideShare ist ein Scribd-Unternehmen logo
1 von 20
Downloaden Sie, um offline zu lesen
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;
}
}

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (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
 

Andere mochten auch

Energy Efficient Data Storage Systems
Energy Efficient Data Storage SystemsEnergy Efficient Data Storage Systems
Energy Efficient Data Storage SystemsXiao 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 ApplicationsXiao 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 SystemXiao Qin
 
Nas'12 overview
Nas'12 overviewNas'12 overview
Nas'12 overviewXiao Qin
 
OS/161 Overview
OS/161 OverviewOS/161 Overview
OS/161 OverviewXiao Qin
 
COMP2710 Software Construction: header files
COMP2710 Software Construction: header filesCOMP2710 Software Construction: header files
COMP2710 Software Construction: header filesXiao 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/161Xiao Qin
 
IPCCC 2012 Conference Program Overview
IPCCC 2012 Conference Program OverviewIPCCC 2012 Conference Program Overview
IPCCC 2012 Conference Program OverviewXiao 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 2014Xiao 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 ManualXiao 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 os161Xiao Qin
 
Surviving a group project
Surviving a group projectSurviving a group project
Surviving a group projectXiao 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/161Xiao 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 techniquesXiao Qin
 
Understanding what our customer wants-slideshare
Understanding what our customer wants-slideshareUnderstanding what our customer wants-slideshare
Understanding what our customer wants-slideshareXiao Qin
 

Andere mochten auch (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
 

Ähnlich wie COMP2710: Software Construction - Linked list exercises

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.pdfEricvtJFraserr
 
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.docxajoy21
 
linkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptxlinkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptxMeghaKulkarni27
 
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.pdfJUSTSTYLISH3B2MOHALI
 
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.pdffeelinggift
 
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 .pdffathimahardwareelect
 
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 .pdfrohit219406
 
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.pdfflashfashioncasualwe
 
Doublylinklist
DoublylinklistDoublylinklist
Doublylinklistritu1806
 
linkedlistwith animations.ppt
linkedlistwith animations.pptlinkedlistwith animations.ppt
linkedlistwith animations.pptMuhammadShafi89
 
linked list.pptx
linked list.pptxlinked list.pptx
linked list.pptxchin463670
 
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.pdfoptokunal1
 
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.pdffatoryoutlets
 

Ähnlich wie 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
 
linked list.pptx
linked list.pptxlinked list.pptx
linked list.pptx
 
Ll.pptx
Ll.pptxLl.pptx
Ll.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
 

Mehr von 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.0Xiao 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 WorksheetXiao 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 TipsXiao Qin
 
Auburn csse faculty orientation
Auburn csse faculty orientationAuburn csse faculty orientation
Auburn csse faculty orientationXiao Qin
 
Auburn CSSE graduate student orientation
Auburn CSSE graduate student orientationAuburn CSSE graduate student orientation
Auburn CSSE graduate student orientationXiao Qin
 
CSSE Graduate Programs Committee: Progress Report
CSSE Graduate Programs Committee: Progress ReportCSSE Graduate Programs Committee: Progress Report
CSSE Graduate Programs Committee: Progress ReportXiao Qin
 
P#1 stream of praise
P#1 stream of praiseP#1 stream of praise
P#1 stream of praiseXiao 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 SystemsXiao Qin
 

Mehr von 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
 

Kürzlich hochgeladen

Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
Indexing Structures in Database Management system.pdf
Indexing Structures in Database Management system.pdfIndexing Structures in Database Management system.pdf
Indexing Structures in Database Management system.pdfChristalin Nelson
 
Using Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea DevelopmentUsing Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea Developmentchesterberbo7
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4JOYLYNSAMANIEGO
 
How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17Celine George
 
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxGrade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxkarenfajardo43
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Association for Project Management
 
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxDIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxMichelleTuguinay1
 
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxBIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxSayali Powar
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...DhatriParmar
 
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDecoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDhatriParmar
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptxmary850239
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 
Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1GloryAnnCastre1
 
ICS 2208 Lecture Slide Notes for Topic 6
ICS 2208 Lecture Slide Notes for Topic 6ICS 2208 Lecture Slide Notes for Topic 6
ICS 2208 Lecture Slide Notes for Topic 6Vanessa Camilleri
 
4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptxmary850239
 

Kürzlich hochgeladen (20)

prashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Professionprashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Profession
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
Indexing Structures in Database Management system.pdf
Indexing Structures in Database Management system.pdfIndexing Structures in Database Management system.pdf
Indexing Structures in Database Management system.pdf
 
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptxINCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
 
Using Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea DevelopmentUsing Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea Development
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4
 
How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17
 
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxGrade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
 
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxDIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
 
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxBIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
 
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDecoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
 
Mattingly "AI & Prompt Design: Large Language Models"
Mattingly "AI & Prompt Design: Large Language Models"Mattingly "AI & Prompt Design: Large Language Models"
Mattingly "AI & Prompt Design: Large Language Models"
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 
Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1
 
ICS 2208 Lecture Slide Notes for Topic 6
ICS 2208 Lecture Slide Notes for Topic 6ICS 2208 Lecture Slide Notes for Topic 6
ICS 2208 Lecture Slide Notes for Topic 6
 
4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.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; } }

Hinweis der Redaktion

  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; } }