0% found this document useful (0 votes)
37 views20 pages

Linked List2

This document provides information about linked lists. It discusses creating a linked list using two arrays - INFO to store data and LINK to store next pointers. It provides algorithms for traversing, printing, counting nodes, and searching in both unsorted and sorted linked lists. It also discusses memory allocation and garbage collection in linked lists, including using a free storage list and the two steps of garbage collection. Overflow and underflow situations are mentioned when there is no available memory space for new insertions.

Uploaded by

karamjot kaur
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)
37 views20 pages

Linked List2

This document provides information about linked lists. It discusses creating a linked list using two arrays - INFO to store data and LINK to store next pointers. It provides algorithms for traversing, printing, counting nodes, and searching in both unsorted and sorted linked lists. It also discusses memory allocation and garbage collection in linked lists, including using a free storage list and the two steps of garbage collection. Overflow and underflow situations are mentioned when there is no available memory space for new insertions.

Uploaded by

karamjot kaur
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/ 20

APEX INSTITUTE OF TECHNOLOGY

Bachelor of Engineering (Computer Science &


Engineering)

Subject: Data Structure


Subject Code: CSH-231
Chapter: Linked List
Subject Coordinator:
Mr. Vishal Kumar
(E12820)

Linked List DISCOVER . LEARN .


Lecture No. 2.1.2 EMPOWER
Index
• Traversing a linked list

• Searching a linked list

2 2
Creating a List
• LIST – Linked list

• Requires two arrays INFO, LINK

• INFO[K] – contains information part

• LINK[K] – contains next pointer field of a node of


list

• START – which contains the location of the


beginning of the list.
Creating a List
START = 9
INFO LINK LINK[9] = 3, INFO[3]=O
LINK[3] =6, INFO[6]=R
1 LINK[6] = 11,
INFO[11]=E,
START 2
LINK[11] = 7
3 O 6 INFO[7]=K
4 LINK[7] = NULL - End
9
5
6 R 11
7 K Null
8
9 N 3
10
11 E 7
12
Creating a List
Struct Node
{
int info;
Struct node *next;
}*Start

/* Pointer type variable that can point to structure */

/* For example int k=10, to point k we will create integer type


pointer variable as int *ptr. Now ptr = &k. */

/* same way to point structure we need structure type pointer


hence, struct node * next – which will point to structure. */
Algorithm for Traversing a Linked List
(Traversing a Linked List) : List – linked list in a memory. The variable
PTR points to the node currently being processed.
1. Set PTR := START /* PTR = START = 1000*/
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.]
5. [End of step 2 loop]
6. Exit.

START = 1000

A 1005 B 2000 C 1010 D NULL 

1000 1005 2000 1010


Algorithm for Traversing a Linked List
(Traversing a Linked List) : List – linked list in a memory. The variable
PTR points to the node currently being processed.
1. Set PTR := START /* PTR = START = 1000*/
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.]
5. [End of step 2 loop]
Process
6. Exit. Set PTR =
INFO[PTR]=
LINK[PTR]=
INFO[1000]=A
LINK[1000]= 1005
START = 1000 PTR = 1000

A 1005 B 2000 C 1010 D NULL 

1000 1005 2000 1010


Algorithm for Traversing a Linked List
(Traversing a Linked List) : List – linked list in a memory. The variable
PTR points to the node currently being processed.
1. Set PTR := START /* PTR = START = 1000*/
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.]
5. [End of step 2 loop]
Process
6. Exit. Set PTR =
INFO[PTR]=
LINK[PTR]=
INFO[1005]=B
LINK[1005]= 2000
START = 1000 PTR = 1005

A 1005 B 2000 C 1010 D NULL 

1000 1005 2000 1010


Algorithm for Traversing a Linked List
(Traversing a Linked List) : List – linked list in a memory. The variable
PTR points to the node currently being processed.
1. Set PTR := START /* PTR = START = 1000*/
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.]
5. [End of step 2 loop]
Process
6. Exit. Set PTR =
INFO[PTR]=
LINK[PTR]=
INFO[2000]=C
LINK[2000]= 1010
START = 1000 PTR = 1005

A 1005 B 2000 C 1010 D NULL 

1000 1005 2000 1010


