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

来源:互联网 发布:sql参考手册 chm 编辑:程序博客网 时间:2024/06/05 13:27

http://www.cnblogs.com/-Lei/archive/2012/04/10/2440399.html

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

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

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;}
复制代码
原创粉丝点击