一、单链表
#include<iostream>
using namespace std;
struct ListNode
{
int val;
ListNode* next;
ListNode(int x) : val(x), next(NULL) {}
};
class LinkedList
{
private:
ListNode* head;
public:
LinkedList(){
head = NULL;
}
~LinkedList(){
ListNode *cur = head;
while(cur){
ListNode* nodeToDelete = cur;
cur = cur->next;
delete nodeToDelete;
}
}
void addAtHead(int val){
ListNode* oldHead = head;
head = new ListNode(val);
head->next = oldHead;
}
void addAtTail(int val){
ListNode* newNode = new ListNode(val);
ListNode* p = head;
if(p){
while(p->next){
p = p->next;
}
p->next = newNode;
}else{
p = newNode;
}
}
void insertNode(int pos, int val){
ListNode* newNode = new ListNode(val);
ListNode* p = head;
if(pos == 0){
cout << "头插" << endl;
addAtHead(val);
}else{
while(p->next && pos > 1){
pos --;
p = p->next;
}
if(p->next){
cout << "中插" << endl;
newNode->next = p->next;
p->next = newNode;
}else{
cout << "尾插" << endl;
addAtTail(val);
}
}
}
void deleteNode(int index){
if(index == 0){
removeAtHead();
return;
}
ListNode* node = head;
ListNode* del;
while(index-- > 1 && node->next){
node = node->next;
}
del = node->next;
if(node){
node->next = del->next;
}
}
void removeAtHead(){
if(!head) return;
ListNode* newHead = head->next;
delete head;
head = newHead;
}
void removeAtTail(){
if(!head) return;
ListNode* p = head;
while(p->next->next){
p = p->next;
};
p->next = NULL;
}
void reverse(){
ListNode* prev = NULL;
ListNode* curr = head;
while(curr != NULL){
ListNode* temp = curr->next;
curr->next = prev;
prev = curr;
curr = temp;
}
head = prev;
}
int reverseNode(int pos){
if(!head || pos == 0){
return -1;
}
ListNode* slow = head;
ListNode* fast = head;
for(int i=0; i<pos-1; i++){
if(fast->next){
fast = fast->next;
}else{
return -1;
}
}
while(fast->next){
fast = fast->next;
slow = slow->next;
}
return slow->val;
}
void print(){
ListNode* cur = head;
while(cur){
cout << cur->val << " ";
cur = cur->next;
}
cout << endl;
}
};
int main()
{
LinkedList list;
cout << "添加头节点" << endl;
list.addAtHead(3);
list.addAtHead(5);
list.addAtHead(10);
list.addAtHead(12);
list.print();
cout << "添加尾节点" << endl;
list.addAtTail(4);
list.addAtTail(6);
list.print();
cout << "删除头节点" << endl;
list.removeAtHead();
list.print();
cout << "删除尾节点" << endl;
list.removeAtTail();
list.print();
cout << "删除第一个节点" << endl;
list.deleteNode(1);
list.print();
cout << "在第二个位置插入一个节点2" << endl;
list.insertNode(1,2);
list.print();
cout << "反转链表" << endl;
list.reverse();
list.print();
cout << "获得链表倒数第三个元素的值" << list.reverseNode(3) << endl;
return 0;
}
二、双链表
#include<iostream>
using namespace std;
struct ListNode
{
int val;
ListNode *next;
ListNode *last;
ListNode(){}
ListNode(int val, ListNode *last, ListNode *next)
{
this->val = val;
this->next = next;
this->last = last;
}
};
class LinkedList
{
private:
ListNode* head;
int count;
public:
LinkedList():count(0){
cout << "构造双链表" << endl;
head = new ListNode();
head->last = head->next = head;
}
~LinkedList(){
cout << "析构双链表" << endl;
ListNode *cur = head;
while(cur){
ListNode* nodeToDelete = cur;
cur = cur->next;
delete nodeToDelete;
}
}
void addAtHead(int val){
ListNode* node = new ListNode(val,head,head->next);
head->next->last = node;
head->next = node;
count ++;
}
void addAtTail(int val){
ListNode* node = new ListNode(val,head->last,head);
head->last->next = node;
head->last = node;
count ++;
}
void insertNode(int pos, int val){
if(pos == 0){
addAtHead(val);
return;
}
ListNode* indexNode = getNode(pos);
ListNode* node = new ListNode(val,indexNode->last,indexNode);
indexNode->last->next = node;
indexNode->last = node;
count ++;
}
void deleteNode(int index){
if(!head) return;
ListNode* indexNode = getNode(index);
indexNode->next->last = indexNode->last;
indexNode->last->next = indexNode->next;
delete indexNode;
count --;
}
void removeAtHead(){
deleteNode(0);
}
void removeAtTail(){
deleteNode(count-1);
}
ListNode* getNode(int index){
if(index < 0 || index >= count){
return NULL;
}
if(index <= count/2){
int i = 0;
ListNode* indexNode = head->next;
while(i++ < index){
indexNode = indexNode->next;
}
return indexNode;
}
int j=0;
int rindex = count - index - 1;
ListNode* indexNode = head->last;
while(j++ < rindex){
indexNode = indexNode->last;
}
return indexNode;
}
void print(){
int i = count;
ListNode* cur = head;
while(i--){
cur = cur->next;
cout << cur->val << " ";
}
cout << endl;
}
};
int main()
{
LinkedList list;
cout << "添加头节点" << endl;
list.addAtHead(3);
list.addAtHead(5);
list.addAtHead(10);
list.addAtHead(12);
list.print();
cout << "添加尾节点" << endl;
list.addAtTail(4);
list.addAtTail(6);
list.print();
cout << "删除头节点" << endl;
list.removeAtHead();
list.print();
cout << "删除尾节点" << endl;
list.removeAtTail();
list.print();
cout << "删除第一个节点" << endl;
list.deleteNode(1);
list.print();
cout << "在第二个位置插入一个节点2" << endl;
list.insertNode(1,2);
list.print();
return 0;
}