// StudentTest2.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;
struct Student
{
int num;
float score;
Student *next; //next指向Student结构体变量
};
Student *Creat_front(); //按头插法创建链表,以-1 -1作为输入结束标志
void Output(Student *head); //输出链表全部元素
Student *Creat_rear(); //按尾插法创建链表,以-1 -1作为输入结束标志
Student *Locate(Student *head,int num); //给定学号,查找元素位置
void LinkInsert(Student *head, Student *p); //向p指向的元素后插入一个新输入的元素
Student *LinkDelete(Student *head, int num); //删除学号为num的元素
void Menu(); //简单的显示菜单
//定义一个全局变量记录链表的长度
int length;
void main()
{
int choose,num;
Student *head,*p;
Menu(); //显示菜单选项
cin>>choose;
while(choose!=0)
{
switch(choose)
{
case 1: //头插法创建链表,与2不能同时使用
head=Creat_front();
break;
case 2: //尾插法创建链表
head=Creat_rear();
break;
case 3: //链表输出
Output(head);
break;
case 4: //查找元素
cout<<"输入待查找NUM: ";
cin>>num;
p=Locate(head,num);
if(p==NULL)
cout<<"没有NUM为: "<<num<<"的同学信息"<<endl;
else
cout<<"NUM为: "<<num<<"的同学成绩为: "<<p->score<<endl;
break;
case 5: //插入元素
LinkInsert(head,p); //在查找到的元素后,插入一个新元素
break;
case 6: //删除元素
cout<<"输入要删除的NUM: ";
cin>>num;
head=LinkDelete(head,num);
break;
}
Menu();
cin>>choose;
}
}
Student *Creat_rear(){
Student *p1,*p2,*header;
p1=p2=new Student;
cout<<"请输入学号和姓名:"<<endl;
cin>>p1->num>>p1->score;
header=NULL;
header=p1;
p2=p1;
while(p1->num!=-1&&p1->score!=-1)
{
length++;
p1->next=p2;
p2=p1;
p1=new Student;
cout<<"请输入学号和姓名:"<<endl;
cin>>p1->num>>p1->score;
}
header->next=NULL;
return(p2);
}
Student *Creat_front(){//定义tou插法
Student *p1,*p2,*header;
p1=p2=new Student;
cout<<"请输入学号和姓名:"<<endl;
cin>>p1->num>>p1->score;
header=NULL;
header=p1;
while(p1->num!=-1&&p1->score!=-1)
{
length++;
p2=p1;
p1=new Student;
p2->next=p1;
cout<<"请输入学号和姓名:"<<endl;
cin>>p1->num>>p1->score;
}
p1=NULL;
p2->next=NULL;
return header;
}
//定义输出函数
void Output(Student *head){
Student *p1;
p1=head;
if(head!=NULL){
for(;;){
cout<<p1->num<<endl;
cout<<p1->score<<endl;
if(p1->next!=NULL){
p1=p1->next;
}else{
break;
}
}
}
}
//定义查找函数
Student *Locate(Student *head,int num){
Student *p1;
p1=head;
do{
if(p1->num!=num){
p1=p1->next;
}else if(p1->num==num){
return p1;
}
}while(p1->next!=NULL);
return NULL;
}
//定义函数
void LinkInsert(Student *head, Student *p)
{
Student *p0,*p1,*p2,*p3;
p3=new Student();
cout<<"请输入你要插入学生的学号和成绩:"<<endl;
cin>>p3->num>>p3->score;
p1=head;
p0=p;
if(head==NULL)
{head=p0;p0->next=NULL;}
else{
while((p0->num!=p1->num)&&(p1->next!=NULL))
{
p1=p1->next;
p2=p1->next;
}
if(p0->num==p1->num&&p0->score==p1->score)
{
p2=p1->next;
p1->next=p3;
p3->next=p2;
length=length+1;
}
else
{
cout<<"找不到匹配项"<<endl;
}
}
}
void Menu()
{
cout<<"-----------菜单----------"<<endl;
cout<<" 1. 头插法创建链表"<<endl;
cout<<" 2. 尾插法创建链表"<<endl;
cout<<" 3. 链表输出"<<endl;
cout<<" 4. 查找元素"<<endl;
cout<<" 5. 插入元素"<<endl;
cout<<" 6. 删除元素"<<endl;
cout<<" 0. 退出"<<endl;
cout<<"-------------------------"<<endl;
cout<<"请选择: ";
}
//定义删除函数
Student *LinkDelete(Student *head, int num){
Student *p1,*p2;
if(head==NULL)
{
cout<<"这是一个空表."<<endl;
return(head);
}
p1=head;
while(num!=p1->num&&p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(num==p1->num)
{
if(p1==head)
head=p1->next;
else
p2->next=p1->next;
delete p1;
cout<<"删除学号为:"<<num<<"成功"<<endl;
length=length-1;
}
else cout<<"找不到学号"<<endl;
return(head);
}
转载于:https://www.cnblogs.com/lixingle/archive/2012/09/16/3313021.html