C++순서 표 의 일반적인 동작 구현(출력 삭제 삽입)

3389 단어 c순서 표
순서 표 의 삽입,삭제,찾기,출력 작업 은 C 언어 에서 자주 사용 된다.다음은 실현 코드 를 정리 해 드 리 겠 습 니 다.같이 보 시 죠.
코드 는 다음 과 같다.

#include<iostream>
using namespace std;
#define MAXSIZE 15
typedef int DataType;
typedef struct
{
DataType data[MAXSIZE]; //                  
int SeqLength; /*     */
} SeqList;
SeqList *Init_SeqList(); //      
void Define_SeqList(SeqList *L,int n); //        
void Display_SeqList(SeqList *L); //         
int Insert_SeqList(SeqList *L,int i,DataType x); //         (   )
int Delete_SeqList(SeqList *L,int i); //         (   )
【Sequence.cpp】
#include "Sequence.h"
#include<iostream>
using namespace std;
SeqList *Init_SeqList()//         ,      
{
SeqList *L;
L=new SeqList;
L->SeqLength=0; /*    -1*/
return L;
}
void Define_SeqList(SeqList *L,int n)//        
{
cout<<"               :"<<endl;
for(int i=0;i<n;i++)
{
cin>>L->data[i]; //      
L->SeqLength++;
}
}
void Display_SeqList(SeqList *L)//        
{
cout<<"          "<<endl;
int i;
for(i=0;i<=L->SeqLength-1;i++)
{
cout<<L->data[i]<<" ";
}
cout<<endl;
}
int Insert_SeqList(SeqList *L,int i,DataType x) //        
{
cout<<"   "<<x<<"     "<<i<<" "<<endl;
int j;
if(L->SeqLength==MAXSIZE-1) //         -1,   
{
cout<<"  "<<endl;
return -1;
}
if(i<1||i>L->SeqLength+1) //          ,              +1
{
cout<<"   "<<endl;
return 0;
}
for(j=L->SeqLength-1;j>=i;j--) //i      
{
L->data[j+1]=L->data[j];
}
L->data[i]=x; //          
L->SeqLength++;
cout<<"    "<<endl;
Display_SeqList(L);
return 1;
}
int Delete_SeqList(SeqList *L,int i)//        
{
cout<<"    "<<i<<"     "<<endl;
int j;
if(i<1||i>L->SeqLength)
{
cout<<"    "<<i<<"   "<<endl;
return 0;
}
for(j=i;j<=L->SeqLength-1;j++)
{
L->data[j]=L->data[j+1]; //i        
}
L->SeqLength--;
cout<<"    "<<endl;
Display_SeqList(L);
return 1;
}
【Test_Sequence.cpp】

#include "Sequence.h"
#include<iostream>
using namespace std;
int main()
{
SeqList *L;//      
L=Init_SeqList();//       
Define_SeqList(L,6);//     
Display_SeqList(L);//      
Insert_SeqList(L,4,3);//      
Insert_SeqList(L,6,21);
Insert_SeqList(L,2,15);
Delete_SeqList(L,5);//      
Delete_SeqList(L,3);
Delete_SeqList(L,12);
return 0;
}
효 과 는 다음 과 같 습 니 다:

위 에서 말 한 것 은 편집장 이 소개 한 C++순서 표를 실현 하 는 데 자주 사용 되 는 작업 입 니 다.(출력 삭제 삽입)도움 이 되 기 를 바 랍 니 다.궁금 한 점 이 있 으 시 면 메 시 지 를 남 겨 주세요.편집장 은 신속하게 답 해 드 리 겠 습 니 다.여기 서도 저희 사이트 에 대한 여러분 의 지지 에 감 사 드 립 니 다!

좋은 웹페이지 즐겨찾기