0% found this document useful (0 votes)
12 views40 pages

Lec 2-DS ALGO (Final)

The document provides an overview of linked lists, including their structure, operations like traversing, searching, insertion, and deletion. It explains how nodes are linked together and the algorithms for managing these operations in both unsorted and sorted lists. Additionally, it introduces the concept of doubly-linked lists for easier backward traversal.

Uploaded by

hamza ahmed
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)
12 views40 pages

Lec 2-DS ALGO (Final)

The document provides an overview of linked lists, including their structure, operations like traversing, searching, insertion, and deletion. It explains how nodes are linked together and the algorithms for managing these operations in both unsorted and sorted lists. Additionally, it introduces the concept of doubly-linked lists for easier backward traversal.

Uploaded by

hamza ahmed
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/ 40

Data Structures & Algorithm

CS-102

Lecture 2

Link List

Lecturer: Syeda Nazia Ashraf

1
Linked List
 Create a structure called a Node.

object next

 The object field will hold the actual list element.


 The next field in the structure will hold the
starting location of the next node.
 Chain the nodes together to form a linked list.

2
Linked List
 Picture of our list (2, 6, 7, 8, 1) stored as a
linked list:

head

2 6 8 7 1 size=5

current

3
Link List

4
Linked List
Note some features of the list:
 Need a head to point to the first node of the list.
Otherwise we won’t know where the start of the
list is.
 The current here is a pointer, not an index.
 The next field in the last node points to nothing.
We will place the memory address NULL which
is guaranteed to be inaccessible.

5
Operations on Linked List:

There are several operations associated with


linked list i.e.
a) Traversing a Linked List
Suppose we want to traverse LIST in order to
process each node exactly once. The
traversing algorithm uses a pointer variable
PTR which points to the node that is currently
being processed. Accordingly, PTR->NEXT
points to the next node to be processed so,
6
PTR:=START [ Moves the pointer to the first node of the
list]
PTR:=LINK[PTR] [ Moves the pointer to the next node in
the list.]
INFO[PTR] [Process information at the node]

7
8
Algorithm: (Traversing a Linked List) Let LIST be a
linked list in memory. This algorithm traverses LIST,
applying an operation PROCESS to each element of
list. The variable PTR point to the node currently being
processed.

1. Set PTR:=START. [Initializes pointer PTR.]


2. Repeat Steps 3 and 4 while PTR ≠ NULL.
3. Apply PROCESS to INFO[PTR].
4. Set PTR:= LINK[PTR]. [PTR now points to the
next node]
[End of Step 2 loop.]
5. Exit. 9
b) Searching a Linked List:(UNSORTED LIST)
Suppose the data in LIST are not necessarily sorted. Then one searches for ITEM in LIST by
traversing through the list using a pointer variable PTR and comparing ITEM with the contents
INFO[PTR] of each node, one by one, of LIST. Before we update the pointer PTR by
PTR:=LINKPTR], we require two tests. First we have to check to see whether we have reached
the end of the LIST i.e. PTR=NULL. If not, then we check to see whether INFO[PTR]=ITEM.
The two tests cannot be performed at the same time, since INFO[PTR] is not defined when
PTR=NULL. Accordingly we use the First test to control the execution of a loop, and we let the
second test take place inside the loop.

Algorithm: SEARCH(INFO, LINK,START, ITEM, LOC)


LIST is a linked list in the memory. This algorithm finds the location LOC of the node where ITEM
first appear in LIST, or sets LOC=NULL.
1. Set PTR:=START.
2. Repeat Step 3 while PTR≠NULL:
3. If ITEM = INFO[PTR], then:
Set LOC:=PTR, and Exit. [Search is successful.]
Else:
Set PTR:=LINK[PTR]. [PTR now points to the next node]
[End of If structure.]
[End of Step 2 loop.]
4. Set LOC:=NULL. [Search is unsuccessful.]
5. Exit. 10
b) Searching a Linked List:(SORTED LIST)
Suppose the data in LIST are sorted. Again we search for ITEM in LIST by traversing the list using
a pointer variable PTR and comparing ITEM with the contents INFO[PTR] of each node, one by
one, of LIST. Now, however, we can stop once ITEM exceeds INFO[PTR]. (List sorted in
descending order)

Algorithm: SRCHSL(INFO, LINK,START, ITEM, LOC)


LIST is sorted list in memory. This algorithm finds the location LOC of the node where ITEM first
appear in LIST, or sets LOC=NULL.

