• step3 . day3 数据结构之线性表 单项循环链表和双向循环链表


    1. 使用单项循环链表解决约瑟夫问题:

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


    typedef struct looplist{
    int date;
    struct looplist *next;
    }looplist;

    //创建循环链表
    looplist * looplist_creat(){
    looplist *head = NULL;
    head = (looplist*)malloc(sizeof(looplist));

    head->date = 1;
    head->next = head;
    return head;
    }

    //插入节点
    void looplist_insert(looplist* head,int value){

    looplist *temp = NULL;
    temp = (looplist*)malloc(sizeof(looplist));
    temp->date = value;

    temp->next = head->next;
    head->next = temp;
    }

    //遍历1
    void looplist_show(looplist *head){
    looplist* p = NULL;
    p = head;

    do{
    printf("%d ",p->date);
    p = p->next;
    }while(p != head);
    puts("");
    }

    void looplist_del(looplist* head,looplist* headpre,looplist* headtemp){

    printf("%d ",headtemp->date);

    looplist *p =NULL;
    p = headtemp;
    headpre->next = headtemp->next;
    printf(" ");
    free(p);
    p=NULL;

    }
    looplist * joseph_creat(int person){

    int i;
    looplist * joseph = NULL;
    joseph = looplist_creat();
    for(i = person;i >= 2;i--){
    looplist_insert(joseph,i);
    }
    return joseph;
    }
    void joseph(int person,int start,int num){

    looplist * head = NULL;
    looplist * headpre = NULL;
    head = joseph_creat(person);
    looplist_show(head);

    looplist* headtemp = head;
    while(headtemp->next != head){
    headtemp = headtemp->next;
    }
    headpre = headtemp;

    headtemp = head;

    while(headtemp->date != start){
    headpre = headtemp;
    headtemp = headtemp->next;
    }//未判断start在链表内,死循环风险
    while(headtemp!=headtemp->next){

    int counttemp = 1;
    while(counttemp != num){
    headpre = headtemp;
    headtemp = headtemp->next;
    counttemp++;
    }

    looplist_del(head,headpre,headtemp);
    headtemp = headtemp->next;
    }

    printf("%d ",headtemp->date);
    free(headtemp);
    headtemp = NULL;
    head = NULL;
    }
    int main(int argc, const char *argv[])
    {
    joseph(8,3,4);//8人,从第3个人开始数,数4个数出列
    return 0;
    }

    2. 双向循环链表

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

    typedef struct node{
    int date;
    struct node * priv;
    struct node * next;
    }doublelist;

    doublelist * doublelist_creat(){
    doublelist * head = NULL;
    head = (doublelist*)malloc(sizeof(doublelist));

    head->priv = head;
    head->next = head;

    return head;
    }

    void doublelist_head_insert(doublelist * head,int value){
    doublelist * temp = doublelist_creat();
    temp->date = value;

    temp->next = head->next;
    head->next->priv = temp;

    temp->priv = head;
    head->next = temp;
    }

    void doublelist_show(doublelist * head){

    doublelist * temp = head->next;
    while(temp != head){
    printf("%d ",temp->date);
    temp = temp->next;
    }
    printf(" ");
    }

    int doublelist_is_empty(doublelist * head){
    return head->next == head ? 1 : 0;
    }

    int doublelist_head_del(doublelist * head){
    if(doublelist_is_empty(head)){
    printf("doublelist is empty ");
    return -1;
    }
    int value = head->next->date;

    doublelist * temp = head->next;
    head->next = temp->next;
    temp->next->priv = head;

    free(temp);
    temp = NULL;
    return value;
    }


    int main(int argc, const char *argv[])
    {
    doublelist * head = NULL;
    head = doublelist_creat();

    doublelist_head_insert(head,1);
    doublelist_head_insert(head,2);
    doublelist_head_insert(head,3);
    doublelist_head_insert(head,4);
    doublelist_head_insert(head,5);

    doublelist_show(head);

    printf("%d ",doublelist_head_del(head));
    doublelist_show(head);

    printf("%d ",doublelist_head_del(head));
    doublelist_show(head);

    printf("%d ",doublelist_head_del(head));
    doublelist_show(head);

    printf("%d ",doublelist_head_del(head));
    doublelist_show(head);

    printf("%d ",doublelist_head_del(head));
    doublelist_show(head);
    printf("%d ",doublelist_head_del(head));
    doublelist_show(head);
    return 0;
    }

  • 相关阅读:
    PyQt(Python+Qt)学习随笔:使用pyqtConfigure建立信号和槽的连接
    PyQt(Python+Qt)学习随笔:调用disconnect进行信号连接断开时的信号签名与断开参数的匹配要求
    PyQt(Python+Qt)学习随笔:什么是信号绑定(Unbound and Bound Signals)?
    PyQt(Python+Qt)学习随笔:信号签名(signature of the signal)是什么?
    第六章、信号和槽进阶--自定义信号及其他信号、槽的高级特性
    第15.19节 PyQt(Python+Qt)入门学习:自定义信号与槽连接
    第五章、信号和槽的实战应用--一个计算器的实现
    第四章 、PyQt中的信号(signal)和槽(slot)机制以及Designer中的使用
    第三章 、使用Qt Designer进行GUI设计
    织梦通过 phpmyadmin 导出的数据,再次导入的时候报错
  • 原文地址:https://www.cnblogs.com/huiji12321/p/11233878.html
Copyright © 2020-2023  润新知