[C 언어] 데이터 구조 에서 선형 표 순서 표 의 기본 조작 (삭제 및 검사 및 합병)

#include 
#define MAXSIZE 20
typedef int Elemtype;

typedef struct Node{
	Elemtype data[MAXSIZE];
	int length;
}sqList;
//    
void initList(sqList *L)
{
	L->length=0;
		printf("OK
"); } // void ListEmpty(sqList *L) { if(L->length==0) printf("
"); else printf("
"); } // void clearList(sqList *L) { L->length=0; /* Length ,Length==0 , */ printf("OK
"); } // int ListLength(sqList *L) { return L->length; } // i void getElem(sqList *L,int i,Elemtype *e) { if(L->length==0||i<1||i>L->length) // L i return; *e=L->data[i-1]; printf("OK
"); } // int LocateElem(sqList *L,Elemtype e) // ( ), int { int i; if(L->length==0) return 0; for(i=0;ilength;i++) if(L->data[i]==e) return i+1; return 0; } // void insertList(sqList *L,int i,Elemtype e) { int j; if(L->length==MAXSIZE) // return; if(i<1||i>L->length+1) // i return; for(j=L->length-1;j>=i-1;j--) // L->data[j+1]=L->data[j]; L->data[i-1]=e; L->length++; } // void deleteList(sqList *L,int i,Elemtype *e) { int j; if(L->length==MAXSIZE) // return; if(i<1||i>L->length) // i return; *e=L->data[i-1]; // for(j=i;jlength;j++) L->data[j-1]=L->data[j]; L->length--; printf("OK
"); } // void TraverseList(sqList *L) { int i; if(L->length==0) printf("NULL
"); else { for(i=0;ilength;i++) printf("%d ",L->data[i]); printf("
"); } } // ( ) void unionList(sqList *La,sqList *Lb,sqList *Lc) { int i,j,flag; for (i = 0; i < La->length; i++) Lc->data[i] = La->data[i]; Lc->length = La->length; flag = 0; for (i = 0; i < Lb->length && Lc->lengthlength && flag==0; j++) { if (La->data[j] == Lb->data[i] ) flag=1; } if (flag == 1) continue; else Lc->data[Lc->length++] = Lb->data[i]; } } void Del_in_range(sqList *L,int x,int y) {// x y , O(n) int backStep=0; for(int i=0;ilength;i++) { if(L->data[i]>=x&&L->data[i]<=y) backStep++; else L->data[i-backStep]=L->data[i]; } L->length=L->length-backStep; } int main() { sqList La,Lb,Lc; int i,j,t,temp;Elemtype e; initList(&Lb); for(i=1;i<=5;i++) insertList(&Lb,i,i); while(i=getchar()) { switch(i) { case '1':initList(&La);break; case '2':ListEmpty(&La);break; case '3':clearList(&La);break; case '4':printf(" %d
",ListLength(&La));break; case '5':printf("
"); scanf("%d %d",&t,&e); insertList(&La,t,e); break; case '6':printf(" "); scanf("%d",&t); deleteList(&La,t,&e); break; case '7':printf("
"); scanf("%d",&t); getElem(&La,t,&e); printf("%d",e); break; case '8':unionList(&La,&Lb,&Lc);break; case '9':TraverseList(&La);break; case '0':TraverseList(&Lc); break; } } return 0; }

좋은 웹페이지 즐겨찾기