面试系列之三:C艹在双链表中插入结点

来源:互联网 发布:什么是绿色版软件 编辑:程序博客网 时间:2024/04/26 14:53

在一个升序的双向链表里插入一个NewNode:

//假设条件Struct Node{    Node *prior;    Node *next;    int *data;}Node *PCurrent, *PrevPC, *PHead, *NewNode;// 指针PHead 指向表头第一个元素if ( PHead  == NULL )    {     return (0);    }PCurrent = PHead;while( PCurrent ){    if ( PCurrrent != PHead )        {            PrevPC = PCurrent->prior;        }    if ( PCurrent->data >= NewNode->data )        {            if ( PCurrent == PHead )            {                //在表头元素前插入新Node                PHead = NewNode;                NewNode->prior = null;                PCurrent->prior = NewNode;                NewNode->next  = PCurrent;            }            else            {                //不是在表头元素前插入新Node                if ( PCurrent->next == null )                {                    //在最末尾插入                    NewNode->next = null;                    PCurrent->next = NewNode;                    NewNode->prior = PCurrent;                }                else                {                    //在中间插入                    NewNode->prior = PrevPC;                    PrevPC->next = NewNode;                    NewNode->next = PCurrent;                    PCurrent->prior = NewNode;                }                return PHead;            }        }        else        {            PCurrent = PCurrent->next;        }    }
0 0