데이터 구조 [실험 2 순서 표 기타 조작]

67076 단어 데이터 구조
실험 요구:
  2              1.                        。        1        ,                    。     :               SeqList reverse(SeqList A) /*         */

void ListTraverse(SeqList L)          /*       */ SeqList create(int n)                 /*       */   2              LA LB, LA LB        LC,LC      。     :               SeqList MergeSeqList(SeqList La,SeqList Lb) /*     */

void ListTraverse(SeqList L)                    /*       */ SeqList create() /*       */   3              LA LB,     m n,  LA      ,    LA, LA LB                。        O(m+n)。     :               SeqList MergeSeqList(SeqList La,SeqList Lb,int m,int n) /*     */

void ListTraverse(SeqList L)          /*       */ SeqList create() /*       */   4       :     N K,         1,2, …,n     ,   1,2,…,n    ,       1    ,  K          ,     。                    ,    ,             。  N=10,K=3,          3,6927185104。     :             void Js(int n,int k)               /*                  */



 
 
참조 코드:
 1 /*

 2   1  3        ,                    。  4  5  SeqList reverse(SeqList A) //          6  void ListTraverse(SeqList L) //       7  SeqList create(int n) //       8 */

 9 

 10 #include <stdio.h>

 11 #include <stdlib.h>

 12 

 13 //      

 14 #define MAXSIZE 1000

 15 

 16 //          

 17 typedef int DateType;  18 

 19 typedef struct{  20  DateType data[MAXSIZE];  21     int length;  22 }SeqList;  23 

 24 SeqList reverse1(SeqList A)           //        

 25 {  26     int i;  27     for(i=0;i<A.length/2;i++){  28         DateType t;        //        

 29         t = A.data[i];  30         A.data[i] = A.data[A.length-i-1];  31         A.data[A.length-i-1] = t;  32  }  33     printf("    
"); 34 return A; 35 } 36 SeqList reverse2(SeqList A) // 37 { 38 SeqList B; 39 int i; 40 for(i=A.length-1;i>=0;i--) 41 B.data[A.length-i-1] = A.data[i]; 42 B.length = A.length; 43 printf("
"); 44 return B; 45 } 46 void ListTraverse(SeqList L) // 47 { 48 int i; 49 for(i=0;i<L.length;i++) 50 printf("%d ",L.data[i]); 51 printf("
"); 52 } 53 SeqList create(int n) // 54 { 55 SeqList sq; // 56 sq.length = n; 57 return sq; 58 } 59 60 int Menu() 61 { 62 int in; 63 printf("[1]
"); 64 printf("[2]
"); 65 printf("[3]
"); 66 printf("[4]
"); 67 printf("[5]
"); 68 scanf("%d",&in); 69 return in; 70 } 71 SeqList Reply(SeqList sq,int in) 72 { 73 int t; 74 switch(in){ 75 case 1: // 76 printf(" ?( )
"); 77 scanf("%d",&t); // 78 sq = create(t); // 79 printf("
"); 80 for(t=1;t<=sq.length;t++){ 81 printf(" %d :
",t); 82 scanf("%d",&sq.data[t-1]); 83 } 84 break; 85 case 2: // 86 ListTraverse(sq); 87 break; 88 case 3: // 89 sq = reverse1(sq); 90 break; 91 case 4: // 92 sq = reverse2(sq); 93 break; 94 default:// 95 printf("Bye~
"); 96 exit(1); 97 } 98 return sq; 99 } 100 int main() 101 { 102 int in; // 103 SeqList sq; 104 sq.length = 0; 105 while(1){ 106 in = Menu(); 107 sq = Reply(sq,in); // 108 system("pause"); 109 system("cls"); 110 } 111 return 0; 112 }

 
 1 /*

 2   2  3              LA LB, LA LB        LC,LC      。  4  5  SeqList MergeSeqList(SeqList La,SeqList Lb) //       6  void ListTraverse(SeqList L) //       7  SeqList create() //       8 */

 9 

 10 #include <stdio.h>

 11 #include <stdlib.h>

 12 

 13 //      

 14 #define MAXSIZE 1000

 15 

 16 //          

 17 typedef int DateType;  18 

 19 typedef struct{  20  DateType data[MAXSIZE];  21     int length;  22 }SeqList;  23 

 24 SeqList La,Lb,Lc;  25 

 26 SeqList MergeSeqList(SeqList La,SeqList Lb)  //     

 27 {  28  SeqList Lc;  29     int i = 0,j = 0,k = 0;  30     while(i!=La.length && j!=Lb.length){    //   La Lb        Lc 

 31         if(La.data[i] < Lb.data[j])  32             Lc.data[k++] = La.data[i++];  33         else

 34             Lc.data[k++] = Lb.data[j++];  35  }  36     while(j!=Lb.length)  37         Lc.data[k++] = Lb.data[j++];  38     while(i!=La.length)  39         Lc.data[k++] = La.data[i++];  40     Lc.length = La.length + Lb.length;  41     printf("       !
"); 42 return Lc; 43 } 44 void ListTraverse(SeqList L) // 45 { 46 int i; 47 for(i=0;i<L.length;i++) 48 printf("%d ",L.data[i]); 49 printf("
"); 50 } 51 SeqList create(int n) // 52 { 53 SeqList sq; // 54 sq.length = n; 55 return sq; 56 } 57 58 int Menu() 59 { 60 int in; 61 printf("[1] La
"); 62 printf("[2] Lb
"); 63 printf("[3] La Lb
"); 64 printf("[4] La
"); 65 printf("[5] Lb
"); 66 printf("[6] Lc
"); 67 printf("[7]
"); 68 scanf("%d",&in); 69 return in; 70 } 71 void Reply(int in) 72 { 73 int t; 74 switch(in){ 75 case 1: // La 76 printf(" ?( )
"); 77 scanf("%d",&t); // 78 La = create(t); // 79 printf("
"); 80 for(t=1;t<=La.length;t++){ 81 printf(" %d :
",t); 82 scanf("%d",&La.data[t-1]); 83 } 84 break; 85 case 2: // Lb 86 printf(" ?( )
"); 87 scanf("%d",&t); // 88 Lb = create(t); // 89 printf("
"); 90 for(t=1;t<=Lb.length;t++){ 91 printf(" %d :
",t); 92 scanf("%d",&Lb.data[t-1]); 93 } 94 break; 95 case 3: // La Lb 96 Lc = MergeSeqList(La,Lb); 97 break; 98 case 4: // 99 ListTraverse(La); 100 break; 101 case 5: // 102 ListTraverse(Lb); 103 break; 104 case 6: // 105 ListTraverse(Lc); 106 break; 107 default:// 108 printf("Bye~
"); 109 exit(1); 110 } 111 } 112 int main() 113 { 114 int in; // 115 La.length = 0; 116 Lb.length = 0; 117 Lc.length = 0; 118 while(1){ 119 in = Menu(); 120 Reply(in); // 121 system("pause"); 122 system("cls"); 123 } 124 return 0; 125 }

 
 1 /*

 2   3  3              LA LB,     m n,  LA      ,    LA, LA LB                。        O(m+n)。  4  5 SeqList MergeSeqList(SeqList La,SeqList Lb,int m,int n) //       6 void ListTraverse(SeqList L) //        7 SeqList create() //        8 */

 9 

 10 #include <stdio.h>

 11 #include <stdlib.h>

 12 

 13 //      

 14 #define MAXSIZE 1000

 15 

 16 //          

 17 typedef int DateType;  18 

 19 typedef struct{  20  DateType data[MAXSIZE];  21     int length;  22 }SeqList;  23 

 24 SeqList La,Lb,Lc;  25 

 26 SeqList MergeSeqList(SeqList La,SeqList Lb,int m,int n)  //    LA     

 27 {  28     int i = La.length - 1,j = Lb.length - 1,k = m + n - 1;  29     while(i>=0 && j>=0){    //          

 30         if(La.data[i] > Lb.data[j])  31             La.data[k--] = La.data[i--];  32         else

 33             La.data[k--] = Lb.data[j--];  34  }  35     while(i>0)  36         La.data[k--] = La.data[i--];  37     while(j>0)  38         La.data[k--] = Lb.data[j--];  39     La.length = m + n;  40     printf("
"); 41 return La; 42 } 43 44 void ListTraverse(SeqList L) // 45 { 46 int i; 47 for(i=0;i<L.length;i++) 48 printf("%d ",L.data[i]); 49 printf("
"); 50 } 51 SeqList create(int n) // 52 { 53 SeqList sq; // 54 sq.length = n; 55 return sq; 56 } 57 58 int Menu() 59 { 60 int in; 61 printf("[1] La
"); 62 printf("[2] Lb
"); 63 printf("[3] La Lb
"); 64 printf("[4] La
"); 65 printf("[5] Lb
"); 66 printf("[6] Lc
"); 67 printf("[7]
"); 68 scanf("%d",&in); 69 return in; 70 } 71 void Reply(int in) 72 { 73 int t; 74 switch(in){ 75 case 1: // La 76 printf(" ?( )
"); 77 scanf("%d",&t); // 78 La = create(t); // 79 printf("
"); 80 for(t=1;t<=La.length;t++){ 81 printf(" %d :
",t); 82 scanf("%d",&La.data[t-1]); 83 } 84 break; 85 case 2: // Lb 86 printf(" ?( )
"); 87 scanf("%d",&t); // 88 Lb = create(t); // 89 printf("
"); 90 for(t=1;t<=Lb.length;t++){ 91 printf(" %d :
",t); 92 scanf("%d",&Lb.data[t-1]); 93 } 94 break; 95 case 3: // La Lb 96 Lc = MergeSeqList(La,Lb,La.length,Lb.length); 97 break; 98 case 4: // 99 ListTraverse(La); 100 break; 101 case 5: // 102 ListTraverse(Lb); 103 break; 104 case 6: // 105 ListTraverse(Lc); 106 break; 107 default:// 108 printf("Bye~
"); 109 exit(1); 110 } 111 } 112 int main() 113 { 114 int in; // 115 La.length = 0; 116 Lb.length = 0; 117 Lc.length = 0; 118 while(1){ 119 in = Menu(); 120 Reply(in); // 121 system("pause"); 122 system("cls"); 123 } 124 return 0; 125 }

 
 1 /*

 2   4  3       :     N K,         1,2, …,n     ,   1,2,…,n    ,       1    ,  K          ,     。                    ,    ,             。  N=10,K=3,          3,6,9,2,7,1,8,5,10,4。  4  5 void Js(int n,int k) //                    6 */

 7 

 8 #include <stdio.h>

 9 #include <stdlib.h>

10 

11 //      

12 #define MAXSIZE 1000

13 

14 //          

15 typedef int DateType; 16 

17 typedef struct{ 18  DateType data[MAXSIZE]; 19     int length; 20 }SeqList; 21 

22 SeqList sq; 23 

24 SeqList create(int n)                //     

25 { 26     SeqList sq;    //        

27     sq.length = n; 28     return sq; 29 } 30 

31 

32 /*           */

33 

34 int ListEmpty(SeqList L) 35 { 36     if(L.length==0)    //   

37         return 1; 38     else    //    

39         return 0; 40 } 41 

42 

43 /*           */

44 

45 void ListDelete(SeqList* L,int i) 46 { 47     int j; 48     for(j=i;j<L->length;j++){    //     

49         L->data[j-1] = L->data[j]; 50  } 51     L->length--;    //   1

52     return ; 53 } 54 

55 void Js(int n,int k)    //                  

56 { 57     int t = 0; 58     printf("        :
"); 59 while(!ListEmpty(sq)){ 60 t = (t + k - 1) % sq.length; 61 printf("%d ",sq.data[t]); 62 ListDelete(&sq,t+1); 63 } 64 printf("
"); 65 } 66 67 int main() 68 { 69 int n,k; 70 printf(" N K:( 0 0 )
"); 71 while(scanf("%d%d",&n,&k)!=EOF){ 72 if(n==0 && k==0) break; 73 sq = create(n); 74 printf("
"); 75 int t; 76 for(t=1;t<=sq.length;t++){ 77 scanf("%d",&sq.data[t-1]); 78 } 79 Js(n,k); // 80 printf("
"); 81 printf(" N K:( 0 0 )
"); 82 } 83 return 0; 84 }

 
Freecode : www.cnblogs.com/yym2013

좋은 웹페이지 즐겨찾기