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;
    }











  • 相关阅读:
    移动端picker插件
    腾讯云主机如何使用root账号登录,不能使用root登录怎么办
    windows安装composer总结
    ubuntu安装git
    workman
    权限设计
    app接口
    git提交流程简述
    mysql分区partition详解
    html元素拖拽
  • 原文地址:https://www.cnblogs.com/to7str/p/2726751.html
Copyright © 2011-2022 走看看