0% found this document useful (0 votes)
11 views28 pages

Linked List

The document provides an overview of linked lists, including C++ code examples for operations such as adding and removing nodes. It discusses the differences between singly-linked and doubly-linked lists, highlighting the complexities of moving backward in singly-linked lists and the advantages of using two pointers in doubly-linked lists. Additionally, it introduces circularly-linked lists as a solution to certain limitations of traditional linked lists.

Uploaded by

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

Linked List

The document provides an overview of linked lists, including C++ code examples for operations such as adding and removing nodes. It discusses the differences between singly-linked and doubly-linked lists, highlighting the complexities of moving backward in singly-linked lists and the advantages of using two pointers in doubly-linked lists. Additionally, it introduces circularly-linked lists as a solution to certain limitations of traditional linked lists.

Uploaded by

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

Data Structures

Linked List
C++ Code for Linked List
// position current before the first
// list element
void start() {
lastCurrentNode = headNode;
currentNode = headNode;
};
C++ Code for Linked List
void remove() {
if( currentNode != NULL &&
currentNode != headNode) {
lastCurrentNode->setNext(currentNode-
>getNext());
delete currentNode;
currentNode = lastCurrentNode->getNext();
size--;
}
}; currentNode
headNode

2 6 8 7 1 size=5

lastcurrentNode
C++ Code for Linked List
void remove() {
if( currentNode != NULL &&
currentNode != headNode) {
1 lastCurrentNode->setNext(currentNode-
>getNext());
delete currentNode;
currentNode = lastCurrentNode->getNext();
size--;
}
}; currentNode
headNode
1

2 6 8 7 1 size=5

lastcurrentNode
C++ Code for Linked List
void remove() {
if( currentNode != NULL &&
currentNode != headNode) {
1 lastCurrentNode->setNext(currentNode-
>getNext());
2
delete currentNode;
currentNode = lastCurrentNode->getNext();
size--;
}
}; currentNode
headNode
1

2 8 7 1 size=5
2
lastcurrentNode
C++ Code for Linked List
void remove() {
if( currentNode != NULL &&
currentNode != headNode) {
1 lastCurrentNode->setNext(currentNode-
>getNext());
2
3
delete currentNode;
4
currentNode = lastCurrentNode->getNext();
size--;
} 3
}; currentNode
headNode
1 4
2 8 7 1 size=4
2
lastcurrentNode
C++ Code for Linked List
int length()
{
return size;
};

private:
int size;
Node *headNode;
Node *currentNode, *lastCurrentNode;
Lecture No.04

Data Structures

Dr. Sohail Aslam


Example of List Usage
#include <iostream>
#include <stdlib.h>
#include "List.cpp"

int main(int argc, char *argv[])


{
List list;

list.add(5); list.add(13); list.add(4);


list.add(8); list.add(24); list.add(48);
list.add(12);
list.start();
while (list.next())
cout << "List Element: "<<
list.get()<<endl;
}
Analysis of Linked List
 add
• we simply insert the new node after the current
node. So add is a one-step operation.
Analysis of Linked List
 add
• we simply insert the new node after the current
node. So add is a one-step operation.
 remove
 remove is also a one-step operation
Analysis of Linked List
 add
• we simply insert the new node after the current
node. So add is a one-step operation.
 remove
 remove is also a one-step operation
 find
 worst-case: may have to search the entire list
Analysis of Linked List
 add
• we simply insert the new node after the current
node. So add is a one-step operation.
 remove
 remove is also a one-step operation
 find
 worst-case: may have to search the entire list
 back
 moving the current pointer back one node requires
traversing the list from the start until the node whose
next pointer points to current node.
Doubly-linked List
 Moving forward in a singly-linked list is easy;
moving backwards is not so easy.
Doubly-linked List
 Moving forward in a singly-linked list is easy;
moving backwards is not so easy.
 To move back one node, we have to start at
the head of the singly-linked list and move
forward until the node before the current.
Doubly-linked List
 Moving forward in a singly-linked list is easy;
moving backwards is not so easy.
 To move back one node, we have to start at
the head of the singly-linked list and move
forward until the node before the current.
 To avoid this we can use two pointers in a
node: one to point to next node and another to
point to the previous node:

prev element next


Doubly-Linked List Node
class Node {
public:
int get() { return object; };
void set(int object) { this->object = object; };

Node* getNext() { return nextNode; };


void setNext(Node* nextNode)
{ this->nextNode = nextNode; };
Node* getPrev() { return prevNode; };

void setPrev(Node* prevNode)
{ this->prevNode = prevNode; };
private:
int object;
Node* nextNode;
 Node* prevNode;
};
Doubly-linked List
 Need to be more careful when adding or
removing a node.
 Consider add: the order in which pointers are
reorganized is important:

head 2 6 8 7 1 size=5

current
Doubly-linked List
1. newNode->setNext( current->getNext() );

current

head 2 6 8 7 1 size=5

newNode 9 1
Doubly-linked List
1. newNode->setNext( current->getNext() );
2. newNode->setprev( current );

current

head 2 6 8 7 1 size=5

newNode 9 1
Doubly-linked List
1. newNode->setNext( current->getNext() );
2. newNode->setprev( current );
3. (current->getNext())->setPrev(newNode);

current

head 2 6 8 7 1 size=5

2 3

newNode 9 1
Doubly-linked List
1. newNode->setNext( current->getNext() );
2. newNode->setprev( current );
3. (current->getNext())->setPrev(newNode);
4. current->setNext( newNode );
current

head 2 6 8 7 1 size=5

2 4 3

newNode 9 1
Doubly-linked List
1. newNode->setNext( current->getNext() );
2. newNode->setprev( current );
3. (current->getNext())->setPrev(newNode);
4. current->setNext( newNode );
5. current = newNode;
6. size++;

head 2 6 8 7 1 size=6

2 4 3

newNode 9 1

current
Circularly-linked lists
 The next field in the last node in a singly-linked
list is set to NULL.
 Moving along a singly-linked list has to be done
in a watchful manner.
 Doubly-linked lists have two NULL pointers:
prev in the first node and next in the last node.
 A way around this potential hazard is to link the
last node with the first node in the list to create
a circularly-linked list.
Circularly-linked lists
 The next field in the last node in a singly-linked
list is set to NULL.
 Moving along a singly-linked list has to be done
in a watchful manner.
 Doubly-linked lists have two NULL pointers:
prev in the first node and next in the last node.
 A way around this potential hazard is to link the
last node with the first node in the list to create
a circularly-linked list.
Circularly-linked lists
 The next field in the last node in a singly-linked
list is set to NULL.
 Moving along a singly-linked list has to be done
in a watchful manner.
 Doubly-linked lists have two NULL pointers:
prev in the first node and next in the last node.
 A way around this potential hazard is to link the
last node with the first node in the list to create
a circularly-linked list.
Circularly-linked lists
 The next field in the last node in a singly-linked
list is set to NULL.
 Moving along a singly-linked list has to be done
in a watchful manner.
 Doubly-linked lists have two NULL pointers:
prev in the first node and next in the last node.
 A way around this potential hazard is to link the
last node with the first node in the list to create
a circularly-linked list.
Cicularly Linked List
 Two views of a circularly linked list:
current

head 2 6 8 7 1 size=5

current
6
8
size=5
head 2

You might also like