[语法]字符串的插入与删除

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

void erase(char text[], int index)
{
    int len = strlen(text);
    for(int i = index; i<len ; i++)
    {
        text[i] = text[i + 1]; // 后面的字符前移
    }
}


int main()
{
    char str[10] = "hello";
    erase(str, 1);
    
    return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void erase(char text[], char del)
{
    int len = strlen(text); // 原字符串长度

    int count = 0;
    char* copy = (char*) malloc(len + 1);
    for(int i=0; i<len; i++)
    {
        char ch = text[i];
        if(ch != del )
        {
            copy[count] = ch;
            count ++;
        }
    }
    copy[count] = 0; // 添加结束符
    
    strcpy(text, copy); // 拷回原字符串
    free(copy); // 释放内存
}

int main()
{
    char str[] = "China is a great country with a long history";
    erase(str, 'a');
    
    return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void insert(char text[], int index, char ins)
{
    int len = strlen(text);
    for(int i = len; i >index ; i--)
    {
        text[i] = text[i - 1]; // 后面的字符前移
    }
    text[index] = ins;
}


int main()
{
    char str[10] = "hello";
    insert(str, 1, 'X');
    
    return 0;
}

 

[语法]删除与插入链表节点

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

struct Student
{
    int id;
    char name[16];
    Student* next;
};

// 定义了一个有头节点
Student m_head = {0};

// 按顺序插入节点
int insert(Student* obj)
{
    Student* cur = m_head.next; // 当前节点current
    Student* pre = &m_head;  // 上一个节点previous
    while(cur)
    {		
        if(obj->id < cur->id) // 找到这个位置
            break;

        pre = cur;
        cur = cur->next;  // 找到最后一个对象
    }

    // 插入到pre节点的后面
    obj->next = pre->next;
    pre->next = obj;
    return 0;
}

// 按id查找并删除节点
void remove(int id)
{
    Student* cur = m_head.next; // 当前节点current
    Student* pre = &m_head;  // 上一个节点previous
    while(cur)
    {		
        if(id == cur->id) // 找到这个位置
        {
            // 删除该节点
            pre->next = cur->next;
            free(cur);
            break;
        }
        pre = cur;
        cur = cur->next;  // 找到最后一个对象
    }
}

// 遍历
void show_all()
{
    Student* p = m_head.next; 
    while(p)
    {
        printf("ID: %d, name: %s\n", p->id, p->name);
        p = p->next; // 下一个对象
    }
}

int main()
{
    Student* obj = NULL;
    

    obj = (Student*)malloc (sizeof(Student));
    obj->id = 8;
    strcpy(obj->name, "888");
    insert(obj);

    obj = (Student*)malloc (sizeof(Student));
    obj->id = 1;
    strcpy(obj->name, "111");
    insert(obj);

    obj = (Student*)malloc (sizeof(Student));
    obj->id = 4;
    strcpy(obj->name, "444");
    insert(obj);

    obj = (Student*)malloc (sizeof(Student));
    obj->id = 3;
    strcpy(obj->name, "333");
    insert(obj);

    obj = (Student*)malloc (sizeof(Student));
    obj->id = 5;
    strcpy(obj->name, "555");
    insert(obj);

    remove(3);
    remove(2);
    show_all();
    return 0;
}

 

[语法]有头链表的构造

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

struct Student
{
    int id;
    char name[16];
    Student* next;
};

// 定义了一个有头节点
Student m_head = {0};

// 插入一个对象到链表中
// void add(Student* obj)
// {
// 	obj->next = m_head.next;
// 	m_head.next = obj;
// }

void add(Student* obj)
{
    Student* p = &m_head;
    while(p->next)
        p = p->next;  // 找到最后一个对象
    p->next = obj;   //  把obj挂在最后一个对象后面
    obj->next = NULL;  // 现在obj作为最后一个对象
}


int main()
{
    Student* obj_1 = (Student*)malloc (sizeof(Student));
    obj_1->id = 1;
    strcpy(obj_1->name, "X");
    add(obj_1);

    Student* obj_2 = (Student*)malloc (sizeof(Student));
    obj_2->id = 2;
    strcpy(obj_2->name, "Y");
    add(obj_2);
    return 0;
}

 

[语法]malloc示例

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


struct Car
{
    char maker[32]; // 制造商
    int  price;  // 价格
};
struct Citizen
{
    char name[32]; // 名字
    int  deposite; // 存款
    Car* car;  // NULL时表示没车
};

void buy(Citizen* owner)
{ 
    // 创建一个对象
    Car* car = (Car*) malloc(sizeof(Car));
    strcpy(car->maker, "Chevrolet");
    car->price = 10;

    // 保存此对象 (确切地说是记住了指针)
    owner->car = car; // 有车了
    owner->deposite -= car->price; // 钱没了
}

void discard(Citizen* owner)
{
    free(owner->car);  // 此对象被销毁
    owner->car = NULL;  // 回到无车状态   
}

//也有可能会买给别人,
void sell(Citizen* owner, Citizen* other)
{
    Car* car = owner->car;

    car->price *= 0.5; //半价出售
    other->deposite -= car->price;
    other->car = car; // 别人拥有了这辆车

    owner->deposite += car->price; // 收回一部分成本
    //free(car); // oh,no! 不能free,这车在别人手里
    owner->car = NULL;  // 回到无车状态   
}



int main()
{
    while(1)
    {
        void* ptr = malloc(1024*512);
        if(!ptr)
        {
            printf("no memory!\n");
        }
    }

    //discard(&shaofa);
    return 0;
}