03CSE354 - Implement a List Class
03CSE354 - Implement a List Class
Implementation of a
Linked List Class
Ordered Lists
Its elements are ordered by some inherent
characteristic of the elements;
e.g. names in alphabetical order.
Types of Lists
Unordered Lists the order of the elements in the list
is not based on a characteristic of the elements, but
is determined by the user of the list
A new element can be put
on the front of the list
on the rear of the list; or
after a particular element already in the list
Examples: shopping list, to-do list, …
Types of Lists
Indexed Lists elements are referenced by their
numeric position in the list, called its index
It’s the position in the list that is important, and the
user can determine the order that the items go in
the list
Every time the list changes, the position (index) of
an element may change
Example: current first-place holder in the race
The Common Operations on a List
List Implementation using Arrays
Container an array
Fix one end of the list at index 0 and shift as needed
when an element is added or removed
Is a shift needed when an element is added
at the front?
somewhere in the middle?
at the end?
Is a shift needed when an element is removed
from the front?
from somewhere in the middle?
from the end?
List Implementation using Arrays
List Implementation using Arrays:
Runtime Analysis
The worst case for these operations (adding and
deleting) is O(N).
On average, half of the list needs to be moved for
either operation, so linear time is still required.
On the other hand, if all the operations occur at the
high end of the list, then no elements need to be
shifted, and then adding and deleting take O(1)
time.
Simple Linked Lists
Source: https://www.programmingsimplified.com/c/data-structures/c-program-implement-linked-list
Simple Linked Lists
The linked list consists of a series of nodes, which
are not necessarily adjacent in memory.
Each node contains the element and a link to a
node containing its successor.
We call this the next link.
The last cell’s next link points to nullptr.
Simple Linked Lists
The remove method can be executed in one next
pointer change.
https://bit.ly/2lHvX54
Example: Implementation of a List
We will need to provide four classes:
The List class itself, which contains links to both
ends, the size of the list, and a host of methods.
The Node class, contains the data and pointers
to the previous and next nodes.
The const_iterator class, which abstracts the
notion of a position, it stores a pointer to
“current” node.
The iterator class, same functionality as
const_iterator, except that operator “*” returns
a reference to the item being viewed, rather
than a constant reference to the item.
Example: Implementation of a List
// C++ program for the above approach
#include <iostream>
using namespace std;
// Default constructor
Node()
{
data = 0;
next = NULL;
}
// Parameterised Constructor
Node(int data)
{
this->data = data; Source
this->next = NULL; https://www.geeksforgeeks.org/program-to-imp
lement-singly-linked-list-in-c-using-class/
Example: Implementation of a List
We will implement a list using C++ in the lab!
https://www.programmingsimplified.com/c/data-structures/c-program-implement-linked-list
Example: Visualization of a List
https://visualgo.net/en/list
Conclusion