SlideShare una empresa de Scribd logo
1 de 45
Topics
 Meaning of a Linked List
 Meaning of a Dynamic Linked List
 Traversal, Insertion and Deletion of Elements in a
Dynamic Linked List
 Specification of a Dynamic Linked Sorted List
 Insertion and Deletion of Elements in a Dynamic
Linked Sorted List
To implement the List ADT
The programmer must
1) choose a concrete data
representation for the list, and
2) implement the list operations
Recall:
4 Basic Kinds of ADT Operations
 Constructors -- create a new instance
(object) of an ADT
 Transformers -- change the state of one or
more of the data values of an instance
 Observers -- allow client to observe the
state of one or more of the data values of
an instance without changing them
 Iterators -- allow client to access the data
values in sequence
List Operations
Transformers
 Insert
 Delete
 Sort
Observers
 IsEmpty
 IsFull
 Length
 IsPresent
change state
observe state
4
ADT List Operations
Iterator
 Reset
 GetNextItem
 Reset prepares for the iteration
 GetNextItem returns the next item in
sequence
 No transformer can be called between
calls to GetNextItem (Why?)
Iteration Pair
Array-based class List
Reset
IsFull
Length
IsPresent
Delete
IsEmpty
Insert
GetNexItem
Private data:
length
data [0]
[1]
[2]
[MAX_LENGTH-1]
currentPos
SelSort
// Specification file array-based list (list.h)
const int MAX_LENGTH = 50;
typedef int ItemType;
class List // Declares a class data type
{
public: // Public member functions
List(); // constructor
bool IsEmpty () const;
bool IsFull () const;
int Length () const; // Returns length of list
void Insert (ItemType item);
void Delete (ItemType item);
bool IsPresent(ItemType item) const;
void SelSort ();
void Reset ();
ItemType GetNextItem ();
private: // Private data members
int length; // Number of values currently stored
ItemType data[MAX_LENGTH];
int CurrentPos; // Used in iteration
};
7
Implementation Structures
 Use a built-in array stored in contiguous
memory locations, implementing operations
Insert and Delete by moving list items
around in the array, as needed
 Use a linked list in which items are not
necessarily stored in contiguous memory
locations
 A linked list avoids excessive data movement
from insertions and deletions
Implementation Possibilities for a List
List
Linked list
Built-in array
Built-in
dynamic data
and pointers
Built-in array
of structs
A Linked List
 A linked list is a list in which the order of
the components is determined by an
explicit link member in each node
 Each node is a struct containing a data
member and a link member that gives the
location of the next node in the list
head ‘X’ ‘C’ ‘L’
Dynamic Linked List
head “Ted” “Irv” “Lee”
 A dynamic linked list is one in which the
nodes are linked together by pointers and
an external pointer (or head pointer)
points to the first node in the list
Nodes can be located anywhere in memory
 The link member holds the memory
