数据结构第二章

线性表的顺序存储结构顺序表,线性表的链式存储结构链表;

觉得没有啥好记载的,课后题还可以写写;

顺序表的实现:

  1 #include<iostream>
  2 #include<cstdlib>
  3 #define MaxSize 50
  4 
  5 using namespace std;
  6 
  7 template <class ElemType> 
  8 struct SqList {
  9     ElemType data[MaxSize];
 10     int length;
 11 };
 12 
 13 template <class ElemType>             // SqList <ElemType> 可能是实例化所需 
 14 void CreateList(SqList <ElemType> *& L,ElemType a[],int n) {        // create 
 15     int i;
 16     L = (SqList <ElemType> *)malloc(sizeof(SqList <ElemType>));
 17     for(i = 0;i<n;i++)
 18         L->data[i] = a[i];
 19     L->length = n; 
 20 }
 21 
 22 template <class ElemType> 
 23 void InitList(SqList <ElemType> *& L) {                // initialize
 24     L = (SqList <ElemType> *)malloc(sizeof (SqList <ElemType>));
 25     L->length = 0;
 26 }
 27 
 28 template <class ElemType> 
 29 void DestroyList(SqList <ElemType> *& L) {            // destory
 30     free(L);                //free()是C语言中释放内存空间的函数,
 31 }                            //通常与申请内存空间的函数malloc()结合使用,
 32                             //可以释放由 malloc()、calloc()、realloc() 等函数申请的内存空间。
 33 template <typename ElemType>
 34 bool EmptyList(SqList <ElemType> * L) {            // empty
 35     return (L->length == 0) ;
 36 }
 37 
 38 template <typename ElemType>
 39 int LengthList(SqList <ElemType> * L) {            // length
 40     return (L->length);
 41 } 
 42 
 43 template <typename ElemType>
 44 void OutList(SqList <ElemType> * L) {            // out
 45     int i;
 46     for(i = 0;i<L->length;i++)
 47         cout<<L->data[i]<<" ";
 48     cout<<endl; 
 49 }
 50 
 51 template <typename ElemType>
 52 bool ElementList(SqList <ElemType> * L,int i,ElemType & e) {        // Some element 
 53     if(i<1 || i>L->length)
 54         return false;
 55     e = L->data[i-1];
 56     return true;
 57 }                        
 58 
 59 template <typename ElemType>
 60 int LocateList(SqList <ElemType> * L,ElemType e) {            // locate
 61     int i = 0;
 62     while(i<L->length && L->data[i] != e)
 63         i++;
 64     if(i >= L->length)
 65         return 0;
 66     else 
 67         return i+1;
 68 }
 69 
 70 template <typename ElemType>
 71 bool InsertList(SqList <ElemType> *& L,int i,ElemType e) {    // insert
 72     int j;
 73     if(i<1 || i>L->length+1)
 74         return false;
 75     i--;
 76     for(j = L->length;j>i;j--)
 77         L->data[j] = L->data[j-1];
 78     L->data[i] = e;
 79     L->length++;
 80     return true;
 81 }
 82 
 83 template <typename ElemType>
 84 bool DeleteList(SqList <ElemType> *& L,int i) {    // detele
 85     int j;
 86     if(i<1 || i>L->length)
 87         return false;
 88     i--;
 89     for(j = i;j<L->length-1;j++)
 90         L->data[j] = L->data[j+1];
 91     L->length--;
 92     return true;
 93 }
 94 
 95 SqList <int> *sqlist;
 96 
 97 int main()
 98 {
 99     int e;
100     int array1[7] = {4,5,3,1,7,6,2};
101     int array2[8] = {8,2,3,4,6,7,1,5};
102     CreateList(sqlist,array1,7);
103     InitList(sqlist);
104     CreateList(sqlist,array2,8);
105     cout<<"EmptyList"<<EmptyList(sqlist)<<endl;
106     cout<<"LengthList"<<LengthList(sqlist)<<endl;
107     OutList(sqlist);
108     cout<<"ElementList"<<ElementList(sqlist,6,e)<<endl;
109     cout<<e<<endl;
110     cout<<"LocateList"<<LocateList(sqlist,2)<<endl;
111     cout<<"InsertList"<<InsertList(sqlist,4,9)<<endl;
112     cout<<"DeleteList"<<DeleteList(sqlist,1)<<endl;
113     OutList(sqlist);
114     DestroyList(sqlist);
115     return 0;
116 } 
View Code

