2025年c++单向链表类(单向链表c语言)

c++单向链表类(单向链表c语言)阅读以下程序并填空 填上正确的语法成分 使其成为完整的程序 假设原链表的结点是按学号 number 由小到大排列的 在已建立的学生链表中插入一新结点 使插入新结点后的链表仍然保持有序 struct Student nbsp nbsp nbsp nbsp nbsp nbsp nbsp long number nbsp nbsp nbsp nbsp nbsp nbsp nbsp float score nbsp nbsp nbsp nbsp nbsp nbsp nbsp Student next Student Insert Student



阅读以下程序并填空(填上正确的语法成分),使其成为完整的程序

假设原链表的结点是按学号(number)由小到大排列的。在已建立的学生链表中插入一新结点,使插入新结点后的链表仍然保持有序。

struct Student

{

        long number;

        float score;

        Student * next;

};

Student * Insert(Student * head, Student * stud)  //插入链表结点

{

        if(    (1)     )    //原链表为空链表

        {

                head=stud;

                stud->next=NULL;

                return(head);

        }

        if(               (2)               )  //结点的插入位置在链首

        {

                stud->next=head;

                head=stud;

                return(head);

        }

        //查找插入位置

        Student * pGuard=head;

while(                          (3)                          )

                pGuard=pGuard->next;

        //插入结点

                         (4)                

                         (5)                

        return(head);

}

 编程题 

1、定义findmax()函数,其功能是寻找数组中的最大元素,将该元素的下标通过参数返回,并返回其地址值,编程实现findmax()函数,并在主函数中输出最大元素的值、下标及其地址。int* findmax(int* array, int size, int* index);

2、设计并测试一个描述学生基本信息的类(Student)。

(1)设计一个学生基本信息的类(Student)包含有数据成员:学号(由系统自动生成)、姓名、性别、家庭地址、电话;包含有如下主要成员函数:

  • 构造函数(用来初始一个学生的基本信息)
  • 以非成员函数的形式重载“<<”使之能显示学生信息
  • 修改学生家庭地址
  • 析构函数

(2)请在主函数中使用所设计的学生类定义学生对象,并输出其基本信息。

阅读以下程序并填空(填上正确的语法成分),使其成为完整的程序 

(1)     head==NULL              

(2)     head->number > stud->number              

(3)     pGuard->next && pGuard->next->number < stud->number        

(4)     stud->next=pGuard->next;              

(5)     pGuard->next=stud;              

 编程题 

1、

#include <iostream>

using namespace std;

int* findmax(int* array, int size, int* index);

int main()

{    int a[10]={33,91,54,67,82,37,85,63,19,68};

      int* maxaddr;

      int idx;

      maxaddr=findmax(a, sizeof(a)/sizeof(*a), &idx);

      cout <<"the index of maximum element is " <<idx <<endl

         <<"the address of it is " <<maxaddr <<endl

         <<"the value of it is " <<a[idx] <<endl;

      return 1; 

}

int* findmax(int* array, int size, int* index)

{    *index=0;

      for(int i=0; i<size; i++)

                    if(array[i]>array[*index])

                  *index=i;

      return &array[*index];           

}

2、

#include <iostream.h>

#include <string.h>

class Student

{

public:

        Student(char *,char*,char,char*,char*);

        void changeAdd(char*);

        ~Student();

        friend ostream & operator <<(ostream & o,const Student &s);

private:

        char number[10];

        char name[10];

        char sex;

        char address[40];

        char telno[10];

};

Student::Student(char *no,char*na,char x,char*add,char*tel)

{

        cout<<"Constructing ..."<<endl;

        strncpy(number,no,sizeof(number));

        number[sizeof(number)-1]='0';

        strncpy(name,na,sizeof(name));

        name[sizeof(name)-1]='0';

        sex=x;

        strncpy(address,add,sizeof(address));

        number[sizeof(address)-1]='0';

        strncpy(telno,tel,sizeof(telno));

        number[sizeof(telno)-1]='0';

cout<<number<<","<<name<<","<<sex<<","<<address<<","<<telno<<endl;

}

void Student::changeAdd(char* newadd)

{

        cout<<"modify..."<<endl;

        strncpy(address,newadd,sizeof(address));

        number[sizeof(address)-1]='0';

}

Student::~Student()

{

        cout<<"Destructing ..."<<endl;

}

ostream & operator <<(ostream & o, const Student &s)

{

        return o<<s.number<<","<<s.name<<","<<s.sex<<","<<s.address<<","<<s.telno<<endl;

}

int main()

{

        Student p("2002008","Frank",'M',"zhangshan road","2300088");

        p.changeAdd("ci tong road");

        cout<<p<<endl;

        return 1;

}

编程小号
上一篇 2025-03-16 07:51
下一篇 2025-01-27 09:21

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/bian-cheng-ri-ji/45648.html