Unit06 DoublyLinkedLists

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

Doubly Linked Lists

1
What is a Doubly-linked list?
• A doubly linked list is a linked list in which every node has a
next pointer and a previous pointer.
• Each node stores:
− Info
− next (pointer to the next node)
− previous (pointer to the previous node)

2
Doubly Linked List Node Definition
Struct to build a node:

struct node {
int info;
node* next;
node* previous;
};

3
Example
• Write a set of statements to insert a new node with info 50
before the node having pointer p.

node *newNode = new node;


newNode->info = 50;
newNode->next = p;
newNode->previous = p->previous;
p->previous->next = newNode;
p->previous = p;

4
Example
• Write a set of statement to delete the node having pointer p.

p->previous-next = p->next;
p->next->previous = p->previous;
delete p;

5
Class-based Doubly Linked List
Create a class that define a double linked list with the following
specification:
• Member variables:
− head, tail: pointer variables of type node (private)
• Member functions (public):
− constructor: initialize head and tail to NULL
− insertBeg: receives an int value; add a new node to list at front
− insertEnd: receives an int value; add a new node to list at the end
− deleteBeg: delete the first node of the list
− deleteEnd: delete the last node of the list
− print: print on screen all items in the list
− printReversed: print on screen all items in the list in reverse order

6
Class Specification
class doublyList {
private:
node* head, * tail;
public:
doublyList();
void insertBeg(int x);
void insertEnd(int x);
void deleteBeg();
void deleteEnd();
void print();
void printReversed();
};

7
Member functions - constructor
doublyList::doubleList(){
head = tail = NULL;
}

8
Member functions - insertBeg
void doublyList::insertBeg(int x){
node* newNode = new node;
newNode->info = x;
newNode->previous = NULL;
if (head == NULL) {
head = tail = newNode;
newNode->next = NULL;
}
else {
newNode->next = head;
head->previous = newNode;
head = newNode;
}
}
9
Member functions - insertEnd
void doublyList::insertEnd(int x) {
node* newNode = new node;
newNode->info = x;
newNode->next = NULL;
if (head == NULL) {
head = tail = newNode;
newNode->previous = NULL;
}
else {
newNode->previous = tail;
tail->next = newNode;
tail = newNode;
}
}

10
Member functions - deleteBeg
void doublyList::deleteBeg() {
if (head == NULL)
cout << "Error: List is empty";
else if (head == tail) { // only one node
delete head;
head = tail = NULL;
}
else {
head = head->next;
delete head->previous;
head->previous = NULL;
}
}

11
Member functions - deleteEnd
void doublyList::deleteEnd() {
if (head == NULL)
cout << "Error: List is empty";
else if (head == tail) { // only one node
delete head;
head = tail = NULL;
}
else {
tail = tail->previous;
delete tail->next;
tail->next = NULL;
}
}

12
Member functions - print
void doublyList::print(){
if (head == NULL)
cout << "List is empty";
else {
node* ptr = head;
cout << "List items: ";
while (ptr != NULL) {
cout << ptr->info <<" ";
ptr = ptr->next;
}
cout << endl;
}
}

13
Member functions - printReversed
void doublyList::printReversed() {
if (head == NULL)
cout << "List is empty";
else {
node* ptr = tail;
cout << "List items: ";
while (ptr != NULL) {
cout << ptr->info << " ";
ptr = ptr->previous;
}
cout << endl;
}
}

14
Main function example
int main()
{
doublyList myDL;
myDL.insertBeg(2);
myDL.insertEnd(5);
myDL.insertEnd(8);
myDL.insertEnd(5);
myDL.insertEnd(9);
myDL.print();
myDL.printReversed();
myDL.deleteEnd();
myDL.print();
}
15
Example:
Add deleteMinumum function
• Extend the previous double linked class by adding the following
function:
• void deleteMinumum(): delete the node that contains the minimum value

class doubleList {
private:
node* head, * tail;
public:
doubleList();
void insertBeg(int x);
void insertEnd(int x);
void deleteBeg();
void deleteEnd();
void print();
void printReversed();
void deleteMinumum();
};

16
void doubleList::deleteMinumum() {
if (head == NULL)
cout << "List is empty";
else if (head == tail) {
delete head;
head = tail = NULL;
}
else {
int min = head->info;
node* ptr = head;
node* minPtr = head;
while (ptr != NULL) {
if (ptr->info < min) {
min = ptr->info;
minPtr = ptr;
}
ptr = ptr->next;
}
if (minPtr == head){ // minimum is the first element
head = head->next;
head->previous = NULL;
}
else if (minPtr == tail){ // minimum is the last element
tail = tail->previous;
tail->next = NULL;
}
else { // minimum is in the middle
minPtr->previous->next = minPtr->next;
minPtr->next->previous = minPtr->previous;
}
delete ptr;
}
} 17
Exercise
• Write a function to merge two doubly linked lists,
• the function receives the head pointers of both lists
void merge (node* head1, node* head2)

18
Doubly-linked list vs Singly linked list
• A doubly-linked list allows traversing the list in either direction.

• Some Insertion and deletion operations are more efficient:


− Delete last node
− Insert a node before the last node

• A singly-linked list uses less memory than an equivalent doubly-


linked list.

19

You might also like