0% found this document useful (0 votes)
16 views

Linked List Implementatio of List ADT

Uploaded by

pandi_m
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Linked List Implementatio of List ADT

Uploaded by

pandi_m
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 20

Linked List

implementation of List
ADT

McGraw-Hill ©The McGraw-Hill Companies, Inc., 2000


Head Node
Head node structure stores the head pointer and other data about the list.
Data are known as Metadata.
Data Node
Data Node includes data fields. One of the data fields can be a key field when
application requires searching by key.(Addresses)

Data Structures: A Pseudocode Approach


with C 2
Operations – Create Node

Data Structures: A Pseudocode Approach


with C 3
Insert into Empty List
•When the head pointer of the list is null, the list is empty.
•To add a new node, the list header pointer should be assigned the address
of the allocated node and make sure that it’s link field is a null pointer.
•List head =null pointer

Data Structures: A Pseudocode Approach


with C 4
Insert at Beginning
•A new node is added before the first node of the list.
•We Determine that addition is at the beginning of the list. If the predecessor
pointer is a null pointer, there is no predecessor, so we are at the beginning of the
list.
•Point the new node to the first node of the list and then set the head pointer to
point to the new first node.
• Logically inserting into an empty list is same as inserting a the beginning.

Data Structures: A Pseudocode Approach


with C 5
Insert in Middle
•When we add a node anywhere in the middle of the list, the predecessor
pointer contains an address.
•Point the new node to it’s successor and then point it’s predecessor to the
new node. The address of the new node’s successor can be found in the
predecessor’s link field.

Data Structures: A Pseudocode Approach


with C 6
Insert at End
•When adding at the end of the list, we only need to point the predecessor to
the new node. There is no successor
•The new node’s link field should be set to null pointer. Also, the last node in
the list has a null pointer. If we use this pointer rather than a null pointer
constant, the code becomes same as the inserting in the middle.

Data Structures: A Pseudocode Approach


with C 7
Insert Node Algorithm
•We are given a pointer to the list, the predecessor, and the data to be
inserted.
•We allocate memory for the new node, set data, and adjust the link
pointers.
•If true, insert successful.

Data Structures: A Pseudocode Approach


with C 8
Data Structures: A Pseudocode Approach
with C 9
Delete Node Algorithm
•Removes a node from the list by changing pointers and then physically deleting the
node from dynamic memory.
•Locate the node to be deleted by knowing it’s address and it’s predecessor’s address.
•Change the predecessor’s link field to point to the deleted node’s successor. We then
recycle the node back to dynamic memory.
•If deleting the only node in the list, results in empty list and set the head to a null
pointer.

Data Structures: A Pseudocode Approach


with C 10
Delete First Node
•When deleting first node, the head pointer must be reset to point to the node’s
successor and then recycle the memory for the deleted node.
•Set list head to pLoc link
•Recycle (pLoc)

Data Structures: A Pseudocode Approach


with C 11
General Delete Case
•Deleting in the middle or at the end is a general case.
•Simply point the predecessor node to the successor of the node being deleted.
•When the node being deleted is the last node of the list, it’s null pointer is moved to
the predecessor’s link field, making the predecessor the new logical end of the list.
•Set pPre link to pLoc link
•Recycle (pLoc)

Data Structures: A Pseudocode Approach


with C 12
Delete Node Algorithm
•We are given a pointer to the list, pointer to the node to be deleted, and
pointer to the delete node’s predecessor.
•We may copy the deleted node’s data to a data out area in the calling
program.
•Adjust the pointers before releasing the node’s memory.

Data Structures: A Pseudocode Approach


with C 13
Data Structures: A Pseudocode Approach
with C 14
List Search
 A list search is used to locate data in a list
 To insert data, we need to know the logical
predecessor to the new data.
 To delete data, we need to find the node to be
deleted and identify its logical predecessor.
 To retrieve data from a list, we need to search the
list and find the data.

Data Structures: A Pseudocode Approach


with C 15
List Search
 We must use a sequential search because there
is no physical relationship among the nodes.
 The classic sequential search returns the location
of an element when it is found and the address of
the last element when it is not found.
 Because the list is ordered, we need to return the
location of the element when it is found and the
location where it should be placed when it is not
found.
Data Structures: A Pseudocode Approach
with C 16
List Search
 Given a target key, the ordered list search
attempts to locate the requested node in the list.
 To search a list on a key, we need a key field. For
simple lists the key and the data can be the same
field.
 If a node in the list matches the target value, the
search returns true; if there are no key matches,
it returns false.

Data Structures: A Pseudocode Approach


with C 17
Data Structures: A Pseudocode Approach
with C 18
Data Structures: A Pseudocode Approach
with C 19
Traversals
 Algorithms that traverse a list start at the first
node and examine each node in succession until
the last node has been processed.
 Traversal logic is used by such types of
algorithms as changing a value in each node,
printing a list, summing a list, calculating the
average, etc.
 To traverse a list, we need a walking pointer that
moves from node to node
Data Structures: A Pseudocode Approach
with C 20

You might also like