1、创建,数据来自文件
2、显示
3、查找(显示比较次数)
4、插入
5、删除(显示比较次数)
6、将链接存储线性表逆置,即最后一个结点变成第1个结点,原来倒数第2个结点变成第2个结点,如此等等。
[解题思路] 从头到尾扫描单链表L,将第1个结点的next域置为NULL,将第2个结点的next域指向第1个结点,将第3个结点的next域指向第2个结点,如此等等,直到最后一个结点,便用head指向它。
提高题:
7*、将一个首结点指针为a的单链表A分解成两个单链表A和B,其首结点指针分别为a和b,使得链表A中含有原链表A中序号为奇数的元素,而链表B中含有原链表A中序号为偶数的元素,且保持原来的相对顺序。
[解题] 将单链表A中序号为偶数的元素删除,并在删除时把这些结点链接起来构成单链表B即可。
/************ COPYRIGHT(C) 2017 DongfangHU *****/ /*****实验名称:链表的基本操作 /**** 实验平台:Dec-c++ /*****作者:DongfangHU /***** /*****时间:2017-07-11 /************************************************/ #include<stdio.h> #include<iostream> #include<stdlib.h> #include<windows.h> using namespace std; typedef struct LIST { int num; LIST*next; }List; /*****************/ List*Creat(List*head); void Visit(List*head); void Search(List*head,int num); List*Insert(List*head,int num,int position); List*Delete(List*head,int num); List*Nizhi(List*head); List*Split(List*head); void color(short x); /******************/ //int n=0; int G=1; int main() { List*head=NULL; head= new List; head->num=-1; head->next=NULL; while(1) { cout<<endl<<endl<<endl; color(8); cout<<" *********************************************************************"<<endl; color(14); cout<<" 1.载入数据"<<endl; cout<<" 2.显示数据"<<endl; cout<<" 3.查找数据"<<endl; cout<<" 4.插入数据"<<endl; cout<<" 5.删除数据"<<endl; cout<<" 6.逆置数据"<<endl; cout<<" 7.拆分链表"<<endl; color(4); cout<<endl<<" 0.退出"<<endl; color(7); int choice; cout<<"Enter the choice:"<<endl; cin>>choice; system("CLS"); switch(choice) { case 1:head=Creat(head);break; case 2:Visit(head);break; case 3: { int num; cout<<"enter the data you search:"<<endl; cin>>num; system("CLS"); Search(head,num); break; } case 4: { int num,position; cout<<"enter the data and its position to insert:"<<endl; cin>>num>>position; head=Insert(head,num,position); break; } case 5: { int num; cout<<"enter the data you wanna delete:"<<endl; cin>>num; head=Delete(head,num);break; } case 6: { Nizhi(head); break; } case 7: head=Split(head);//Part(head); break; case 0:exit(0); default:cout<<"Invalid!"<<endl; } } return 0; } List*Creat(List*head) { head->next=NULL; int num,n; List *p,*tail; p=tail=NULL; FILE *fp; if((fp=fopen("C:\\Users\\Administrator\\Desktop\\abc.txt","r"))==NULL)//此处需按需要修改即可 { cout<<"File open error!"<<endl; exit(0); } cout<<"Please enter the number of date(0-15):"; cin>>n; if(n<0||n>15) { cout<<"The number is out of range!"<<endl; return head; } head->num=n; for(int i=0;i<n;i++) { p=new List; p->next=NULL; fscanf(fp,"%d",&num); p->num=num; if(head->next==NULL) head->next=p; else tail->next=p; tail=p; } fclose(fp); cout<<"Enter successfully!"<<endl; return head; } void Visit(List*head) { List*temp=NULL; if(head->next==NULL) { cout<<"NO date!"<<endl; return; } for(temp=head->next;temp!=NULL;temp=temp->next) cout<<temp->num<<' '; cout<<endl<<"All date has been display!"<<endl; } void Search(List*head,int num) { List*temp=NULL; int B=0,i=1; for(temp=head->next;temp!=NULL;temp=temp->next,i++) { B++; if(temp->num==num) { cout<<"The location of the data is "<<i<<endl; cout<<"The number of comparisons was "<<B<<endl; return; } } cout<<"The data was not found!"<<endl; } List*Insert(List*head,int num,int position) { List*temp=head->next,*temp2=NULL; List*p=new List; p->num=num; p->next=NULL; if(position==1) { p->next=head->next; head->next=p; head->num++; //n++; if(G) cout<<"Insert successfully!"<<endl; return head; } if(position<1||position>head->num+1) { cout<<"The position is out of range!"<<endl; return head; } int i; for(i=1;i<position;i++) { temp2=temp; temp=temp->next; } p->next=temp; temp2->next=p; head->num++; //n++; if(G) cout<<"Insert successfully!"<<endl; return head; } List*Delete(List*head,int num) { int B=0; List*temp=head->next,*temp2=NULL; if(temp==NULL) { cout<<"No data!"<<endl; return head; } else if(temp->num==num) { B++; head->next=temp->next; delete temp; head->num--; //n--; if(G) { cout<<"Delete successfully!"<<endl; cout<<"The number of comparisons was "<<B<<endl; } return head; } B++; while(temp!=NULL) { if(temp->num==num) { temp2->next=temp->next; delete temp; head->num--; // n--; if(G) { cout<<"Delete successfully!"<<endl; cout<<"The number of comparisons was "<<B<<endl; } return head; } B++; temp2=temp; temp=temp->next; } if(G) cout<<"No data!"<<endl; return head; } List*Nizhi(List*head) { List*temp1=NULL,*temp2=head->next,*temp3=NULL; if((head->next==NULL)||(head->next->next==NULL)) { cout<<"Counter completion!"<<endl; return head; } temp1=head->next; temp2=temp1->next; temp3=NULL; temp1->next=NULL; while(temp2!=NULL) { temp3=temp2->next; temp2->next=temp1; temp1=temp2; temp2=temp3; } head->next=temp1; cout<<"Counter completion!"<<endl; return head; } List*Split(List*head) { G=0; List *newhead=NULL; newhead=new List; newhead->num=1; newhead->next=NULL; cout<<"The data in orginal is :"<<endl; Visit(head); int i=1; int m=1; List*temp=head->next; List*temp2=temp; while(temp!=NULL) { if(i%2==1) temp=temp->next; else { newhead=Insert(newhead,temp->num,m); m++; int k=temp->num; temp=temp->next; head=Delete(head,k); } i++; } cout<<"Split successfully!"<<endl; cout<<"The data in the odd numbered serial lists is:"<<endl; Visit(head); cout<<"The data in the even numbered serial lists is"<<endl; Visit(newhead); G=1; return head; } void color(short x) //自定义函根据参数改变颜色 { if(x>=0 && x<=15)//参数在0-15的范围颜色 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), x); //只有一个参数,改变字体颜色 else//默认的颜色白色 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7); } /************************************************************/
