0% found this document useful (0 votes)
45 views12 pages

Linked List

The document discusses linked lists. It defines a linked list as a data structure where each element consists of the data and a pointer to the next element. This allows linked lists to dynamically grow and shrink in size. The key operations on linked lists are defined as insertion, deletion, finding length and traversing. Methods for implementing these operations like inserting a node at the beginning or end of the list are described.

Uploaded by

hussienboss99
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)
45 views12 pages

Linked List

The document discusses linked lists. It defines a linked list as a data structure where each element consists of the data and a pointer to the next element. This allows linked lists to dynamically grow and shrink in size. The key operations on linked lists are defined as insertion, deletion, finding length and traversing. Methods for implementing these operations like inserting a node at the beginning or end of the list are described.

Uploaded by

hussienboss99
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/ 12

LECTURE 3

LINKED LIST
By
Dr Mourad Raafat
What is array?
• One memory block is allocated for the entire array to hold the elements of the array. The array elements can be
accessed in constant time by using the index of the particular element as the subscript.

• Advantages of Arrays
Simple and easy to use
Faster access to the elements (constant access)
• Disadvantages of Arrays
Preallocates all needed memory up front and wastes memory space for indices in the array that are empty.
Fixed size: The size of the array is static (specify the array size before using it).
One block allocation: To allocate the array itself at the beginning, sometimes it may not be possible to get
the memory for the complete array (if the array size is big).
Complex position-based insertion: To insert an element at a given position, we may need to shift the
existing elements.
What is a Linked List?
• A linked list is a data structure used for storing collections of data. A linked list has the
following properties.
• Successive elements are connected by pointers
• The last element points to None
• Can grow or shrink in size during execution of a program
• Can be made just as long as required (until systems memory exhausts)
• Does not waste memory space (but takes some extra memory for pointers). It
allocates memory as list grows
Comparison of Linked Lists with
Arrays and Dynamic Arrays
What is
node?
class Node:
def __init__(self, data):
self.data = data
self.next = None
Singly
• This list consists of a number of nodes in which each
node has a next pointer to the following

Linked Lists element. The link of the last node in the list is None,
which indicates the end of the list.
Linked Lists ADT
The following operations make linked lists an ADT:

Main Linked Lists Operations

• Insert: inserts an element into the list


• Delete: removes and returns the specified position element from the list

Auxiliary Linked Lists Operations

• Delete List: removes all elements of the list (dispose of the list)
• Count: returns the number of elements in the list
• Find n^th node from the end of the list
Traversing
linked list
• Traversing means visiting all the def traverse(self):
node once i.e, we want to read if self.head:
all the data available in the temp = self.head
linked list.
while temp is not None:
print(temp.data)
temp = temp.next
else:
print(None)
Length of a Linked List

• To find length of a linked list we


def length(self):
can simply use a temporary
count = 0
variable and initialize it by
assigning the head node. Now, temp = self.head
move it to every node one by one while temp:
and increase the counter variable. temp = temp.next
count += 1
return count
Insertion in a
Linked List
• While inserting a node at the begining
position, two situation may arise: def insert_at_start(self, data):
1. Insertion when Linked List is emtpy: In new_node = Node(data)
this case we can simply create a new node new_node.next = self.head
and put the value in data part and null into
the next part (which is already done in self.head = new_node
Class Node constructor) then assign it to
the head.
2. Insertion when Linked List is not
empty: In this case create a new node, add
value into the data part and assign the
node pointed pointed by head to next of
newly created node and then head will
• def insert_at_end(self, data):
• While inserting a node at the end position,
two situation may arise: new_node = Node(data)
1. Insertion when Linked List is emtpy: In
this case we can simply create a new node
if self.head:
and put the value in data part and null into
the next part (which is already done in Class temp = self.head
Node constructor) then assign it to the head.
2. Insertion when Linked List is not
while temp.next:
empty: In this case create a new node, and
then traverse till the end of the Linked List temp = temp.next
then add the new node in the next part of last
node and put null into the next part because temp.next = new_node
it is the last node now.
else:
self.head = new_node

You might also like