Linear table (chain storage)

Source: Internet
Author: User

Ordered tables can be stored in two ways: 1. Sequential storage, random reading (ordered table), 2. Random storage, and sequential reading (chained table)
[Cpp] view plaincopy
// Single-chain table implementation
# Include <iostream>
# Include <stdlib. h>
Using namespace std;
Typedef int ElemType;
Struct List
{
ElemType data;
List * next;
};
Void InitList (List * & HL) // initialize the linear table
{
HL = NULL;
}
Void ClearList (List * & HL) // deletes a linear table.
{
List * cp, * np;
Cp = HL;
While (cp)
{
Np = cp-> next;
Delete cp;
Cp = np;
}
HL = NULL;
}
Int LengthList (List * HL) // obtain the length of a single-chain table
{
Int I = 0;
While (HL)
{
I ++;
HL = HL-> next;
}
Return I;
}
Bool EmptyList (List * HL) // checks whether a single linked List is empty.
{
Return HL = NULL;
}
ElemType GetList (List * HL, int pos) // obtain the element in the pos node of the single-chain table.
{
If (pos <1)
{
Cout <"pos is invalid" <endl;
Exit (1 );
}
Int I = 0;
While (HL)
{
I ++;
If (I = pos) break;
HL = HL-> next;
}
If (HL)
Return HL-> data;
Else
{
Cout <"pos is out range" <endl;
Exit (1 );
}
}
Void TraverseList (List * HL) // traverses a single-chain table
{
While (HL)
{
Cout <HL-> data <"";
HL = HL-> next;
}
Cout <endl;
}
Bool FindList (List * HL, ElemType & item) // you can check whether the element exists.
{
While (HL)
{
If (HL-> data = item)
{
Item = HL-> data;
Return true;
}
Else HL = HL-> next;
}
Return false;
}
Bool UpdateList (List * HL, const ElemType & item) // update an element
{
While (HL)
{
If (HL-> data = item) break;
Else HL = HL-> next;
}
If (! HL) return false;
Else
{
HL-> data = item;
Return true;
}
}
Bool InsertList (List * & HL, ElemType item, int pos) // insert an element
{
If (pos <-1)
{
Cout <"pos is invalid" <endl;
Return false;
}
List * newpt;
Newpt = new List;
Newpt-> data = item;
List * cp = HL, * ap = NULL;
If (pos = 0)
{
While (cp)
{
If (item <cp-> data) break;
Else
{
Ap = cp;
Cp = cp-> next;
}
}
}
Else if (pos =-1)
While (cp)
{
Ap = cp;
Cp = cp-> next;
}
Else
{
Int I = 0;
While (cp)
{
I ++;
If (I = pos) break;
Else
{
Ap = cp;
Cp = cp-> next;
}
}
If (cp = NULL & I + 1 <pos)
{
Cout <"pos is rang out" <endl;
Return false;
}
}
If (ap = NULL)
{
Newpt-> next = HL;
HL = newpt;
}
Else
{
Newpt-> next = cp;
Ap-> next = newpt;
}
Return true;
}
Bool DeleteList (List * & HL, ElemType & item, int pos)
{
If (HL = NULL)
{
Cout <"List is Empty! "<Endl;
Return false;
}
If (pos <-1)
{
Cout <"pos is invalid" <endl;
Return false;
}
List * cp = HL, * ap = NULL;
If (pos = 0)
{
While (cp! = NULL)
{
If (item = cp-> data) break;
Else
{
Ap = cp;
Cp = cp-> next;
}
}
If (cp = NULL)
{
Cout <"List is not node to delete! "<Endl;
Return false;
}
}
Else if (pos =-1)
While (cp)
{
Ap = cp;
Cp = cp-> next;
}
Else
{
Int I = 0;
While (cp! = NULL)
{
I ++;
If (I = pos) break;
Else
{
Ap = cp;
Cp = cp-> next;
}
}
If (cp = NULL)
{
Cout <"pos value is invalid" <endl;
Return false;
}
}
If (ap = NULL) HL = HL-> next;
Else ap-> next = cp-> next;
Delete cp;
Return true;
}
Int main ()
{
Return 0;
}

Related Article

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.