0% found this document useful (0 votes)
26 views4 pages

Lab4 LinkedList Doubly Circular

Here are the key steps to reverse a doubly linked list without allocating new nodes: 1. Initialize three pointers - current, prev and next to the head node 2. Iterate through the list using current pointer - Swap current->prev and current->next - Update prev and current pointers to traverse in the reverse order 3. Once the iteration is done, update the head and tail pointers So the pseudo code would be: void reverse() { Node* current = head; Node* prev = NULL; Node* next; while(current != NULL) { next = current->next; current->next = current->prev; current->prev = next; if

Uploaded by

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

Lab4 LinkedList Doubly Circular

Here are the key steps to reverse a doubly linked list without allocating new nodes: 1. Initialize three pointers - current, prev and next to the head node 2. Iterate through the list using current pointer - Swap current->prev and current->next - Update prev and current pointers to traverse in the reverse order 3. Once the iteration is done, update the head and tail pointers So the pseudo code would be: void reverse() { Node* current = head; Node* prev = NULL; Node* next; while(current != NULL) { next = current->next; current->next = current->prev; current->prev = next; if

Uploaded by

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

Cairo University

Faculty of Computer Science and Artificial Intelligence


Data Structures, 2nd Term, 2023

Lab/Section (5): Double and Circular L


Double and Circular Linked List

Section/Lab Objective
This lab is designed to give an overview about double and circular linked list supported with
practical examples.

Types of Linked Lists:

 Singly linked list (Already taught in the previous section): Singly linked lists contain nodes which
have a data field as well as a next field, which points to the next node in line of
nodes.

 Doubly linked list: In a doubly linked list, each node contains, besides the next the previous node in the
sequence. The two links may be called forward(s) and backwards, or next and prev(previous).

 Circular list: In the last node of a list, the link field often contains a null reference, a special value used to
indicate the lack of further nodes. A less common convention is to make it point to the first node of the list; in
that case the list is said 'circular' or 'circularly linked'; otherwise it is said to be 'open' or 'linear'.

Linked List
In the case of a circular doubly linked list, the only change that occurs is that the end, or "tail", of the said list is lin
to the front, or "head", of the list and vice versa.
Cairo University
Faculty of Computer Science and Artificial Intelligence
Data Structures, 2nd Term, 2023

Doubly Linked List Implementation:


We implement the doubly linked list via a class called Node and another class called DoublyLinkedList.

The Node Class


Inside this class we define a variable and 2 pointers:
- The data variable: that holds the value to be stored and and it is going to be generic so that it can hold data of
any data type.
- The next pointer: which is of type Node* that will hold the address of the next node.
- The prev pointer: which is of type Node* that will hold the address of the previous node.

struct Node {

int value;

Node *next;

Node *previous;

Node(){

value = 0;

next = nullptr;

previous = nullptr;

};

The DoublyLinkedList (DLL) class


Inside this class we define two variables:
- head that holds the address of the first Node, it would be Null if the list is empty.
Cairo University
Faculty of Computer Science and Artificial Intelligence
Data Structures, 2nd Term, 2023

- tail that holds the address of the last Node, it would be Null if the list is empty.

class DoublyLinkedList {

private:

int size;

Node *head;

Node *tail;

public:

DoublyLinkedList(){

head = nullptr;

tail = nullptr;

size = 0;

Task 1:
Implement member function remove for your double linked list class. Remove takes an
element, search for it, if found it will remove it from the list.
Cairo University
Faculty of Computer Science and Artificial Intelligence
Data Structures, 2nd Term, 2023

bool DLL<T>::remove(T element);

Note that it should consider special cases like:


 Item at the beginning of the list
 Item at the end of the list
 List contains only one element

Task 2:
Given a null-terminated doubly linked list, reverse the order of its nodes. Do not allocate any new
nodes in the list.

Hint: you will use two pointers, one to iterate and one for swapping

void DLL<T>::reverse();

You might also like