0% found this document useful (0 votes)
58 views38 pages

Session 4

Uploaded by

ISAAC SICHALWE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views38 pages

Session 4

Uploaded by

ISAAC SICHALWE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

LINKED LISTS BIT250 Data Structures

OBJECTIVES

To understand the concepts of Linked lists


INTRODUCTION
•In computing, a data structure is a particular way of storing
and organizing data in a computer’s memory so that it can be
used efficiently.
•Data may be organized in many different ways; the logical
or mathematical model of a particular organization of data
is called a data structure. The choice of a particular data
model depends on the two considerations first; it must
•be rich enough in structure to mirror the actual relationships
of the data in the real world.
•On the other hand, the structure should be simple enough that
one can effectively process the data whenever necessary.
LINKED LISTS
Here is something part way between an example and an analogy. You
have some errands to do, so you grab a piece of paper and write:
 bank
 groceries
 drop off dry cleaning

Then you remember that you also need to buy stamps. Because of the
geography of your town, you need to do that after the bank. You could
copy your whole list onto a new piece of paper:
 bank
 stamps
 groceries
 drop off dry cleaning

or you could scribble on the one you had:


 bank ....... STAMPS
 groceries
 drop off dry cleaning
LINKED LISTS
As you thought of other errands, you might write them at the bottom of
the list, but with arrows reminding yourself what order to do them in.
This is a linked list. It's quicker and easier than copying the whole list
around every time you add something.

Then your cell phone rings while you're at the bank "hey, I got the
stamps, don't pick up any more". You just cross STAMPS off the list, you
don't rewrite a whole new one without STAMPS in it.

Now you could actually implement an errands list in code (maybe an


app that puts your errands in order based on your geography) and
there's a reasonable chance you would actually use a linked list for that
in code. You want to add and remove lots of items, order matters, but
you don't want to recopy the whole list after each insertion or deletion.
as opposed to the array where you need to create a new array
LINKED LISTS
Linked lists are dynamic storage data types i.e. instead of
allocating space at compile time, as in the case of arrays

With linked lists you can allocate space at runtime


depending on your actual needs.
A linked list can grow or shrink in size as the program
runs
A linked list can easily grow or shrink in size.
Insertion and deletion of nodes is quicker with linked
lists than with arrays.
VARIATIONS OF LINKED LISTS
Types of linked lists:
 Singly linked list
 Begins with a pointer to the first node
 Terminates with a null pointer
 Only traversed in one direction
 Circular
 Pointer in the last node points back to the first node
 Doubly linked list
 Two “start pointers” – first element and last element
 Each node has a forward pointer and a backward pointer
 Allows traversals both forwards and backwards
LINKED LIST
•A linked list or one way list is a linear collection of
data elements, called nodes, where the linear order is
given by means of pointers.
•Each node is divided into two parts:
· The first part contains the information of the
element/node
· The second part contains the address of the next
node (link /next pointer field) in the list.
•There is a special pointer Start/List (Header)contains
the address of first node in the list. If this special
pointer contains null, means that List is empty.
EXAMPLE
LINKED LISTS
A node’s successor is the next node in the sequence
 The last node has no successor

A node’s predecessor is the previous node in the sequence


 The first node has no predecessor

A list’s length is the number of elements in it


 A list may be empty (contain no elements)
LINKED LISTS
A node in a linked list is a structure that has at least two fields. One
of the fields is a data field; the other is a pointer that contains the
address of the next node in the sequence.

A node with one data field:


struct node{
int number;
struct node * link;
};

number link
LINKED LISTS
A node with three data fields:
struct student {
char name[20];
int id;
double GPA;
struct student *next_student;
}

name id GPA next_student


LINKED LISTS
A structure in a node:
struct person{
char name[20];
char address[30];
char phone[10];
};
struct person_node{
struct person data; //data
struct person_node *next; // pointer to next node
};
LINKED LISTS
 A linked list is called "linked" because each
node in the series has a pointer that points
to the next node in the list.
LINKED LISTS
Operations on list
1. Add a node.
2. Delete a node.
3. Search for a node.
4. Traverse (walk) the list. Useful for counting operations or
aggregate operations.

NB: Revision on structures and pointers is important to


