学生线性表

本文介绍了一个使用C++编写的链表结构,包括Student Information结构、ChainList和HeadNode,展示了如何创建链表、插入学生数据并按学号顺序输出。通过实例演示了如何在链表中存储和操作学生信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <stdlib.h>
#include <stdio.h>
#include <iostream>
using namespace std;
typedef struct student_information
{   
    int    num;      //学号
    char    name[10];   //姓名
} student;
typedef struct chainlist
{
    student data;
    chainlist *next;
}chainlist;
typedef struct headnode
{
    int length;
    chainlist *first;
}headnode,*head;

headnode *Creat(headnode *list)
{// 构造一个空链表

    list=new headnode;//产生一个仅有表头结点的链表
    list->first=NULL;//first的值设为空(NULL)值 
    list->length=0;
    return list;
}
bool insert(headnode *head,int k,student stu1)
{
    if (k < 0||k>head->length) 
        return false;
    int index=1;
    chainlist *current=head->first;
    while(index<k&&current)
    {
        index++;
        current=current->next;
    }
    chainlist *q=new chainlist;
    q->data=stu1;
    if(k>1)
    {
        q->next=current->next;//next存储current的next current的next指向原来下一个数据的地址 每一个节点有自己的地址,但也存储着下一个节点的地址 
        current->next=q;//current的next存储q的地址,相当于当前节点指向q 
    }
    else
    {
        q->next=head->first;
        head->first=q;
    }
    head->length++;
    return true; 
}
void Output(headnode *head)
{// 逐个地输出链表L中的数据元素
    chainlist *current=head->first;
    while (current)
    {
        if(current->data.num!=0)
        cout<<current->data.num<<current->data.name<<" ";
        current=current->next ;
    }
    cout<<endl;
}

int main(){
    int i=0;
    student stu1;
    headnode *l;
    l=Creat(l);
    cout<<"请输入学生数据\n";
    while(cin>>stu1.num)
    {
        if(stu1.num==0)
        break;
        else
        {
            cin>>stu1.name;
            insert(l,i,stu1);
            i++;
        }
        
        
    }
    Output (l);
    printf("共有学生%d人",i); 
    
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

paul_chen21

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值