0% found this document useful (0 votes)
12 views2 pages

C Code

The document contains a C++ implementation of a doubly linked list with basic operations such as inserting nodes at the front and end, removing nodes from the front and end, and printing the list. It defines a ListNode class for individual nodes and a LinkedList class for managing the list. The LinkedList class uses dummy head and tail nodes to simplify node insertion and removal operations.

Uploaded by

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

C Code

The document contains a C++ implementation of a doubly linked list with basic operations such as inserting nodes at the front and end, removing nodes from the front and end, and printing the list. It defines a ListNode class for individual nodes and a LinkedList class for managing the list. The LinkedList class uses dummy head and tail nodes to simplify node insertion and removal operations.

Uploaded by

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

#include <iostream>

using std::cout;
using std::endl;

class ListNode {
public:
int val_;
ListNode* next = nullptr;
ListNode* prev = nullptr;

ListNode(int val) {
val_ = val;
}
};

// Implementation for Doubly Linked List


class LinkedList {
public:
ListNode* head;
ListNode* tail;

LinkedList() {
// Init the list with a 'dummy' node which makes
// removing a node from the beginning of list easier.
head = new ListNode(-1);
tail = new ListNode(-1);
head->next = tail;
tail->prev = head;
}

void insertFront(int val) {


ListNode* newNode = new ListNode(val);
newNode->prev = head;
newNode->next = head->next;

head->next->prev = newNode;
head->next = newNode;
}

void insertEnd(int val) {


ListNode* newNode = new ListNode(val);
newNode->next = tail;
newNode->prev = tail->prev;

tail->prev->next = newNode;
tail->prev = newNode;
}

// Remove first node after dummy head (assume it exists)


void removeFront() {
head->next->next->prev = head;
head->next = head->next->next;
}

// Remove last node before dummy tail (assume it exists)


void removeEnd() {
tail->prev->prev->next = tail;
tail->prev = tail->prev->prev;
}

void print() {
ListNode* curr = head->next;
while (curr != tail) {
cout << curr->val_ << " -> ";
curr = curr->next;
}
cout << endl;
}
};

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy