Free doubly linked list C

All Courses
Log in
Software Development
Data Science & Business AnalyticsAI & Machine LearningProject ManagementCyber SecurityCloud ComputingDevOpsBusiness and LeadershipQuality ManagementSoftware DevelopmentAgile and ScrumIT Service and ArchitectureDigital MarketingBig DataCareer Fast-trackEnterpriseOther Segments
Video TutorialsArticlesEbooksOn-demand WebinarsFree Practice Tests
HomeResourcesSoftware DevelopmentData Structure TutorialDoubly Linked List: A Complete Implementation Guide

Tutorial Playlist

Data Structure Tutorial

Overview

Arrays in Data Structures: A Guide With Examples

Lesson - 1

All You Need to Know About Two-Dimensional Arrays

Lesson - 2

All You Need to Know About a Linked List in a Data Structure

Lesson - 3

The Complete Guide to Implement a Singly Linked List

Lesson - 4

The Ultimate Guide to Implement a Doubly Linked List

Lesson - 5

The Fundamentals for Understanding Circular Linked List

Lesson - 6

The Ultimate Guide To Understand The Differences Between Stack And Queue

Lesson - 7

Implementing Stacks in Data Structures

Lesson - 8

Your One-Stop Solution for Stack Implementation Using Array

Lesson - 9

Your One-Stop Solution for Queue Implementation Using Array

Lesson - 10

Your One-Stop Solution to Learn Depth-First Search[DFS] Algorithm From Scratch

Lesson - 11

Your One-Stop Solution for Stack Implementation Using Linked-List

Lesson - 12

The Definitive Guide to Understand Stack vs Heap Memory Allocation

Lesson - 13

All You Need to Know About Linear Search Algorithm

Lesson - 14

All You Need to Know About Breadth-First Search Algorithm

Lesson - 15

A One-Stop Solution for Using Binary Search Trees in Data Structure

Lesson - 16

The Best Tutorial to Understand Trees in Data Structure

Lesson - 17

A Complete Guide to Implement Binary Tree in Data Structure

Lesson - 18

A Holistic Look at Using AVL Trees in Data Structures

Lesson - 19

All You Need to Know About Tree Traversal in Data Structure

Lesson - 20

The Best Guide Youll Ever Need to Understand B-Tree in Data Structure

Lesson - 21

The Best Guide You'll Ever Need to Understand Spanning Tree in Data Structure

Lesson - 22

The Best and Easiest Way to Understand an Algorithm

Lesson - 23

Your One-Stop Solution to Understand Shell Sort Algorithm

Lesson - 24

Your One-Stop Solution to Quick Sort Algorithm

Lesson - 25

The Most Useful Guide to Learn Selection Sort Algorithm

Lesson - 26

Everything You Need to Know About Radix Sort Algorithm

Lesson - 27

Everything You Need to Know About the Counting Sort Algorithm

Lesson - 28

Everything You Need to Know About the Merge Sort Algorithm

Lesson - 29

Insertion Sort Algorithm: One-Stop Solution That Will Help You Understand Insertion Sort

Lesson - 30

Everything You Need to Know About the Bubble Sort Algorithm

Lesson - 31

The Best Guide Youll Ever Need to Understand Bucket Sort Algorithm

Lesson - 32

Your One-Stop Solution to Understand Recursive Algorithm in Programming

Lesson - 33

The Definitive Guide to Understanding Greedy Algorithm

Lesson - 34

Your One-Stop Solution to Understand Backtracking Algorithm

Lesson - 35

The Fundamentals of the Bellman-Ford Algorithm

Lesson - 36

Your One-Stop Solution for Graphs in Data Structures

Lesson - 37

The Best Guide to Understand and Implement Solutions for Tower of Hanoi Puzzle

Lesson - 38

A Simplified and Complete Guide to Learn Space and Time Complexity

Lesson - 39

All You Need to Know About the Knapsack Problem : Your Complete Guide

Lesson - 40

The Fibonacci Series: Mathematical and Programming Interpretation

Lesson - 41

The Holistic Look at Longest Common Subsequence Problem

Lesson - 42

The Best Article to Understand What Is Dynamic Programming

Lesson - 43

A Guide to Implement Longest Increasing Subsequence Using Dynamic Programming

Lesson - 44

A Holistic Guide to Learn Stop Solution Using Dynamic Programming

Lesson - 45

One Stop Solution to All the Dynamic Programming Problems

Lesson - 46

Understanding the Fundamentals of Binomial Distribution

Lesson - 47

Heres All You Need to Know About Minimum Spanning Tree in Data Structures

Lesson - 48

Understanding the Difference Between Array and Linked List

Lesson - 49

The Best Article Out There to Understand the B+ Tree in Data Structure

Lesson - 50

A Comprehensive Look at Queue in Data Structure

Lesson - 51

Your One-Stop Solution to Understand Coin Change Problem

Lesson - 52

The Best Way to Understand the Matrix Chain Multiplication Problem

Lesson - 53

