Chapter 5 - Lists - Full
Chapter 5 - Lists - Full
Chapter 5 – Lists
1
Lists
•A finite, ordered sequence of elements (ordered means, each element has
a position)
•Most important concept is that of position
• First element, second element, etc.
•Notation: <a0, a1, …, an-1>
•Each list of elements has a data type, and we define a set of operations for it.
• ADT → list can be used for integers, characters, employees, etc.
•A list is defined by
• Front / Head: the start of the list (position 0)
• Back / Tail: the last element in the list (position n-1)
3
Linked lists implementation
14
Linked Lists implementation
•The linked list uses dynamic memory allocation, that is, it allocates memory
for new list elements as needed.
•A linked list is made up of a series of objects, called the nodes of the list.
•Space grows with number of elements.
•Every element requires overhead.
15
Linked Lists implementation
Insertion at the head: O(1) – we keep a pointer to head
Insertion at the end: O(1) if we keep a pointer to tail
Insertion at any position O(n), go to position and just adjust the pointers.
Deletion from the head: O(1) – we keep a pointer to head
Deletion from the end: O(n) – loop till the last element
Deletion at any position O(n) – search for element or position and just adjust
pointers
Search could be linear O(n)
▪ If sorted we could also apply binary search but it might not be very efficient since we don’t keep
positions.
16
The linked list operations (as defined in the std API in C++)
front(): Returns the value of the first element in the list.
back(): Returns the value of the last element in the list.
begin(): Returns an iterator pointing to the first element of the list.
end(): Returns an iterator pointing to the last element
empty(): Returns whether the list is empty (true) or not(false).
size(): Returns the number of elements in the list
push_front(g): Adds a new element ‘g’ at the beginning of the list.
push_back(g): Adds a new element ‘g’ at the end of the list.
Insert(g,p): Inserts a new element ‘g’ at the position ‘p’
pop_front(): removes the first element of the list
pop_back(): removes the last element of the list
remove (g): removes all elements equal to a ‘g’ and returns the number of occurrences removed
clear(): removes all elements from the list, making its size = 0
print(): prints the content of the list
17
Singly linked-List Implementation
List.h //returns the number of elements in the list
virtual int size() = 0;
#include<iostream>
using namespace std; //adds a new element at the beginning of the list
virtual void push_front(const T& element) = 0;
template<typename T>
//adds a new element at the end of the list
virtual void push_back(const T& element) = 0;
class List
//inserts new element in the list before the element at a
{
specified position
//returns the value of the first element in the list
virtual void insert (const T &element, int pos) = 0;
virtual const T &front() = 0;
//removes the first element of the list
//return the value of the last element in the list
virtual T pop_front() = 0;
virtual const T &back() = 0;
//returns an iterator pointing to the first element in the list //removes the last element of the list
virtual Node <T>* begin() = 0; virtual T pop_back() = 0;
//return an iterator pointing to the theoretical last element //removes all elements equal to a given element and
virtual Node<T>* end() = 0; returns the number of occurrences removed
virtual int remove (const T& element) = 0;
//return whether the list is empty
virtual bool empty() = 0; //removes all elements from the list, making its size = 0
virtual void clear() = 0;
class Node
{
private:
T data;
Node<T>* next;
public:
//constructors
Node(){}
Node( T data, Node<T>* next = NULL)
{
this->data = data;
this->next = next;
}
//getters
T getData() {return data;}
Node<T>*getNext() {return next;}
//setters
void setData (T data){this->data = data;}
void setNext (Node<T>* next){this->next=next;}
//print
void print(){cout<<data<<" ";}
//destructor
//~Node() { delete this;}
19
};
Singly linked-List Implementation
SinglyLinkedList.h ~SinglyLinkedList(){
clear();
}
#include <iostream>
using namespace std; const T &front()
{
#include "List.h" // if (empty()) // if(head == nullptr)
// exit(-1); // the whole application will stop
// better to check if empty in the main or use exception
handling: throw exception
template <typename T>
// else
return head->getData();
class SinglyLinkedList: public List<T> }
{ // also works with T front() {return head->getData();}
private:
Node<T>* head; // back() -O(1)
Node<T>* tail = nullptr; const T &back()
int sz = 0; { // double check in main if empty
return tail->getData();
public: }
SinglyLinkedList (Node<T>* h = nullptr)
// empty -O(1)
{
bool empty()
head = h;
{
if (head!=nullptr) return head == nullptr; // return sz==0;
{ }
tail = head;
sz = 1; // end() -O(1)
} Node<T> *end()
} {
return tail; // if empty --> return nullptr
} 20
Singly linked-List Implementation
// begin() -O(1)
Node<T> *begin(){
return head; // if empty --> return nullptr
}
if (prev != nullptr) {
//size function prev->setNext(next);
int size() { }
return sz;
} prev = curr;
curr = curr->getNext();
if (curr != nullptr) {
next = curr->getNext();
}
}
tail = prev;
}
int main()
{
SinglyLinkedList <int> ssl;
ssl.insert(0,0);
ssl.push_front(1);
ssl.push_front(7);
ssl.push_front(1);
ssl.push_front(8);
ssl.push_front(3);
ssl.print(); output
return 0;
}
26
To do: Doubly Linked Lists