seqlist template

 1 #include <iostream.h>
 2 typedef int ElemType;
 3 typedef struct{
 4    ElemType *elem;
 5    int length;
 6 }SeqList;
 7 
 8 void InitSeq(SeqList &L,int n,ElemType a[])
 9 {  int i=0;
10    L.elem=new ElemType[n];
11    while(i<n)
12    {L.elem[i]=a[i];i++;}
13    L.length=n;
14 }
15 void DispSeq(SeqList L)
16 {
17     int i=0;
18        while(i<L.length)
19    {cout<<L.elem[i]<<'\t';i++;}
20 }
21 void UnionSeq(SeqList L1,SeqList L2,SeqList &L3)
22 {
23    L3.length=L1.length+L2.length;
24    L3.elem=new ElemType[L3.length];
25    ElemType* pa,*pb,*pc;
26    pa=&L1.elem[0];
27    pb=&L2.elem[0];
28    pc=&L3.elem[0];
29    while(pa<L1.elem+L1.length&&pb<L2.elem+L2.length)
30    {
31        if(*pa<*pb)
32        {*pc=*pa;pa++;pc++;}
33        else
34        {*pc=*pb;pb++;pc++;}
35    }
36    while(pb<L2.elem+L2.length)
37    {*pc=*pb;pb++;pc++;}
38     while(pa<L1.elem+L1.length)
39    {*pc=*pa;pa++;pc++;}
40 }
41 
42 
43 
44 void main()
45 {
46    SeqList L1,L2,L3;
47    ElemType a[100]={2,6,8};
48    ElemType b[100]={1,3,5,7};
49    InitSeq(L1,3,a);
50    InitSeq(L2,4,b);
51    DispSeq(L1);cout<<endl;
52    DispSeq(L2);
53    UnionSeq(L1,L2,L3);
54    cout<<endl;
55    DispSeq(L3);
56 }

 

posted on 2017-03-03 15:40  ewitt  阅读(284)  评论(0)    收藏  举报

导航