1222双向循环链表1.0

本文详细介绍了一个双向循环链表的实现过程,包括创建、添加(头部、尾部、中间位置)、释放和显示等基本操作。通过具体的C语言代码示例,展示了如何管理链表节点,进行插入、删除及遍历等核心功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这个链表是双向循环链表,程序实现功能:创建,添加(头,尾,中间),释放,显示。

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define N 10
#define Mal_OK 1
#define Mal_ERR 0

struct Dou_node
{
    int number;
    int element;
    struct Dou_node *piror;
    struct Dou_node *next;
};

typedef struct Dou_node DouNode;
typedef struct Dou_node* Link;

int is_malloc(Link new_node)
{
    if(NULL == new_node)
    {
        printf("malloc err _1\n");
        return Mal_ERR;
    }
    else
    {
        return Mal_OK;
    }
}

void creat_link(Link *head)
{
    do
    {
        *head = (Link)malloc(sizeof (DouNode));
    }while(Mal_ERR == is_malloc(*head));
    (*head) -> piror = (*head) -> next = (*head); 
}

void creat_node(Link *new_node , int num , int ele)
{
    do
    {
        *new_node = (Link)malloc(sizeof (DouNode));
    }while(Mal_ERR == is_malloc(*new_node));
    (*new_node) -> number = num;
    (*new_node) -> element = ele;
}

void insert_node_tail(Link head , Link new_node)
{
    Link p = head -> piror;   
    if(head == head -> next && head == head -> piror)
    {
        printf("The link is empty_2\n"); 
    }
    p -> next = new_node;
    head -> piror = new_node;
    new_node -> next = head;
    new_node -> piror = p;
}

void insert_node_head(Link head , Link new_node)
{
    Link p = head -> next;   
    if(head == head -> next && head == head -> piror)
    {
        printf("The link is empty_3\n");
    }
    head -> next = new_node;
    p -> piror = new_node;
    new_node -> next = p;
    new_node -> piror = head;
}

void display_link(Link head)
{
    Link p = head -> next;
    if(head == p)
    {
        printf("The link is empty\n");
    }
    while(head != p)
    {
        printf("NO. %d\n" , p -> number);
        printf("The element is %d\n" , p -> element);
        p = p -> next;
    }
    printf("Display_over\n");
    return ; 
}

void del_node(Link head)
{
    int loc ;
    int ele ;
    int choice;
    int choice_rw;
    Link p = head -> next;
    if(head == p)
    {
        printf("The link is empty");
        return ; 
    }
    printf("*******************\n\n");
    printf("   1: del by No.   \n");
    printf(" 2: del by element \n");
    printf("     3: Exit        \n\n");
    printf("*********************\n");
loop:   choice = 0;   
    scanf("%d" , &choice);
    if(1 == choice)
    {
loop_1:        printf("Plz input your No. \n");
        scanf("%d" , &loc);
        while(loc != p -> number)
        {
            p = p -> next;
        }
        if(head == p -> next)
        {
            printf("Not found _4");
loop_11:            printf("If you want to input again , plz inputr 1;\n");
            printf("If you dont want to input again , plz inputr 2;\n");
            if(1 == choice_rw)
            {
                choice_rw = 0;
                goto loop_1;
            }
            else if(2 == choice_rw)
            {
                printf("Exit\n");
                return ;
            }
            else
            {
                choice_rw = 0;
                printf("Wrong input_5");
                goto loop_11;
            }
        }
        (p -> piror) -> next = p -> next;
        (p -> next) -> piror = p -> piror;
        free(p);
        printf("Delele success_6\n");
        return ;
    }

    else if(2 == choice)
    {
loop_0:        printf("Plz input your element: \n");
        scanf("%d" , &ele); 
        while(ele != p -> element)
        {
            p = p -> next;
        }
        if(head == p -> next)
        {
            printf("Not found _4");
loop_01:            printf("If you want to input again , plz inputr 1;\n");
            printf("If you dont want to input again , plz inputr 2;\n");
            if(1 == choice_rw)
            {
                choice_rw = 0;
                goto loop_0;
            }
            else if(2 == choice_rw)
            {
                printf("Exit\n");
                return ;
            }
            else
            {
                choice_rw = 0;
                printf("Wrong input_5");
                goto loop_01;
            }
        }
        (p -> piror) -> next = p -> next;
        (p -> next) -> piror = p -> piror;
        free(p);
        printf("Delele success_6\n");
        return ;
    }
    else if(3 == choice)
    {
        printf("Exit_5\n");
        return;
    }
    else 
    {
        choice = 0;
        printf("Wrong choice ,plz input again_5\n\n");
        goto loop;
    }

}

void make_empty(Link head)
{
   // printf("*****make_empty_head\n");
    Link k = NULL;
    k = head -> next ;
    printf("*****make_empty\n");
    do
    {
       // printf("*****make_while_head\n");
        head -> next = k -> next;
        (k -> next) -> piror = head;
        free(k);
        k = k -> next;
       //printf("****while\n");
    }while(head -> next != head);
    printf("Relese ok\n");
    return ;
}

