링크 연습 (데이터 구조 선형 표 test 2)

// 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);
}

좋은 웹페이지 즐겨찾기