青岛数据结构实训时 写的 一些代码

插入排序

 1 #include <iostream>
 2 #include <cstring>
 3 #include <algorithm>
 4 using namespace std;
 5 #define MAXL 10000
 6 
 7 void InsertSort(int a[],int n)
 8 {
 9     int i,j,temp;
10     for(i=0; i<n; i++)
11     {
12       temp=a[i];
13       j=i-1;
14       while(j>=0&&temp<a[j])
15       {
16         a[j+1]=a[j];
17         j--;
18       }
19       a[j+1]=temp;
20     }
21     for(i=0; i<n; i++)
22     cout<<a[i]<<" "; 
23     cout<<endl;
24 }
25 int main()
26 {
27      int n,i,a[MAXL];
28      cin>>n;
29      for(i=0; i<n; i++)
30          cin>>a[i];
31      InsertSort(a,n);
32     return 0;
33 }

 

冒泡排序

 1 //冒泡排序
 2 #include <iostream>
 3 #include <cstring>
 4 #include <algorithm>
 5 using namespace std;
 6 #define MAXL 10000
 7 
 8 int BubbleSort(int a[],int n)
 9 {
10     int i,j,t;
11     for(i=0; i<=n-2; i++)
12         for(j=0; j<=n-i-2; j++)
13             if(a[j+1]<a[j])
14             {
15                 t=a[j]; a[j]=a[j+1]; a[j+1]=t;
16             }
17             
18             for(i=0; i<n; i++)
19                 cout<<a[i]<<" ";
20             cout<<endl;
21             return 0;
22 }
23 int main()
24 {
25      int i,n,a[MAXL];
26      cin>>n;
27     for(i=0; i<n; i++)
28         cin>>a[i];
29     BubbleSort(a,n);
30     return 0;
31 }

 

选择排序

 1 #include <iostream>
 2 #include <cstring>
 3 #include <algorithm>
 4 using namespace std;
 5 #define MAXL 10000
 6 
 7 int a[MAXL];
 8 void SelectSort(int a[],int n)
 9 {
10    int i,j,k,x;
11    for(i=1; i<n; i++)
12    {
13      k=i-1;
14      for(j=i; j<n; j++)
15      {
16        if(a[j]<a[k])
17            k=j;
18      }
19      x=a[i-1];
20      a[i-1]=a[k];
21      a[k]=x;
22    }
23 }
24 
25 int main()
26 {
27     int n,i;
28     cin>>n;
29     for(i=0; i<n; i++)
30     {
31       cin>>a[i];
32     }
33     SelectSort(a,n);
34     for(i=0; i<n; i++)
35        cout<<a[i]<<" ";
36    cout<<endl;
37     return 0;
38 }

 

练习1

 1 #include <stdio.h>
 2 #include <string.h>
 3 #define MAXSIZE 10000
 4 typedef struct node
 5 {
 6     int elem[MAXSIZE];
 7     int last;
 8 }Sqlist;
 9 Sqlist list;
10 
11 void show(Sqlist * Last)
12 {
13     int i;
14    for(i=0; i<(*Last).last; i++)
15    printf("%d ",(*Last).elem[i]);
16    printf("\n");
17 }
18 
19 int Sinsert(Sqlist &Last,int e,int loc)
20 {
21     int i;
22     if(loc<0||loc>=MAXSIZE)
23     return 0;
24     else
25     {
26      for(i=Last.last-1; i>=loc; i--)
27      Last.elem[i+1]=Last.elem[i];
28      Last.elem[loc]=e;
29      Last.last++;
30      return 1;
31     }
32 }
33 
34 int del(Sqlist &Last,int loc,int e)
35 {
36 
37    int i;
38    if(loc<0||loc>=MAXSIZE)
39        return 0;
40    else
41    {
42      e=Last.elem[loc];
43      for(i=loc; i<Last.last-1; i++)
44      Last.elem[i]=Last.elem[i+1];
45      Last.last--;
46      return 1;
47    }
48 }
49 
50 int main()
51 {
52   int i;
53   int e,x;
54   scanf("%d",&list.last);
55   for(i=0; i<list.last; i++)
56   scanf("%d",&list.elem[i]);
57   scanf("%d%d",&e,&x);
58   Sinsert(list,e,x);
59   scanf("%d",&x);
60   del(list,x,e);
61   show(&list);
62   return 0;
63 }

 

