데이터 구조 연습 - 양 방향 링크
                                            
 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;
}  이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
정수 반전Udemy 에서 공부 한 것을 중얼거린다 Chapter3【Integer Reversal】 (예) 문자열로 숫자를 반전 (toString, split, reverse, join) 인수의 수치 (n)가 0보다 위 또는 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.