Single Linked list (template class)

Source: Internet
Author: User

#include <iostream>
#include <assert.h>
using namespace Std;

Template <class t>
struct Node
{
Node (const t& x)
: _data (x)
, _pnext (NULL)
{ }

Node<t> *_pnext;
T _data;
};
Template <class t>
Class SList
{
Public
SList ()
: _phead (NULL)
, _size (0)
{ }

SList (const slist<t>& L)
{
Node<t>*pnode = L._phead;

while (Pnode)
{
Pushback (pnode->_data);
Pnode = pnode->_pnext;
}
}

~slist ()
{
Clear ();
}

void Clear ()
{
Node<t>*pnode = _phead;

while (Pnode)
{
node<t>* Pdel = Pnode;
Pnode = pnode->_pnext;
Delete Pdel;
}
_phead = NULL;
_size = 0;
}

slist& operator = (const slist& Other)
{
if (This! = &other)
{
while (_size! = 0)
{
Popback ();
}

node<t> *pnode = Other._phead;
while (NULL! = pnode)
{
Pushback (pnode->_data);
Pnode = pnode->_pnext;
}
}

return *this;
}

//tail interpolation
void Pushback (const t& x)
{
Node<t>*pnode = _buynode (x);
Node<t>*cur = _phead;
if (Empty ())
{
_phead = Pnode;
}
Else
{
while (Cur->_pnext)
{
cur = cur->_pnext;
}
Cur->_pnext = Pnode;
}
_size++;
}
//END Delete
void Popback ()
{
if (Empty ())
{
return;
}
Else if (_size = = 1)
{
Delete _phead;
_phead = NULL;
_size = 0;
}
Else
{
Node<t>*pnode = _phead;
Node<t>*pprenode = NULL;
while (Pnode->_pnext)
{
Pprenode = Pnode;
Pnode = pnode->_pnext;
}
Pprenode->_pnext = NULL;
Delete Pnode;
_size--;
}

}
//header plug
void Pushfront (const t&x)
{
Node<t>*pnode = _buynode (x);
if (Empty ())
{
_phead = Pnode;
}
Else
{
Pnode->_pnext = _phead;
_phead = Pnode;
}
_size++;
}
//Header delete
void Popfront ()
{
if (Empty ())
{
return;
}
Else if (_size = = 1)
{
Delete _phead;
_phead = NULL;
_size = 0;
}
Else
{
Node<t>*pnewnode = _phead;
_phead = _phead->_pnext;
Delete Pnewnode;
Pnewnode = NULL;
_size--;
}
}
//display
void Print ()
{
Node<t>*cur = _phead;
while (cur)
{
cout << cur->_data << ";
Cur = cur->_pnext;
}
cout << "NULL";
}
//Find
node<t>* serach (const t&x)
{
if (Empty ())
{
return 0;
}
Node<t>*cur = _phead;
while (cur)
{
if (Cur->_data = = x)
{
return cur;
}
cur = cur->_pnext;
}
return NULL;
}
//delete
void Erase (Node<t>*pos)
{

if (Empty ())
{
Return
}
if (pos = = _phead)
{
_phead = pos->_pnext;
Delete pos;
}
Else
{
Node<t>*cur = _phead;
while (cur)
{
if (Cur->_pnext = = pos)
{
Cur->_pnext = pos->_pnext;
Delete pos;
}
Cur = cur->_pnext;
}
}
}

Sort (bubbling method)
void Bubblesort ()
{
node<t>* ptail = NULL;
Node<t>*pnode = NULL;
node<t>* Pprenode = NULL;

if (_phead = = NULL | | _phead->_pnext = = NULL)
{
Return
}
while (_phead! = ptail)
{
int exchange = 0;
Pprenode = _phead;
Pnode = _phead->_pnext;
while (pnode! = ptail)
{
if (Pprenode->_data > Pnode->_data)
{
T temp;
temp = pprenode->_data;
Pprenode->_data = pnode->_data;
Pnode->_data = temp;
Exchange = 1;
}
Pprenode = Pnode;
Pnode = pnode->_pnext;
}
if (Exchange = = 0)
{
Return
}
Ptail = Pprenode;
}
}

Reverse (front insert)
void Reverse ()
{
node<t>* Pprenode = NULL;
node<t>* Pnewnode = NULL;
node<t>* pnode = _phead;
if (_phead = = NULL | | (_phead)->_pnext = = NULL)
{
Return
}
while (Pnode)
{
Pprenode = Pnode;
Pnode = pnode->_pnext;
Pprenode->_pnext = Pnewnode;
Pnewnode = Pprenode;
}
_phead = Pnewnode;
}

BOOL Empty ()
{
return _size = = 0;
}

T & operator[] (size_t index)
{
if (index >= _size)
{
cout << "Index error" << Endl;
Abort ();
}
node<t>* pnode = _phead;
while (index--)
{
Pnode = pnode->_pnext;
}
Return pnode->_data;
}

Const T & operator[] (size_t index) const
{
if (index >= _size)
{
cout << "Index error" << Endl;
Abort ();
}
node* pnode = _phead;
while (index--)
{
Pnode = pnode->_pnext;
}
Return pnode->_data;
}

Friend ostream& operator << (ostream& _cout, const SLIST<T>&L)
{
node<t> *pnode = L._phead;

while (Pnode)
{
_cout << pnode->_data << "--";
Pnode = pnode->_pnext;
}
_cout << "NULL";
return _cout;
}

Node<t>*findmidnode ();

Private
node<t>* _buynode (const t& data)
{
return new node<t> (data);
}

Private
Node<t> *_phead;
size_t _size;
};
Finding the middle node of a single linked list
Template <typename t>
Node<t>*slist<t>::findmidnode ()
{
Node<t>*pfast = _phead;
Node<t>*pslow = _phead;
while (Pfast&&pfast->_pnext)
{
Pfast = pfast->_pnext->_pnext;
Pslow = pslow->_pnext;
}
return pslow;
}

void Main ()
{
Slist<int> s;

S.pushback (6);
S.pushback (3);
S.pushback (7);
cout << s[0] << Endl;
cout << s << endl;

slist<int> S1 (s);

cout << s1 << Endl;

S.print ();

}

Single Linked list (template class)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.