#include <iostream>
#include <string.h>
#include "./singlelist/singlelist.h"
using namespace std;
typedef struct{
int idx;
char name[64];
}ELEMENT2;
int main(int argv, char** argc)
{
cout << "hello world" << endl;
cout << "SingleList_main01" << endl;
SingleList_main01();
cout << "SingleList_main02" << endl;
SingleList_main02();
cout << "SingleList_main03" << endl;
SingleList_main03();
cout << "SingleList_main04" << endl;
SingleList<ELEMENT2> sList2;
sList2.clear();
return 0;
}
#include <iostream>
#include <string.h>
#include "singlelist.h"
using namespace std;
template<typename T>
int SingleList<T>::insertHead(const T& ele)
{
Node<T> *p = nullptr;
p = new Node<T>;
memcpy(&p->ele, &ele, sizeof(T));
p->next = m_nodeHead;
if(nullptr == m_nodeHead)
{
m_nodeTail = p;
}
m_nodeHead = p;
cnt ++;
return 0;
}
template<typename T>
int SingleList<T>::getHead(T& ele)
{
if(nullptr == m_nodeHead)
{
return -1;
}
memcpy(&ele, m_nodeHead, sizeof(T));
return 0;
}
template<typename T>
int SingleList<T>::deleteHead(void)
{
Node<T> *p = nullptr;
p = m_nodeHead;
if(nullptr != p)
{
m_nodeHead = p->next;
if(nullptr == m_nodeHead)
{
m_nodeTail = nullptr;
}
delete p;
cnt --;
}
return 0;
}
template<typename T>
int SingleList<T>::insertTail(const T& ele)
{
Node<T> *p = nullptr;
p = new Node<T>;
memcpy(&p->ele, &ele, sizeof(T));
if(nullptr != m_nodeTail)
{
m_nodeTail->next = p;
m_nodeTail = p;
}
else
{
m_nodeTail = p;
m_nodeHead = p;
}
cnt ++;
}
template<typename T>
int SingleList<T>::getTail(T& ele)
{
if(nullptr == m_nodeTail)
{
return -1;
}
memcpy(&ele, m_nodeTail, sizeof(T));
return 0;
}
template<typename T>
int SingleList<T>::deleteTail(void)
{
Node<T> *p = nullptr;
if(m_nodeHead == m_nodeTail && nullptr != m_nodeTail)
{
delete m_nodeTail;
m_nodeTail = nullptr;
m_nodeHead = nullptr;
cnt --;
}
else if(nullptr != m_nodeTail)
{
p = m_nodeHead;
while(nullptr != p && m_nodeTail != p->next)
{
p = p->next;
}
p->next = nullptr;
delete m_nodeTail;
m_nodeTail = p;
cnt--;
}
return 0;
}
template<typename T>
int SingleList<T>::clear(void)
{
Node<T> *p = nullptr;
while(nullptr != m_nodeHead)
{
p = m_nodeHead;
m_nodeHead = p->next;
delete p;
cout << "delete: " << p << endl;
p = nullptr;
}
m_nodeTail = nullptr;
cout << "clear!" << endl;
return 0;
}
template<typename T>
void SingleList<T>::print(void)
{
Node<T> *p = m_nodeHead;
cout << m_nodeHead << " " << m_nodeTail << endl;
while(nullptr != p)
{
p = (Node<T> *)(p->next);
}
cout << endl;
}
int SingleList_main01()
{
SingleList<int> sList;
sList.insertHead(101);
sList.print();
sList.insertHead(102);
sList.print();
sList.insertHead(103);
sList.print();
sList.deleteHead();
sList.print();
sList.deleteHead();
sList.print();
sList.deleteHead();
sList.print();
sList.deleteHead();
sList.print();
return 0;
}
int SingleList_main02()
{
SingleList<int> sList;
sList.insertTail(201);
sList.print();
sList.insertTail(202);
sList.print();
sList.insertTail(203);
sList.print();
sList.deleteHead();
sList.print();
sList.insertHead(204);
sList.print();
sList.deleteHead();
sList.print();
sList.deleteHead();
sList.print();
sList.deleteHead();
sList.print();
sList.insertHead(205);
sList.print();
sList.insertHead(206);
sList.print();
sList.insertHead(207);
sList.print();
return 0;
}
int SingleList_main03()
{
SingleList<int> sList;
sList.insertTail(301);
sList.print();
sList.insertTail(302);
sList.print();
sList.insertTail(303);
sList.print();
sList.deleteTail();
sList.print();
sList.deleteTail();
sList.print();
sList.deleteTail();
sList.print();
sList.insertHead(304);
sList.print();
sList.insertHead(305);
sList.print();
sList.insertHead(306);
sList.print();
sList.clear();
sList.print();
}
int SingleList_main04()
{
}
#ifndef __SINGLELIST__H
#define __SINGLELIST__H
#include <iostream>
using namespace std;
#define nullptr NULL
template<typename T>
class Node{
public:
T ele;
Node *next;
Node(){next=nullptr;}
};
template<typename T>
class SingleList{
private:
Node<T> *m_nodeHead;
Node<T> *m_nodeTail;
private:
int cnt;
public:
SingleList(void)
{
m_nodeHead = nullptr;
m_nodeTail = nullptr;
cnt = 0;
cout << "["<< __FILE__ << "|" << __LINE__ << "]" << "constructor!" << endl;
}
~SingleList(void)
{
Node<T> *p = nullptr;
while(nullptr != m_nodeHead)
{
p = m_nodeHead;
m_nodeHead = p->next;
delete p;
cout << "delete: " << p << endl;
p = nullptr;
m_nodeTail = nullptr;
}
cout << "["<< __FILE__ << "|" << __LINE__ << "]" << "deconstructor!" << endl;
}
int insertHead(const T& ele);
int getHead(T& ele);
int deleteHead(void);
int insertTail(const T& ele);
int getTail(T& ele);
int deleteTail(void);
int clear(void);
void print(void);
};
int SingleList_main01();
int SingleList_main02();
int SingleList_main03();
int SingleList_main04();
#endif