单向链表队列

//链表队列
#include<iostream>
#include<string.h>
#include<malloc.h>
#include<stdlib.h>
using namespace std;
typedef struct Student
{
    int num;
    char name[100];
    struct Student *next;
}Liststudent;
Liststudent * creat(int n)
{
    Student *p,*head,*q;
    head=(Liststudent *)malloc(sizeof(Liststudent));
    head->next=NULL;
    q=head;
    int i;
    for(i=0;i<n;i++)
    {
        p=(Liststudent *)malloc(sizeof(Liststudent));
        int a;char s[100];
        cout<<"姓名:";cin>>a;
        cout<<"学号:";cin>>s;
        strcpy(p->name,s);
        p->num=a;
        q->next=p;
        q=p;
    }
    q->next=NULL;
    return head;

}

//打印链表

Liststudent * pop(Liststudent *p)

{
    Liststudent *h;
    h=p->next;
    while(h!=NULL)
    {
        cout<<"姓名:";cout<<h->num;
        cout<<"学号:";cout<<h->name;
        h=h->next;
    }

}

//删除节点

Liststudent * del(Liststudent *p,int n)
{
    Liststudent *h;
    for(int i=0;i<n-1;i++)
    {
        p=p->next;
    }
    h=p->next;
    p->next=h->next;

}

//增加节点

Liststudent * ins(Liststudent *p,int n,int num,char s[])
{
    Liststudent *h;
    for(int i=0;i<n-1;i++)
    {
        p=p->next;
    }
    h=(Liststudent *)malloc(sizeof(Liststudent));
    strcpy(h->name,s);
    h->num=num;
    h->next=p->next;
    p->next=h;

}

//查询节点

Liststudent *search(Liststudent *p,int n)
{
    int i;
    for(i=0;i<n-1;i++)
    {
        p=p->next;
    }
    cout<<"学生名字:"<<p->name;
    cout<<"学生学号:"<<p->num;
}
int main()
{
    Liststudent *p;
    int n,i;
    int num;
    char s[100];
    while(i)
    {
        cout<<"1"<<"请输入学生信息"<<endl;
        cout<<"2"<<"删除学生信息"<<endl;
        cout<<"3"<<"增加学生信息"<<endl;
        cout<<"4"<<"查询学生信息"<<endl;
        cout<<"5"<<"打印所有学生信息"<<endl;
        cout<<"0"<<"退出该程序"<<endl;
        cout<<"您要操作的对象序号为:"<<endl;cin>>i;
        switch(i)
        {
            case 1:
                cout<<"学生总人数为:"<<endl;cin>>n;
                p=creat(n);
                pop(p);
                cout<<endl;
                break;
            case 2:
                cout<<"所要删除学生的位置为:"<<endl;
                cin>>i;
                del(p,i);
                pop(p);
                cout<<endl;
                break;
            case 3:
                cout<<"所要增加的学生位置为:"<<endl;
                cin>>i;
                cout<<"增加的学生信息"<<endl;
                cout<<"姓名:"<<endl;
                cin>>s;
                cout<<"学号:"<<endl;
                cin>>num;
                ins(p,i,num,s);
                pop(p);
                cout<<endl;
                break;
            case 4:
                cout<<"所要查询的学生位置为:"<<endl;
                cin>>i;
                search(p,i);
                cout<<endl;
                break;
            case 5:
                cout<<"所有学生信息为:"<<endl;
                pop(p);
                cout<<endl;
                break;
            case 0:
                return 0;
        }


    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值