用C语言编写一组程序,基于单链表,用头插法建表,实现某班学生姓名数据的建表、展示、查找、定位、插入、删除、判定表空,求表长等操作

时间: 2023-10-19 21:21:34 浏览: 48
好的,我可以为您提供有关C语言编写单链表的程序代码。这是一个可以展示、查找、插入、删除和操作表长等功能的程序。 ``` #include <stdio.h> #include <stdlib.h> typedef struct node { char name[20]; struct node* next; } Node; Node* create_node(char name[20]) { Node* new_node = (Node*)malloc(sizeof(Node)); if (new_node == NULL) { printf("Error: unable to allocate memory\n"); exit(1); } strcpy(new_node->name, name); new_node->next = NULL; return new_node; } Node* insert_front(Node* head, char name[20]) { Node* new_node = create_node(name); new_node->next = head; return new_node; } int is_empty(Node* head) { return (head == NULL); } int get_length(Node* head) { int length = 0; Node* current = head; while (current != NULL) { length++; current = current->next; } return length; } Node* search(Node* head, char name[20]) { Node* current = head; while (current != NULL) { if (strcmp(current->name, name) == 0) { return current; } current = current->next; } return NULL; } Node* delete(Node* head, char name[20]) { Node* current = head; Node* previous = NULL; while (current != NULL) { if (strcmp(current->name, name) == 0) { if (previous == NULL) { head = current->next; } else { previous->next = current->next; } free(current); return head; } previous = current; current = current->next; } printf("Error: unable to find %s\n", name); return head; } void display(Node* head) { if (is_empty(head)) { printf("The list is empty\n"); return; } Node* current = head; printf("The list: "); while (current != NULL) { printf("%s ", current->name); current = current->next; } printf("\n"); } int main() { Node* head = NULL; head = insert_front(head, "Alice"); head = insert_front(head, "Bob"); head = insert_front(head, "Charlie"); display(head); // Alice Bob Charlie Node* search_result = search(head, "Bob"); if (search_result != NULL) { printf("%s is in the list\n", "Bob"); } else { printf("%s is not in the list\n", "Bob"); } head = delete(head, "Bob"); display(head); // Alice Charlie int length = get_length(head); printf("The list has %d elements\n", length); // The list has 2 elements return 0; } ``` 这个程序将创建一个包含学生姓名的单链表,并使用头插法将其构建。它还提供了许多其他功能,例如展示、查找、插入、删除和操作表长等功能。为了保持代码的简洁性,我们只使用了一个结构体节点,存储学生姓名和指向下一个节点的指针。

相关推荐

最新推荐

recommend-type

C语言实现带头结点的链表的创建、查找、插入、删除操作

主要介绍了C语言实现带头结点的链表的创建、查找、插入、删除操作方法,对于了解数据结构中链表的各项操作有很好的借鉴价值,需要的朋友可以参考下
recommend-type

使用C语言编写圣诞表白程序

圣诞节快到了,让我们用C语言制作一个圣诞表白程序吧,下面通过本文学习下实现代码
recommend-type

C语言编写基于TCP和UDP协议的Socket通信程序示例

主要介绍了C语言编写基于TCP和UDP协议的Socket通信程序示例,其中TCP的客户端与服务器端采用多线程实现,需要的朋友可以参考下
recommend-type

C语言程序设计实现学生成绩记录簿

1)C语言课程设计的主要目的是培养学生综合运用C语言程序设计课程所学到的知识,编写C程序解决实际问题的能力,以及严谨的工作态度和良好的程序设计习惯。 2)通过课程设计的训练,学生应该能够了解程序设计的基本...
recommend-type

使用C语言编写基于TCP协议的Socket通讯程序实例分享

主要介绍了使用C语言编写基于TCP协议的Socket通讯程序实例分享,能够实现包括重新连接与每分钟通信一次等的功能,需要的朋友可以参考下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

可见光定位LED及其供电硬件具体型号,广角镜头和探测器,实验设计具体流程步骤,

1. 可见光定位LED型号:一般可使用5mm或3mm的普通白色LED,也可以选择专门用于定位的LED,例如OSRAM公司的SFH 4715AS或Vishay公司的VLMU3500-385-120。 2. 供电硬件型号:可以使用常见的直流电源供电,也可以选择专门的LED驱动器,例如Meanwell公司的ELG-75-C或ELG-150-C系列。 3. 广角镜头和探测器型号:一般可采用广角透镜和CMOS摄像头或光电二极管探测器,例如Omron公司的B5W-LA或Murata公司的IRS-B210ST01。 4. 实验设计流程步骤: 1)确定实验目的和研究对象,例如车辆或机器人的定位和导航。
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。