Skip to content

double linkedlist #171

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 110 additions & 0 deletions Structures/Doublelinkedlist.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
#pragma once
#include<iostream>
using namespace std;

template<typename T>
class Node
{
public: T data;
Node<T> *pNext;
Node<T> *pPrev;

Node() {
data = 0;
pNext = NULL;
pPrev = NULL;
}
~Node() {
data = 0;
pNext = NULL;
pPrev = NULL;
}
Node(T data) {
this->data = data;
pNext = NULL;
pPrev = NULL;
}
};

template <typename T>
class Doublelinkedlist
{
public:
Node<T> *pHead;
Node<T> *pTail;

Doublelinkedlist()
{
pHead = NULL;
pTail = NULL;
}
~Doublelinkedlist()
{
pHead = NULL;
pTail = NULL;
}

void Insert(T data)
{
Node<T> *newnode = new Node(data);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// set the newnode by dynamic allocation


if (pHead == NULL)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if pHead == NULL, set the newnode as a pHead and pTail

{
pHead = newnode;
pTail = newnode;
return;
}
else {
Node<T> *walker = pHead;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// if not, set the pHead as a walker

while (walker->pNext != NULL) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// walker's movement while walker->pNext != NULL

walker = walker->pNext;
}
walker->pNext = newnode;
newnode->pPrev = walker;
newnode = pTail;
return;
}
}

void Delete(T data)
{
if (pHead == NULL) return;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// if pHed == NULL, set the pHead as a walker

Node<T> *walker = pHead;
while (walker != NULL)
{
if (walker->data == data)
{
if (walker == pHead)
{
walker->pNext->pPrev = walker->pPrev;
pHead = walker->pNext;
delete walker;
return;
}
else if (walker == pTail) {
walker->pPrev->pNext = walker->pNext;
pTail = walker->pPrev;
delete walker;
}
else {
walker->pPrev->pNext = walker->pNext;
walker->pNext->pPrev = walker->pPrev;
delete walker;
}
}
walker = walker->pNext;
}
}

void Print()
{
if (pHead == NULL)return;
Node<T>* walker = pHead;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// set the pHead as a walker

while (walker != NULL) {
cout << walker->data << " ";
}
cout << endl;
}

};