address of the next node in the list
head 3000 “Ted” 5000 “Irv” 2000 “Lee” NULL
3000 5000 2000
// Type declarations
struct NodeType
{
char info;
NodeType* link;
}
typedef NodeType* NodePtr;
// Variable DECLARATIONS
NodePtr head;
NodePtr ptr;
13
Declarations for a Dynamic Linked List
.info .link
‘A’ 6000
Pointer Dereferencing and Member Selection
.info .link
‘A’ 6000
ptr
ptr
ptr
.info .link
‘A’ 6000
*ptr
ptr
.info .link
(*ptr).info
ptr->info
‘A’ 6000
15
ptr is a pointer to a node
.info .link
‘A’ 6000
ptr
ptr
16
*ptr is the entire node pointed to by ptr
ptr
.info .link
‘A’ 6000
*ptr
17
ptr->info is a node member
ptr
.info .link
ptr->info
(*ptr).info // Equivalent
‘A’ 6000
18
ptr->link is a node member
ptr
.info .link
ptr->link
(*ptr).link // Equivalent
‘A’ 6000
Traversing a Dynamic Linked List
// Pre: head points to a dynamic linked list
ptr = head;
while (ptr != NULL)
{
cout << ptr->info;
// Or, do something else with node *ptr
ptr = ptr->link;
}
ptr
3000 “Ted” 5000 “Irv” 2000 “Lee” NULL
3000 5000 2000
head
// Pre: head points to a dynamic linked list
ptr = head;
while (ptr != NULL)
{
cout << ptr->info;
// Or, do something else with node *ptr
ptr = ptr->link;
}
ptr 3000
3000 “Ted” 5000 “Irv” 2000 “Lee” NULL
3000 5000 2000
head
Traversing a Dynamic Linked List
// Pre: head points to a dynamic linked list
ptr = head;
while (ptr != NULL)
{
cout << ptr->info;
// Or, do something else with node *ptr
ptr = ptr->link;
}
ptr 3000
3000 “Ted” 5000 “Irv” 2000 “Lee” NULL
3000 5000 2000
head
Traversing a Dynamic Linked List
// Pre: head points to a dynamic linked list
ptr = head;
while (ptr != NULL)
{
cout << ptr->info;
// Or, do something else with node *ptr
ptr = ptr->link;
}
ptr 3000
3000 “Ted” 5000 “Irv” 2000 “Lee” NULL
3000 5000 2000
head
Traversing a Dynamic Linked List
// Pre: head points to a dynamic linked list
ptr = head;
while (ptr != NULL)
{
cout << ptr->info;
// Or, do something else with node *ptr
ptr = ptr->link;
}
ptr 5000
3000 “Ted” 5000 “Irv” 2000 “Lee” NULL
3000 5000 2000
head
Traversing a Dynamic Linked List
// Pre: head points to a dynamic linked list
ptr = head;
while (ptr != NULL)
{
cout << ptr->info;
// Or, do something else with node *ptr
ptr = ptr->link;
}
ptr 5000
3000 “Ted” 5000 “Irv” 2000 “Lee” NULL
3000 5000 2000
head
Traversing a Dynamic Linked List
// Pre: head points to a dynamic linked list
ptr = head;
while (ptr != NULL)
{
cout << ptr->info;
// Or, do something else with node *ptr
ptr = ptr->link;
}
ptr 5000
3000 “Ted” 5000 “Irv” 2000 “Lee” NULL
3000 5000 2000
head
Traversing a Dynamic Linked List
// Pre: head points to a dynamic linked list
ptr = head;
while (ptr != NULL)
{
cout << ptr->info;
// Or, do something else with node *ptr
ptr = ptr->link;
}
ptr 2000
3000 “Ted” 5000 “Irv” 2000 “Lee” NULL
3000 5000 2000
head
Traversing a Dynamic Linked List
// Pre: head points to a dynamic linked list
ptr = head;
while (ptr != NULL)
{
cout << ptr->info;
// Or, do something else with node *ptr
ptr = ptr->link;
}
ptr 2000
3000 “Ted” 5000 “Irv” 2000 “Lee” NULL
3000 5000 2000
head
Traversing a Dynamic Linked List
// Pre: head points to a dynamic linked list
ptr = head;
while (ptr != NULL)
{
cout << ptr->info;
// Or, do something else with node *ptr
ptr = ptr->link;
}
ptr 2000
3000 “Ted” 5000 “Irv” 2000 “Lee” NULL
3000 5000 2000
head
Traversing a Dynamic Linked List
// Pre: head points to a dynamic linked list
ptr = head;
while (ptr != NULL)
{
cout << ptr->info;
// Or, do something else with node *ptr
ptr = ptr->link;
}
ptr NULL
3000 “Ted” 5000 “Irv” 2000 “Lee” NULL
3000 5000 2000
head
Traversing a Dynamic Linked List
// Pre: head points to a dynamic linked list
ptr = head;
while (ptr != NULL)
{
cout << ptr->info;
// Or, do something else with node *ptr
ptr = ptr->link;
}
ptr NULL
3000 “Ted” 5000 “Irv” 2000 “Lee” NULL
3000 5000 2000
head
Traversing a Dynamic Linked List
Using Operator new
Recall
 If memory is available in the free store (or
heap), operator new allocates the requested
object and returns a pointer to the memory
allocated
 The dynamically allocated object exists until
