【第三週】項目4-順序表應用

/*   
*Copyright (c) 2017,煙臺大學計算機與控制工程學院   
*All rights reserved.   
*文件名稱:項目4——順序表的應用   
*作    者:高兆港   
*版 本 號:v1.0   
*  定義一個採用順序結構存儲的線性表,設計算法完成下面的工作:    
  1、刪除元素在[x, y]之間的所有元素,要求算法的時間複雜度爲O(n),空間複雜度爲O(1);    
  2、將所在奇數移到所有偶數的前面,要求算法的時間複雜度爲O(n),空間複雜度爲O(1)。   
*/    

#include<stdio.h>  
#include<malloc.h>  
#define MaxSize 50  
typedef int ElemType;  
typedef struct  
{  
    ElemType data[MaxSize];  
    int length;  
} SqList;  
void CreateList(SqList *&L, ElemType a[], int n);//用數組創建線性表  
void InitList(SqList *&L);//初始化線性表InitList(L)  
void DestroyList(SqList *&L);//銷燬線性表DestroyList(L)  
bool ListEmpty(SqList *L);//判定是否爲空表ListEmpty(L)  
int ListLength(SqList *L);//求線性表的長度ListLength(L)  
void DispList(SqList *L);//輸出線性表DispList(L)  
bool GetElem(SqList *L,int i,ElemType &e);//求某個數據元素值GetElem(L,i,e)  
int LocateElem(SqList *L, ElemType e);//按元素值查找LocateElem(L,e)  
bool ListInsert(SqList *&L,int i,ElemType e);//插入數據元素ListInsert(L,i,e)  
bool ListDelete(SqList *&L,int i,ElemType &e);//刪除數據元素ListDelete(L,i,e)  
void delx2y(SqList *&L, ElemType x,  ElemType y);  
void delx2y(SqList *&L, ElemType x,  ElemType y)  
{  
    int k=0,i;//k記錄非x的元素個數  
    ElemType t;  
    if(x>y)  
    {  
        t=x;  
        y=x;  
        y=t;  
    }  
    for(i=0;i<L->length;i++)  
        if(L->data[i]<x||L->data[i]>y)//複製不在[x,y]之間的數  
        {  
            L->data[k]=L->data[i];  
            k++;  
        }  
        L->length=k;  
}  
  
  
  
//用數組創建線性表  
void CreateList(SqList *&L, ElemType a[], int n)  
{  
    int i;  
    L=(SqList *)malloc(sizeof(SqList));  
    for (i=0; i<n; i++)  
        L->data[i]=a[i];  
    L->length=n;  
}  
  
//初始化線性表InitList(L)  
void InitList(SqList *&L)   //引用型指針  
{  
    L=(SqList *)malloc(sizeof(SqList));  
    //分配存放線性表的空間  
    L->length=0;  
}  
  
//銷燬線性表DestroyList(L)  
void DestroyList(SqList *&L)  
{  
    free(L);  
}  
  
//判定是否爲空表ListEmpty(L)  
bool ListEmpty(SqList *L)  
{  
    return(L->length==0);  
}  
  
//求線性表的長度ListLength(L)  
int ListLength(SqList *L)  
{  
    return(L->length);  
}  
  
//輸出線性表DispList(L)  
void DispList(SqList *L)  
{  
    int i;  
    if (ListEmpty(L)) return;  
    for (i=0; i<L->length; i++)  
        printf("%d ",L->data[i]);  
    printf("\n");  
}  
  
//求某個數據元素值GetElem(L,i,e)  
bool GetElem(SqList *L,int i,ElemType &e)  
{  
    if (i<1 || i>L->length)  return false;  
    e=L->data[i-1];  
    return true;  
}  
  
//按元素值查找LocateElem(L,e)  
int LocateElem(SqList *L, ElemType e)  
{  
    int i=0;  
    while (i<L->length && L->data[i]!=e) i++;  
    if (i>=L->length)  return 0;  
    else  return i+1;  
}  
  
//插入數據元素ListInsert(L,i,e)  
bool ListInsert(SqList *&L,int i,ElemType e)  
{  
    int j;  
    if (i<1 || i>L->length+1)  
        return false;   //參數錯誤時返回false  
    i--;            //將順序表邏輯序號轉化爲物理序號  
    for (j=L->length; j>i; j--) //將data[i..n]元素後移一個位置  
        L->data[j]=L->data[j-1];  
    L->data[i]=e;           //插入元素e  
    L->length++;            //順序表長度增1  
    return true;            //成功插入返回true  
}  
  
//刪除數據元素ListDelete(L,i,e)  
bool ListDelete(SqList *&L,int i,ElemType &e)  
{  
    int j;  
    if (i<1 || i>L->length)  //參數錯誤時返回false  
        return false;  
    i--;        //將順序表邏輯序號轉化爲物理序號  
    e=L->data[i];  
    for (j=i; j<L->length-1; j++) //將data[i..n-1]元素前移  
        L->data[j]=L->data[j+1];  
    L->length--;              //順序表長度減1  
    return true;              //成功刪除返回true  
}  
int main()  
{  
    SqList *sq;  
    ElemType a[10]= {5,8,7,0,2,4,9,6,7,3};  
    CreateList(sq, a, 10);  
    printf("刪除前 ");  
    DispList(sq);  
  
    delx2y(sq, 4, 7);  
  
    printf("刪除後 ");  
    DispList(sq);  
    return 0;  
}  

