数据结构C语言描述

高级数据结构

1顺序存储线性表

顺序存储线性表由数组来实现,就和java中的ArrayList一样

头文件

#ifndef REMOTE_SERVER_SQLIST_H
#define REMOTE_SERVER_SQLIST_H

#endif //REMOTE_SERVER_SQLIST_H

#define DATASIZE 1024

typedef int datatype;

typedef struct {
   
    datatype data[DATASIZE];
    int last;
} sqlist;

//初始化列表
sqlist * sqlist_create();

void sqlist_create1(sqlist **);

//增加数据
datatype sqlist_insert(sqlist *, datatype *, int idx);

//删除数据
datatype sqlist_delete(sqlist *, int idx);

//查询数据,返回下标
int sqlist_find(sqlist *, datatype *);

//线性表是否清空
int sqlist_isEmpty(sqlist *);

实现

#include "sqlist.h"
#include <stdio.h>
#include <malloc.h>

//初始化列表
sqlist * sqlist_create() {
   
    sqlist * lst = malloc(sizeof (sqlist));
    if (lst == NULL) {
   
        return NULL;
    }
    lst->last = -1;
    return lst;
}

void sqlist_create1(sqlist **lst) {
   
    *lst = malloc(sizeof (sqlist));
    if (*lst == NULL) return;
    (*lst)->last = -1;
    return;
}

//增加数据
datatype sqlist_insert(sqlist * lst, datatype * data, int idx) {
   
    if (lst == NULL) return -1;
    if (lst->last == DATASIZE - 1) return -1;
    if (idx < 0 || idx > lst->last + 1) return -1;
    for (int i = lst->last + 1; i >= idx + 1; i--) {
   
        lst->data[i] = lst->data[i - 1];
    }
    lst->data[idx] = *data;
    lst->last++;
    return *data;
}

//删除数据
datatype sqlist_delete(sqlist * lst, int idx) {
   
    if (lst == NULL || idx < 0 || idx > lst->last) return -1;
    datatype tmp = lst->data[idx];
    for (int i = idx; i < lst->last; i++) {
   
        lst->data[i] = lst->data[i + 1];
    }
    lst->data[lst->last] = 0;
    lst->last--;
    return tmp;
};

//查询数据,返回下标
int sqlist_find(sqlist * lst, datatype * data) {
   
    if (lst == NULL) return -1;
    for (int i = 0; i <= lst->last; i++) {
   
        if (lst->data[i] == *data) {
   
            return i;
        }
    }
    return -1;
}

//线性表是否清空
int sqlist_isEmpty(sqlist * lst) {
   
    if (lst == NULL || lst->last == -1) return 1;
    return 0;
}

//将线性表清空
int sqlist_setEmpty(sqlist * lst) {
   
    if (lst == NULL) return -1;
    for (int i = 0; i <= lst->last; i++) lst->data[i] = 0;
    lst->last = -1;
    return 0;
}

//查询元素数量
int sqlist_size(sqlist * lst) {
   
    if (lst == NULL) return 0;
    return lst->last + 1;
}

//合并线性表
int sqlist_union(sqlist * dest, const sqlist * src) {
   
    if (dest->last + src->last + 2 > DATASIZE) return -1;
    for (int i = dest->last + 1; i < dest->last + src->last + 2; i++) {
   
        dest->data[i] = src->data[i - dest->last - 1];
    }
    dest->last += src->last + 1;
    return 0;
}

//销毁列表
int sqlist_destroy(sqlist * lst) {
   
    free(lst);
}

//遍历输出列表
void sqlist_display(sqlist * lst) {
   
    for (int i = 0; i <= lst->last; i++) {
   
        printf("%d ", lst->data[i]);
    }
    printf("\n");
}

2 单向链表

2.1 单向链表

头文件

#ifndef REMOTE_SERVER_LINKEDLIST_H
#define REMOTE_SERVER_LINKEDLIST_H

#endif //REMOTE_SERVER_LINKEDLIST_H


typedef int datatype;

typedef struct ListNode{
   
    datatype data;
    struct ListNode * next;
} ListNode;

typedef struct {
   
    ListNode * head;
    int size;
} LinkedList;

//初始化链表
LinkedList * list_create();

//链表中指定位置插入节点
int list_insert_at(LinkedList *, int idx, datatype *);

//按序插入
int list_order_insert(LinkedList *, datatype *);

//删除指定位置节点
datatype list_delete_at(LinkedList *, int idx);

//删除某个值
datatype list_delete(LinkedList *, datatype *);

