Involute of a circle,Square, pentagon,HexagonInvolute_Engineering Drawing.pdf
Week-03.pdf
1. Linked Lists
Sesion 03
Ing. Gómez Marı́n, Jaime1
1Desarrollo y Diseño de Software
Departamento de TdG
September 2020
JGM Data Structure and Algorithmic
2. Table of Contents
Introduction
Arrays Overview
Advantages and Disadvantages of Arrays
Dynamic Arrays Overview
Linked Lists
Advantages and Disadvantages of Linked Lists
Linked Lists vs Arrays vs Dynamic Arrays
Singly Linked Lists
Conclusion
Bibliography
JGM Data Structure and Algorithmic
3. Introduction
In this session, we will learn about Linked Lists, which is a very
common dynamic data structure that is used to create other data
structures likes tree, graphs, hashing, etc.
JGM Data Structure and Algorithmic
4. Arrays Overview
The arrays need one memory block for hold the elements of
the array.
The array elements can be accessed in constant time by using
the index of the particular element.
To access an array element, the address of an element is
computed as an offset from the base address of the array
JGM Data Structure and Algorithmic
5. Advantages and Disadvantages of Arrays
Advantages
Simple and easy to use
Faster access to the elements
Disadvantages
Fixed size
One block allocation
Complex position-based insertion.
JGM Data Structure and Algorithmic
6. Dynamic Arrays Overview
Dynamic array is a random access, variable-size list data structure
that allows elements to be added or removed.
Start the dynamic array with a fixed size.
As soon as that array becomes full, create the new array
double the size of the original array.
In other case, reduce the array size to half if the elements in
the array are less than half.
JGM Data Structure and Algorithmic
7. Linked Lists
It is a data structure user for storing collections of data, its
characteristics are:
Successive elements are connected by pointers.
The last element points to NULL.
Can grow or shrink in size during execution of a program
Does not waste memory space, but takes some extra memory
for pointers.
Figure: 01
JGM Data Structure and Algorithmic
8. Advantages and Disadvantages of Linked Lists
Advantages
The dynamic allocation of storage is a great advantage.
We can start with space for just one allocated elements and
add on new elements.
Disadvantages
Linked Lists take O(n) for access to an element in the list in
the worst case.
Do not have spacial locality in memory and lost the benefits
from modern CPU caching methods.
There are overhead with storing and retrieving data.
Linked lists waste memory in terms of extra reference points.
JGM Data Structure and Algorithmic
9. Linked Lists vs Arrays vs Dynamic Arrays
Figure: 0‘
JGM Data Structure and Algorithmic
10. Linked Lists ADT
Main operations
Insert : inserts an element into the list
Delete: removes and returns the specified position element
from the list
Auxiliary operations
Delete List: removes all elements of the list (dispose of the
list).
Count: returns the number of elements in the list.
Find nth
node from the end of the list.
JGM Data Structure and Algorithmic
11. Singly Linked Lists (SLL)
This list consists of a number of nodes in which each node has a
next pointer to the following element. The link of the last node in
the list is NULL, which indicates the end of the list.
JGM Data Structure and Algorithmic
14. Code : Traversing the SLL
1
2 def listLength(self ):
3
4 current = self.head
5 count = 0
6
7 while current != None:
8 count = count + 1
9 current = current.getNext ()
10
11 return count
JGM Data Structure and Algorithmic
15. Inserting a Node in SLL at the Beginning
Figure: 05
Figure: 06
JGM Data Structure and Algorithmic
16. Code : Inserting a Node in SLL at the Beginning
1 import Node
2
3 # method for inserting a new node at
4 # the beginning of the Linked List (at the head)
5 def insertAtBeginning (self ,data ):
6
7 newNode = Node ()
8 newNode.setData(data)
9
10 if self.length == 0:
11 self.head = newNode
12 else:
13 newNode.setNext(self.head)
14 self.head = newNode
15
16 self.length += 1
JGM Data Structure and Algorithmic
17. Inserting a Node in SLL at the Ending
Figure: 07
Figure: 08
JGM Data Structure and Algorithmic
18. Code : Inserting a Node in SLL at the Ending
1 import Node
2
3 # Method for inserting a new node at
4 # the end of a Linked List
5 def insertAtEnd(self ,data ):
6
7 newNode = Node ()
8 newNode.setData(data)
9
10 current = self.head
11
12 while current.getNext () != None:
13 current = current.getNext ()
14
15 current.setNext(newNode)
16 self.length += 1
JGM Data Structure and Algorithmic
19. Inserting a Node in SLL at the Middle
Figure: 09
Figure: 10
JGM Data Structure and Algorithmic
20. Code : Inserting a Node in SLL at the Middle
1 # Method for inserting a new node at any position in
2 def insertAtPos(self ,pos ,data ):
3 if pos > self.length or pos < 0:
4 return None
5 else:
6 if pos == 0:
7 self.insertAtBeg(data)
8 else:
9 if pos == self.length:
10 self.insertAtEnd(data)
11 else:
12 newNode = Node ()
13 newNode.setData(data)
14 count = 0
15 current = self.head
16 while count < pos -1:
17 count += 1
18 current = currcnt.getNext ()
19
20 newNode.setNext(current.getNext ())
JGM Data Structure and Algorithmic
21. Deleting the First Node in SLL
Figure: 11
Figure: 12
JGM Data Structure and Algorithmic
22. Deleting the Last Node in SLL
Figure: 13
Figure: 14
Figure: 15
JGM Data Structure and Algorithmic
24. Code : Deleting SLL
1 def clear(self) :
2 self.head = None
JGM Data Structure and Algorithmic
25. Conclusion
In this lesson we learned how to traverse, append, prepend,
insert and delete nodes from a linked list.
Backtracking is a recursive algorithm for finding all (or some)
solutions to some computational problems.
JGM Data Structure and Algorithmic
26. Bibliography
Narasimha Karumanchi. Data Structure and Algorithmic -
Thinking with Python, 2019.
John Wiley & Sons. Data Structure And Algorithms In Java
internet, 2010.
JGM Data Structure and Algorithmic