链表查找学生信息

链表查找学生信息

#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; 
			}
	}
}
	
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值