A Practical linked list

Source: Internet
Author: User
Tags define null

Even this linked list class has been used for a long time, and many unreasonable things have been ruled out. Some places do not want to use STL stuff. I feel that I can control and modify something I write, it can also run well, and if you have time, you can practice writing standard C ++ Code, especially if you want to practice templates, encapsulate abstractions, and so on. If you are lucky enough to be used by someone, you may feel very honored. If you find anything unreasonable, please leave a message for me.

/*************************************** ***************************
* File name: QDList. h
* File Description: two-way linked list
* Gysoft
**************************************** **************************/
Using namespace std;
# Define NULL 0

# Ifndef QDLIST_H
# Define QDLIST_H
Template <class T>
Class QDList;

/* --- Bidirectional linked list node class ---*/
Template <class T>
Class QDListNode
{
Friend QDList <T>;
Private:
T data;
QDListNode <T> * left, * right;
};

/* --- Two-way linked list ---*/

Template <class T>
Class QDList
{
Public:
QDList ();
~ QDList ();
Void RemoveAll ();
Void Append (const T & newData );
BOOL IsEmpty (ostream & out );
Bool RemoveAt (int nPos );
Int GetLength () const;
Int Search (const T & x, int & nPos );
Void Insert (int nPos, const T & x );
BOOL GetItem (int nPos, T & x );
// Void SortItem ();
Private:
QDListNode <T> * LeftEnd;
QDListNode <T> * RightEnd;
Int m_nLength;
};

/* --- Two-way linked list class: constructor ---*/
Template <class T>
QDList <T>: QDList ()
{
Leftend = rightend = NULL;
M_nlength = 0;
}

/* --- Two-way linked list class: destructor ---*/
Template <class T>
Qdlist <t> ::~ Qdlist ()
{
Removeall ();
}

/* --- Two-way linked list: Clear the linked list ---*/
Template <class T>
Void qdlist <t >:: removeall ()
{
Qdlistnode <t> * curr = leftend;
Qdlistnode <t> * next;

While (curr! = Rightend)
{
Next = curr-> right;
Delete curr;
Curr = next;
}

If (rightend) delete rightend;

Leftend = rightend = NULL;
M_nlength = 0;
};

/* --- Two-way linked list: Add a node at the end of the linked list ---*/
Template <class T>
Void qdlist <t >:: append (const T & newdata)
{
Qdlistnode <t> * newnode = new qdlistnode <t>;
Newnode-> DATA = newdata;
Newnode-> right = NULL;

If (leftend)
{
Newnode-> left = rightend;
Rightend-> right = newnode;
Rightend = newnode;
}
Else
{
Leftend = rightend = newnode;
Newnode-> left = newnode-> right = NULL;
Leftend = newnode;
}

M_nlength ++;
}

/* --- Two-way linked list: whether the output linked list is empty ---*/
Template <class T>
Bool qdlist <t >:: isempty (ostream & out)
{
If (! LeftEnd | m_nLength = 0)
{
Out <"Empty List! ";
Return TRUE;
}
Else
Return FALSE;

 
}

/* --- Two-way linked list: delete a linked list node ---*/
Template <class T>
Bool QDList <T>: RemoveAt (int nPos)
{
If (m_nLength = 0)
{
LeftEnd = RightEnd = NULL;
Return false;
}

QDListNode <T> * curr;
QDListNode <T> * next;

If (nPos <= 0)
{
Curr = LeftEnd;
Next = curr-> right;
Delete curr;
Leftend = next;
}
Else if (NPOs> = m_nlength)
{
Curr = rightend;
Next = curr-> left;
Delete curr;
Rightend = next;
}
Else
{
Curr = leftend;
While (NPOs)
{
Curr = curr-> right;
NPOs --;
}

Next = curr-> right;
Curr-> left-> right = next;
Next-> left = curr-> left;

Delete curr;
}
 
M_nlength --;
Return true;
};

/* --- Two-way linked list: Get the length of the linked list ---*/
Template <class T>
Int qdlist <t >:: getlength () const
{
Return m_nlength;
};

/* --- Two-way linked list: Find an element in the linked list ---*/
Template <class T>
Int qdlist <t>: Search (const T & X, Int & NPOs)
{
Qdlistnode <t> * curr = leftend;
NPOs =-1;
 
While (curr)
{
NPOs ++;
If (curr-> DATA = X)
{
Return NPOs;
}
Curr = curr-> right;
}
 
NPOs =-1;
Return NPOs;
};
/* --- Two-way linked list: Get the value of the specified position ---*/
Template <class T>
BOOL QDList <T>: GetItem (int nPos, T & x)
{
If (nPos <0 | nPos> m_nLength)
Return FALSE;
QDListNode <T> * curr = LeftEnd;
While (nPos> 1)
{
NPos --;
Curr = curr-> right;
}
X = curr-> data;
Return TRUE;

};
 

/* --- Two-way linked list: insert x --- */into nPos ---*/
Template <class T>
Void QDList <T>: Insert (int nPos, const T & x)
{
If (m_nLength = 0)
{
Append (x );
Return;
}

QDListNode <T> * newNode = new QDListNode <T>;
NewNode-> data = x;

If (nPos <= 0)
{
NewNode-> right = LeftEnd;
NewNode-> left = newNode;
LeftEnd = newNode;
}
Else if (nPos> = m_nLength)
{
NewNode-> left = RightEnd;
RightEnd-> right = newNode;
RightEnd = newNode;
}
Else
{
QDListNode <T> * curr = LeftEnd;

While (nPos> 1)
{
NPos --;
Curr = curr-> right;
}

QDListNode <T> * next = curr-> right;
NewNode-> left = curr;
Curr-> right = newNode;
NewNode-> right = next;
Next-> left = newNode;
}

M_nLength ++;
};

# Endif

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.