#include<stdio.h>    
#include<malloc.h>    
#define MaxSize 50    
typedef int ElemType;    
typedef struct    
{    
    ElemType data[MaxSize];    
    int length;    
} SqList;    
void CreateList(SqList *&L, ElemType a[], int n);//用數組創建線性表    
void InitList(SqList *&L);//初始化線性表InitList(L)    
void DestroyList(SqList *&L);//銷燬線性表DestroyList(L)    
bool ListEmpty(SqList *L);//判定是否爲空表ListEmpty(L)    
int ListLength(SqList *L);//求線性表的長度ListLength(L)    
void DispList(SqList *L);//輸出線性表DispList(L)    
bool GetElem(SqList *L,int i,ElemType &e);//求某個數據元素值GetElem(L,i,e)    
int LocateElem(SqList *L, ElemType e);//按元素值查找LocateElem(L,e)    
bool ListInsert(SqList *&L,int i,ElemType e);//插入數據元素ListInsert(L,i,e)    
bool ListDelete(SqList *&L,int i,ElemType &e);//刪除數據元素ListDelete(L,i,e)    
void delx2y(SqList *&L, ElemType x,  ElemType y);    
void delx2y(SqList *&L, ElemType x,  ElemType y)    
{    
    int k=0,i;//k記錄非x的元素個數    
    ElemType t;    
    if(x>y)    
    {    
        t=x;    
        y=x;    
        y=t;    
    }    
    for(i=0;i<L->length;i++)    
        if(L->data[i]<x||L->data[i]>y)//複製不在[x,y]之間的數    
        {    
            L->data[k]=L->data[i];    
            k++;    
        }    
        L->length=k;    
}    
    
    
    
//用數組創建線性表    
void CreateList(SqList *&L, ElemType a[], int n)    
{    
    int i;    
    L=(SqList *)malloc(sizeof(SqList));    
    for (i=0; i<n; i++)    
        L->data[i]=a[i];    
    L->length=n;    
}    
    
//初始化線性表InitList(L)    
void InitList(SqList *&L)   //引用型指針    
{    
    L=(SqList *)malloc(sizeof(SqList));    
    //分配存放線性表的空間    
    L->length=0;    
}    
    
//銷燬線性表DestroyList(L)    
void DestroyList(SqList *&L)    
{    
    free(L);    
}    
    
//判定是否爲空表ListEmpty(L)    
bool ListEmpty(SqList *L)    
{    
    return(L->length==0);    
}    
    
//求線性表的長度ListLength(L)    
int ListLength(SqList *L)    
{    
    return(L->length);    
}    
    
//輸出線性表DispList(L)    
void DispList(SqList *L)    
{    
    int i;    
    if (ListEmpty(L)) return;    
    for (i=0; i<L->length; i++)    
        printf("%d ",L->data[i]);    
    printf("\n");    
}    
    
//求某個數據元素值GetElem(L,i,e)    
bool GetElem(SqList *L,int i,ElemType &e)    
{    
    if (i<1 || i>L->length)  return false;    
    e=L->data[i-1];    
    return true;    
}    
    
//按元素值查找LocateElem(L,e)    
int LocateElem(SqList *L, ElemType e)    
{    
    int i=0;    
    while (i<L->length && L->data[i]!=e) i++;    
    if (i>=L->length)  return 0;    
    else  return i+1;    
}    
    
//插入數據元素ListInsert(L,i,e)    
bool ListInsert(SqList *&L,int i,ElemType e)    
{    
    int j;    
    if (i<1 || i>L->length+1)    
        return false;   //參數錯誤時返回false    
    i--;            //將順序表邏輯序號轉化爲物理序號    
    for (j=L->length; j>i; j--) //將data[i..n]元素後移一個位置    
        L->data[j]=L->data[j-1];    
    L->data[i]=e;           //插入元素e    
    L->length++;            //順序表長度增1    
    return true;            //成功插入返回true    
}    
    
//刪除數據元素ListDelete(L,i,e)    
bool ListDelete(SqList *&L,int i,ElemType &e)    
{    
    int j;    
    if (i<1 || i>L->length)  //參數錯誤時返回false    
        return false;    
    i--;        //將順序表邏輯序號轉化爲物理序號    
    e=L->data[i];    
    for (j=i; j<L->length-1; j++) //將data[i..n-1]元素前移    
        L->data[j]=L->data[j+1];    
    L->length--;              //順序表長度減1    
    return true;              //成功刪除返回true    
}    
void move(SqList *&L)    
{    
    int i=0,j=L->length-1;    
    ElemType tmp;    
    while (i<j)    
    {    
        while ((i<j) && (L->data[j]%2==0))  //從右往左,找到第一個奇數(偶數就忽略不管)    
            j--;    
        while ((i<j) && (L->data[i]%2==1))  //從左往右,找到第一個偶數(奇數就忽略不管)    
            i++;    
        if (i<j)   //如果未到達“分界線”,將右邊的奇數和左邊的偶數交換    
        {    
            tmp=L->data[i];    
            L->data[i]=L->data[j];    
            L->data[j]=tmp;    
        }    
    }   //待循環上去後,繼續查找,並在必要時交換    
}    
int main()    
{    
    SqList *sq;    
    ElemType a[10]= {5,8,7,0,2,4,9,6,7,3};    
    CreateList(sq, a, 10);    
    printf("操作前 ");    
    DispList(sq);    
    
    move(sq);    
    
    printf("操作後 ");    
    DispList(sq);    
    return 0;    
}    


發佈了41 篇原創文章 · 獲贊 5 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章