zoukankan      html  css  js  c++  java
  • 双向循环链表

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>

    typedef struct node
    {
    int data;
    struct node *next;
    struct node *pre;
    }Node;

    Node *pHead=NULL;

    void insert(Node *pNode)
    {
    if(NULL == pHead)
    {
    pHead = pNode;
    pHead->next = pNode;
    pHead->pre = pNode;
    return;
    }
    /*插头法*/
    pNode->next = pHead;
    pNode->pre = pHead->pre;
    pHead->pre->next = pNode;
    pHead->pre = pNode;
    pHead = pNode;
    }

    void del(int data)
    {
    Node *p;
    if(NULL == pHead)
    return;
    p = pHead;
    if(data == pHead->data)/*刚好为head*/
    {
    if(pHead == p->next)/*只有一个node*/
    {
    free(pHead);
    pHead = NULL;
    }
    else
    {
    pHead = p->next;
    p->next->pre = p->pre;
    p->pre->next = p->next;
    free(p);
    }
    return;
    }
    p = pHead->pre;
    do
    {
    if(data == p->data)
    {
    break;
    }
    p = p->pre;
    }
    while(pHead->pre != p);

    p->next->pre = p->pre;
    p->pre->next = p->next;
    free(p);

    return ;
    }

    void display(void )
    {
    Node *p;
    p = pHead->pre;
    do
    {
    printf("%d ", p->data);
    p = p->pre;
    }
    while(pHead->pre != p);

    printf("\n\r");
    }

    int main(void )
    {
    int i;
    Node *p;
    for(i=0; i<100; i++)
    {
    p = (Node *)malloc(sizeof(Node));
    p->data = i;
    insert(p);
    }
    display();
    for(i=30; i<90; i++)
    {
    del(i);
    }
    del(99);
    display();
    return 0;
    }











  • 相关阅读:
    C++ 的查漏补缺
    Model元数据解析
    Controller
    路由
    win8系统 Reflect 破解
    MVC运行原理
    源代码Log
    linq 分类
    EF 实体关系
    第二十六章 计算限制的异步操作
  • 原文地址:https://www.cnblogs.com/to7str/p/2726751.html
Copyright © 2011-2022 走看看