Your One-Stop Solution to Learn Floyd-Warshall Algorithm for Using Dynamic Programming

Lesson - 54

Doubly Linked List: A Complete Implementation Guide

Lesson 5 of 54By Simplilearn

Last updated on Sep 18, 20212866
PreviousNext
  • Tutorial Playlist

    Data Structure Tutorial

    Overview

    Arrays in Data Structures: A Guide With Examples

    Lesson - 1

    All You Need to Know About Two-Dimensional Arrays

    Lesson - 2

    All You Need to Know About a Linked List in a Data Structure

    Lesson - 3

    The Complete Guide to Implement a Singly Linked List

    Lesson - 4

    The Ultimate Guide to Implement a Doubly Linked List

    Lesson - 5

    The Fundamentals for Understanding Circular Linked List

    Lesson - 6

    The Ultimate Guide To Understand The Differences Between Stack And Queue

    Lesson - 7

    Implementing Stacks in Data Structures

    Lesson - 8

    Your One-Stop Solution for Stack Implementation Using Array

    Lesson - 9

    Your One-Stop Solution for Queue Implementation Using Array

    Lesson - 10

    Your One-Stop Solution to Learn Depth-First Search[DFS] Algorithm From Scratch

    Lesson - 11

    Your One-Stop Solution for Stack Implementation Using Linked-List

    Lesson - 12

    The Definitive Guide to Understand Stack vs Heap Memory Allocation

    Lesson - 13

    All You Need to Know About Linear Search Algorithm

    Lesson - 14

    All You Need to Know About Breadth-First Search Algorithm

    Lesson - 15

    A One-Stop Solution for Using Binary Search Trees in Data Structure

    Lesson - 16

    The Best Tutorial to Understand Trees in Data Structure

    Lesson - 17

    A Complete Guide to Implement Binary Tree in Data Structure

    Lesson - 18

    A Holistic Look at Using AVL Trees in Data Structures

    Lesson - 19

    All You Need to Know About Tree Traversal in Data Structure

    Lesson - 20

    The Best Guide Youll Ever Need to Understand B-Tree in Data Structure

    Lesson - 21

    The Best Guide You'll Ever Need to Understand Spanning Tree in Data Structure

    Lesson - 22

    The Best and Easiest Way to Understand an Algorithm

    Lesson - 23

    Your One-Stop Solution to Understand Shell Sort Algorithm

    Lesson - 24

    Your One-Stop Solution to Quick Sort Algorithm

    Lesson - 25

    The Most Useful Guide to Learn Selection Sort Algorithm

    Lesson - 26

    Everything You Need to Know About Radix Sort Algorithm

    Lesson - 27

    Everything You Need to Know About the Counting Sort Algorithm

    Lesson - 28

    Everything You Need to Know About the Merge Sort Algorithm

    Lesson - 29

    Insertion Sort Algorithm: One-Stop Solution That Will Help You Understand Insertion Sort

    Lesson - 30

    Everything You Need to Know About the Bubble Sort Algorithm

    Lesson - 31

    The Best Guide Youll Ever Need to Understand Bucket Sort Algorithm

    Lesson - 32

    Your One-Stop Solution to Understand Recursive Algorithm in Programming

    Lesson - 33

    The Definitive Guide to Understanding Greedy Algorithm

    Lesson - 34

    Your One-Stop Solution to Understand Backtracking Algorithm

    Lesson - 35

    The Fundamentals of the Bellman-Ford Algorithm

    Lesson - 36

    Your One-Stop Solution for Graphs in Data Structures

    Lesson - 37

    The Best Guide to Understand and Implement Solutions for Tower of Hanoi Puzzle

    Lesson - 38

    A Simplified and Complete Guide to Learn Space and Time Complexity

    Lesson - 39

    All You Need to Know About the Knapsack Problem : Your Complete Guide

    Lesson - 40

    The Fibonacci Series: Mathematical and Programming Interpretation

    Lesson - 41

    The Holistic Look at Longest Common Subsequence Problem

    Lesson - 42

    The Best Article to Understand What Is Dynamic Programming

    Lesson - 43

    A Guide to Implement Longest Increasing Subsequence Using Dynamic Programming

    Lesson - 44

    A Holistic Guide to Learn Stop Solution Using Dynamic Programming

    Lesson - 45

    One Stop Solution to All the Dynamic Programming Problems

    Lesson - 46

    Understanding the Fundamentals of Binomial Distribution

    Lesson - 47

    Heres All You Need to Know About Minimum Spanning Tree in Data Structures

    Lesson - 48

    Understanding the Difference Between Array and Linked List

    Lesson - 49

    The Best Article Out There to Understand the B+ Tree in Data Structure

    Lesson - 50

    A Comprehensive Look at Queue in Data Structure

    Lesson - 51

    Your One-Stop Solution to Understand Coin Change Problem

    Lesson - 52

    The Best Way to Understand the Matrix Chain Multiplication Problem

    Lesson - 53

    Your One-Stop Solution to Learn Floyd-Warshall Algorithm for Using Dynamic Programming

    Lesson - 54