the delete operator destroys it
31
Inserting a Node at the Front of a List
char item = ‘B’;
NodePtr location;
location = new NodeType;
location->info = item;
location->link = head;
head = location;
head ‘X’ ‘C’ ‘L’
‘B’
item
Inserting a Node at the Front of a List
char item = ‘B’;
NodePtr location;
location = new NodeType;
location->info = item;
location->link = head;
head = location;
head ‘X’ ‘C’ ‘L’
‘B’
item
location
Inserting a Node at the Front of a List
char item = ‘B’;
NodePtr location;
location = new NodeType;
location->info = item;
location->link = head;
head = location;
head ‘X’ ‘C’ ‘L’
‘B’
item
location
Inserting a Node at the Front of a List
char item = ‘B’;
NodePtr location;
location = new NodeType;
location->info = item;
location->link = head;
head = location;
head ‘X’ ‘C’ ‘L’
‘B’
item
location ‘B’
Inserting a Node at the Front of a List
char item = ‘B’;
NodePtr location;
location = new NodeType;
location->info = item;
location->link = head;
head = location;
head ‘X’ ‘C’ ‘L’
‘B’
item
location ‘B’
Inserting a Node at the Front of a List
char item = ‘B’;
NodePtr location;
location = new NodeType;
location->info = item;
location->link = head;
head = location;
head ‘X’ ‘C’ ‘L’
‘B’
item
location ‘B’
When you use the operator delete
 The object currently pointed to by the
pointer is deallocated and the pointer is
considered undefined
 The object’s memory is returned to the free
store
Using Operator delete
38
Deleting the First Node from the List
NodePtr tempPtr;
item = head->info;
tempPtr = head;
head = head->link;
delete tempPtr;
head
item
‘B’ ‘X’ ‘C’ ‘L’
tempPtr
Deleting the First Node from the List
NodeType * tempPtr;
item = head->info;
tempPtr = head;
head = head->link;
delete tempPtr;
head ‘B’ ‘X’ ‘C’ ‘L’
tempPtr
item ‘B’
Deleting the First Node from the List
NodeType * tempPtr;
item = head->info;
tempPtr = head;
head = head->link;
delete tempPtr;
head ‘B’ ‘X’ ‘C’ ‘L’
tempPtr
item ‘B’
Deleting the First Node from the List
NodeType * tempPtr;
item = head->info;
tempPtr = head;
head = head->link;
delete tempPtr;
head ‘B’ ‘X’ ‘C’ ‘L’
tempPtr
item ‘B’
Deleting the First Node from the List
NodeType * tempPtr;
item = head->info;
tempPtr = head;
head = head->link;
delete tempPtr;
head ‘X’ ‘C’ ‘L’
tempPtr
item ‘B’
What is a Sorted List?
A sorted list is a variable-length,
linear collection of homogeneous
elements, ordered according to the
value of one or more data members
The transformer operations must
maintain the ordering
In addition to Insert and Delete, let’s
add two new operations to our list
InsertAsFirst and RemoveFirst
ADT HybridList Operations
Transformers
 InsertAsFirst
 Insert
 RemoveFirst
 Delete
Same observers and iterators as ADT List
Since we have two insertion and two deletion
operations, let’s call this a Hybrid List
change state
45

Más contenido relacionado

La actualidad más candente

Letting In the Light: Using Solr as an External Search Component
Letting In the Light: Using Solr as an External Search ComponentLetting In the Light: Using Solr as an External Search Component
Letting In the Light: Using Solr as an External Search Component
Jay Luker
 
Fundamentals of data structures
Fundamentals of data structuresFundamentals of data structures
Fundamentals of data structures
Niraj Agarwal
 
Data structures and algorithms lab10
Data structures and algorithms lab10Data structures and algorithms lab10
Data structures and algorithms lab10
Bianca Teşilă
 