单链表的实现:

#include<iostream>
#include<cstdlib>
using namespace std;

template <class ElemType> 
struct LinkNode{
    ElemType data;
    struct LinkNode * next;
};
                                        // create
template <class ElemType>                 // 头插   
void CreateListF(LinkNode <ElemType> *& L,ElemType *a,int n) {
    int i;
    LinkNode <ElemType> *s;
    L = (LinkNode <ElemType> *)malloc(sizeof(LinkNode <ElemType>));
    L->next = NULL;
    for(i = 0;i < n;i++) {                // 原始与头结点之间 
        s = (LinkNode <ElemType> *)malloc(sizeof(LinkNode <ElemType>));
        s->data = a[i];
        s->next = L->next;
        L->next = s;
    }
}

template <class ElemType>                 // 尾插 
void CreateListR(LinkNode <ElemType> *& L,ElemType *a,int n) {
    int i;
    LinkNode <ElemType> *s,*r;
    L = (LinkNode <ElemType> *)malloc(sizeof(LinkNode <ElemType>));
    r = L;
    for(i = 0;i < n;i++) {                // one by one  
        s = (LinkNode <ElemType> *)malloc(sizeof(LinkNode <ElemType>));
        s->data = a[i];
        r->next = s;
        r = s;
    }
    r->next = NULL;
}
                                    
template <class ElemType>                 //initialize
void InitList(LinkNode <ElemType> *& L) {                
    L = (LinkNode <ElemType> *)malloc(sizeof (LinkNode <ElemType>));
    L->next = NULL;                        // 头结点 
}

template <class ElemType>                 // destory
void DestroyList(LinkNode <ElemType> *& L) {            
    LinkNode <ElemType> * pre = L,* p = L->next;    // 第四版有个小错误 
    while(p != NULL) {
        free(pre);
        pre = p;
        p = pre->next;
    }
    free(pre);
}

template <typename ElemType>            // empty
bool EmptyList(LinkNode <ElemType> * L) {            
    return (L->next == NULL) ;
}

template <typename ElemType>
int LengthList(LinkNode <ElemType> * L) {
    int n = 0;
    LinkNode <ElemType> * p = L;
    while(p->next != NULL) {
        n++;
        p = p->next;
    }
    return (n);
}

template <typename ElemType>            // out
void OutList(LinkNode <ElemType> * L) {        
    LinkNode <ElemType> * p = L->next;
    while(p != NULL) {
        cout<<p->data<<" ";
        p = p->next;
    }
    cout<<endl; 
}

template <typename ElemType>            // Some element 
bool ElementList(LinkNode <ElemType> * L,int i,ElemType & e) {    
    int j = 0;
    LinkNode <ElemType> * p = L;
    while(j < i && p != NULL) {
        j++;
        p = p->next;
    }
    if(p == NULL)
        return false;
    else {
        e = p->data;
        return true;
    }
}

template <typename ElemType>            // locate
int LocateList(LinkNode <ElemType> * L,ElemType e) {
    int i = 1;
    LinkNode <ElemType> * p = L;
    while(p != NULL && p->data != e) {
        i++;
        p = p->next;
    }
    if(p == NULL)
        return 0;
    else
        return i;
}    

template <typename ElemType>            // insert
bool InsertList(LinkNode <ElemType> *& L,int i,ElemType e) {    
    int j = 0;
    LinkNode <ElemType> * p = L,*s;
    while(j < i-1 && p != NULL) {
        j++;
        p = p->next; 
    }
    if(p == NULL)
        return false;
    else {
        s = (LinkNode <ElemType> *)malloc(sizeof(LinkNode <ElemType>));
        s->data = e;
        s->next = p->next;
        p->next = s;
        return true;
    }
}

