目前共有2篇帖子。 內容轉換:不轉換▼
 
點擊 回復
608 1
【代码】顺序表和链表
一派護法 十九級
1樓 發表于:2016-9-1 21:15
#include <stdio.h>
#include <stdlib.h>

/* 链表的结构体描述 */
typedef struct chain
{
    int data;
    struct chain *next;
} C;

/* 找到指定节点 */
C *find(C *head, int i)
{
    C *node = head;
    if (node == NULL)
        return NULL; // 若头节点为空则退出
    while (i--)
    {
        node = node->next;
        if (node == NULL)
            return NULL; // 未找到
    }
    return node;
}

/* 链表节点的插入, 在i的后面插入 */
/* 不能在头节点前插入节点 */
int insert(C *head, int i, int value)
{
    C *node, *new;
    if (i < 0)
        return 0;

    node = find(head, i); // 找到插入点
    if (node == NULL)
        return 0;

    // 创建新节点
    new = (C *)malloc(sizeof(C));
    if (new == NULL)
        return 0;
    new->data = value;

    new->next = node->next;
    node->next = new;
    return 1;
}

/* 链表节点的删除 */
int delete(C* head, int i)
{
    C *prev, *node;
    if (i <= 0)
        return 0; // 头节点不可删除
    prev = find(head, i - 1);
    node = prev->next; // 要删除的节点
    if (prev == NULL || node == NULL)
        return 0; // 节点不存在

    prev->next = node->next;
    free(node);
    return 1;
}








/* 以下为测试代码 */
void show(C *head)
{
    C *node;
    for (node = head; node != NULL; node = node->next)
        printf("%d ", node->data);
    putchar('\n');
}

void destroy(C *head)
{
    C *node = head->next;
    C *p;
    while (node != NULL)
    {
        p = node->next;
        free(node);
        node = p;
    }
}

int main(void)
{
    C c = {0};
    c.data = 15; // 头节点
    
    insert(&c, 0, 73);
    insert(&c, 1, -48);
    insert(&c, 2, -264);
    show(&c);

    insert(&c, 1, 185);
    show(&c);
    insert(&c, 3, 224);
    show(&c);
    insert(&c, 5, 1);
    show(&c);
    insert(&c, 6, 2);
    show(&c);

    putchar('\n');
    delete(&c, 7);
    show(&c);
    delete(&c, 3);
    show(&c);
    delete(&c, 4);
    show(&c);
    
    destroy(&c);
    return 0;
}
一派護法 十九級
2樓 發表于:2016-9-1 21:15
#include <stdio.h>
#include <stdlib.h>

#define STEP 10 // 每次增加的容量

/* 顺序表的结构体描述 */
typedef struct table
{
    int *data;
    int capacity; // 容量
    int length; // 当前拥有的数据个数
} T;

/* 顺序表的插入操作,在i的前面插入 */
int insert(T *t, int i, int value)
{
    int j;
    void *ptr;

    if (i < 0 || i > t->length)
        return 0;

    if (t->capacity == t->length)
    {
        // 扩容
        t->capacity += STEP;
        ptr = realloc(t->data, t->capacity * sizeof(int));
        if (ptr == NULL)
            return 0; // 如果内存分配失败, 则操作失败
        t->data = (int *)ptr;
    }

    // 把len-1>=j>=i的内容向后移动到len>=j+1>=i+1处
    for (j = t->length - 1; j >= i; j--)
        t->data[j + 1] = t->data[j];
    
    t->length++;
    t->data[i] = value;
    return 1; // 成功
}

/* 顺序表的删除操作 */
int delete(T *t, int i)
{
    int j;
    if (i < 0 || i >= t->length)
        return 0; // 元素不存在
    
    // 后续内容向前移动
    // 把i+1<=j<=len-1移动到i<=j-1<=len-2处, 并舍弃i处的内容
    for (j = i + 1; j <= t->length - 1; j++)
        t->data[j - 1] = t->data[j];
    
    t->length--;
    return 1;
}








/* 以下为测试代码 */
void show(T *t)
{
    int i;
    for (i = 0; i < t->length; i++)
        printf("%d ", t->data[i]);
    putchar('\n');
}

int main(void)
{
    T t = {0};

    insert(&t, 0, 15);
    insert(&t, 1, 73);
    insert(&t, 2, -48);
    insert(&t, 3, -264);
    show(&t);

    insert(&t, 1, 185);
    show(&t);
    insert(&t, 3, 224);
    show(&t);
    insert(&t, 5, 1);
    show(&t);
    insert(&t, 7, 2);
    show(&t);

    putchar('\n');
    delete(&t, 7);
    show(&t);
    delete(&t, 0);
    show(&t);
    delete(&t, 3);
    show(&t);
    delete(&t, 4);
    show(&t);
    
    free(t.data);
    return 0;
}

回復帖子

內容:
用戶名: 您目前是匿名發表
驗證碼:
(快捷鍵:Ctrl+Enter)
 

本帖信息

點擊數:608 回複數:1
評論數: ?
作者: 巨大八爪鱼
最後回復:巨大八爪鱼
最後回復時間:2016-9-1 21:15
 
©2010-2024 Arslanbar Ver2.0
除非另有聲明,本站採用創用CC姓名標示-相同方式分享 3.0 Unported許可協議進行許可。