La actualidad más candente (14)

Unit 4 tree
Unit 4   treeUnit 4   tree
Unit 4 tree
 
Lecture4
Lecture4Lecture4
Lecture4
 
linked list (c#)
 linked list (c#) linked list (c#)
linked list (c#)
 
Stacks,queues,linked-list
Stacks,queues,linked-listStacks,queues,linked-list
Stacks,queues,linked-list
 
Link List
Link ListLink List
Link List
 
Linear data structure concepts
Linear data structure conceptsLinear data structure concepts
Linear data structure concepts
 
Lecture3
Lecture3Lecture3
Lecture3
 
Data Structure Lecture 5
Data Structure Lecture 5Data Structure Lecture 5
Data Structure Lecture 5
 
Letting In the Light: Using Solr as an External Search Component
Letting In the Light: Using Solr as an External Search ComponentLetting In the Light: Using Solr as an External Search Component
Letting In the Light: Using Solr as an External Search Component
 
Fundamentals of data structures
Fundamentals of data structuresFundamentals of data structures
Fundamentals of data structures
 
16 Linear data structures
16 Linear data structures16 Linear data structures
16 Linear data structures
 
Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]
 
Data structures and algorithms lab10
Data structures and algorithms lab10Data structures and algorithms lab10
Data structures and algorithms lab10
 
Below is a given ArrayList class and Main class Your Dreams Our Mission/tuto...
Below is a given ArrayList class and Main class  Your Dreams Our Mission/tuto...Below is a given ArrayList class and Main class  Your Dreams Our Mission/tuto...
Below is a given ArrayList class and Main class Your Dreams Our Mission/tuto...
 

Destacado

07 ds and algorithm session_10
07 ds and algorithm session_1007 ds and algorithm session_10
07 ds and algorithm session_10
Niit Care
 
04 ds and algorithm session_05
04 ds and algorithm session_0504 ds and algorithm session_05
04 ds and algorithm session_05
Niit Care
 
05 ds and algorithm session_07
05 ds and algorithm session_0705 ds and algorithm session_07
05 ds and algorithm session_07
Niit Care
 
Sparse matrices
Sparse matricesSparse matrices
Sparse matrices
Zain Zafar
 
Data structure and its types
Data structure and its typesData structure and its types
Data structure and its types
Navtar Sidhu Brar
 
Lecture 1 data structures and algorithms
Lecture 1 data structures and algorithmsLecture 1 data structures and algorithms
Lecture 1 data structures and algorithms
Aakash deep Singhal
 

Destacado (19)

07 ds and algorithm session_10
07 ds and algorithm session_1007 ds and algorithm session_10
07 ds and algorithm session_10
 
04 ds and algorithm session_05
04 ds and algorithm session_0504 ds and algorithm session_05
04 ds and algorithm session_05
 
Chapter 15
Chapter 15Chapter 15
Chapter 15
 
Operations on linked list
Operations on linked listOperations on linked list
Operations on linked list
 
Chap 13(dynamic memory allocation)
Chap 13(dynamic memory allocation)Chap 13(dynamic memory allocation)
Chap 13(dynamic memory allocation)
 
05 ds and algorithm session_07
05 ds and algorithm session_0705 ds and algorithm session_07
05 ds and algorithm session_07
 
Linked Lists
Linked ListsLinked Lists
Linked Lists
 
Applications of data structures
Applications of data structuresApplications of data structures
Applications of data structures
 
Sparse Matrix and Polynomial
Sparse Matrix and PolynomialSparse Matrix and Polynomial
Sparse Matrix and Polynomial
 
Multiplication of two 3 d sparse matrices using 1d arrays and linked lists
Multiplication of two 3 d sparse matrices using 1d arrays and linked listsMultiplication of two 3 d sparse matrices using 1d arrays and linked lists
Multiplication of two 3 d sparse matrices using 1d arrays and linked lists
 
Sparse matrices
Sparse matricesSparse matrices
Sparse matrices
 
linked list
linked list linked list
linked list
 
Linked list
Linked listLinked list
Linked list
 
Linked list
Linked listLinked list
Linked list
 
Data structure and its types
Data structure and its typesData structure and its types
Data structure and its types
 
Top10 Innovative Ideas You Haven't Heard of in the Developing World
Top10 Innovative Ideas You Haven't Heard of in the Developing WorldTop10 Innovative Ideas You Haven't Heard of in the Developing World
Top10 Innovative Ideas You Haven't Heard of in the Developing World
 
Data Structure
Data StructureData Structure
Data Structure
 
Lecture 1 data structures and algorithms
Lecture 1 data structures and algorithmsLecture 1 data structures and algorithms
Lecture 1 data structures and algorithms
 
DATA STRUCTURES
DATA STRUCTURESDATA STRUCTURES
DATA STRUCTURES
 

Similar a Lec6 mod linked list

Lec6 mod linked list
Lec6 mod linked listLec6 mod linked list
Lec6 mod linked list
Saad Gabr
 
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
 
Objective Manipulate the Linked List Pointer.Make acopy of LList..pdf
Objective Manipulate the Linked List Pointer.Make acopy of LList..pdfObjective Manipulate the Linked List Pointer.Make acopy of LList..pdf
Objective Manipulate the Linked List Pointer.Make acopy of LList..pdf
rajeshjangid1865
 

Similar a Lec6 mod linked list (20)

Lec6 mod linked list
Lec6 mod linked listLec6 mod linked list
Lec6 mod linked list
 
Chapter 5 ds
Chapter 5 dsChapter 5 ds
Chapter 5 ds
 
DS Unit 2.ppt
DS Unit 2.pptDS Unit 2.ppt
DS Unit 2.ppt
 
Unit ii(dsc++)
Unit ii(dsc++)Unit ii(dsc++)
Unit ii(dsc++)
 
Linked lists a
Linked lists aLinked lists a
Linked lists a
 
CSE240 Doubly Linked Lists
CSE240 Doubly Linked ListsCSE240 Doubly Linked Lists
CSE240 Doubly Linked Lists
 
Linked List.pptx
Linked List.pptxLinked List.pptx
Linked List.pptx
 
강의자료10
강의자료10강의자료10
강의자료10
 
Ll.pptx
Ll.pptxLl.pptx
Ll.pptx
 
linked list.pptx
linked list.pptxlinked list.pptx
linked list.pptx
 
Lec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdfLec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdf
 
dynamicList.ppt
dynamicList.pptdynamicList.ppt
dynamicList.ppt
 
Data Structures with C Linked List
Data Structures with C Linked ListData Structures with C Linked List
Data Structures with C Linked List
 
Linked list1.ppt
Linked list1.pptLinked list1.ppt
Linked list1.ppt
 
DS_LinkedList.pptx
DS_LinkedList.pptxDS_LinkedList.pptx
DS_LinkedList.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
 
Linked List
Linked ListLinked List
Linked List
 
Objective Manipulate the Linked List Pointer.Make acopy of LList..pdf
Objective Manipulate the Linked List Pointer.Make acopy of LList..pdfObjective Manipulate the Linked List Pointer.Make acopy of LList..pdf
Objective Manipulate the Linked List Pointer.Make acopy of LList..pdf
 
Linked list
Linked listLinked list
Linked list
 

Más de Ibrahim El-Torbany (16)

Idea2
Idea2Idea2
Idea2
 
Cpp lernaufgabe linked_list
Cpp lernaufgabe linked_listCpp lernaufgabe linked_list
Cpp lernaufgabe linked_list
 
C++ examples &revisions
C++ examples &revisionsC++ examples &revisions
C++ examples &revisions
 
Lec5
Lec5Lec5
Lec5
 
Lec3
Lec3Lec3
Lec3
 
Lec2
Lec2Lec2
Lec2
 
Lec2&3 data structure
Lec2&3 data structureLec2&3 data structure
Lec2&3 data structure
 
Lec1
Lec1Lec1
Lec1
 
Lec4
Lec4Lec4
Lec4
 
Ass logic
Ass logicAss logic
Ass logic
 
Math lecture 4 Part 1
Math lecture 4 Part 1Math lecture 4 Part 1
Math lecture 4 Part 1
 
Tutorial 1
Tutorial 1Tutorial 1
Tutorial 1
 
Lec2&3_DataStructure
Lec2&3_DataStructureLec2&3_DataStructure
Lec2&3_DataStructure
 
Lecture 2 math 2
Lecture 2 math 2Lecture 2 math 2
Lecture 2 math 2
 
Lec1
Lec1Lec1
Lec1
 
Chapter 1 what is statistics
Chapter 1 what is statisticsChapter 1 what is statistics
Chapter 1 what is statistics
 

Último

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 

Último (20)

The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 

Lec6 mod linked list

  • 1. Topics  Meaning of a Linked List  Meaning of a Dynamic Linked List  Traversal, Insertion and Deletion of Elements in a Dynamic Linked List  Specification of a Dynamic Linked Sorted List  Insertion and Deletion of Elements in a Dynamic Linked Sorted List
  • 2. To implement the List ADT The programmer must 1) choose a concrete data representation for the list, and 2) implement the list operations
  • 3. Recall: 4 Basic Kinds of ADT Operations  Constructors -- create a new instance (object) of an ADT  Transformers -- change the state of one or more of the data values of an instance  Observers -- allow client to observe the state of one or more of the data values of an instance without changing them  Iterators -- allow client to access the data values in sequence
  • 4. List Operations Transformers  Insert  Delete  Sort Observers  IsEmpty  IsFull  Length  IsPresent change state observe state 4
  • 5. ADT List Operations Iterator  Reset  GetNextItem  Reset prepares for the iteration  GetNextItem returns the next item in sequence  No transformer can be called between calls to GetNextItem (Why?) Iteration Pair
  • 6. Array-based class List Reset IsFull Length IsPresent Delete IsEmpty Insert GetNexItem Private data: length data [0] [1] [2] [MAX_LENGTH-1] currentPos SelSort
  • 7. // Specification file array-based list (list.h) const int MAX_LENGTH = 50; typedef int ItemType; class List // Declares a class data type { public: // Public member functions List(); // constructor bool IsEmpty () const; bool IsFull () const; int Length () const; // Returns length of list void Insert (ItemType item); void Delete (ItemType item); bool IsPresent(ItemType item) const; void SelSort (); void Reset (); ItemType GetNextItem (); private: // Private data members int length; // Number of values currently stored ItemType data[MAX_LENGTH]; int CurrentPos; // Used in iteration }; 7
  • 8. Implementation Structures  Use a built-in array stored in contiguous memory locations, implementing operations Insert and Delete by moving list items around in the array, as needed  Use a linked list in which items are not necessarily stored in contiguous memory locations  A linked list avoids excessive data movement from insertions and deletions
  • 9. Implementation Possibilities for a List List Linked list Built-in array Built-in dynamic data and pointers Built-in array of structs
  • 10. A Linked List  A linked list is a list in which the order of the components is determined by an explicit link member in each node  Each node is a struct containing a data member and a link member that gives the location of the next node in the list head ‘X’ ‘C’ ‘L’
  • 11. Dynamic Linked List head “Ted” “Irv” “Lee”  A dynamic linked list is one in which the nodes are linked together by pointers and an external pointer (or head pointer) points to the first node in the list
  • 12. Nodes can be located anywhere in memory  The link member holds the memory address of the next node in the list head 3000 “Ted” 5000 “Irv” 2000 “Lee” NULL 3000 5000 2000
  • 13. // Type declarations struct NodeType { char info; NodeType* link; } typedef NodeType* NodePtr; // Variable DECLARATIONS NodePtr head; NodePtr ptr; 13 Declarations for a Dynamic Linked List .info .link ‘A’ 6000
  • 14. Pointer Dereferencing and Member Selection .info .link ‘A’ 6000 ptr ptr ptr .info .link ‘A’ 6000 *ptr ptr .info .link (*ptr).info ptr->info ‘A’ 6000
  • 15. 15 ptr is a pointer to a node .info .link ‘A’ 6000 ptr ptr
  • 16. 16 *ptr is the entire node pointed to by ptr ptr .info .link ‘A’ 6000 *ptr
  • 17. 17 ptr->info is a node member ptr .info .link ptr->info (*ptr).info // Equivalent ‘A’ 6000
  • 18. 18 ptr->link is a node member ptr .info .link ptr->link (*ptr).link // Equivalent ‘A’ 6000
  • 19. Traversing a Dynamic Linked List // Pre: head points to a dynamic linked list ptr = head; while (ptr != NULL) { cout << ptr->info; // Or, do something else with node *ptr ptr = ptr->link; } ptr 3000 “Ted” 5000 “Irv” 2000 “Lee” NULL 3000 5000 2000 head
  • 20. // Pre: head points to a dynamic linked list ptr = head; while (ptr != NULL) { cout << ptr->info; // Or, do something else with node *ptr ptr = ptr->link; } ptr 3000 3000 “Ted” 5000 “Irv” 2000 “Lee” NULL 3000 5000 2000 head Traversing a Dynamic Linked List
  • 21. // Pre: head points to a dynamic linked list ptr = head; while (ptr != NULL) { cout << ptr->info; // Or, do something else with node *ptr ptr = ptr->link; } ptr 3000 3000 “Ted” 5000 “Irv” 2000 “Lee” NULL 3000 5000 2000 head Traversing a Dynamic Linked List
  • 22. // Pre: head points to a dynamic linked list ptr = head; while (ptr != NULL) { cout << ptr->info; // Or, do something else with node *ptr ptr = ptr->link; } ptr 3000 3000 “Ted” 5000 “Irv” 2000 “Lee” NULL 3000 5000 2000 head Traversing a Dynamic Linked List
  • 23. // Pre: head points to a dynamic linked list ptr = head; while (ptr != NULL) { cout << ptr->info; // Or, do something else with node *ptr ptr = ptr->link; } ptr 5000 3000 “Ted” 5000 “Irv” 2000 “Lee” NULL 3000 5000 2000 head Traversing a Dynamic Linked List
  • 24. // Pre: head points to a dynamic linked list ptr = head; while (ptr != NULL) { cout << ptr->info; // Or, do something else with node *ptr ptr = ptr->link; } ptr 5000 3000 “Ted” 5000 “Irv” 2000 “Lee” NULL 3000 5000 2000 head Traversing a Dynamic Linked List
  • 25. // Pre: head points to a dynamic linked list ptr = head; while (ptr != NULL) { cout << ptr->info; // Or, do something else with node *ptr ptr = ptr->link; } ptr 5000 3000 “Ted” 5000 “Irv” 2000 “Lee” NULL 3000 5000 2000 head Traversing a Dynamic Linked List
  • 26. // Pre: head points to a dynamic linked list ptr = head; while (ptr != NULL) { cout << ptr->info; // Or, do something else with node *ptr ptr = ptr->link; } ptr 2000 3000 “Ted” 5000 “Irv” 2000 “Lee” NULL 3000 5000 2000 head Traversing a Dynamic Linked List
  • 27. // Pre: head points to a dynamic linked list ptr = head; while (ptr != NULL) { cout << ptr->info; // Or, do something else with node *ptr ptr = ptr->link; } ptr 2000 3000 “Ted” 5000 “Irv” 2000 “Lee” NULL 3000 5000 2000 head Traversing a Dynamic Linked List
  • 28. // Pre: head points to a dynamic linked list ptr = head; while (ptr != NULL) { cout << ptr->info; // Or, do something else with node *ptr ptr = ptr->link; } ptr 2000 3000 “Ted” 5000 “Irv” 2000 “Lee” NULL 3000 5000 2000 head Traversing a Dynamic Linked List
  • 29. // Pre: head points to a dynamic linked list ptr = head; while (ptr != NULL) { cout << ptr->info; // Or, do something else with node *ptr ptr = ptr->link; } ptr NULL 3000 “Ted” 5000 “Irv” 2000 “Lee” NULL 3000 5000 2000 head Traversing a Dynamic Linked List
  • 30. // Pre: head points to a dynamic linked list ptr = head; while (ptr != NULL) { cout << ptr->info; // Or, do something else with node *ptr ptr = ptr->link; } ptr NULL 3000 “Ted” 5000 “Irv” 2000 “Lee” NULL 3000 5000 2000 head Traversing a Dynamic Linked List
  • 31. Using Operator new Recall  If memory is available in the free store (or heap), operator new allocates the requested object and returns a pointer to the memory allocated  The dynamically allocated object exists until the delete operator destroys it 31
  • 32. Inserting a Node at the Front of a List char item = ‘B’; NodePtr location; location = new NodeType; location->info = item; location->link = head; head = location; head ‘X’ ‘C’ ‘L’ ‘B’ item
  • 33. Inserting a Node at the Front of a List char item = ‘B’; NodePtr location; location = new NodeType; location->info = item; location->link = head; head = location; head ‘X’ ‘C’ ‘L’ ‘B’ item location
  • 34. Inserting a Node at the Front of a List char item = ‘B’; NodePtr location; location = new NodeType; location->info = item; location->link = head; head = location; head ‘X’ ‘C’ ‘L’ ‘B’ item location
  • 35. Inserting a Node at the Front of a List char item = ‘B’; NodePtr location; location = new NodeType; location->info = item; location->link = head; head = location; head ‘X’ ‘C’ ‘L’ ‘B’ item location ‘B’
  • 36. Inserting a Node at the Front of a List char item = ‘B’; NodePtr location; location = new NodeType; location->info = item; location->link = head; head = location; head ‘X’ ‘C’ ‘L’ ‘B’ item location ‘B’
  • 37. Inserting a Node at the Front of a List char item = ‘B’; NodePtr location; location = new NodeType; location->info = item; location->link = head; head = location; head ‘X’ ‘C’ ‘L’ ‘B’ item location ‘B’
  • 38. When you use the operator delete  The object currently pointed to by the pointer is deallocated and the pointer is considered undefined  The object’s memory is returned to the free store Using Operator delete 38
  • 39. Deleting the First Node from the List NodePtr tempPtr; item = head->info; tempPtr = head; head = head->link; delete tempPtr; head item ‘B’ ‘X’ ‘C’ ‘L’ tempPtr
  • 40. Deleting the First Node from the List NodeType * tempPtr; item = head->info; tempPtr = head; head = head->link; delete tempPtr; head ‘B’ ‘X’ ‘C’ ‘L’ tempPtr item ‘B’
  • 41. Deleting the First Node from the List NodeType * tempPtr; item = head->info; tempPtr = head; head = head->link; delete tempPtr; head ‘B’ ‘X’ ‘C’ ‘L’ tempPtr item ‘B’
  • 42. Deleting the First Node from the List NodeType * tempPtr; item = head->info; tempPtr = head; head = head->link; delete tempPtr; head ‘B’ ‘X’ ‘C’ ‘L’ tempPtr item ‘B’
  • 43. Deleting the First Node from the List NodeType * tempPtr; item = head->info; tempPtr = head; head = head->link; delete tempPtr; head ‘X’ ‘C’ ‘L’ tempPtr item ‘B’
  • 44. What is a Sorted List? A sorted list is a variable-length, linear collection of homogeneous elements, ordered according to the value of one or more data members The transformer operations must maintain the ordering In addition to Insert and Delete, let’s add two new operations to our list InsertAsFirst and RemoveFirst
  • 45. ADT HybridList Operations Transformers  InsertAsFirst  Insert  RemoveFirst  Delete Same observers and iterators as ADT List Since we have two insertion and two deletion operations, let’s call this a Hybrid List change state 45