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

Lecture3 Linked List

Uploaded by

Bakunzi Daniel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Lecture3 Linked List

Uploaded by

Bakunzi Daniel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Linked List

Introduction
• Array is a static data structure; continuous memory usage
• There is need for an alternative where any part of memory is used
• “I am Swaib, I know where Principal sits”. Swaib need not to sit next
to Principal.
• Swaib + Principal’s address is a node where Swaib is data value and
principal’s address is location of next node(data).
• Node
Data Link
Linked List
• Is an ordered collection of finite, homogenous data elements
(nodes)where the linear order is maintained by pointers/link.

Header
• Node1 Node2 Node3
Null pointer

• The header node has no data value while the last node points to no
data
• Linked list can be single, double or circular linked list
Nodes
• Each element of a Linked List is a separate Node object

Data Link

• Each Node tracks a single piece of data plus a reference


(pointer) to the next
• Create a new Node every time we add something to the List
• Remove nodes when item is removed from the list
Types of Linked Lists
• Singly linked list: a node refers only to the next node in the structure
Header
Node1 •Node2 Node3 Null pointer
• doubly linked List: the current node has a reference to the next node
and the previous node
Circular Linked List
• This is a single linked list in which the last node points at the first node

Header

Node1 Node2 Node3


• The advantage with circular linked list is that you can access a given
data element from any node, not necessarily from header as is in
single linear linked list.
Representation of Linked List in
memory
• Dynamic representation using free pool of storage
• When creating a node, memory is allocated to that node
• And corresponding links from the node and to the node are managed
accordingly
• In case node is deleted, the pointer to that node is made null and
garbage collector system makes the memory address available.
• The memory need not to be continuous
Operations on Single Linear Linked
List (SLLL)
• Traversing
• Inserting; front/start, end/last, arbitrary
• Deleting; frond/start, end/last, arbitrary
• Copying the list to make a duplicate
• Merging two linked lists
• Searching for an element in the list
Traversing
1. Let a variable be assigned the header node
address
2. While it is not the end of the list
3. Process the current node by printing or
counting
4. Change to the next node
5. Repeat until the next pointer is NULL
Traversal algorithm
Method
{
Ptr=HEADERLINK //ptr is initialized to pointer to a current node
While(ptr!=NULL) //continue until the last node
{
Process(ptr) //print current content or so
Ptr=ptrLINK //move to the next node
}
}
Note: HEADER is the pointer to header node
Inserting node at the front of SLLL
1. Create a node new by allocating memory
2. Test whether the new memory location is not
NULL
3. Let new node point where the header node is
pointing
4. Let the header node point at the new node
Inserting at the front/start of SLLL
Method
{
new=getNode(NODE) //allocate memory for new node
If(new==NULL)
Memory under flow, no insertion //no memory available
Else
{
newLINK=HEADERLINK //new memory points at where HEADER points
newDATA=X //assign new memory location data X
HEADERLINK=new //Let Header node point to new address/memory
}
}
Inserting at the end
1. Create a new node and
2. Traverse to the last node
3. Make the last node point at the new node
4. Assign value to the new node
5. Make the new node point at NULL
Inserting at the end
Method
{
New=getnew()
Ptr=HEADER //start from the HEADER node
While(ptrLINK!=NULL) //traverse to the end
{
Ptr=ptrLINK //change pointer to the next node
}
PtrLINK=new //change link field of last node to new data
NewDATA=x // copy content of x into the new node
NewLINK=NULL //let the new node point to NULL
}
Insert after data KEY
Method
{
New=getnew()
Ptr=HEADER
While(ptrDATA!=KEY)
{
Ptr=ptrLINK
} //while stops when at key
NewLINK=ptrLINK // new should point at where key is pointing
NewDATA=x //allocate new data value x
PtrLINK=new //key point at new node.
}
Delete SLLL at the Front
• Let the header pointer point to the second node
• Method
{
1. Test if not empty
2. Ptr=HEADER //pointer to first node
3. ptr1=ptr  LINK //ptr1 is the pointer to second node
4. HEADER LINK=ptr1 //next node becomes the first node
}
Delete at the end of SLLL
• Traverse to the end of the list while keeping track of the previous
pointer
Method
{
Ptr=HEADER //start from the HEADER node
While(ptrLINK!=NULL) //traverse to the end
{
Ptr1=ptr //stores the previous pointer
Ptr=ptrLINK //change pointer to the next node
}
Ptr1LINK=NULL //second last node points to NULL
Delete data element KEY
• Traverse to the data element KEY, what points to KEY must point to what
KEY points to.
• Method
Ptr1=HEADER //start from the HEADER node
Ptr=ptr1  LINK
While(ptr!=NULL) //traverse to the end
{
If (ptrDATA !=KEY) //if the data is not found, move next
Ptr1=ptr //stores the previous pointer
Ptr=ptrLINK //change pointer to the next node
Else
ptr1LINK =ptrLINK //link of the predecessor to point to the successor
}
Merge Two SLLLs
• Let the last node of the first list point to the first node of the second
list
• Method
Ptr=HEADER1 //start from the HEADER node
While(ptrLINK!=NULL) //traverse to the end
{
Ptr=ptrLINK //change pointer to the next node
}
ptrLINK =HEADER2LINK //Last node in L1 points to first node in L2
HEADER=HEADER1

You might also like