DoubleLinkList.h
#pragma once
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cstring>
#define DATASIZE 64
class Node {
public:
char m_data1[DATASIZE];
char m_data2[DATASIZE];
Node* next;
Node* prev;
Node(const char* data1, const char* data2) {
strcpy(this->m_data1, data1);
strcpy(this->m_data2, data2);
}
};
class LinkList {
private:
int m_len;
Node* m_head;
Node* m_tail;
public:
LinkList();
~LinkList();
void push_back(const char *data1, const char *data2);
void push_front(const char *data1, const char *data2);
void push_insert(const char *data1, const char *data2, int index);
void pop_back();
void pop_front();
void pop_insert(int index);
void change(const char *data1, const char *data2, int index);
void show();
private:
Node *get(int index);
};
DoubleLinkList.cpp
#include "Doublelinklist.h"
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
LinkList::LinkList() : m_len(0), m_head(NULL), m_tail(NULL) {
}
LinkList::~LinkList() {
static unsigned int count = 0;
Node* np = m_head;
Node* p;
while (np) {
count++;
p = np;
np = p->next;
delete p;
m_len--;
}
cout << "delete count num : " << count << endl;
}
void LinkList::push_back(const char * data1, const char * data2) {
if (sizeof(data1) > DATASIZE || sizeof(data2) > DATASIZE) {
cout << "error, The string you entered is too long" << endl;
return;
}
Node* np = new Node(data1, data2);
if (NULL == np) {
cout << "error, You are running out of memory space" << endl;
return;
}
if (0 == m_len) {
m_head = np;
}
else {
m_tail->next = np;
np->prev = m_tail;
}
m_tail = np;
m_len++;
}
void LinkList::push_front(const char * data1, const char * data2) {
if (sizeof(data1) > DATASIZE || sizeof(data2) > DATASIZE) {
cout << "error, The string you entered is too long" << endl;
return;
}
Node* np = new Node(data1, data2);
if (NULL == np) {
cout << "error, You are running out of memory space" << endl;
return;
}
if (0 == m_len) {
m_tail = np;
}
else {
m_head->prev = np;
np->next = m_head;
}
m_head = np;
m_len++;
}
void LinkList::push_insert(const char * data1, const char * data2, int index) {
if (sizeof(data1) > DATASIZE || sizeof(data2) > DATASIZE) {
cout << "error, The string you entered is too long" << endl;
return;
}
Node* np = new Node(data1, data2);
if (NULL == np) {
cout << "error, You are running out of memory space" << endl;
return;
}
if (index == 0) {
push_front(data1, data2);
}
else if (index == m_len) {
push_back(data1, data2);
}
else {
Node* np = new Node(data1, data2);
if (np == NULL) {
cout << "error, You are running out of memory space" << endl;
return;
}
Node *next = get(index - 1);
Node *prev = next->prev;
prev->next = np;
np->prev = prev;
np->next = next;
next->prev = np;
m_len++;
}
}
void LinkList::pop_back() {
if (m_len < 0) {
cout << "error, This linked list is empty" << endl;
return;
}
Node* tp = get(m_len - 1);
m_tail = m_tail->prev;
m_tail->next = NULL;
delete tp;
m_len--;
}
void LinkList::pop_front() {
if (m_len < 0) {
cout << "error, This linked list is empty" << endl;
return;
}
Node* tp = get(0);
m_head = m_head->next;
m_head->prev = NULL;
delete tp;
m_len--;
}
void LinkList::pop_insert(int index) {
if (index < 0 || index > m_len) {
cout << "error, You entered the wrong ordinal number" << endl;
return;
}
if (0 == index) {
pop_front();
}
else if (m_len == index) {
pop_back();
}
else {
Node* tp = get(index - 1);
Node* prev = tp->prev;
Node* next = tp->next;
prev->next = next;
next->prev = prev;
delete tp;
m_len--;
}
}
void LinkList::change(const char * data1, const char * data2, int index) {
if (sizeof(data1) > DATASIZE || sizeof(data2) > DATASIZE) {
cout << "error, The string you entered is too long" << endl;
return;
}
if (index < 0 || index > m_len) {
cout << "error, You entered the wrong ordinal number" << endl;
return;
}
Node* tp = get(index - 1);
memset(tp->m_data1, 0, DATASIZE);
memset(tp->m_data2, 0, DATASIZE);
strcpy(tp->m_data1, data1);
strcpy(tp->m_data2, data2);
}
void LinkList::show() {
printf("this linklist length = %d\n", this->m_len);
Node* np = m_head;
for (int i = 1; i <= m_len; i++) {
printf("%s \t", np->m_data1);
printf("%s \n", np->m_data2);
np = np->next;
}
printf("\n");
}
Node * LinkList::get(int index) {
if (index < 0 || index > m_len) {
printf("ERROR: index idex is out bounds \n");
exit(0);
}
Node* np = m_head;
while (index--) {
np = np->next;
}
return np;
}
main.cpp
#include <iostream>
#include "Doublelinklist.h"
using namespace std;
int main() {
LinkList A;
A.push_back("cacd", "dha");
A.push_back("abcas", "cdnac");
A.push_back("cachdac", "11516ca");
A.push_back("c67qfgcqe", "chr92hc");
A.show();
A.pop_back();
A.pop_back();
A.pop_front();
A.show();
A.push_front("zhang", "cnah");
A.push_front("hui", "cnah");
A.push_insert("hi", "hello", 2);
A.show();
A.change("jjj", "hhh", 2);
A.show();
A.pop_insert(2);
A.show();
system("pause");
return 0;
}
运行结果