数据结构
本系列是C语言学完的基础上,对数据结构的一些练习,代码偏多。
`注意啦:每日一看
数据结构:
1.逻辑结构(关系)
线性结构:**线性表** 栈 队列
非线性结构: 树 图
2.存储结构:
**顺序存储**
链式存储
注意啦:以下是本人介绍哦
📕作者简介:S学长,致力于C/C++、嵌入式。从事嵌入式行业,热爱健身,身体倍棒的一位博主。
📗本文收录于爱上Linux系列,大家有兴趣的可以看一看
📘相关专栏C语言嵌入式开发、C语言入门系列等,日常Bug集期待你的指导。
📙S学长爱上Linux系列正在发展中,喜欢嵌入式的朋友们可以关注一下哦!
核心讲解
1.顺序表结构
对照上面结构定义数据
2.功能函数
1>插入函数
2>删除函数
具体代码
1.头文件 seqlist.h
#ifndef _seqlist_
#define _seqlist_
#define SIZE 50
#include <stdio.h>
#include <stdlib.h>
#include<stdbool.h>
struct hero {
int num;
char hao[20];
char name[20];
};
typedef struct hero datetype;
typedef struct seqlist {
datetype date[SIZE];
int last;
}seq_list,*seq_plist;
extern void seqlist_init(seq_plist* l);
extern bool seqlist_empty(seq_plist l);
extern bool seqlist_full(seq_plist l);
extern bool seqlist_insert(seq_plist l, int i);
extern void seqlist_autoinsert(seq_plist l);
extern bool seqlist_delete(seq_plist l, int i);
extern void seqlist_travel(seq_plist l);
extern void menu();
#endif
2.函数实现 seqlist.c
#include "seqlist.h"
//初始化
void seqlist_init(seq_plist* l) {
*l = (seq_plist)malloc(sizeof(seq_list));
if (l == NULL) {
perror("malloc");
}
(*l)->last = -1;
}
//菜单
void menu(void)
{
printf("=======================================\n");
printf("=================点将系统==============\n");
printf("------1.水浒------------2.查看---------\n");
printf("------3.删除------------4.自定义-------\n");
printf("------5.退出---------------------------\n");
printf("---------------------------------------\n");
}
//判空
bool seqlist_empty(seq_plist l) {
if (l->last == - 1)
return true;
else
return false;
}
//判满
bool seqlist_full(seq_plist l) {
if (l->last ==SIZE -1)
return true;
else
return false;
}
//添加
bool seqlist_insert(seq_plist l, int i) {
if (seqlist_full(l)) {
printf("顺序表已满\n");
return false;
}
if (i<0 || i>SIZE) {
printf("位置不合理\n");
return false;
}
printf("请输入你要插入的信息:编号 称号 名称\n");
struct hero date1;
scanf("%d%s%s",&date1.num, date1.hao, date1.name);
int j;
for ( j = l->last; j >= i; j--)
l->date[j + 1] = l->date[j];
l->date[i] = date1;
l->last++;
return true;
}
//水浒插入
void seqlist_autoinsert(seq_plist l) {
struct hero shui[40] = { {1001,"天魁星","宋 江"},{1002,"天罡星","卢俊义"} ,{1003,"天机星","吴 用"}, {1004,"天闲星","公孙胜"},
{1005,"天勇星","关 胜"},{1006,"天雄星","林 冲"},{1007,"天猛星","秦 明"},{1008,"天威星","呼延灼"},{1009,"天英星","花 荣"},
{1010,"天贵星","柴 进"},{1011,"天富星","李 应"},{1012,"天满星","朱 仝"},{1013,"天孤星","鲁智深"},{1014,"天伤星","武 松"},
{1015,"天立星","董 平"},{1016,"天捷星","张 清"},{1017,"天暗星","杨 志"},{1018,"天佑星","徐 宁"},{1019,"天空星","索 超"},
{1020,"天速星","戴 宗"},{1021,"天异星","刘 唐"},{1022,"天杀星","李 逵"},{1023,"天微星","史 进"},{1024,"天究星","穆 弘"},
{1025,"天退星","雷 横"},{1026,"天寿星","李 俊"},{1027,"天剑星","阮小二"},{1028,"天平星","张 横"},{1029,"天罪星","阮小五"},
{1030,"天损星","张 顺"},{1031,"天败星","阮小七"},{1032,"天牢星","杨 雄"},{1033,"天慧星","石 秀"},{1034,"天暴星","解 珍"},
{1035,"天哭星","解 宝" }, { 1036,"天巧星","燕 青" } };
for (int i = 0; i <= 36; i++)
{
l->date[i] = shui[i];
l->last++;
}
}
//删除
bool seqlist_delete(seq_plist l,int i) {
if (seqlist_empty(l)) {
printf("顺序表为空\n");
return false;
}
if (i<0 || i>l->last) {
printf("位置不合理\n");
return false;
}
int j;
for (j = i; j < l->last; j++)
l->date[j] = l->date[j + 1];
l->last--;
printf("删除成功\n");
return true;
}
void seqlist_travel(seq_plist l) {
int i;
printf("=======================================\n");
printf("==编号======名号======姓名=============\n");
printf("---------------------------------------\n");
for (i = 0; i < l->last; i++) {
printf("%-6d %s %s\n", l->date[i].num, l->date[i].hao, l->date[i].name);
}
}
3.测试函数 test.c
#include "seqlist.h"
int main() {
int sno,i,n,num;
seq_plist l;
seqlist_init(&l);
while (1) {
menu();
scanf("%d",&n);
switch (n) {
case 1:
seqlist_autoinsert(l);
break;
case 2:
seqlist_travel(l);
break;
case 3:
printf("请输入要删除的编号\n");
scanf("%d",&sno);
for (i = 0; i <= l->last; i++)
if (sno == l->date[i].num) {
seqlist_delete(l,i);
break;
}
if (i > l->last)
printf("数据不存在\n");
break;
case 4:
printf("请输入要插入位置:\n");
scanf("%d",&num);
seqlist_insert(l,num);
break;
case 5:
printf("英雄慢走\n");
exit(0);
break;
}
}
return 0;
}
4.Makefile 文件
CC = gcc
CFLAGS = -Wall -O0 -g
SRC = ${wildcard *.c}
OBJS=test
all:$(OBJS)
$(OBJS):$(SRC)
$(CC) $(CFLAGS) $^ -o $@
clean:
$(RM) $(OBJS)
结果展示
1.运行make编译文件
2.水浒初始化并查看(36天罡星)
3.删除 1001编号 并自定义1个
4.退出
如果这份博客对大家有帮助,希望各位给S学长一个免费的点赞👍作为鼓励,并评论收藏一下⭐,谢谢大家!!!
制作不易,如果大家有什么疑问或给S学长的意见,欢迎评论区留言。