understand lists and the coming data structures
LINKED LISTS :
OPERATIONS
Adding Nodes to a Linked List
There are four steps to add a node to a
linked list:
 Allocate memory for the new node.
 Determine the insertion point (you need to know only the new node’s
predecessor (point_Prec)
 Point the new node to its successor.
 Point the predecessor to the new node.

Pointer to the predecessor (point_Prec) can be in one of two


states:
 it can contain the address of a node (i.e. you are adding somewhere after
the first node – in the middle or at the end)
 it can be NULL (i.e. you are adding either to an empty list or at the
beginning of the list)
LINKED LISTS : OPERATIONS
Before addition to the empty list
pointer_New 39

pointer_Head

pointer_Prec

After addition to the empty list


pointer_New 39

pointer_Head

pointer_Prec
LINKED LISTS : OPERATIONS
Before addition to the beginning of a list that is not empty

pointer_New 39

pointer_Head
75 124

pointer_Prec
After addition to the beginning of the none empty list

pointer_New 39

pointer_Head
75 124

pointer_Prec
LINKED LISTS : OPERATIONS
Before addition in the middle of a list that is not empty

pNew
pointer_New 64

55 124

pointer_Prec
After addition in the middle of the none empty list

pNew
pointer_New 64

124

pointer_Prec 55
LINKED LISTS : OPERATIONS
Before addition at the end of a list that is not empty

pNew
pointer_New 64

55 124

pointer_Prec

After addition at the end of the none empty list

pNew
pointer_New 64

55 124
pointer_Prec
LINKED LISTS :OPERATIONS

Deleting a node from a liked list


Deleting a node requires that we logically
remove the node from the list by changing
various links and then physically deleting the
node from the list (i.e., return it to the heap).
Any node in the list can be deleted. Note that if
the only node in the list is to be deleted, an
empty list will result. In this case the head
pointer will be set to NULL.
LINKED LISTS :
OPERATIONS
Deleting a node from a liked list
To logically delete a node:
 First locate the node itself (pointer_Current) and its
logical predecessor (pointer_Predecessor).
 Change the predecessor’s link field to point to the
deleted node’s successor (located at pointer_Curren
-> next).
 Recycle the node using the free() function.
DOUBLY LINKED LIST
•A Doubly Linked List (DLL) contains an extra pointer,
typically called previous pointer, together with next
pointer and data which are there in singly linked list.
•In doubly linked list each node contains two pointers.
•which points has a reference to both the next point
and pervious point of node in list.
•A doubly linked list is a two-way list because one can
move in either from left to right or from right to left
NODE DATA
Info : the user’s data.
Prev, Next : the address of next and previous
node in list
OPERATIONS OF DLL
•Create list.
•Insert element at beginning in list.
•Insert element at end in list.
•Insert element at any place in list.
•Delete element from the beginning of list.
•Delete element from the end of list.
•Delete element from any place from list.
CREATE DOUBLY LINKED LIST
INSERT AN ELEMENT AT BEGINNING
DOUBLY LINKED LIST
DELETE AN ELEMENT AT ANY PLACE
DOUBLY LINKED LIST
DOUBLY LINKED LIST
ADVANTAGES DISADVANTAGES
1. We can traverse in both It requires more space per
directions i.e. from starting space per node because
to end and as well as from one extra field is required
end to starting. for pointer to previous
node.
2. It is easy to reverse the
linked list. 2. Insertion and deletion
take more time than linear
3. If we are at a node, then linked list because more
we can go to any node. But pointer operations are
in linear linked list, it is not required than linear linked
possible to reach the list.
previous node.
CIRCULAR LINKED LISTS
A Circular Linked List is a special type of Linked List
It supports traversing from the end of the list to the beginning
by making the last node point back to the head of the list
A Rear pointer is often used instead of a Head pointer

10 20 40 55 70

Rear
MOTIVATION

Circular linked lists are usually sorted


Circular linked lists are useful for playing
video and sound files in “looping” mode
They are also a stepping stone to
implementing graphs, an important topic in
this course
CLL

Circular linked lists can be used to help the


traverse the same list again and again if
needed. A circular list is very similar to the
linear list where in the circular list the
pointer of the last node points not NULL but
the first node.
CLL
CLL
APPLICATIONS OF LINKED LISTS
Linked lists are used to implement stacks,
queues, graphs, etc.
Linked lists let you insert elements at the
beginning and end of the list.
In Linked Lists we don't need to know the
size in advance.
POINTS TO REMEMBER

•head points to the first node of the linked


list
•next pointer of last node is NULL, so if next
of current node is NULL, we have reached
end of linked list.
CONCLUSION

•Lists are one of the most popular and efficient


data structures, with implementation in every
programming language like C, C++, Python, Java
and C#.
•Apart from that, linked lists are a great way to
learn how pointers work. By practicing how to
manipulate linked lists, you can prepare yourself
to learn more advanced data structures like
graphs and trees.
EXCERCISE
Write a C programme using
Structures and Pointers, create a
simple Linked List with three items
to understand how this works.

You might also like