Singly and Circular SLL
Singly and Circular SLL
Singly and Circular SLL
II Semester
18ECSP102– Problem Solving with Data Structures
2021-2022
1
Data Structures
Motivation
A data structure is a special way of organizing and storing data in a
computer so that it can be used efficiently.
2
Data Structures
Classification
3
KLE Technological University
II Semester
18ECSP102– Problem Solving with Data Structures
List
2021-2022
4
List
Motivation
6
List
Basic operations
1. Inserting: Insert a new data in the list
2. Deleting : To remove the existing data from the list
3. Iterating a list: Navigation of list
4. Searching : Find out a specific data in the list
7
List
Motivation
9
List
Disadvantages of arrays
• Fixed in size.
• Insertions and Deletions are time consuming process.
Linked List
A linked list is a data structure in which data items can be added
and removed during run time of the program.
Characteristics of Linked List:
1. Dynamic behavior – Adding and removing of data items during
runtime.
2. Successive elements are connected by pointers.
3. Last element points to NULL (for selective Linked Lists).
4. Keeping track of a linked list:
know the pointer to the first element of the list (called as start,
head, first etc…).
head
11
Linked List
Types
Depending on the way in which the links are used to maintain
adjacency, several different types of linked lists are possible.
1. Singly-linked list
2. Circular Singly-linked list
3. Doubly linked list
4. Circular Doubly linked list
12
Linked List
13
Linked List
14
Linked List
15
Singly Linked List
16
Linked List
How to begin?
To start with, we have to create a node (the first node), and make head
point to it.
Algorithm: To create a node
Step 1: Start
Step 2: Declare a pointer for node.
Step 3: Allocate the memory for node.
Step 4: Check whether memory is allocated.
Step 5: Read the details
Step 6: Return the address of node.
Step 7: Stop
18
Singly Linked List
20
Singly Linked List
21
Linked List
22
Singly Linked List
23
Linked List
24
Singly Linked List
Step 1: Start
Step 2: Check whether the List is empty.
if yes, display message that List is empty.
Step 3: Check whether the List has only one node.
if yes, display the data and make head=NULL.
Step 4: Otherwise, Declare two pointers, prev and cur.
Step 5: Assign address of head node to cur.
Step 6: Traverse till the last node of the List. Pointer prev points to
last but one node. Pointer cur points to last node.
Step 7: Disconnect the prev from cur [last node].
Step 8: Display the data of cur [last node].
Step 9: Deallocate the memory of cur [last node].
Step 10: Stop
25
Linked List
26
Singly Linked List
After the above operation, the second node in the Singly linked list
becomes the head node of the list.
27
Linked List
28
Singly Linked List
29
Singly Linked List
30
Singly Linked List
V. Summary
Summarize the way you have solved the problem.
32
Singly Linked List
33
Singly Linked List
34
Singly Linked List
How to begin?
To start with, we have to create a node (the first node), and make head
point to it.
Algorithm: To create a node
Step 1: Start
Step 2: Declare a pointer for node.
Step 3: Allocate the memory for node.
Step 4: Check whether memory is allocated.
Step 5: Read the details
Step 6: Return the address of node.
Step 7: Stop
35
Singly Linked List
36
Singly Linked List
38
Singly Linked Lists
39
Singly Linked List
40
Singly Linked List
41
Singly Linked List
OUTPUT
Name Rank Age Address
Kiran 581 18 Bangalore
Raj 105 18 Hubballi
Amar 252 18 Hubballi
Abdul 187 19 Karwar
42
Singly Linked List
43
Singly Linked List
44
Singly Linked List
45
Singly Linked Lists
46
Singly Linked Lists
47
Singly Linked List
CLASS ACTIVITY – 4
Karnataka CET started admission for first year under graduate program for the year 2021. After the
seat allotment process, candidates who got their seat allotted in KLE Tech. started visiting the campus
for admission. Every candidate provided the his/her basic details for admission process. Management
of KLE Tech. wanted to know the admitted list of candidates after first round. Apply Problem Solving
Framework to create and display the list of candidates.
48
Singly Linked List
49
Singly Linked List
50
Singly Linked List
51
Singly Linked List
Step 1: Start
Step 2: Check whether the List is empty.
if yes, display message that List is empty.
Step 3: Check whether the List has only one node.
if yes, display the data and make head=NULL.
Step 4: Otherwise, Declare two pointers, prev and cur.
Step 5: Assign address of head node to cur.
Step 6: Traverse till the last node of the List. Pointer prev points to
last but one node. Pointer cur points to last node.
Step 7: Disconnect the prev from cur [last node].
Step 8: Display the data of cur [last node].
Step 9: Deallocate the memory of cur [last node].
Step 10: Stop
52
Singly Linked List
53
Singly Linked List
CLASS ACTIVITY – 5
Karnataka CET started admission for first year under graduate program for the year 2021.
After the seat allotment process, candidates who got their seat allotted in KLE Tech. started
visiting the campus for admission. Every candidate provided the his/her basic details for
admission process. Management of KLE Tech. wanted to know the admitted list of
candidates after first round. Apply Problem Solving Framework to create and display the list
of candidates.
During the second round of admission process, the last candidate from the list who is from
Mangalore, got admission in the Engineering College in his hometown. He withdrew his
admission from KLE Tech. Display the updated list of candidates admitted in KLE Tech.
after withdrawal of last candidate.
55
Singly Linked List
After the above operation, the second node in the Singly linked list
becomes the head node of the list.
56
Singly Linked List
57
Singly Linked List
58
Singly Linked List
59
Singly Linked List
60
Singly Linked List
61
Singly Linked List
62
Singly Linked List
63
Singly Linked List
64
Singly Linked List
PRACTICE PROBLEMS
1. Chicago college Placement Officer Mr. Mahon Raj has all the details of
the students who are placed from the college along with the company
name and passed out year. Since the ABN visit is in March 2021,
Placement Officer has two tasks. The first one displays the students
who are placed in 2020 and the second one displays the student
details who has the highest Package. Please help Mr. Mahon Raj to do
the tasks.
65
Singly Linked List
66
Linked List
67
Circular Singly linked list
Introduction
Circular linked list is a linked list where all nodes are connected to
form a circle.
There is no NULL at the end.
We can traverse the whole list by starting from any node. We just
need to stop when the first visited node is visited again.
head
101 20 30 40
68
Circular Singly linked list
Introduction
Class Activity-1 (Individual)
69
Circular Singly linked list
70
Circular Singly linked list
V. Summary
Summarize the way you have solved the problem.
72
Circular Singly linked list
73
Circular Singly linked list
Create a node
Algorithm: To create a node in Circular List
Step 1: Start
Step 2: Declare a pointer for node.
Step 3: Allocate the memory for node.
Step 4: Check whether memory is allocated.
Step 5: Make the next pointer point to same node (circular!).
Step 5: Return the address of node.
Step 6: Stop
74
Circular Singly linked list
75
Circular Singly linked list
76
Circular Singly linked list
77
Circular Singly linked list
78
Circular Singly linked list
79
Circular Singly linked list
Everyday a Postman starts distribution of the letters at 10.00 am in Lingaraj Nagar area and
returns to BVB CAMPUS post office at 12.00 noon. On first Day of the week, postman has 3
letters to deliver in his allotted area. He prepares a list of visiting houses before leaving the Post
Office and notes down the delivery address of each house. Apply Problem Solving Framework to
create and display the list of houses visited by the postman.
80
Circular Singly linked list
81
Circular Singly linked list
Consider second Day of the week, postman has 1 more additional letter to be delivered
compared to first Day (4 letters) to distribute in his allotted area. The 4th letter belongs to the
house which is located at the end of all the houses which he visited on first Day. Update and
Display the list of houses to be visited by the postman on second Day.
83
Circular Singly linked list
84
Circular Singly linked list
85
Circular Singly linked list
86
Circular Singly linked list
87
Circular Singly linked list
88
Circular Singly linked list
89
Circular Singly linked list
90
Circular Singly linked list
91
Circular Singly linked list
92
Circular Singly linked list
94
Circular Singly linked list
PRACTICE PROBLEMS
1. Wilson is resident of India, returns from America to India for a short visit. He
wishes to go for Brand Factory for shopping. Wilson buys 3 jeans of different
companies each with 40%, 50% and 60% discount and 3 casual shirts of
different companies with 40% discount on each. Apply Problem Solving to
Prepare a bill for Wilson using Circular Singly Linked List which stores the
details of each item purchased. Calculate Total Bill amount and Display the
bill.
95
Circular Singly linked list
96