template <typename ElemType>            // detele
bool DeleteList(LinkNode <ElemType> *& L,int i) {    
    int j = 0;
    LinkNode <ElemType> * p = L,*q;
    while(j < i-1 && p != NULL) {
        j++;
        p = p->next; 
    }
    if(p == NULL)
        return false;
    else {
        q = p->next;
        if(q == NULL)
            return false;
        p->next = q->next;
        free(q);
        return true;
    }
}

int main()
{
    LinkNode <int> * linknode;
    int e;
    int array1[7] = {1,2,3,4,5,6,7};
    int array2[8] = {1,2,3,4,5,6,7,8};
    CreateListF(linknode,array1,7);
    OutList(linknode);
    InitList(linknode);
    CreateListR(linknode,array2,8);
    OutList(linknode);
    cout<<"EmptyList"<<EmptyList(linknode)<<endl;
    cout<<"LengthList"<<LengthList(linknode)<<endl;
    cout<<"ElementList"<<ElementList(linknode,6,e)<<endl;
    cout<<e<<endl;
    cout<<"LocateList"<<LocateList(linknode,5)<<endl;
    cout<<"InsertList"<<InsertList(linknode,4,9)<<endl;
    cout<<"DeleteList"<<DeleteList(linknode,1)<<endl;
    OutList(linknode);
    DestroyList(linknode);
    return 0;
}

/*
CreateLinkF 顺序  ; CreateLinkR  逆序  ;
*/
View Code

双链表的实现(由于和单链表差不多就随便写了一下):

template <class ElemType> 
struct DLinkNode{
    ElemType data;
    struct DLinkNode * prior;
    struct DLinkNode * next;
};

template <class ElemType>                 // 头插   
void CreateListF(DLinkNode <ElemType> *& L,ElemType *a,int n) {
    int i;
    LinkNode <ElemType> *s;
    L = (DLinkNode <ElemType> *)malloc(sizeof(DLinkNode <ElemType>));
    L->prior = L->next = NULL;
    for(i = 0;i < n;i++) {                // 原始与头结点之间 
        s = (DLinkNode <ElemType> *)malloc(sizeof(DLinkNode <ElemType>));
        s->data = a[i];
        s->next = L->next;
        if(L->next != NULL)
            L->next->prior = s;
        L->next = s;
        s->prior = L;
    }
}

template <class ElemType>                 // 尾插 
void CreateListR(DLinkNode <ElemType> *& L,ElemType *a,int n) {
    int i;
    DLinkNode <ElemType> *s,*r;
    L = (DLinkNode <ElemType> *)malloc(sizeof(DLinkNode <ElemType>));
    r = L;
    for(i = 0;i < n;i++) {                // one by one  
        s = (DLinkNode <ElemType> *)malloc(sizeof(DLinkNode <ElemType>));
        s->data = a[i];
        r->next = s;
        s->prior = r;
        r = s;
    }
    r->next = NULL;
}
        
View Code

顺序表的删除(xsbb,书上那个抄了一遍):

 1 #include<iostream>
 2 #include<cstdlib>
 3 #define MaxSize 50
 4 
 5 using namespace std;
 6 
 7 template <class ElemType> 
 8 struct SqList {
 9     ElemType data[MaxSize];
10     int length;
11 };
12 
13 template <class ElemType>
14 void Delnode1(SqList <ElemType> *& L,ElemType x) {
15     int k = 0,i;
16     for(i = 0;i<L->length;i++)
17         if(L->data[i] != x)
18         {    L->data[k] = L->data[i];
19             k++;
20         }
21     L->length = k;
22 }
23 
24 template <typename ElemType>
25 void Delnode2(SqList <ElemType> *& L,ElemType x) {
26     int k = 0,i = 0;
27     while(i < L->length)
28     {
29         if(L->data[i] == x)
30             k++;
31         else
32             L->data[i-k] = L->data[i];
33         i++; 
34     }
35     L->length -= k; 
36 }
37 
38 template <typename ElemType>
39 void OutList(SqList <ElemType> * L) {
40     int i;
41     for(i = 0;i<L->length;i++)
42         cout<<L->data[i]<<" ";
43     cout<<endl; 
44 }
45 
46 template <class ElemType>         
47 void CreateList(SqList <ElemType> *& L,ElemType a[],int n) {
48     int i;
49     L = (SqList <ElemType> *)malloc(sizeof(SqList <ElemType>));
50     for(i = 0;i<n;i++)
51         L->data[i] = a[i];
52     L->length = n; 
53 }
54 
55 SqList <int> *sqlist;
56 
57 int main()
58 {
59     int array[10] = {1,4,5,7,8,9,2,3,6,0};
60     CreateList(sqlist,array,10);
61     Delnode1(sqlist,6);
62     OutList(sqlist);
63     Delnode2(sqlist,7);
64     OutList(sqlist);
65     return 0;
66 } 
View Code

