Data Structures & Algorithms: Lecture 4: Linked Lists
Data Structures & Algorithms: Lecture 4: Linked Lists
22/10/2020
Recap slide
▪ Referencing vs pointers
► Reference a reference variable is implemented as a
constant pointer to the variable
▪ Dynamic memory allocation
► Stack vs heap
- Stack is special region of computer's memory that stores
temporary variables created by each function, including
the main()
- memory managed automatically
- Heap is region of memory NOT managed automatically
- The new operator & memory leaks
▪ Concept of shalow/deep copying
► Copy constructor for deep copying
▪ Void pointer
▪ Linked structures
► Singly Linked Lists
Advantage 1: Dynamic
▪ A linked list is appropriate when the number of data
elements to be stored in the list is unknown
▪ Because linked lists are dynamic, their size can grow
or shrink to accommodate the actual number of
elements in the list
▪ A linked list is full only when the computer runs out of
memory in which to store nodes
M. Shahzad: Data Structures & Algorithms Lecture 4: Linked Lists 5
Advantages of Linked Lists
struct ListNode {
float value;
struct ListNode *next;
};
► Insert at last
▪ Deleting nodes
► Delete from beginning, middle, last
▪ Traversing the list
▪ Searching a specified item in the list
▪ Destroying the list
public:
FloatList(void) { // Constructor
head = NULL;
}
~FloatList(void) { }; // Destructor
void appendNode(float);
void displayList(void);
void deleteNode(float);
};
M. Shahzad: Data Structures & Algorithms Lecture 4: Linked Lists 12
//floatList.h
class FloatList {
private:
// Declare a structure for the list
struct ListNode {
float value;
struct ListNode *next;
};
public:
FloatList(void) // Constructor {
head = NULL;
}
~FloatList(void) { }; // Destructor
void appendNode(float);
void displayList(void);
void deleteNode(float);
};
M. Shahzad: Data Structures & Algorithms Lecture 4: Linked Lists 13
Appending a node to the list
// If there are no nodes in the list make newNode the first node
if (!head) // head == NULL
head = newNode;
else // Otherwise, insert newNode at end
{ nodePtr = head;
while (nodePtr->next) // Find the last node in the list
nodePtr = nodePtr->next;
// If there are no nodes in the list make newNode the first node
if (!head) // head == NULL
head = newNode;
else // Otherwise, insert newNode at end
{ nodePtr = head;
while (nodePtr->next) // Find the last node in the list
nodePtr = nodePtr->next;
// If there are no nodes in the list make newNode the first node
if (!head) // head == NULL
head = newNode;
else // Otherwise, insert newNode at end
{ nodePtr = head;
while (nodePtr->next) // Find the last node in the list
nodePtr = nodePtr->next;
// If there are no nodes in the list make newNode the first node
if (!head) // head == NULL
head = newNode;
else // Otherwise, insert newNode at end
{ nodePtr = head;
while (nodePtr->next) // Find the last node in the list
nodePtr = nodePtr->next;
// If there are no nodes in the list make newNode the first node
if (!head) // head == NULL
head = newNode;
else // Otherwise, insert newNode at end
{ nodePtr = head;
while (nodePtr->next) // Find the last node in the list
nodePtr = nodePtr->next;
// If there are no nodes in the list make newNode the first node
if (!head) // head == NULL
head = newNode;
else // Otherwise, insert newNode at end
{ nodePtr = head;
while (nodePtr->next) // Find the last node in the list
nodePtr = nodePtr->next;
void FloatList::displayList(void)
{
ListNode *nodePtr;
nodePtr = head;
while (nodePtr)
{
cout << nodePtr->value << endl;
nodePtr = nodePtr->next;
}
}
► Remove the node from the list without breaking the links
created by the next pointers
else {
// Initialize nodePtr to head of list
nodePtr = head;
// Skip all nodes whose value member is not equal to num.
while (nodePtr != NULL && nodePtr->value != num)
{
previousNode = nodePtr;
nodePtr = nodePtr->next;
}
// Link the previous node to the node after nodePtr, then delete
nodePtr.
previousNode->next = nodePtr->next;
delete nodePtr;
}
}
From: http://www.tutorialspoint.com/data_structures_algorithms/data_structures_algorithms_tutorial.pdf
▪ Declare two classes; one for nodes of the list while the other for
access to the list
class AccessNode{
private:
class Node {
node *head, *tail;
public:
Public:
Node() {
AccessNode() { head=tail=null;}
next = 0;
int isEmpty() {return head==0;}
}
void addToHead(int);
Node(int i, Node *in = 0) {
void addToTail(int);
info = i; next = in;
int deleteFromHead();
}
int deleteFromTail();
int info;
void deleteNode(int);
Node *next;
bool isInList(int) const;
};
~ AccessNode();}
M. Shahzad: Data Structures & Algorithms Lecture 4: Linked Lists 52
Adding a node at the beginning