Algorithm for Traversing a Linked List
(Traversing a Linked List) : List – linked list in a memory. The variable
PTR points to the node currently being processed.
1. Set PTR := START /* PTR = START = 1000*/
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.]
5. [End of step 2 loop]
Process
6. Exit. Set PTR =
INFO[PTR]=
LINK[PTR]=
INFO[1010]=D
LINK[1010]= NULL
START = 1000 PTR = 1005

A 1005 B 2000 C 1010 D NULL 

1000 1005 2000 1010


Algorithm for Printing a Linked List
(Printing a Linked List) : List – linked list in a memory. This procedure
prints the information at each node of the list.
PRINT (INFO, LINK, START)
1. Set PTR := START
2. Repeat Steps 3 and 4 while PTR ≠ NULL
3. Write: INFO[PTR].
4. Set PTR:= LINK[PTR]. [PTR now points to the next node.]
5. [End of step 2 loop]
6. Exit.
Algorithm for counting total number of nodes in a Linked List

List – linked list in a memory. This procedure finds the total number of
elements in the LIST.

COUNT (INFO, LINK, START, NUM)


1. Set PTR := START [Initialize Pointer]
2. Set NUM:=0. [ Initialize Counter]
3. Repeat Steps 3 and 4 while PTR ≠ NULL
4. Set NUM : = NUM+1. [Increase NUM by 1]
5. Write: INFO[PTR].
6. Set PTR:= LINK[PTR]. [PTR now points to the next node.]
7. [End of step 2 loop]
8. Exit.
Searching an element in an unsorted Linked List

List – linked list in a memory. This procedure search location (LOC) of


particular ITEM in a list.
Set LOC = NULL
SEARCH (INFO, LINK, START, ITEM,LOC)
1. Set PTR := START [Initialize Pointer]
2. Repeat Steps 3 while PTR ≠ NULL:
If ITEM = INFO[PTR], then:
Set LOC:= PTR and EXIT
Else:
Set PTR:=LINK[PTR]
[End of IF Structure]
[End of Step 2 loop.]
3. Exit.
Searching an element in a sorted Linked List

List – linked list in a memory. This procedure search location (LOC) of particular ITEM
in a list.
Set LOC = NULL
SEARCH (INFO, LINK, START, ITEM,LOC)
1. Set PTR := START [Initialize Pointer]
2. Repeat Steps 3 while PTR ≠ NULL:
3. If ITEM < INFO[PTR], then:
Set PTR:=LINK[PTR]. [PTR now points to next_node]
Else If ITEM =INFO[PTR], then:
Set LOC:=PTR and Exit
Else:
Set LOC:=NULL, and Exit
[End of IF Structure]
[End of Step 2 loop.]
4. Set LOC:=NULL.
5. Exit.
Memory Allocation, Garbage Collection
• For insertion- provide unused memory space to the new
nodes.

• Some mechanism required where unused memory space


of deleted nodes become available for future use.

• With linked list – maintain a special list which consists of


unused memory cells.

• Special list is known as list of available space or the free


storage list or the free pool.
Memory Allocation, Garbage Collection

INFO LINK

1
START 2
3 O 6
4
9
5
6 R 11
7 K Null
8 10
9 N 3
AVAIL
10 4
12 11 E 7
12 8
Garbage Collection
• Suppose some memory space becomes reusable because a node is
deleted from a list or an entire list is deleted from a program.

• We want the space available for future use. - One way to bring this
about is to immediately reinsert the space into the free storage list.

• The operating system of a computer may periodically collect all the


deleted space onto the free storage list – any technique which does
this collection is called garbage collection.

• Garbage Collection works in two steps:


• First the computer runs through all lists, tagging those cells which are
currently in use

• And then computer runs through the memory, collecting all untagged space
onto the free storage list.
Overflow and Underflow
• Sometimes new data are to be inserted into a data structure but there
is no available space, the free storage list is empty.

• This situation is called overflow.

• When AVALI=NULL – Overflow

• When one want to delete data from a data structure that is empty –
underflow.

• When START = NULL - Underflow


References
WEB LINKS
• https://www.geeksforgeeks.org/data-structures/
• https://www.javatpoint.com/data-structure-tutoria
l
• https://www.tutorialspoint.com/data_structures_al
gorithms/index.htm
VIDEO LINK
• https://www.youtube.com/watch?v=AT14lCXuMKI
&list=PLdo5W4Nhv31bbKJzrsKfMpo_grxuLl8LU
Research Paper
• https://books.google.co.in/books?id=S-tXjl1hsUYC
THANK YOU

For queries
Email: [email protected]

20

You might also like