eg2-4.cpp  SqList--大小移动:

 1 #include<iostream>
 2 #include<cstdlib>
 3 #define MaxSize 50
 4 
 5 using namespace std;
 6 
 7 template <class ElemType> 
 8 struct SqList {
 9     ElemType data[MaxSize];
10     int length;
11 };
12 
13 template <class ElemType> 
14 void Move1(SqList <ElemType> *& L) {
15     int i = 0,j = L->length-1;
16     ElemType pivot = L->data[0],tmp;
17     while(i < j) {
18         while(i < j && L->data[j] > pivot)
19             j--;
20         while(i < j && L->data[i] <= pivot)
21             i++;
22         if(i < j) {
23             tmp = L->data[i];
24             L->data[i] = L->data[j];
25             L->data[j] = tmp;
26         }
27     }
28     tmp = L->data[0];
29     L->data[0] = L->data[i];
30     L->data[i] = tmp;
31 }
32 
33 template <class ElemType> 
34 void Move2(SqList <ElemType> *& L) {
35     int i = 0,j = L->length-1;
36     ElemType pivot = L->data[0];
37     while(i < j) {
38         while(i < j && L->data[j] > pivot)
39             j--;
40         L->data[i] = L->data[j];
41         i++;
42         while(i < j && L->data[i] <= pivot)
43             i++;
44         L->data[j] = L->data[i];
45         j--;
46     }
47     L->data[i] = pivot;
48 }
49 
50 template <typename ElemType>
51 void OutList(SqList <ElemType> * L) {
52     int i;
53     for(i = 0;i<L->length;i++)
54         cout<<L->data[i]<<" ";
55     cout<<endl; 
56 }
57 
58 template <class ElemType>         
59 void CreateList(SqList <ElemType> *& L,ElemType a[],int n) {
60     int i;
61     L = (SqList <ElemType> *)malloc(sizeof(SqList <ElemType>));
62     for(i = 0;i<n;i++)
63         L->data[i] = a[i];
64     L->length = n; 
65 }
66 
67 int main()
68 {
69     SqList <int> *sqlist;
70     int array[10] = {3,8,2,7,1,5,3,4,6,0};
71     CreateList(sqlist,array,10);
72     Move1(sqlist);
73     OutList(sqlist);
74     CreateList(sqlist,array,10);
75     Move2(sqlist);
76     OutList(sqlist);
77     return 0;
78 }
View Code

