Double Linked List is a linked list that uses just a pointer variable to store a lot of data using the linked list method, a list of contents that are interconnected.
OK, Santekno will provide an implementation of Double Linked List using the C++ language. The most important thing in making a Double Linked List is that we will create a link that can later be connected to each other so that in order to be connected to each other we need several functions that can operate it, such as, insert front, insert back, insert in the middle, delete, size, and others.
The Linked List that Santekno will create applies the OOP (Object Oriented Programming) system so that it is easier to understand. In OOP we have to create a class which we will call DLL, or an abbreviation for Double Linked List.
Functions to be created
- Created
make()Function Node - Add Node behind
push_back() - Add Node in front of
push_front() - Add Nodes after n
push_after() - Search Node
find() - Searches for Nodes before n
find_before() - Delete Node n
del() - Prints the linked list
print() - Play Programs
SLL Class Program Code
1#include <iostream>
2#include <list>
3
4using namespace std;
5typedef list<int> LI;
6
7class DLL{
8 LI dt;
9public:
10 int isEmpty(){ return dt.empty();}
11 void push_back(int val);
12 void push_front(int val);
13 void push_after(int val,int after);
14 LI::iterator find(int val);
15 void del(int val);
16 void print();
17};
18
19void DLL::print(){
20 LI::iterator it;
21 for(it=dt.begin();it!=dt.end();++it)
22 cout << (*it) << "->";
23 cout << "NULL" << endl;
24}
25
26LI::iterator DLL::find(int val){
27 LI::iterator it;
28 for(it=dt.begin();it!=dt.end();++it)
29 if((*it) == val) return it;
30 return it;
31}
32void DLL::push_back(int val){
33 dt.push_back(val);
34}
35
36void DLL::push_front(int val){
37 dt.push_front(val);
38}
39
40void DLL::push_after(int val,int after){
41 LI::iterator it=find(after);
42 if(it!=dt.end()){
43 ++it;
44 dt.insert(it,val);
45 }
46}
47void DLL::del(int val){
48 LI::iterator it=find(val);
49 if(it!=dt.end()) dt.erase(it);
50}
51
52int main(){
53 DLL list;
54 list.push_back(10);
55 list.push_front(20);
56 list.push_front(30);
57 list.push_after(10,50);
58 list.del(20);
59 list.print();
60 return 0;
61}Output