0% found this document useful (0 votes)
6 views

03CSE354 - Implement a List Class

Uploaded by

aymansamy1772004
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)
6 views

03CSE354 - Implement a List Class

Uploaded by

aymansamy1772004
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/ 24

Lecture 3

Implementation of a
Linked List Class

CSE354-Algorithms and Data Structure © Dr. Ahmed Ayoub, 2023


Learning Objectives
By the end of this lecture you will be able to:

 Abstract Data Types (ADTs)


 Types of Lists
 Simple Array Implementation of Lists
 Simple Linked Lists
 Example: Implementation of list
Abstract Data Types (ADTs)
 An abstract data type (ADT) is:

“a set of objects together with a set of operations”

 Objects such as lists, sets, and graphs, along with


their operations, can be viewed as ADTs, just as
integers, reals, and booleans are data types.
 For the set ADT, we might have such operations as
add, remove, size, and contains.
 The C++ class allows for the implementation of
ADTs, with appropriate hiding of implementation
details.
The List ADT

 We will deal with a general list of the form:

A0, A1, A2, . . ., AN−1

 We say that the size of this list is N.


 We will call the special list of size “0” an empty list.
The List ADT: Definitions
 For any list except the empty list, we say that Ai
follows (or succeeds) Ai−1 (i < N) and that Ai−1
precedes Ai (i > 0).
 The first element of the list is A0, and
 the last element is AN−1.
 We will not define the predecessor of A0 or the
successor of AN−1.
 The position of element Ai in a list is i.
 We will assume, to simplify matters, that the
elements in the list are integers
The List ADT: Operations
 Associated with these “definitions” is a set of
operations that we would like to perform on the
List ADT.
 Some popular operations are:
 printList and makeEmpty,
 find, which returns the position of the first
occurrence of an item;
 insert and remove, which generally insert and
remove some element from some position in the
list; and
 findKth, which returns the element in some
position (specified as an argument).
The List ADT: Operations
 If the list is
34, 12, 52, 16, 12

 find(52) might return 2;


 insert(x,2) might make the list into
34, 12, x, 52, 16, 12
 remove(52) might turn that list into
34, 12, x, 16, 12
Types of Lists

 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

 In order to avoid the linear cost of insertion and


deletion, we need to ensure that the list is not
stored contiguously, since otherwise entire parts of
the list will need to be moved.
 The general idea of a linked list looks like:
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.

 The insert method requires obtaining a new node


from the system by using a new call and then
executing two next pointer maneuvers.
Linked Lists Animated

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;

// Node class to represent


// a node of the linked list.
class Node {
public:
int data;
Node* next;

// 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

 Abstract Data Types (ADTs)


 Types of Lists
 Simple Array Implementation of Lists
 Simple Linked Lists
 Example: Implementation of list

You might also like