eg2-5.cpp  SqList--奇偶移动:

 1 #include<iostream>
 2 #include<cstdlib>
 3 #define MaxSize 50
 4 
 5 using namespace std;
 6 
 7 template <class ElemType> 
 8 void Swap(ElemType & a,ElemType & b) {
 9     ElemType temp = a;
10     a = b;
11     b = temp;
12 }
13 
14 template <class ElemType> 
15 struct SqList {
16     ElemType data[MaxSize];
17     int length;
18 };
19 
20 template <class ElemType> 
21 void Move1(SqList <ElemType> *& L) {
22     int i = 0,j = L->length-1;
23     while(i < j) {
24         while(i < j && L->data[j]%2 == 0)
25             j--;
26         while(i < j && L->data[i]%2 == 1)
27             i++;
28         if(i < j)
29             Swap(L->data[i],L->data[j]);
30     }
31 }
32 
33 template <class ElemType> 
34 void Move2(SqList <ElemType> *& L) {
35     int i = -1,j;
36     for(j = 0;j <= L->length -1;j++) 
37         if(L->data[j]%2 == 1) {
38             i++;
39             if(i != j)
40                 Swap(L->data[i],L->data[j]);
41         }
42 }
43 
44 template <typename ElemType>
45 void OutList(SqList <ElemType> * L) {
46     int i;
47     for(i = 0;i<L->length;i++)
48         cout<<L->data[i]<<" ";
49     cout<<endl; 
50 }
51 
52 template <class ElemType>         
53 void CreateList(SqList <ElemType> *& L,ElemType a[],int n) {
54     int i;
55     L = (SqList <ElemType> *)malloc(sizeof(SqList <ElemType>));
56     for(i = 0;i<n;i++)
57         L->data[i] = a[i];
58     L->length = n; 
59 }
60 
61 int main()
62 {
63     SqList <int> *sqlist;
64     int array[10] = {3,8,2,7,1,5,3,4,6,0};
65     CreateList(sqlist,array,10);
66     Move1(sqlist);
67     OutList(sqlist);
68     CreateList(sqlist,array,10);
69     Move2(sqlist);
70     OutList(sqlist);
71     return 0;
72 }
73 
74 // T(n) = O(n)  S(n) = O(1)
75 
76 /*
77 与 "eg2-4.cpp SqList--大小移动"相同,不做叙述;
78 快速排序基础 
79 */ 
View Code

eg2-6.cpp  LinkNode--拆分:

 1 #include<iostream>
 2 #include<cstdlib>
 3 #define MaxSize 50
 4 
 5 using namespace std;
 6 
 7 template <class ElemType> 
 8 struct LinkNode{
 9     ElemType data;
10     struct LinkNode * next;
11 };
12 
13 template <typename ElemType>            // out
14 void OutList(LinkNode <ElemType> * L) {        
15     LinkNode <ElemType> * p = L->next;
16     while(p != NULL) {
17         cout<<p->data<<" ";
18         p = p->next;
19     }
20     cout<<endl; 
21 }
22 
23 template <class ElemType>                 // 尾插 
24 void CreateListR(LinkNode <ElemType> *& L,ElemType *a,int n) {
25     int i;
26     LinkNode <ElemType> *s,*r;
27     L = (LinkNode <ElemType> *)malloc(sizeof(LinkNode <ElemType>));
28     r = L;
29     for(i = 0;i < n;i++) {                // one by one  
30         s = (LinkNode <ElemType> *)malloc(sizeof(LinkNode <ElemType>));
31         s->data = a[i];
32         r->next = s;
33         r = s;
34     }
35     r->next = NULL;
36 }
37 
38 template <class ElemType>
39 void Split(LinkNode <ElemType> *& L,LinkNode <ElemType> *&L1,LinkNode <ElemType> *& L2) {
40     LinkNode <ElemType> *p = L->next,*q,*r1;
41     L1 = L;
42     r1 = L1;
43     L2 = (LinkNode <ElemType> *)malloc(sizeof(LinkNode <ElemType>));
44     L2->next = NULL;
45     while(p != NULL){
46         r1->next = p;
47         r1 = p;
48         p = p->next;
49         q = p->next;
50         p->next = L2->next;
51         L2->next = p;
52         p = q;
53     }
54     r1->next = NULL;
55 } 
56 
57 int main()
58 {
59     LinkNode <int> * linknode,* L1,* L2;
60     int array[10] = {1,0,3,2,5,4,7,6,9,8};
61     CreateListR(linknode,array,10);
62     Split(linknode,L1,L2);
63     OutList(L1);
64     OutList(L2);
65     return 0;
66 }
View Code

