链表结构<1>

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
// 创建新学生节点
Student*creat_student(int xuehao,char *name,float score){
    Student*new_student=(Student*)malloc(sizeof(Student));
    new_student->xuehao=xuehao;
    strcpy(new_student->name,name);
    new_student->score=score;
    new_student->next=NULL;
    return new_student;
}
    strcpy(new_student->name, name);
    new_student->score = score;
    new_student->next = NULL;
    return new_student;
}
// 向链表中添加学生
void add_student(Student **head, int xuehao, char *name, float score) {
    Student *new_student = create_student(xuehao, name, score);
    new_student->next = *head;
    *head = new_student;
}
// 从链表中删除学生
void delete_student(Student **head, int xuehao) {
    Student *temp = *head, *prev;
    if (temp != NULL && temp->xuehao == xuehao) {
        *head = temp->next;
        free(temp);
        return;
    }
    while (temp != NULL && temp->xuehao != xuehao) {
        prev = temp;
        temp = temp->next;
    }
    if (temp == NULL) return;
    prev->next = temp->next;
    free(temp);
}
// 在链表中查找学生
Student *find_student(Student *head, int xuehao) {
    Student *current = head;
    while (current != NULL) {
        if (current->xuehao == xuehao) {
            return current;
        }
        current = current->next;
    }
    return NULL;
}
// 显示所有学生信息
void display_students(Student *head) {
    Student *current = head;
    while (current != NULL) {
        printf("学号: %d, 名字: %s, 成绩: %.2f", current->xuehao, current->name, current->score);
        current = current->next;
    }
}
int main() {
    Student *head = NULL;
    // 添加学生信息
    add_student(&head, 1, "张三", 90.0);
    add_student(&head, 2, "李四", 80.0);
    add_student(&head, 3, "王五", 85.0);
    // 显示学生信息
    display_students(head);
    // 删除学生信息
    delete_student(&head, 2);
    // 查找学生信息
    Student *found_student = find_student(head, 1);
    if (found_student != NULL) 

{
        printf("找到学生: 学号: %d, 名字: %s, 成绩: %.2f", found_student->xuehao, found_student->name, found_student->score);
    }

else

{
        printf("未找到学生");
    }

    return 0;
}

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
改进以下代码#include<iostream> #include<string.h> #include<stdio.h> using namespace std; //链表类的前向声明 template<class T> class list; template<class T> //声明模板 class node { //定义结构模板0-[-[ T val; //val取任意类型,即模板参数类型 node<T>* next; //此处node为结构模板 public: node(){ next = NULL; } node(T a) { val = a; next = NULL; } friend class list<T>; }; //请完成链表类的设计 template<class T> class list { public: list(){} void insert(T t) { if(pFirst==NULL) { pFirst=new node(t); pTail=pFirst; } else { node<T> *p=new node(t); pTail->next=p; pTail=p; } } void print() { for(node<T> *p=pFirst;p;p=p->next) cout<<p->val; } private: static node<T> pFirst; static node<T> pTail; }; template <class T> node<T> list<T>::pFirst=NULL; template <class T> node<T> list<T>::pTail=NULL; int main() { int kind = 0; // 0:表示整型,1:单精度浮点数, 2:字符串 int cnt = 0; cin >> kind >> cnt; //整数链表 if (kind == 0) { list<int> intlist; int nTemp = 0; for (int i = 0; i < cnt; i++) { cin >> nTemp; intlist.insert(nTemp); } intlist.print(); } //浮点数链表 else if (kind == 1){ list<float> floatlist; float fTemp = 0; cout.setf(ios::fixed); cout.precision(1); for (int i = 0; i < cnt; i++) { cin >> fTemp; floatlist.insert(fTemp); } floatlist.print(); } //字符串链表 else if (kind == 2){ list<string> charlist; char temp[100] ; for (int i = 0; i < cnt; i++){ cin >> temp; charlist.insert(temp); } charlist.print(); } else cout << "error"; return 0; }
05-25

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值