双向链表

实现了下双项链表,比较简单,代码在下面。。。

#include 

using namespace std;

#define ElementType int

typedef struct DouLinkNode{
    ElementType data;
    struct DouLinkNode* prior;
    struct DouLinkNode* next;
}Node,*pNode;

class DouLink{
public :
	bool InitLink(pNode *L);//Initial Node
	bool InsertNode(pNode *L,ElementType data,int num);//Insert Node To Link
	bool DeleteNode(pNode *L,int NumberDel);//Delete Node From Link
	int LengthOfLink(pNode *L);//Get Length of Link
	bool DestoryLink(pNode *L);//Clear Link
	void PrintLink(pNode L); //display Node of Link
	pNode Header; //Header of Link
};

void prinf();

int main()
{
	DouLink L;
	if(!L.InitLink(&(L.Header)))
        return false;
    int iChoice;
    while(1)
    {
        prinf();
        cin >> iChoice;
        switch(iChoice)
        {
            case 1:
            {

                int num;
                int data;
            //  cout <<"Input Number and Data Insert : " << endl;
              // cin >> num >> data;
                for(int i = 1; i < 10;++i)
                    L.InsertNode(&(L.Header),i,i-1);
                L.PrintLink(L.Header);
                cout << endl;
                break;
            }
            case 2:
            {
                int num;
                cout << "Inout Number To Delete " << endl;
                cin >> num;
                L.DeleteNode(&(L.Header),num);
                L.PrintLink(L.Header);
                cout << endl;
                break;
            }
            case 3:
                cout << "Node of The Link is " << L.LengthOfLink(&(L.Header)) << endl;
                break;
            case 4:
                L.DestoryLink(&(L.Header));
                L.PrintLink(L.Header);
                if(!L.InitLink(&(L.Header)))
                    return false;
                break;

            default :
                break;
        }
    }
    return 0;
}

bool DouLink::InitLink(pNode *L)
{
    *L = new Node[sizeof(Node)];
	if(!*L)
	{
		return false;
	}
	(*L)->data = 0;
	(*L)->prior = NULL;
    (*L)->next = NULL;
	return true;
}

bool DouLink::InsertNode(pNode *L,ElementType data,int num)
{
    if(num > (*L)->data)
    {
       cout << "Please Input Right Number" << endl;
       return false;
    }

    pNode pNew = NULL,pTail = NULL,pTemp = *L;

    if(!InitLink(&pNew))
        return false;
    pNew->data= data;

    for(int i = 0; i < num;++i)         //Find The Right Node
    {
        pTemp = pTemp->next;
    }
    if(!pTemp->next)                    //The lastest Node
    {
        pTemp->next = pNew;
        pNew->prior = pTemp;
    }
    else                                //Not The lastest Node
    {
        pTail = pTemp->next;
        pTemp->next = pNew;
        pNew->prior = pTemp;
        pNew->next = pTail;
        pTail->prior = pNew;
    }
    (*L)->data++;                        //sum of link ++
    return true;
}

bool DouLink::DeleteNode(pNode *L,int NumberDel)
{
    if(NumberDel > (*L)->data)
    {
       cout << "Number To Delete Not Exsist" << endl;
       return false;
    }

    pNode pTemp = *L;
    for(int i = 1; i < NumberDel;++i)
        pTemp = pTemp->next;

    pNode pDelNode = pTemp->next;
    if(!pDelNode->next)         //Delete The Lastest Node
    {
        pTemp->next = NULL;
        pDelNode->prior = NULL;
        delete pDelNode;
        pDelNode = NULL;
    }
    else                        //Not The lastest Node
    {
        pDelNode->prior->next = pDelNode->next;
        pDelNode->next->prior = pDelNode->prior;
        pDelNode->next = NULL;
        pDelNode->prior = NULL;
        delete pDelNode;
        pDelNode = NULL;
    }
    (*L)->data--;                  //sum of node --
    return true;

}

bool DouLink::DestoryLink(pNode *L)
{
    pNode pTemp = *L,pTail = *L;
    while(pTemp)
    {
        pTail = pTemp->next;
        pTemp->prior = NULL;
        pTemp->next = NULL;
        delete pTemp;
        pTemp = pTail;
    }
    (*L)->prior = NULL;
    (*L)->next = NULL;
    (*L)->data = 0;
}

void DouLink::PrintLink(pNode L)
{
    if(!L)
    {
        cout << "No Node" << endl;
        return ;
    }
    pNode pTemp = L->next;
    cout << L->data << " Node in The Link " << endl;
    for(int i = 1;i <= L->data;++i)
    {
            cout << "No." << i <<" Node  " << pTemp->data << endl;
            pTemp = pTemp->next;
    }
}

int DouLink::LengthOfLink(pNode *L)
{
        return (*L)->data;
}

void prinf()
{
        cout << "1:Insert Node"<< endl;
        cout << "2:delete Node" << endl;
        cout << "3:Length of Link" << endl;
        cout << "4:Clear Link" << endl;
}

标签:none