//判断是否为空
int list_isEmpty(LinkedList *);

//返回链表的大小
int list_size(LinkedList *);

//打印整个链表
void list_display(LinkedList *);

//销毁链表
void list_destory(LinkedList **);

实现

#include "linkedList.h"
#include <stdio.h>
#include <malloc.h>


//初始化链表
LinkedList * list_create() {
   
    LinkedList * lst = malloc(sizeof (LinkedList));
    if (lst == NULL) return NULL;
    lst->size = 0;
    lst->head = malloc(sizeof (ListNode));
    lst->head->next = NULL;
    return lst;
}

//链表中指定位置插入节点
int list_insert_at(LinkedList * lst, int idx, datatype * data) {
   
    if (idx < 0 || idx > lst->size) return -1;
    ListNode * pre = lst->head;
    ListNode * cur = lst->head->next;
    ListNode * node = malloc(sizeof (ListNode));
    if (node == NULL) return -1;
    node->next = NULL;
    node->data = *data;
    for (int i = 0; i < idx; i++) {
   
        pre = cur;
        cur = cur->next;
    }
    pre->next = node;
    node->next = cur;
    lst->size++;
    return 0;
}

//按序插入
int list_order_insert(LinkedList * lst, datatype * data) {
   
    return list_insert_at(lst, lst->size, data);
}

//删除指定位置节点
datatype list_delete_at(LinkedList * lst, int idx) {
   
    if (idx < 0 || idx >= lst->size) return -1;
    ListNode * pre = lst->head;
    ListNode * cur = lst->head->next;
    for (int i = 0; i < idx; i++) {
   
        pre = cur;
        cur = cur->next;
    }
    pre->next = cur->next;
    datatype ret = cur->data;
    free(cur);
    cur = NULL;
    lst->size--;
    return ret;
}

//删除某个值
datatype list_delete(LinkedList * lst, datatype * data) {
   
    ListNode * pre = lst->head;
    ListNode * cur = lst->head->next;
    while (cur != NULL) {
   
        if (cur->data == *data) {
   
            pre->next = cur->next;
            free(cur);
            lst->size--;
            cur = pre->next;
        } else {
   
            pre = cur;
            cur = cur->next;
        }
    }
    return *data;
}

//判断是否为空
int list_isEmpty(LinkedList * lst) {
   
    if (lst == NULL) return 1;
    return lst->size == 0;
}

//返回链表的大小
int list_size(LinkedList * lst) {
   
    if (lst == NULL) return 0;
    return lst->size;
}

//打印整个链表
void list_display(LinkedList * lst) {
   
    ListNode * cur = lst->head->next;
    while (cur != NULL) {
   
        printf("%d ", cur->data);
        cur = cur->next;
    }
    printf("\n");
}

//销毁链表
void list_destory(LinkedList ** lst) {
   
    ListNode * cur = (*lst)->head;
    while (cur != NULL) {
   
        ListNode * tmp = cur->next;
        free(cur);
        cur = tmp;
    }
    free(*lst);
    *lst = NULL;
}

2.2 单向循环链表

单向循环链表解决约瑟夫环问题

头文件

#ifndef REMOTE_SERVER_LIST_H
#define REMOTE_SERVER_LIST_H

typedef int datatype;

typedef struct ListNode {
   
    datatype data;
    struct ListNode * next;
} Node;

typedef struct {
   
    int size;
    Node * head;
} LoopList;

//初始化单向循环链表
LoopList * looplist_create();

//在链表的第i个节点后插入节点
int looplist_insert_at(LoopList ** lst, datatype * data, int idx);

//删除链表的第i个节点
datatype looplist_deleteat(LoopList ** lst, int idx);

//返回链表中的元素个数
int looplist_size(LoopList * lst);

//判断链表是否为空
int looplist_isEmpty(LoopList * lst);

//打印链表
void looplist_show(LoopList * lst);

#endif //REMOTE_SERVER_LIST_H

实现

#include <stdio.h>
#include <malloc.h>
#include "list.h"

//初始化单向循环链表
LoopList * looplist_create() {
   
    LoopList * lst = NULL;
    lst = malloc(sizeof (LoopList));
    if (lst == NULL) return NULL;
    lst->size = 0;
    lst->head = NULL;
    return lst;
}

//在链表的第i个节点后插入节点
int looplist_insert_at(LoopList ** lst, datatype * data, int idx) {
   
    Node * node = malloc(sizeof (Node));
    node->data = *data;
    if ((*lst)->head == NULL && idx == 0) {
   
        (*lst)->head 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值