数据结构练习——双向链表

复习一下数据结构。。。。说不准下个星期就用上了

只不过写的很简单,没有封装

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("\n");
}

void ReversePrintList(ListNode* head)
{
    ListNode* tailPtr=GetTailPtr(head);
    while(tailPtr!=NULL)
    {
        printf("%d ",tailPtr->data);
        tailPtr=tailPtr->previous;
    }
    printf("\n");
}

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:\n");
    PrintList(head);
    
    printf("Now  print the reversal DoubleLinkList:\n");
    ReversePrintList(head);

    DestroyList(head);
    PrintList(head);
    system("PAUSE");
    return 0;
}

 

转载于:https://www.cnblogs.com/-Lei/archive/2012/04/10/2440399.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值