#include <iostream>
#include <string.h>
using namespace std;
// Definition of the data element
struct NBAplayer{
char name[20];
int height;
int age;
int Threept;
int dunk;
int block;
int overall;
};
//Node
template <class T>
struct Node{
T data; // data field
Node<T> *next; //A pointer which points to a node
};
// class declaration
template <class T>
class LinkedList{
public:
LinkedList (); // None-parameter constructor
LinkedList (T a[], int n); //Constructor.
~LinkedList () {} //Destructor.
int Length (); //Return the length of the list
void Get(int i); //Search by position
int Locate (T x); //Search by player
void Insert (int i, T x); //insert player x to the position i
void Delete (int i); //delete the ith player
void PrintList (); //output
private:
Node<T> *first;
};
// Following are the functions of the class
template <class T>
LinkedList<T>::LinkedList (){ //implementation of none-parameter constructor
first=new Node<T>;
first->next=NULL;
}
template <class T>
LinkedList<T>:: LinkedList (T a[], int n){ //implementation of constructor
int i;
Node<T> *s;
first=new Node<T>;
//first->next=NULL;
for(i=0;i<n;i++){
s=new node<T>;
s->data=https://bbs.csdn.net/topics/a[i];
s->next=first->next;
first->next=s;
}
}
//
template <class T>
int LinkedList<T>::Length(){ //implementation of Length()
S=first->next;int count=0;
while(p!NULL)
{
p=p->next;
count++;
}
return count;
/*
return length;
*/
}
template <class T>
void LinkedList<T>::Get(int i) { //implementation of Get(), search by position
S=first->next;int count=1;
while (S!NULL&&count<i)
{
S=S->next;
count++;
}
if(S==NULL)throw "weizhifeifa";
else
cout << i << ' ' << S->data[i - 1].name << ' ' <<S->data[i - 1].height << ' ' <<S->data[i - 1].age
<<S->data[i - 1].Three_pt <<S->data[i - 1].dunk <<S->data[i - 1].block <<S->data[i - 1].overall<<endl;
/*
if (i < 1 && i > length) throw "查找位置非法";
// "cout" means print
cout << i << ' ' << S->data[i - 1].name << ' ' <<S->data[i - 1].height << ' ' <<S->data[i - 1].age
<<S->data[i - 1].Three_pt <<S->data[i - 1].dunk <<S->data[i - 1].block <<S->data[i - 1].overall<<endl;
//output the details of the player. "endl" means end line
*/
}
template <class T>
int LinkedList<T>::Locate (NBAplayer x){ //implementation of Locate(), search by position
S=first->next;int count =1;
while(S!NULL){
if (strcmp(S->data[i].name, x.name)==0) {
//"strcmp" is means "string compare". If two strings are the same, the function returns 0;
return i+1;
}
}
return 0;
}
template <class T>
void LinkedList<T>::Insert (int i, NBAplayer x) { //implementation of Insert()
S=first;int count=0;
while(S!NULL&&count<i-1)
{
S=S->next;
count++;
}
if(S==NULL) threw "weizhifeifa";
else{
p=new Node;p->DATE=x;
p->next=S->next;
S->next=p;
}
}
template <class T>
NBAplayer LinkedList<T>::Delete(int i) { //implementation of Delete()
S=first;int count=0;
while(S!NULL&&count<i-1)
{
S=S->next;
count++;
}
if(S==NULL||S->next==NULL)
throw "weizhifeifa";
else
p=s->next;x=p->data;
S->next=p->next;
delete p;
// return x;
}
template <class T>
void LinkedList<T>::PrintList() { //implementation of PrintList()
S=first->next;
while(S!NULL){
cout << i << ' ' <<S->data[i - 1].name << ' ' <<S->data[i - 1].height << ' ' <<S->data[i - 1].age
<< ' '<<S->data[i - 1].Threept << ' '<<S->data[i - 1].dunk<< ' ' <<S->data[i - 1].block << ' '
<<S->data[i - 1].overall<< endl;
S=S->next;
}
}
// Above is the complete class
// main function
int main() {
NBAplayer X;
int i,j;
// First, we define 3 players, and put them in an array
NBAplayer player[3] = {
{"James", 2.03, 31,60,99,90,95},
{"Harden", 1.96, 26,80,89,75,90},
{"Curry", 1.90, 27,90,70,60,93}
};
// Then call the constructor "SeqList (NBAplayer a[], int n)" to make a list
LinkedList<NBAplayer> mylist(player, 3);
// Generate a simple menu on the screen using a loop
while (true){
// a simple menu
cout << " ##############################" << endl;
cout << " 1 . search by position ;" << endl;
cout << " 2 . search by name ;" << endl;
cout << " 3 . insert ;" << endl;
cout << " 4 . delete ;" << endl;
cout << " 5 . printlist ;" << endl;
cout << " 6 . length ;" << endl;
cout << " 7 . exit ;" << endl;
cout << " ###############################" << endl;
cout << " please input 1~7 : ";
int choice = 1;
cin >> choice;// "cin" reads a data from the keyboard and assign to the variable "choice"
// Using "switch" to behave differently according to user choice
switch (choice){
case 1 : {
cout << " 1 .search by position" << endl;
cout << " input position please :";
cin >> j;
mylist.Get(j);
break;
}
case 2 : {
cout << " 2 . search by name" << endl;
cout << " please input player name: ";
cin >> X.name;
cout<<mylist.Locate(X)<<endl;
break;
}
case 3 : {
cout << " 3 . insert" << endl;
cout << "which place to insert:";
cin >> i;
cout << " please input player name, height, age, three_pt, dunk, block, and overall:";
cin >> X.name >> X.height >> X.age>> X.Threept >> X.dunk >> X.block>> X.overall;
mylist.Insert(i, X);
break;
}
case 4 :{
cout << " 4 . delete" << endl;
cout << " which player to be deleted : ";
int d;
cin >> d;
mylist.Delete(d);
cout <<"You have deleted"<< ' '<< X.name << ' '<< X.height << ' '<< X.age<< ' '<< X.Threept<< ' ' << X.dunk << ' '<< X.block<< ' '<< X.overall<<endl;
break;
}
case 5 : {
cout << " 5 . printlist" << endl;
mylist.PrintList();
break;
}
case 6 : {
cout << " 6 . length " << endl;
cout<<mylist.Length()<<endl;
break;
}
case 7 : {
cout << " 7 . exit " << endl;
return 0;
}break;
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/91552.html
標籤:基礎類