Table of Contents

View More

A Doubly linked list is used in navigation systems or to represent a classic deck of cards. A Doubly linked list is a bidirectional linked list; i.e., you can traverse it from head to tail node or tail to head node. Unlike singly-linked lists, its node has an extra pointer that points at the last node.

How Do You Implement a Doubly Linked List?

You create nodes of doubly-linked lists using classes or structures. These nodes are then linked with each other using the next and the previous pointer.

Code:

//A c++ program to implement linked list

#include

using namespace std;

/* A class to create node */

class Node

{

public:

int data;

Node *next;

Node *prev;

};

//A function to insert at the

//beginning of the list

void push[Node** head, int newdata]

{

//create new node

Node* newnode = new Node[];

/* put in the data */

newnode->data = newdata;

/* As we are adding at the beginning,

prev is always NULL */

newnode->prev = NULL;

/* link new node's next to head */

newnode->next = [*head];

/* change prev of head node to newnode */

if[[*head] != NULL]

[*head]->prev = newnode ;

/* changing head node */

[*head] = newnode;

}

/* A c++ program to print the list */

void printlist[Node *head]

{

while[head != NULL]

{

cout data next;

}

}

int main[]

{

/* We will start with an empty list */

Node* head = NULL;

/*lets create a linked list: 2->3->5->7 */

push[&head, 7];

push[&head, 5];

push[&head, 3];

push[&head, 2];

cout prev = NULL;

/* link the next of newnode to the head */

newnode->next = [*head];

/* change prev of head node to newnode */

if[[*head] != NULL]

[*head]->prev = newnode ;

/* changing head */

[*head] = newnode;

}

/* A c++ program to traverse the linked list */

void traverse[Node *node]

{

while[node != NULL]

{

cout data next;

}

}

/* Function to reverse traverse a Linked List */

void revtraverse[Node **head]

{

Node* tail = *head;

// Traversing till tail of the linked list

while [tail->next != NULL] {

tail = tail->next;

}

// Traversing linked list from tail

// and printing the node->data

while [tail != *head] {

cout data prev;

}

cout data 3->5->7 */

push[&head, 7];

push[&head, 5];

push[&head, 3];

push[&head, 2];

cout prev = newnode;

/* changing head */

[*head] = newnode;

}

/* A function to insert a node after a given node */

void insertAfter[Node* prevnode, int newdata]

{

/*1. check if the given prevnode is NULL */

if [prevnode == NULL]

{

coutnext = prevnode->next;

/* 5. Make the prevnode's next as newnode */

prevnode->next = newnode;

/* 6. Make prevnode as newnode's prev */

newnode->prev = prevnode;

/* 7. Change previous of newnode's next node */

if [newnode->next != NULL]

newnode->next->prev = newnode;

}

/* A function to insert at the end of the list */

void append[Node** head, int newdata]

{

/* create newnode */

Node* newnode = new Node[];

Node* last = *head;

/* put in the data */

newnode->data = newdata;

/*This newnode is going to be the last node, so

we will make next of it as NULL*/

newnode->next = NULL;

/* check if the Linked List is empty, then make the new

node as head */

if [*head == NULL]

{

newnode->prev = NULL;

*head = newnode;

return;

}

/* Else traverse till the last node */

while [last->next != NULL]

last = last->next;

/* Change the next of last node */

last->next = newnode;

/* Make last node as new node's prev */

newnode->prev = last;

return;

}

// A function to print the list

void printList[Node* node]

{

while [node != NULL]

{

cout7->6->NULL

// Insert 4 at the end

append[&head, 4]; //1->7->6->4->NULL

// Insert 8, after 7

insertAfter[head->next, 8]; //1->7->8->6->4->NULL

cout next;

/* Change next only if node to be

deleted is NOT the last node */

if [del->next != NULL]

del->next->prev = del->prev;

/* Change prev only if node to be

deleted is NOT the first node */

if [del->prev != NULL]

del->prev->next = del->next;

/* Finally, free the memory occupied by del*/

free[del];

return;

}

/* A function to insert a node at the beginning of the list*/

void push[Node** head, int newdata]

{

/* create newnode */

Node* newnode = new Node[];

/* put in the data */

newnode->data = newdata;

/* link the new node's next to head

and previous as NULL */

newnode->next = [*head];

newnode->prev = NULL;

/* link the head node's prev to new node */

if [[*head] != NULL]

[*head]->prev = newnode;

/* changing head */

[*head] = newnode;

}

/* Function to print nodes in a given linked list

This function is the same as printList[] of singly linked list */

void printList[Node* node]

{

while [node != NULL]

{

cout data next;

}

}

int main[]

{

/* Start with the empty list */

Node* head = NULL;

/* Let us create the linked list 2357 */

push[&head, 7];

push[&head, 5];

push[&head, 3];

push[&head, 2];

cout next]; /*delete middle node*/

deleteNode[&head, head->next]; /*delete last node*/

/* Modified linked list will be NULLNULL */

cout

Chủ Đề