链表查找学生信息
#include<iostream>
#include<string.h>
using namespace std;
#include <stdio.h>
typedef struct {
char no[8];
char name[20];
int price;
}Student;
typedef struct LNode{
Student data;
struct LNode *next;
}LNode,*LinkList;
void InitList(LinkList &L)
{
L = new LNode;
L->next = NULL;
}
void ListInput(LinkList &L, int n)
{
int i=1;
LinkList p, r;
r = L;
while (i<=n) {
cout<<"请输入第"<<i<<"个学生的信息(按照学号、姓名、成绩的顺序)"<<endl;
p = new LNode;
cin>>p->data.no>>p->data.name>>p->data.price;
p->next = NULL;
r->next = p;
r = p;
i++;
}
}
void ListOutput(LinkList L)
{
LinkList p;
p = L->next;
cout<<"学生信息如下;"<<endl;
while (p != NULL) {
cout << p->data.no << " "<<p->data.name<<" "<<p->data.price<<endl;
p = p->next;
}
cout << endl;
}
bool LocateElem(LinkList L,Student &s)
{
LinkList p;
p = L->next;
while (p != NULL) {
if (strcmp(p->data.name,s.name)==0){
strcpy(s.no,p->data.no);
s.price=p->data.price;
return true;
}
p = p->next;
}
return false;
}
bool GetElem(LinkList L,int i, Student &s)
{
LinkList p;
p = L->next;
int j=1;
while (p &&j<i) {
p = p->next;
j++;
}
if(!p||j>i)
return false;
strcpy(s.no,p->data.no);
strcpy(s.name,p->data.name);
s.price=p->data.price;
return true;
}
bool ListInsert(LinkList &L, int i, Student s)
{
LinkList p,r;
r=L;
int j=0;
while(r&&j<i-1){
r=r->next;
j++;
}
if(!r||j>i-1)
return false;
p = new LNode;
p->data = s;
p->next = r->next;
r->next = p;
return true;
}
bool ListDelete_L(LinkList &L, int i) {
LinkList p, q;
int j;
p = L;
j = 0;
while ((p->next) && (j < i - 1))
{
p = p->next;
++j;
}
if (!(p->next) || (j > i - 1))
return false;
q = p->next;
p->next = q->next;
delete q;
return true;
}
int ListLength(LinkList &L){
int i=0;
LinkList p=L->next;
while(p){
i++;
p=p->next;
}
return i;
}
void start(){
cout<<"\t\t\t\t\t*****请选择您需要的操作******"<<endl<<endl;
cout<<"\t\t\t\t\t*****输入学生信息,请按1******"<<endl<<endl;
cout<<"\t\t\t\t\t*****显示信息,请按2******"<<endl<<endl;
cout<<"\t\t\t\t\t*****按姓名查找学生信息,请按3******"<<endl<<endl;
cout<<"\t\t\t\t\t*****按表中指定位置查找信息,请按4******"<<endl<<endl;
cout<<"\t\t\t\t\t*****插入学生信息,请按5******"<<endl<<endl;
cout<<"\t\t\t\t\t*****删除指定学生信息,请按6******"<<endl<<endl;
cout<<"\t\t\t\t\t*****显示表中学生个数,请按******7"<<endl<<endl;
cout<<"\t\t\t\t\t*****结束,请按0******"<<endl<<endl;
}
int main(){
LinkList L;
InitList(L);
int n,i,j,z,k;
while(1){
start();
cin>>k;
if(k==0)
break;
else if(k>=1&&k<=7){
switch(k){
case 1: cout<<"请输入学生个数"<<endl;
cin>>n;
ListInput(L,n);
case 2:ListOutput(L);
cout<<"当前学生个数为:"<<ListLength(L)<<endl;
case 3:cout<<"请输入要查找的学生姓名"<<endl;
Student s1,s2,s3;
cin>>s1.name;
if(LocateElem(L,s1)){
cout<<s1.no<<" "<<s1.price<<endl;
}else{
cout<<"未找到此学生!"<<endl;
}
case 4: cout<<"请输入要查找的结点位置"<<endl;
cin>>i;
if(GetElem(L,i,s2)){
cout<<s2.no<<" "<<s2.name<<" "<<s2.price<<endl;
}else{
cout<<"输入的位置不合法!"<<endl;
}
case 5: cout<<"请输入要插入的学生信息(按照学号、姓名、成绩的顺序)"<<endl;
cin>>s3.no>>s3.name>>s3.price;
cout<<"请输入插入位置"<<endl;
cin>>j;
if(ListInsert(L,j,s3)){
cout<<"插入成功!"<<endl;
ListOutput(L);
}else{
cout<<"插入位置不合法!"<<endl;
}
cout<<"当前学生个数为:"<<ListLength(L)<<endl;
case 6:cout<<"请输入要删除学生的位置"<<endl;
cin>>z;
if(ListDelete_L(L,z)) {
cout<<"删除成功!"<<endl;
ListOutput(L);
}else{
cout<<"删除位置不合法!"<<endl;
}
case 7: cout<<"当前学生个数为:"<<ListLength(L)<<endl;
return 0;
}
}
else if(k>7||k<0){
cout<<"\t\t\t*****没有该选项!"<<endl<<endl;
}
}
}