0603作业

/*                                                      
 * function:    直接插入排序
 * @param [ in] 
 * @param [out] 
 * @return      
 */
void insert_sort_linklist(linklist* head,datatype data){
    linklist* p=head;
    while(NULL!=p->pnext && p->pnext->param < data){
        p=p->pnext;
    }

    linklist* temp=create_linklist(data);

    temp->pnext = p->pnext;
    p->pnext = temp;
    head->param++;
}
 /*
  * function:    倒序链表
  * @param [ in] 
  * @param [out] 
  * @return      
  */
void linklist_desc(linklist* head){

    linklist* p=head->pnext;
    linklist* q=NULL;

    while(next!=NULL){
        q=p->pnext;
        p->pnext=head->pnext;
        head->pnext=p;
        p=pnext;
    }

}
/*
 * function:    快慢指针
 * @param [ in]         
 * @param [out] 
 * @return      
 */
void fast_slow_point(linklist* head){

    if(head==NULL||head->pnext==NULL){
        return;
    }

    linklist* fast,slow;
    fast=slow=head->pnext;

    while(fast!=NULL||fast->pnext!=NULL){
        fast=fast->pnext->pnext;
        slow=slow->pnext;
    }
    printf("中间值:%d\n",(int)slow->param);

}

#include "./t2_impl.h"


//创建单向循环链表 头结点
lk* create_head_loop_linklist(datatype data){
    lk* head=create_node_loop_linklist();

    head->data=data;
    head->next=head;
    return head;
}
//创建节点
lk* create_node_loop_linklist(){
    lk* node=(lk*)malloc(sizeof(lk));
    if(NULL==node){
        puts("节点创建失败");
    }
    return node;
}

//头插
void insert_head_loop_linklist(lk* head,datatype data){
    lk* node=create_node_loop_linklist();

    node->next=head->next;
    head->next=node;
    node->data=data;
    head->data++;
    puts("头插成功");
}
//尾插
void insert_last_loop_linklist(lk* head,datatype data){
    lk* node=create_node_loop_linklist();
    lk* temp=head;
    while(temp->next!=head){
        temp=temp->next;
    }
    node->next=temp->next;
    node->data=data;
    temp->next=node;

    head->data++;
    puts("尾插成功");
}
//遍历
void print_linklist(lk* head){
    lk* temp=head;
    puts("---------遍历----------");
    while(temp->next!=head){
        temp=temp->next;
        printf("%d \n",temp->data);
    }
    puts("-----------------------");
}
//头删
void delete_head_loop_linklist(lk* head){
    lk* temp=head->next->next;
    free(head->next);
    head->next=temp;
    head->data--;
    puts("头删完成");
}
//尾删
void delete_last_loop_linklist(lk* head){
    lk* temp=head->next;
    while(temp->next->next != head){
        temp=temp->next;
    }
    free(temp->next);
    temp->next=head;
    head->data--;
    puts("尾删完成");
}

部分代码及其结果

乔瑟夫问题:

node* joseph_createNode_linklist(int n){
        node* head=NULL;
        node* temp=NULL;
        for(int i=1; i<=n; i++){
                node* newNode = (node*)malloc(sizeof(node));
                newNode->data = i;
                newNode->next = NULL;

                if(head == NULL){
                        head = newNode;
                        temp = head;
                } else {
                        temp->next = newNode;
                        temp = newNode;
                }
        }
        temp->next = head;
        return head;
}
void printList(node* head) {
    node* temp = head;
    do {
        printf("%d ", temp->data);
        temp = temp->next;
    } while (temp != head);
    printf("\n");
}

void joseph_linklist(node* head, int n, int k, int m){
    node* current = head;
    node* prev = NULL;
    int count = 0;

    for(int i=0; i<k-1; i++) {
        current = current->next;
    }

    while(current->next != current) {
        count++;
        if(count == m) {
            printf("Out: %d\n", current->data);
            prev->next = current->next;
            count = 0;
            current = prev->next;
        } else {
            prev = current;
            current = current->next;
        }
    }

    printf("Last: %d\n", current->data);
}

相关推荐

  1. 作业..........

    2024-06-06 10:58:03       55 阅读
  2. <span style='color:red;'>0703</span>_ARM7

    0703_ARM7

    2024-06-06 10:58:03      26 阅读
  3. 003 CSS介绍

    2024-06-06 10:58:03       32 阅读
  4. 003 传参

    2024-06-06 10:58:03       32 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-06 10:58:03       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-06 10:58:03       106 阅读
  3. 在Django里面运行非项目文件

    2024-06-06 10:58:03       87 阅读
  4. Python语言-面向对象

    2024-06-06 10:58:03       96 阅读

热门阅读

  1. 爬山算法的详细介绍

    2024-06-06 10:58:03       34 阅读
  2. Android12.0 SIM卡语言自适应

    2024-06-06 10:58:03       20 阅读
  3. web 预览显示本地图片、音频

    2024-06-06 10:58:03       34 阅读
  4. 简单几步,用Python实现VPN搭建

    2024-06-06 10:58:03       39 阅读
  5. 数据仓库分层模型

    2024-06-06 10:58:03       32 阅读
  6. Hyperf 框架常见面试题

    2024-06-06 10:58:03       32 阅读
  7. 虚拟环境导致的段错误

    2024-06-06 10:58:03       32 阅读
  8. RGMII接口--->(010)FPGA实现RGMII接口(十)

    2024-06-06 10:58:03       27 阅读