C++学习链表

#include"pch.h"
#include<iostream>
#include<string>
using namespace std;
struct Student
{
    int id;
    char number[16];
    char name[16];
    Student* next;
};
//使用全局变量
Student ss[4] =
{
    {201501,"John",0},
{201502,"Jennifer",0},
{201503,"AnXi",0},
{201504,"Unnamed",0}
};
Student* find(Student*head, int id)
{
    Student*p = head;
    while (p)
    {
        if (p->id == id)//符合条件
            return p;
        p->next = p;
    }
    return NULL;//没有找到符合条件的对象
    //注意指针的时效性,指针指向的对象是否还活着
}
int main()
{
    //构造链表
    ss[0].next = &ss[1];
    ss[1].next = &ss[2];
    ss[2].next = &ss[3];
    ss[3].next = 0;
    //链表的遍历
    Student* p = &ss[0];
    while (p)
    {
        cout << p->id << p->name;
        p = p->next;
    };
    Student*result = find(&ss[0], 201503);
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/chuxinbubian/p/10516320.html