利用单链表实现学生信息管理
// Exe3.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream.h>
#include <string.h>
struct Student
{
char id[10];
char name[10];
char sex[10];
char grade[10];
char tel[20];
int age;
Student *next;
};
class SeqList
{public :
Student *head;
Student *current,*previous;
int length;
SeqList(){
head= new Student;
head->next=NULL;
length=0;
}
bool IsListEmpty(){
return length==0;
}
void ListDelete(char ID[]){
previous=head;
current=head->next;
for(int i=0;i<length&¤t!=NULL;i++){
//如果当前数据的id等于用户输入要删除数据的id,进行删除操作。
if(strcmp(current->id,ID)==0){
previous->next=current->next;
length=length-1;
return;
}
previous=current;
current=current->next;
}
cout<<"ID="<<ID<<"的学生没有到,可能是不存在!";
}
void ListInsert(Student x){
Student *tmp=new Student;
*tmp=x;
tmp->next=head->next;
head->next=tmp;
length++;
}
void ShowAll(){
current=head->next;
for(int i=0;i<length;i++){
cout<<"ID="<<current->id<<"\tNAME="<<current->name<<"\tSEX="<<current->sex<<"\tGRADE="<<current->grade<<"\tTEL="<<current->tel<<"\tAGE="<<current->age<<endl;
current=current->next;
}c语言listinsert函数
}
};
int main(int argc, char* argv[])
{
printf("Hello World!\n");
SeqList GIS2010;
int x=1;
while(x!=0){
printf("请输入以下选项:\n");
printf("1:信息浏览\n");
printf("2:插入信息\n");
printf("3:删除信息\n");
printf("0:退出程序\n");
cin>>x;
switch(x){
case 1:
GIS2010.ShowAll();
break;
case 2:
Student newStudent;
cout<<"ID="<<endl;
cin>>newStudent.id;
cout<<"NAME="<<endl;
cin>>newStudent.name;
cout<<"SEX="<<endl;
cin>>newStudent.sex;
cout<<"GRADE="<<endl;
cin>&ade;
cout<<"TEL="<<endl;
cin>&l;
cout<<"AGE="<<endl;
cin>>newStudent.age;
=NULL;
GIS2010.ListInsert(newStudent);
break;
case 3:
char delID[10];
cout<<"请输入想要删除数据项的ID号码"<<endl;
cin>>delID;
GIS2010.ListDelete(delID);
break;
case 0:
break;
}
};
return 0;
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论