1. Set PTR:=START.
2. Repeat Step 3 while PTR≠NULL:
3. If ITEM < INFO[PTR], then:
Set PTR:=LINK[PTR]. [PTR now points to the next node]
Else If ITEM=INFO[PTR],then
Set LOC:=PTR, and Exit. [Search is successful.]
Else:
LOC:=NULL , and Exit. [ITEM now exceeds INFO[PTR].]
[End of If structure.]
[End of Step 2 loop.]
4. Set LOC:=NULL.
5. Exit.
11
Search Linked List for insertion and deletion of Nodes:
Both insertion and deletion operations need searching the linked list.
• To add a new node, we must identify the logical predecessor (address of
previous node) where the new node is to be inserting.
• To delete a node, we must identify the location (addresses) of the node to
be deleted and its logical predecessor (previous node).

Basic Search Concept


Assume there is a sorted linked list and we wish that after each
insertion/deletion this list should always be sorted. Given a target value,
the search attempts to locate the requested node in the linked list.
Since nodes in a linked list have no names, we use two pointers, pre (for
previous)and cur (for current) nodes. At the beginning of the search, the
pre pointer is null and the cur pointer points to the first node (Head). The
search algorithm moves the two pointers together towards the end of the
list. Following Figure shows the movement of these two pointers through
the list in an extreme case scenario: when the target value is larger than
any value in the list.
12
Moving of pre and cur pointers in searching a linked list
13
Values of pre and cur pointers in different cases

14
Linked List Operations
 add(9): Create a new node in memory to hold ‘9’
Node* newNode = new Node(9); newNode 9

15
Linked List Operations
 add(9): Create a new node in memory to hold ‘9’
Node* newNode = new Node(9); newNode 9

 Link the new node into the list

head

2 6 8 7 1 size=5 6

current 2 1
3
9

newNode

16
Building a Linked List
List list; headNode size=0

17
Building a Linked List
List list; headNode size=0

currentNode

headNode 2 size=1
list.add(2);
lastcurrentNode

18
Building a Linked List
List list; headNode size=0

currentNode

headNode 2 size=1
list.add(2);
lastcurrentNode

currentNode

list.add(6); headNode 2 6 size=2

lastcurrentNode

19
Building a Linked List

List.add(8); list.add(7); list.add(1);

currentNode

headNode 2 6 8 7 1 size=5

lastcurrentNode

20
Insertion into a Linked List:
If a node N is to be inserted into the list between nodes A and B in a
linked list named LIST. Its schematic diagram would be:

Insertion at the beginning of a List

21
Inserting at the Beginning of a List:
If the linked list is sorted list and new node has the least low value
already stored in the list i.e. (if New->info < Head->info) then new node
is inserted at the beginning / Top of the list.

22
23
Insertion In Sorted Linked List
Steps:
1.Find the Location of the node after witch new node will be inserted
2.Use Insertion method to insert into the Linked List
Find the Location of the node
3.Search the Item after which insertion needs to be made . As the Linked List is sorted
searching will result in the location of element >given ITEM
4.As Insertion can not be made before a Node therefore another pointer will keep track of
the previous node i.e before moving the PTR , Save := PTR
5.Thus PTR points to current node Element which is > ITEM while SAVE points to
Element<ITEM
6.Thus Insertion can be made after SAVE

24
2

25
Deletion from a Link List

currentNode
headNode

2 6 8 7 1 size=5

lastcurrentNode

26
Deletion from a Linked List

currentNode
headNode
1

2 6 8 7 1 size=5

lastcurrentNode

27
Deletion from a Linked List

currentNode
headNode
1
2 8 7 1 size=5
2
lastcurrentNode

28
Deletion from a Linked List

3
currentNode
headNode
1 4
2 8 7 1 size=4
2
lastcurrentNode

29
30
Analysis of Linked List
 add
• we simply insert the new node after the current
node. So add is a one-step operation.
 remove
 remove is also a one-step operation
 find
 worst-case: may have to search the entire list
 back
 moving the current pointer back one node requires
traversing the list from the start until the node whose
next pointer points to current node.

31
Two-Way or Doubly-linked List
 Moving forward in a singly-linked list is easy;
moving backwards is not so easy.
 To move back one node, we have to start at the
head of the singly-linked list and move forward
until the node before the current.
 To avoid this we can use two pointers in a
node: one to point to next node and another to
point to the previous node:

prev element next

32
Doubly-linked List
 Need to be more careful when adding or
removing a node.
 Consider add: the order in which pointers are
reorganized is important:

head 2 6 8 7 1 size=5

current

33
34
35
Insertion in Doubly-linked List
current

head 2 6 8 7 1 size=5

newNode 9 1

36
Doubly-linked List
current

head 2 6 8 7 1 size=5

newNode 9 1

37
Doubly-linked List
current

head 2 6 8 7 1 size=5

2 3

newNode 9 1

38
Doubly-linked List
current

head 2 6 8 7 1 size=5

2 4
3
newNode 9 1

39
Doubly-linked List

head 2 6 8 7 1 size=6

2 4 3

newNode 9 1

current

40

You might also like