数据结构无头结点单向不循环链表(C语言版)
main.c(负责测试)
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include "nohead.h"
int main()
{LNode *list = NULL;struct score_st data[10],mydata,data1;int i = 0;srand((unsigned)time(NULL));//初始化数组for (i = 0; i < 10; i++){data[i].chinese = rand() % 100;data[i].english = rand() % 100;data[i].math = rand() % 100;data[i].student_id = i;snprintf(data[i].name, 32, "学生的名字是%d", i);}data1.chinese = rand() % 100;data1.english = rand() % 100;data1.math = rand() % 100;data1.student_id = 100;snprintf(data1.name, 32, "学生的名字是%d", 100);//打印链表中的数据list_display(list);//链表中插入数据,头部插入,无头节点单向不循环链表for (i = 0; i < 10; i++){list_insert(&list, &data[i]);}list_display(list);//查找id = 3的学生信息list_find(list,3,&mydata);printf("%4s %20s %4s %4s %4s\n", "学号", "姓名", "数学", "语文", "外语");printf("%4d %20s %4d %4d %4d\n", mydata.student_id,mydata.name, mydata.math, mydata.chinese,mydata.english);//删除链表中的第一个有效节点(单向不循环无头节点链表)//list_delete(&list);//list_display(list);list_insert_at(&list,0,&data1);list_display(list);list_destroy(list);
}
nohead.c(负责函数实现)
#include<stdio.h>
#include<stdlib.h>
#include "nohead.h"int list_insert_at(struct LNode **list,int i, struct score_st *data)
{struct LNode *node = *list,*newnode = NULL;int j = 0;if (i < 0){return -1;}if (i == 0){newnode = (LNode*)malloc(sizeof(LNode));if (newnode == NULL){return -3;}newnode->score = *data;newnode->next = *list;*list = newnode;return 0;}//找到第i-1个有效节点while ((j < i-1)&&(node!=NULL)){node = node->next;j++;}if (node == NULL){return -2;}newnode = (LNode*)malloc(sizeof(LNode));if (newnode == NULL){return -3;}newnode->score = *data;newnode->next = node->next;node->next = newnode;return 0;
}int list_insert(struct LNode **list, struct score_st *data)
{LNode *newnode = NULL;newnode = (LNode*)malloc(sizeof(LNode));if (newnode == NULL)return -1;newnode->score = *data;newnode->next = *list;*list = newnode;
}void list_display(LNode *list)
{LNode *ps = list;if (ps == NULL){printf("链表为空\n");return -1;}printf("学生的信息如下:\n");printf("%4s %20s %4s %4s %4s\n", "学号", "姓名", "数学", "语文", "外语");while (ps){printf("%4d %20s %4d %4d %4d\n", ps->score.student_id,ps->score.name, ps->score.math, ps->score.chinese,ps->score.english);ps = ps->next;}
}int list_find(LNode *ps,int id,struct score_st *data)
{while (ps){if (ps->score.student_id == id){*data = ps->score;return 0;}ps=ps->next;}return -1;
}//删除无头节点链表中第一个有效节点
int list_delete(LNode **ps)
{LNode* curnode = *ps;if (*ps == NULL){printf("链表为空删除失败\n");return -1;}(*ps) = curnode->next;free(curnode);return 0;
}void list_destroy(LNode *p)
{LNode *nextnode = NULL;while (p){nextnode = p->next;free(p);p = nextnode;}}
nohead.h(负责函数声明)
#ifndef NOHEAD_H__
#define NOHEAD_H__
#define MAX_SIZE 32
//定义结构体类型,保存学生成绩
struct score_st
{char name[MAX_SIZE];int student_id;int math;int chinese;int english;
};
typedef struct LNode
{struct score_st score;struct LNode *next;
}LNode;
//无头节点单向不循环链表,在链表头部插入数据,第一个有效节点的下标为0
int list_insert(struct LNode *list, struct score_st *score);
//按位置插入节点
int list_insert_at(struct LNode **list, int i, struct score_st *data);
void list_display(LNode *list);
int list_delete(LNode **ps);
void list_destroy(LNode *list);
//查找id==**的结点
int list_find(LNode *ps, int id,struct score_st *data);
#endif
数据结构无头结点单向不循环链表(C语言版)相关推荐
- 数据结构带头结点单向不循环链表(C语言版)
main.c,负责测试 #define _CRT_SECURE_NO_WARNINGS 1 #include <stdio.h> #include<stdlib.h> #inc ...
- python通讯录管理系统设计_数据结构课程设计-通讯录管理系统(C语言版)
##数据结构课程设计-通讯录管理系统 一,前言 自从上次C语言课设写完后,这次数据结构课设就写的游刃有余了,很快啊,不足三天就写完了它(年轻人不讲武德),如果你认真看过我之前写的C语言课程设计-球队管 ...
- 数据结构——链式队列解析(C语言版)
摘自:数据结构学习--链式队列解析(C语言版) 作者:正弦定理 发布时间:2020-11-26 21:07:08 网址:https://blog.csdn.net/chinesekobe/articl ...
- c语言算法设计 pdf下载,数据结构算法设计与实现指导(C语言版).pdf
3 章 栈--实验三 3.1 实验目的及要求 1.理解特 的线性结构--顺序栈的抽象数据类型的定义,及其在 C 语言环境中的 表示方法. 2 .理解顺序栈的基本操作的算法,及其在C 语言环境中一些主要 ...
- [数据结构] 无头结点的头插法建立单链表(c语言)
代码如下: #include<stdio.h> #include<stdlib.h> typedef struct LNode{ //单链表结点的定义 int data;str ...
- 数据结构-栈详解(类C语言版)
目录 栈的定义 概念 栈的抽象数据类型定义 顺序栈的基本操作 存储方式 顺序栈的表示 顺序栈的初始化 顺序栈判断是否为空 清空顺序栈 销毁顺序栈 顺序栈的入栈 顺序栈的出栈 取顺序栈的栈顶元素 链栈的 ...
- 数据结构-队列详解(类C语言版)
目录 队列的相关概念 定义 逻辑结构 存储结构 运算规则 实现方式 队列的基本操作 循环队列--队列的顺序表示和实现 队列的顺序存储结构 假溢出-引出循环队列 判断循环队列队空队满 循环队列的操作 队 ...
- 数据结构哈夫曼树(C语言版)
文章目录 一. 问题 需求分析 代码分析 结构体定义使用 建立哈夫曼树,首先需要找到两个权值最小的两个叶子结点,然后建树 哈夫曼编码(我采用的是从叶子结点-->根节点,所以实际是反过来的) 使用 ...
- 【数据结构--哈夫曼编码(C语言版)】
文章目录 哈夫曼树及其应用 哈夫曼树 哈夫曼树的特点 哈夫曼树的构造 哈夫曼编码 哈夫曼树及其应用 哈夫曼树 介绍哈夫曼树前先介绍下面几个名词: 1. 结点的路径长度l 从根结点到该结点的路径上分支的 ...
最新文章
- DIV限制宽度,字符断行,避免变形
- “情感计算”的蓬勃发展依赖于收集大量的行为和情感数据
- P值(P-value),“差异具有显著性”和“具有显著差异”
- 从0搭建一个Springboot+vue前后端分离项目(二)使用idea进行页面搭建+页面搭建
- IDEA的UML图详细介绍(二)
- BZOJ2194 快速傅立叶之二 【fft】
- Solr 配置文件之schema.xml
- JavaScript正则表达式19例(7)
- 电商产品经理:如何搭建会员管理体系(多图干货)
- @media用法解释
- CodeForces 643 D.Bearish Fanpages(set+multiset)
- Excel删除重复项,不保留重复项数据
- 论文阅读-AKS_CoRR_2011
- chatty: uid=10549(u0_a549) com.exampleidentical 40 lines
- java窗体 个人信心_帮忙写下代码java swing,个人信息实例
- iphone6 问题总结
- 奇技淫巧玄妙无穷| M1 mac os(苹果/AppleSilicon)系统的基本操作和设置
- 简单答题系统(判断题)
- LibreOJ545. 「LibreOJ β Round #7」小埋与游乐场【网络流】
- 带本科生作工程实习(1)