void insert_node(Link head)
{
    Link new_node = NULL;
    int num;
    int ele;
    int choice;
    Link p = head -> next;
    printf("Plz input where you want to insert: \n");
    scanf("%d", &num);
    printf("Plz input what you want to insert: \n");
    scanf("%d", &ele);
loop_20:    printf("Plz input which kind you want to insert(1:front 2:behind 3:exit): \n");
    scanf("%d", &choice);
    printf("\n");
    creat_node(&new_node , num , ele);
    while(num != p -> number)
    {
        p = p -> next;
    }
    if(p -> next == head)
    {
        printf("Not found\n");
        return;
    }
    if(1 == choice)
    {
        new_node -> next = p;
        new_node -> piror = p -> piror;
        (p -> piror) -> next = new_node;
        p -> piror = new_node;
        printf("Insert complete_1\n");
    }
    else if(2 == choice)
    {
        new_node -> piror = p;
        new_node -> next = p -> next;
        (p -> next) -> piror = new_node;
        p -> next = new_node;
        printf("Insert complete_2;\n");
    }
    else if(3 == choice)
    {
        printf("Exit_3\n");
        return;
    }
    else
    {
        choice = 0;
        printf("Input error\n");
        goto loop_20;
    }
    return;

}

int main()
{
    srand(time(NULL));
    Link head = NULL;
    Link new_node = NULL;
    int i ;
    int j ;
    int num_loc;
    int num_ele;

    creat_link(&head);
    display_link(head);
    for(i = 0 ; i < N ; i++)
    {
        creat_node(&new_node , i , rand()%100);
        insert_node_tail(head , new_node);
    }
    display_link(head);
    del_node(head);
   // printf("*****main_0\n");
    display_link(head);
   // printf("******main\n");
    insert_node(head);
    display_link(head);
    make_empty(head);
    display_link(head);
    return 0;
}
### 高精度计算 Pi 值的 C 语言实现 在高精度计算 Pi 的过程中,可以利用双向链表来存储中间结果并完成逐位运算。以下是基于双向链表的 Pi 计算方法及其核心逻辑。 #### 双向链表定义 为了便于操作和管理大数运算中的每一位数值,可以通过定义一个双向链表节点结构体 `Node` 来表示每一段数据[^2]: ```c typedef struct Node { int num; // 存储当前节点的数据部分 struct Node* next;// 指向下一位节点 struct Node* prev;// 指向上一位节点 } Node; ``` 此结构允许程序动态扩展或缩减链表长度,从而适应不同精度的需求。 #### 初始化链表 初始化链表时需创建头结点以及设置初始值为零或其他默认状态。例如: ```c Node* initList() { Node* head = (Node*)malloc(sizeof(Node)); head->num = 0; head->next = NULL; head->prev = NULL; return head; } ``` 此处分配内存给新节点,并将其指针域置为空以标记单元素列表结束位置。 #### 插入与删除节点函数 对于复杂的大数加减乘除来说,频繁地插入或者移除某些特定位置上的节点是非常必要的功能之一。下面提供两个基本操作——向前/后方向追加新的节点实例代码片段作为参考: ```c void appendAfter(Node *current, int value){ Node *new_node = (Node *) malloc(sizeof(struct Node)); new_node->num=value; if(current==NULL){ printf("The given previous node cannot be NULL"); return ; } new_node->next=current->next; current->next=new_node; new_node->prev=current; if(new_node->next != NULL) new_node->next->prev= new_node; } // 类似可编写appendBefore() ``` 以上实现了在一个已存在节点之后新增另一个含有指定整数值的新节点的功能。 #### 使用级数展开法求解圆周率 π 一种常见的近似表达式如下所示: \[ \frac{\pi}{4}=1-\frac{1}{3}+\frac{1}{5}-\frac{1}{7}\cdots \] 转换成适合计算机处理的形式即累加项形式,则有: \[ S_k=(-1)^k/(2*k+1), k>=0 \] 因此我们只需要不断迭代上述公式直到达到所需精确度即可停止循环[^1]。 具体算法伪码描述如下: 1. 设定变量sum初值为0; 2. 对于每一个正奇数i从1开始做以下两步直至满足误差条件为止: a). 将(-1)^(floor(i/2))/(i)加入到sum当中去; b). 判断当前绝对差值是否小于目标阈值threshold;如果是则退出循环。 最终得到的结果就是所求数字π的一半,记得最后要乘二恢复原形哦! 实际编码时还需要考虑溢出保护等问题,在这里就不详述了。 ```c double pi_series(int terms_count){ double sum = 0.0f; bool sign=true; for(int i=1;i<=terms_count;i+=2){ sum +=sign*(1.0/i); sign=!sign; } return sum*4; } ``` 注意这只是一个简单的例子演示如何用C语言编程实现Pi估算过程的一部分而已,并未涉及任何关于提高效率的技术细节比如多线程加速之类的高级话题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值