#include<iostream> #include<windows.h> #include<string> /* (1) 初始化单链表h; (2) 依次插入5个元素:{“张三” , 85}, {“李四” , 95}, {“王五” , 75}, {“陈军” , 80}, {“程涛" , 90} (3) 输出单链表h的内容; (4) 输出单链表的长度; (5) 输出单链表h的第3个元素; (6) 输入一个姓名(如:陈军),在单链表中查找该元素,输出该元素的编号; (7) 删除第4个元素,并输出被删除元素的内容; (8) 输出单链表h的内容; (9) 释放单链表h。 注意: 每个过程要显示出各种提示信息。如:要求输出单链表的长度,则要显示:当前单链表的长度为:5. */ using namespace std ; struct Student{ string name; int score; } ; typedef Student ElemType ; struct LinkList{ ElemType data ; LinkList * link ; LinkList( LinkList *ptr = NULL ){ link = ptr ;} } ; //重载写这里 istream &operator>>(istream &is,ElemType &c){ is >> c.name>>c.score ; return is ; } ostream &operator<<(ostream &os,ElemType &c){ os << c.name<< c.score ; return os ; } void print(LinkList *first){ LinkList *cur = first ; cur = cur -> link; while (cur != NULL ){ cout <<cur->data <<endl ; cur = cur ->link ; } }//代码有误 //出现的问题在于,first的link是空的,没有接着连接下去 void printlen(LinkList *first){ int length = 0 ; if(first->link == NULL) { cout <<"fuckashole"<<endl ; } while (first != NULL){//cout<<cur->dat<<endl; length++ ; first =first->link ; } cout <<"链表的长度为 : "<< length-1 <<endl; } LinkList *Locate (LinkList *first ,int w_place ) { // 定位函数,插入与删除时用来确定某一位置 int k = 0 ; LinkList *cur = first ; while( k != w_place ) { cur = cur->link ; k++; } return cur ; } void LocPri (LinkList *&first ) { cout <<"请输入您想输出的位置 : "; int lp_number ; cin >> lp_number ; Locate (first ,lp_number ) ; cout <<Locate (first ,lp_number ) ->data <<endl; } void create(LinkList *&first ,int n){ //n就是第一个结点的 cout<<"请输入对象: "<<endl; ElemType n_data ; cin >> n_data ; //first->link = NULL ; //first ->data = n_data ;//这边需要用到重载 LinkList *cur = new LinkList ; cur->link =first ->link ; first->link = cur ; cur->data = n_data ; LinkList *f = new LinkList ; f = cur; for(int i=0 ; i< n - 1 ; i++){ cout<<"请输入对象: "<<endl; ElemType n_data ; cin >> n_data ; //first->link = NULL ; //first ->data = n_data ;//这边需要用到重载 LinkList *cur = new LinkList ; cur->link =f ->link ; f->link = cur ; cur->data = n_data ; f = cur ; //the wrong part // first = cur ; } } /*void Insert (LinkList *first){ //cout<<"请输入您想插入的位置 :" ; //int place ; //cin >> place ; LinkList *cur = Locate(first,place) ; LinkList *newNode = new LinkList ; cout<<"请输入您想插入的元素的值 :"; ElemType data ; newNode ->link =NULL ; cin >> data.name>>data.score ;//还是需要用到重载 newNode->data = data ; newNode->link = cur->link ; cur->link = newNode ; }*/ void Delete(LinkList *first){ cout<<"请输入您想删除的位置 : " ; int place ; cin >> place ; LinkList *cur =Locate (first,place-1 ) ; cur->link =cur->link->link ; } void Search (LinkList *first, ElemType test) { //cout << "请输入您想查找的元素名字 :" ; //ElemType test ; //cin >> test ; int flag = 0 ; int k = 0 ; // if(first->link == NULL) cout <<"Search 的link为空"<<endl; while (first != NULL) { if(first->data.name == test.name ) { //cout <<" 找到该元素 "<<endl ; flag = 1 ; k++ ; //continue ; break ; } else { first = first->link ; k++ ; //Search (first , test ) ; } } if(flag == 0 ) { cout <<" 没有找到该元素 "<<endl ; } if(flag == 1) { // cout <<"找到该元素 "<<endl ; cout <<"所找的元素位置是 : "<< k-1<<endl; } } int main () { //cout<<"请输入想插入的元素个数 :" ; /* int w_number ; cin >>w_number ; for(int i =0 ;i < w_number ; i++) { //插入每一个元素 } */ cout <<"请输入您想创建的对象个数 :" ; int w_number ; cin >>w_number ; LinkList *test = new LinkList ; test -> link = NULL; //新建的头为空 //cout<<"请输入对象: "<<endl; //ElemType t_data ; //cin>>t_data ; //test ->data = t_data ; create (test,w_number ) ; cout <<endl; cout <<endl; print(test) ; cout << endl; printlen(test) ; //查找部分 cout <<endl ; LocPri(test) ; cout << "请输入您想查找的元素名字 :" ; ElemType tes ; cin >> tes.name ; cout <<endl; Search (test ,tes ) ; cout <<endl; Delete(test) ; print (test) ; cout <<endl ; print (test) ; system("pause") ; return 0 ; }
时间: 2024-11-05 18:55:25