练习2

 1 #include <iostream>
 2 #include <cstring>
 3 #include <algorithm>
 4 using namespace std;
 5 #define MAXL 10000
 6 
 7 typedef struct node
 8 {
 9    char key;
10    int data;
11 }Nodetype;
12 typedef Nodetype Sqlist[MAXL];    
13 
14 int Seqsearch(Sqlist R,int n,char k)
15 {
16    int i=0;
17    while(i<n&&R[i].key!=k) ++i;
18    if(i>=n)
19    return -1;
20    else
21    return R[i].data;
22 }
23 int main()
24 {
25     Sqlist R;
26     int i,n,f;
27     char k;
28     cin>>n;
29     cin>>k;
30     for(i=0; i<n; i++)
31     cin>>R[i].key>>R[i].data;
32     f=Seqsearch(R,n,k);
33     if(f==1)
34     cout<<f<<endl;
35     else
36     cout<<"NO!"<<endl;
37     return 0;
38 }

 

作业1

 1 #include <iostream>
 2 #include <cstring>
 3 #include <algorithm>
 4 using namespace std;
 5 
 6 typedef struct node
 7 {
 8     int data;
 9     struct node *next;
10 }Sqlist;
11 
12 Sqlist *creat(int n)
13 {
14     int i;
15     Sqlist *head,*tail,*p;
16     head = new Sqlist;
17     head->next=NULL;
18     tail=head;
19     
20     for(i=0; i<n; i++)
21     {
22       p=new Sqlist;
23       cin>>p->data;
24       p->next=NULL;
25       tail->next=p;
26       tail=p;
27     }
28     return head;
29 }
30 
31 int locate(Sqlist *List, int loc)
32 {
33     int sum=0;
34     Sqlist *p;
35     p=List;
36     while(p->next!=NULL)
37     {
38       sum++;
39       if(sum==loc)
40       return p->next->data;
41       p=p->next;
42     }
43     return -1;
44 }
45 int main()
46 {
47     int n,loc;
48     Sqlist *head;
49     cin>>n;
50     head=creat(n);
51     cin>>loc;
52     cout<<locate(head,loc)<<endl;
53     /*r=head;
54     while(r->next!=NULL)
55     {
56       cout<<r->next->data<<" ";
57       r=r->next;
58     }
59     cout<<endl;*/
60     return 0;
61 }

 

作业2

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <stdlib.h>
 4 #define MAXL 10000
 5 int a[MAXL],n;
 6 
 7 void QSort(int low,int high)
 8 {
 9     if(low<high)
10     {
11         int x,l,h;
12         l=low,h=high;
13         x=a[low];
14         while(low<high)
15         {
16             while(low<high&&a[high]>=x)  --high;
17             a[low]=a[high];
18             while(low<high&&a[low]<=x) ++low;
19             a[high]=a[low];
20         }
21         a[low]=x;
22         QSort(l,low-1);
23         QSort(low+1,h);
24     }
25 }
26 
27 void show()
28 {
29    int i;
30    for(i=0; i<n; i++)
31    {
32        if(i!=n-1)
33        printf("%d ",a[i]);
34        else
35        printf("%d\n",a[i]);
36    }
37 }
38 int main()
39 {
40    printf("请在运行前在D盘建一个文件名为data,\n属性为txt的文本文档,并在里面输入一些整型数据!\n\n");
41    system("pause");
42    FILE *fp;
43    int i;
44    n=-1;
45    fp=fopen("D:\\data.txt","rt");
46    while(~fscanf(fp,"%d",&a[++n]))
47    {}
48    fclose(fp);
49    printf("排序前的顺序为:\n");
50    show();
51    QSort(0,n-1);
52    printf("排序后的顺序为:\n");
53    show();
54    fp=fopen("D:\\data.txt","at+");
55    fprintf(fp,"排序后的顺序为:\n");
56    for(i=0; i<n; i++)
57    {
58        if(i!=n-1)
59        fprintf(fp,"%d ",a[i]);
60        else
61        fprintf(fp,"%d\n",a[i]);
62    }
63    fclose(fp);
64    return 0;
65 }

 

posted @ 2014-01-17 19:47  水门  阅读(304)  评论(0编辑  收藏  举报