Stay Hungry , Stay Foolish

君子生非异也,善假于物也

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

#include <stdio.h>

typedef struct _Node
{
   int value;
   _Node *next;
}Node;

void AddNodeTail(Node *&head, int value)
{
    Node *newNode = new Node;
    newNode->value = value;
    newNode->next = NULL;
    if(head == NULL)
    {
        head = newNode;
    }
    else
    {
        Node *p = head;
        while(p->next != NULL)
        {
            p = p->next;
        }
        p->next = newNode;
    }
}

bool FindNode(const Node *head, int value)
{
    Node *p = const_cast<Node*>(head);
    bool found = false;
    while(p != NULL)
    {
        if(p->value == value)
        {
            found = true;
            break;
        }
        p = p->next;
    }
    return found;
}

void DeleteNode(Node *&head, int value)
{
    if(head == NULL)
    {
        return;
    }

    Node *delNode = NULL;
    if(head->value == value)
    {
        delNode = head;
        head = head->next;
    }
    else
    {
        Node *pNode = head;
        while(pNode->next != NULL && pNode->next->value != value)
        {
            pNode = pNode->next;
        }
        if(pNode->next != NULL && pNode->next->value == value)
        {
            delNode = pNode->next;
            pNode->next = pNode->next->next;
        }
    }

    if(delNode)
    {
        delete delNode;
    }
}

void DeleteAllNode(Node *&head, int value)
{
    while(head != NULL && head->value == value)
    {
        Node *delNode = head;
        head = head->next;
        delete delNode;
    }
    Node *pNode = head;
    while(pNode->next != NULL)
    {  
        if(pNode->next->value == value)
        {
            Node *delNode = pNode->next;
            pNode->next = pNode->next->next;
            delete delNode;
        }
        else
        {
            pNode = pNode->next;
        }
    }
}

void PrintList(Node *head)
{
    while(head != NULL)
    {
        printf("%d  ", head->value);
        head = head->next;
    }
    printf("\n");
}

void ReverseList(Node *&head)
{
    //空或者仅包含一个节点
    if(head == NULL || head->next == NULL)
    {
        return;
    }
    Node *preNode = head;
    Node *curNode = head->next;
    preNode->next = NULL;
    while(curNode != NULL)
    {
        Node *nextNode = curNode->next;
        curNode->next = preNode;
        preNode = curNode;
        curNode = nextNode;
    }
    head = preNode;
}

int main()
{
    PrintList(NULL);
    Node *head = NULL;
    AddNodeTail(head, 10);
    AddNodeTail(head, 20);
    AddNodeTail(head, 15);
    AddNodeTail(head, 40);
    AddNodeTail(head, 15);
    PrintList(head);
   
    printf("found %d in list %s\n", 20, FindNode(head, 20) ? "true" : "false");
    printf("found %d in list %s\n", 30, FindNode(head, 30) ? "true" : "false");

    DeleteNode(head, 10);
    PrintList(head);
    DeleteAllNode(head, 15);
    PrintList(head);
    DeleteNode(head, 15);
    PrintList(head);

    Node *nullNode = NULL;
    DeleteNode(nullNode, 0);

    Node *sameHead = NULL;
    AddNodeTail(sameHead, 10);
    AddNodeTail(sameHead, 10);
    AddNodeTail(sameHead, 15);
    AddNodeTail(sameHead, 15);
    AddNodeTail(sameHead, 10);
    AddNodeTail(sameHead, 15);
    AddNodeTail(sameHead, 10);
    PrintList(sameHead);
    DeleteAllNode(sameHead, 10);
    PrintList(sameHead);

    Node *oneNode = NULL;
    AddNodeTail(oneNode, 1);
    ReverseList(oneNode);
    PrintList(oneNode);

    Node *twoNode = NULL;
    AddNodeTail(twoNode, 1);
    AddNodeTail(twoNode, 2);
    ReverseList(twoNode);
    PrintList(twoNode);

    Node *fiveNode = NULL;
    AddNodeTail(fiveNode, 1);
    AddNodeTail(fiveNode, 2);
    AddNodeTail(fiveNode, 3);
    AddNodeTail(fiveNode, 4);
    AddNodeTail(fiveNode, 5);
    ReverseList(fiveNode);
    PrintList(fiveNode);
    return 0;
}

posted on 2017-05-19 16:29  octocat  阅读(154)  评论(0编辑  收藏  举报