eg2-8.cpp  插入排序:

 1 #include<iostream>
 2 #include<cstdlib>
 3 
 4 using namespace std;
 5 
 6 template <class ElemType> 
 7 struct LinkNode{
 8     ElemType data;
 9     struct LinkNode * next;
10 };
11 
12 template <class ElemType>                 // 尾插 
13 void CreateListR(LinkNode <ElemType> *& L,ElemType *a,int n) {
14     int i;
15     LinkNode <ElemType> *s,*r;
16     L = (LinkNode <ElemType> *)malloc(sizeof(LinkNode <ElemType>));
17     r = L;
18     for(i = 0;i < n;i++) {                // one by one  
19         s = (LinkNode <ElemType> *)malloc(sizeof(LinkNode <ElemType>));
20         s->data = a[i];
21         r->next = s;
22         r = s;
23     }
24     r->next = NULL;
25 }
26 
27 template <typename ElemType>            // out
28 void OutList(LinkNode <ElemType> * L) {        
29     LinkNode <ElemType> * p = L->next;
30     while(p != NULL) {
31         cout<<p->data<<" ";
32         p = p->next;
33     }
34     cout<<endl; 
35 }
36 
37 template <typename ElemType>
38 void SortList(LinkNode <ElemType> * L) {
39     LinkNode <ElemType> * p,*pre,*q;
40     p = L->next->next;
41     L->next->next = NULL;
42     while(p != NULL) {
43         q = p->next;
44         pre = L;
45         while(pre->next != NULL && pre->next->data < p->data)
46             pre = pre->next;
47         p->next = pre->next;
48         pre->next = p;
49         p = q; 
50     } 
51 } 
52 
53 int main()
54 {
55     LinkNode <int> * linknode;
56     int array[7] = {1,2,7,5,3,6,4};
57     CreateListR(linknode,array,7);
58     SortList(linknode);
59     OutList(linknode);
60     return 0; 
61 }
View Code

(课后题)exp2-7.cpp  链表合并:

 1 #include<iostream>
 2 #include<cstdlib>
 3 
 4 using namespace std;
 5 
 6 typedef struct Node                        //单链表的类型 
 7 {
 8     int data;
 9     struct Node *next;
10 }List;
11 
12 
13 void CreateList(List *& L,int n) {        // 尾插法,顺序排列 
14     int i;
15     List *s,*r;
16     L = (List *)malloc(sizeof(List));    // 开辟空间 
17     r = L;
18     for(i = 0;i < n;i++) {                // one by one  
19         s = (List *)malloc(sizeof(List));
20         cin>>s->data;                    //输入数据 
21         r->next = s;
22         r = s;
23     }
24     r->next = NULL;                        //其next域置为NULL
25 }
26 
27 void CreateList3(List *L1,List* L2,List *L3) {    // 合成链表L3 
28     List * r = L3;                                // L3头结点 ,用r来完成后续建立步骤 
29     List *p1 = L1->next,*p2 = L2->next;         // 分别是L1,L2首个数据结点 
30     while(p1 != NULL && p2 != NULL) {            // 当L1或者L2结束时,循环结束 
31         r->next = p1;                            // 把L1(p1)的结点给L3(r) 
32         r = r->next;                            
33         p1 = p1->next;
34         r->next = p2;                            // 把L2(p2)的结点给L3(r) 
35         r = r->next;
36         p2 = p2->next;                            // 这样就实现了L1与L2的结点交替的给L3 
37     }
38     if(p1 == NULL)                                // 哪个链表的长度长就把后续的结点都给L3 
39         r->next = p2;
40     else
41         r->next = p1;
42 }
43 
44 void Print(List *& L) {                            //输出链表的函数 
45     List *p = L->next;                            //跳过头结点到首结点 
46     while(p != NULL)                            //结点不为空,继续 
47     {
48         cout<<p->data;
49         p = p->next;
50     }
51 }
52 
53 int main()
54 {
55     List *L1,*L2,*L3;
56     int n,m;
57     L3=(List*)malloc(sizeof(List));
58     cin>>n;
59     CreateList(L1,n);
60     cin>>m;
61     CreateList(L2,m);
62     CreateList3(L1,L2,L3);
63     Print(L3);
64      return 0;
65 }
View Code

先写到这,以后再补吧;

2020-03-24

原文地址:https://www.cnblogs.com/2015-16/p/12561399.html