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

    双链表:
    1 2 3 4 5 6 7 8 9 10 11 12

    1 3 5 7 9 11 12 10 8 6 4 2
    1。设计节点
    typedef int datatyped
    typeddef struct node
    {
    typedata data;
    struct node * next;
    struct node *prev;

    }listnode ,*linklist;


    2.初始化空双向循环链表
    linklist init_list()
    {
    linklist l=malloc(sizeof(listnode))
    if(l!=NULL)
    {

    l->next=l;
    l->prev=l;
    }
    return l;

    }

    3.插入
    bool insert_prev(linklist new,linklist L)
    {
    //新节点前后关系
    new->n=L
    new->p=L-p;


    //新节点前一节点的后面关系
    L->p->n=new;
    //新节点后一节点的前面关系
    L->p=new;

    return true;

    }

    4.显示
    show()
    {

    }
    remove_node(linklist pl)
    {
    pl->p->n=pl->n;
    pl->n->p=pl->p;
    pl->p=pl->n=NULL;


    }
    move_p(linklist pl,linklist L)
    {
    remove_node(pl);
    insert_prev(pl,L);

    }

    5.
    ran(linklist L)
    {
    linklist pl=L->p,q;

    int i=0;
    while(pl!=L)
    {
    if((pl->data)%2==0 && i==1)
    {
    //删除和插入操作
    move_p(pl,L);
    pl=q;
    }
    else
    q=pl;

    pl=pl->p;

    i=1;
    }


    }


    int main()
    {
    linlist L=init_list();

    for(i=1;i<=n;i++)
    {
    linklist new =malloc();
    new->data=i;

    insert_prev(new,L);

    }


    ran(L)

    show(L)

    }

    #include<stdio.h>
    #include<stdlib.h>
    #include<stdbool.h>
    typedef int datatype;
    typedef struct node
    {
    datatype data;
    struct node *next;
    struct node *prve;
    }listnode,*linklist;

    linklist init_list()
    {
    linklist L = malloc(sizeof(listnode));
    if(L!=NULL)
    {
    L->next = L;
    L->prve = L;
    }
    return L;
    }

    bool insert_prve(linklist new,linklist L)//在L的前驱插入节点
    {
    new->next = L;
    new->prve = L->prve;
    L->prve->next = new;
    L->prve = new;
    return true;

    }
    /* while(q->prve ! = q)
    {

    }
    */
    bool cir_insert(linklist L)//显示奇偶显示1 3 5 7 9 10 8 6 4 2
    {
    linklist p=L->prve,q;
    // s = L->next;
    while(p != L)//注意循环条件
    {

    if(p->data%2==0)
    { q=p->prve;//记录p的前驱,以便p指针向前移动
    p->prve->next = p->next;
    p->next->prve = p->prve;
    p->prve = L->prve;
    L->prve->next = p;
    p->next = L;
    L->prve = p;
    p=q;//之前记录的指针。

    }
    p=p->prve;

    }
    return true;
    }


    void show(linklist L)//打印循环链表数据
    { linklist p =L;
    while(p->next!=L)
    { p = p->next;
    printf("%d ",p->data);
    }
    printf(" ");

    }
    int main()
    {
    linklist L;
    L = init_list();
    int i;
    for(i=1;i<=10;i++)
    {
    linklist new = malloc(sizeof(listnode));
    new->data = i;
    insert_prve(new,L);
    }
    cir_insert(L);
    show(L);
    return 0;

    }

  • 相关阅读:
    Java面试题:栈和队列的实现
    Java面试题:如何对HashMap按键值排序
    经典的Java基础面试题集锦
    9个Java初始化和回收的面试题
    20个高级Java面试题汇总
    Spring、Spring MVC、MyBatis整合文件配置详解2
    Spring、Spring MVC、MyBatis整合文件配置详解
    Spring:基于注解的Spring MVC
    margin百分比的相对值--宽度!
    jquery.cxSelect插件,城市没单位
  • 原文地址:https://www.cnblogs.com/defen/p/5203314.html
Copyright © 2011-2022 走看看