데이터 구조 연습 - 양 방향 링크

10654 단어 데이터 구조
데이터 구조 복습...다음 주 에 쓸 지 모 르 겠 어 요.
단지 매우 간단하게 썼 을 뿐, 포장 이 없다.
DoubleLinkList.h
#ifndef GUARD_DoubleLinkList_h

#define GUARD_DoubleLinkList_h



#include <stdio.h>



struct ListNode

{

    int data;

    ListNode *previous,*next;

};





ListNode* GetNewNode(int value);

void Insert(ListNode*& head,int value);

void Delete(ListNode*& head,int value);

void PrintList(const ListNode* head);

void ReversePrintList(ListNode* head);

void DestroyList(ListNode*& head);

ListNode* GetTailPtr(ListNode* head);





#endif

 
DoubleLinkList.cpp
#include "DoubleLinkList.h"



ListNode* GetNewNode(int value)

{

    ListNode* newNode=new ListNode;

    newNode->data=value;

    newNode->next=NULL;

    newNode->previous=NULL;

    return newNode;

}



//         value   

void Insert(ListNode*& head,int value)

{

    ListNode* currentPtr=head;

    ListNode* previousPtr=NULL;



    while(currentPtr!=NULL)

    {

        previousPtr=currentPtr;

        currentPtr=currentPtr->next;

    }



    if(previousPtr==NULL)

        head=GetNewNode(value);

    else

    {

        previousPtr->next=GetNewNode(value);

        previousPtr->next->previous=previousPtr;

    }

}



//        value   

void Delete(ListNode*& head,int value)

{

    ListNode* currentPtr=head;

    ListNode* previousPtr=NULL;



    while(currentPtr!=NULL)

    {

        //          

        if(currentPtr->data==value)

        {

            //         

            if(previousPtr==NULL)

            {

                ListNode* tempPtr=head;

                head=head->next;

                head->previous=NULL; //          

                delete tempPtr;

                previousPtr=NULL;

                currentPtr=head;

            }

            else

            {

                ListNode* tempPtr=currentPtr;

                currentPtr=currentPtr->next;  //  currentPtr      

                previousPtr->next=currentPtr;

                if(currentPtr!=NULL) 

                    currentPtr->previous=previousPtr;

                delete tempPtr;

            }

        }    

        else

        {

            previousPtr=currentPtr;

            currentPtr=currentPtr->next;

        }

    }

}



void PrintList(const ListNode* head)

{

    while(head!=NULL)

    {

        printf("%d ",head->data);

        head=head->next;

    }

    printf("
"); } void ReversePrintList(ListNode* head) { ListNode* tailPtr=GetTailPtr(head); while(tailPtr!=NULL) { printf("%d ",tailPtr->data); tailPtr=tailPtr->previous; } printf("
"); } void DestroyList(ListNode*& head) { ListNode* tempPtr=head; while(head!=NULL) { tempPtr=head; head=head->next; delete tempPtr; tempPtr=NULL; } } ListNode* GetTailPtr(ListNode* head) { ListNode* currentPtr=head; ListNode* previousPtr=NULL; while(currentPtr!=NULL) { previousPtr=currentPtr; currentPtr=currentPtr->next; } return previousPtr; }

 
main.cpp
#include "DoubleLinkList.h"



int main()

{

    int a[]={1,1,3,4,5,6,7,5,9,10};

    ListNode* head=NULL;

    for(int i=0;i<10;i++)

        Insert(head,a[i]);



    //    

    Delete(head,1);

    Delete(head,5);

    Delete(head,10);



    printf("Now print the DoubleLinkList:
"); PrintList(head); printf("Now print the reversal DoubleLinkList:
"); ReversePrintList(head); DestroyList(head); PrintList(head); system("PAUSE"); return 0; }

좋은 웹페이지 즐겨찾기