version1

C语言链表操作:创建、插入与删除

#define  _CRT_SECURE_NO_WARNINGS
#include<stdio.h> 
#include<malloc.h> 

struct node {
    char data;
    struct node* next;
};

typedef struct node NODE;

/* This function creates a linked list with N nodes. */
NODE* create_link_list(int n)
{
    int i;
    NODE* head, * p, * q;
    if (n == 0) return NULL;
    head = (NODE*)malloc(sizeof(NODE));
    p = head;
    printf("Please input %d chars for the linked list:\n", n);
    for (i = 0; i < n; i++)
    {
        scanf(" %c", &(p->data));
        q = (NODE*)malloc(sizeof(NODE));
        p->next = q;
        p = q;
    }
    p->next = NULL; // 将最后一个节点的next指针设置为NULL
    return (head);
}

/* This function inserts a node whose value is b before the node whose value is a, if the node is not exist,
then insert it at the end of the list. */
void insert(NODE** p_head, char a, char b)
{
    NODE* p, * q;
    q = (NODE*)malloc(sizeof(NODE));
    q->data = b;
    q->next = NULL;
    if (*p_head == NULL) *p_head = q;
    else
    {
        p = *p_head;
        while (p->data != a && p->next != NULL)
            p = p->next;
        if (p->next == NULL)
            p->next = q;
        else
        {
            q->next = p->next;
            p->next = q;
        }
    }
}

/* The function deletes the node whose value is a, if success, return 0, or return 1. */
int deletenode(NODE** p_head, char a)
{
    NODE* p, * q;
    p = NULL;
    q = *p_head;
    if (q == NULL) return(1);
    if (q->data == a)
    {
        *p_head = q->next;
        free(q);
        return (0);
    }
    else
    {
        while (q->data != a && q->next != NULL)
        {
            p = q;
            q = q->next;
        }
        if (q->data == a)
        {
            p->next = q->next;
            free(q);
            return(0);
        }
        else
            return(1);
    }
}

int main()
{
    NODE* my_head, * p;
    int m;
    char ch_a, ch_b;

    /* create a linked list with m nodes */
    printf("Please input the number of nodes for the linked list:\n");
    scanf("%d", &m);
    my_head = create_link_list(m);

    /* output the linked list */
    printf("The linked list is like:\n");
    p = my_head;
    while (p != NULL)
    {
        printf("%c ", p->data);
        p = p->next;
    }
    printf("\n");

    /* insert a node whose value is b before a */
    printf("Please input the position for a:\n");
    scanf(" %c", &ch_a);
    printf("Please input the value that you want to insert:\n");
    scanf(" %c", &ch_b);
    insert(&my_head, ch_a, ch_b);
    printf("The linked list after insertion is like:\n");
    p = my_head;
    while (p != NULL)
    {
        printf("%c ", p->data);
        p = p->next;
    }
    printf("\n");

    /* delete a node whose value is a */
    printf("Please input the position for a:\n");
    scanf(" %c", &ch_a);
    if (deletenode(&my_head, ch_a) == 0)
    {
        printf("The linked list after deleting is like:\n");
        p = my_head;
        while (p != NULL)
        {
            printf("%c ", p->data);
            p = p->next;
        }
        printf("\n");
    }
    else
        printf("Failed to delete.\n");

    return 0;
}
 

### Python 中 `check_version1.strip()` 的用法及常见问题 `strip()` 是 Python 字符串的一个内置方法,用于移除字符串开头和结尾的指定字符(默认为空白字符,包括空格、制表符 `\t` 和换行符 `\n`)。如果未指定参数,则默认移除所有空白字符。以下是关于 `check_version1.strip()` 的详细说明: #### 1. 基本用法 假设变量 `check_version1` 包含一个字符串值,`strip()` 方法可以用来清理该字符串的首尾空白字符[^1]。例如: ```python check_version1 = " v1.2.3 " cleaned_version = check_version1.strip() print(cleaned_version) # 输出: v1.2.3 ``` #### 2. 自定义字符移除 如果需要移除特定字符而非空白字符,可以通过传递参数给 `strip()` 来实现。例如: ```python check_version1 = "xxxv1.2.3xxx" cleaned_version = check_version1.strip("x") print(cleaned_version) # 输出: v1.2.3 ``` 上述代码中,`strip("x")` 移除了字符串两端的所有 `x` 字符[^2]。 #### 3. 常见错误与解决方法 - **错误类型 1:尝试对非字符串对象调用 `strip()`** 如果 `check_version1` 不是字符串类型,直接调用 `strip()` 会导致 `AttributeError` 错误。例如: ```python check_version1 = 123 # 非字符串 cleaned_version = check_version1.strip() # 抛出 AttributeError ``` 解决方法:在调用 `strip()` 前确保变量为字符串类型。 ```python check_version1 = str(check_version1) cleaned_version = check_version1.strip() ``` - **错误类型 2:忽略中间的空白字符** `strip()` 只会移除字符串的首尾空白字符,而不会影响字符串中间的内容。例如: ```python check_version1 = " v1.2.3 alpha " cleaned_version = check_version1.strip() print(cleaned_version) # 输出: v1.2.3 alpha ``` 如果需要移除所有空白字符,可以结合其他方法,如 `replace()` 或正则表达式[^3]。 #### 4. 实际应用场景 在实际开发中,`strip()` 常用于清理用户输入或从文件、网络请求中获取的数据。例如,在检查版本号时: ```python def validate_version(version_str): if not isinstance(version_str, str): return False cleaned_version = version_str.strip() if cleaned_version.startswith("v"): return True return False check_version1 = " v1.2.3 " print(validate_version(check_version1)) # 输出: True ``` #### 5. 结合其他方法 有时需要结合多个字符串方法来完成复杂任务。例如,清理并分割版本号: ```python check_version1 = " v1.2.3-beta " cleaned_version = check_version1.strip().split("-")[0] print(cleaned_version) # 输出: v1.2.3 ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值