-
Notifications
You must be signed in to change notification settings - Fork 207
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
base: master
Are you sure you want to change the base?
double linkedlist #171
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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); | ||
|
||
if (pHead == NULL) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. // if not, set the pHead as a walker |
||
while (walker->pNext != NULL) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
|
||
}; | ||
|
There was a problem hiding this comment.
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