• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

尼古拉斯豆

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

数据结构--线性表--线性存储

前16个是根据网上资料写的。17,18方法是我自己添加的。稍微有点逻辑的也就17,18.

一共18个方法基本全部包含了所有线性表的操作。

编译器为GCC,支持所有ANSI C标准。

#include <stdio.h>

typedef int elemType;

struct List{
    elemType *list;
    int size;
    int maxsize;
};

/* 1.初始化线性表。*/ 
int initList(struct List *L,int ms)
{
   elemType *p = (elemType *)malloc(ms * sizeof(elemType));
   if(p == NULL)
   {
        printf("空间申请失败\n");
        return 1;    
    } 
    L->list=p;
    L->size=0;
    L->maxsize=ms;
    return 0;
} 
/* 2.扩展线性表空间为原来的2倍,成功返回1,失败返回0*/ 
int againMalloc(struct List *L)
{
   elemType *p = (elemType *)realloc(L->list, 2* L->maxsize * sizeof(elemType));
   if(p == NULL)
   {
        printf("空间申请失败\n");
        return 1;    
    } 
    L->list=p;
    L->maxsize = 2*L->maxsize;
    return 0;
}

/* 3.遍历线性表,并打印*/ 
void travelList(struct List *L)
{
    int i;
    for(i = 0; i<L->size; i++)
    {
        printf("%d ",L->list[i]);    
    }    
    printf("\n");
}
/* 4.清除线性表中所有元素,释放存储空间,使之成为一个空表*/
void clearList(struct List *L)
{
    if(L->list != NULL)
    {
        free(L->list);
        L->list=NULL;
        L->size=L->maxsize=0;    
    }
    return;    
}

/* 5.获取线性表大小*/ 
int getSize(struct List *L)
{
    return L->size;    
}

/* 6.判定线性表L是否为空,若为空,则返回1,否则返回0*/
int emptyList(struct List *L)
{
    if(L->size == 0) return 1;
    else return 0;    
}

/* 7.返回线性表L中第pos个元素的值,若pos超出范围,返回-1*/
elemType getElem(struct List *L,int pos)
{
    if(pos<1 && pos>L->size)
    {
        printf("pos越界\n");
        return -1;
    }
    return L->list[pos-1]; 
}

/* 8.从线性表中查找与x相等的元素,若超找成功返回其位置,否则返回-1*/
int findList(struct List *L, elemType x)
{
    if(L->list == NULL) return -1;
    int i;
    for(i=0; i<L->size; i++)
    {
        if(L->list[i] == x) return i;    
    }    
} 

/* 9.向线性表头插入元素x*/
void insertFirstList(struct List *L, elemType x)
{
    if(L->size == L->maxsize)
    {
        againMalloc(L);
    }    
    int i;
    for(i=L->size; i>0; i--)
    {
            L->list[i] = L->list[i-1];
    }
    L->list[0]=x;
    L->size+=1;
}

/* 10.向线性表L的尾插入元素x*/
void insertLastList(struct List *L, elemType x)
{
    if(L->size == L->maxsize)
    {
        againMalloc(L);
    }    
    L->list[L->size]=x;
    L->size+=1;    
}

/* 11.向线性表第pos个位置插入元素x,若成功返回1,否则返回0*/
int insertPosList(struct List *L, int pos, elemType x)
{
    if(pos<1 && pos >L->size) return 0;
    
    if(L->size == L->maxsize)
    {
        againMalloc(L);
    }
    int i;
    for(i=L->size ; i>=pos; i--)
    {
        L->list[i] = L->list[i-1];
    }    
    L->list[pos-1] =x;
    L->size+=1;
    return 1;
}

/* 12.向有序线性表L中插入元素x,使得插入后仍然有序*/
void insertOrderList(struct List *L, elemType x)
{
    int i,j;
    
    if(L->size == L->maxsize)
    {
        againMalloc(L);
    }
    
    for(i=0; i<L->size; i++)
    {
        if(x < L->list[i]) break;    
    }    
    
    for(j=L->size-1;j>=i; j--)
    {
        L->list[j+1] = L->list[j];    
    }
    L->list[i] = x;
    L->size+=1;
}
/* 13.从线性表L中删除表头元素并返回它,若删除失败则返回-1*/
elemType deletefirstList(struct List *L)
{
    if(L->list==NULL) return -1;
    
    int i;
    elemType temp = L->list[0];
    
    for(i=0; i<L->size-1; i++)
    {
        L->list[i]=L->list[i+1];    
    }
    L->list[L->size-1]=0;
    L->size-=1;
    return temp;    
}

/* 14.从线性表中删除表尾元素并返回它,若删除失败则返回-1*/
elemType deleteLastList(struct List *L)
{
    if(L->list == NULL) return -1;
    
    elemType temp = L->list[L->size-1]; 
    L->list[L->size-1] =0;
    L->size-=1;
 
    return temp;
}

/* 15.从线性表中删除第pos个元素,并返回它,失败返回-1*/
elemType deletePosList(struct List *L, int pos)
{
    if(pos<1 && pos>L->size) return -1;
    
    elemType temp = L->list[pos-1];
    
    int i;
    for(i=pos-1; i<L->size-1; i++)
    {
           L->list[i]=L->list[i+1];
    }
    L->list[L->size-1]=0;
    L->size-=1;
    
    return temp;    
}
/* 16.从线性表中删除第一个x,成功返回1,失败返回0*/
int deleteValueList(struct List *L, elemType x)
{
    int i,j;
    
    for(i=0; i<L->size; i++)
    {
        if(L->list[i] == x)
        {
            for(j=i; j<L->size-1; j++)
            {
                L->list[j] = L->list[j+1];    
            }
            L->list[L->size-1] = 0;
            return 1;       
        }    
    } 
    
    return 0;   
}
/* 17.删除与元素x相同的startPos位置之后的元素*/
void deleteSameList(struct List *L, elemType x, int startPos)
{
    if(startPos<1 && startPos >L->size) return; /*起始位置越界*/ 
    
    int i,j;
    int counts = 0;
    /*
        遍历检查表中与x相同元素个数。
        当startPos为0时,必须要2个或以上。
        当startPos不为0时,必须要1个或以上。
        否则停止程序
    */
    for(i=startPos; i<L->size; i++)  
    {
        if(L->list[i] == x)
        {
            counts++;
        }
    }
    if(startPos == 0 &&counts < 2)
    {
        printf("No repeate.exit...\n");
        return;    
    }
    if(startPos != 0 && counts < 1)
    {

        return; 
    }
    
    /*遍历线性表,若当前位置为x,则与后面不为x的第一个元素互换。然后i+1*/ 
    for(i=startPos; i<L->size; i++)  
    {
        if(L->list[i] == x)
        {
            for(j=i+1; j<L->size; j++)
            {
                /*后面有与x不同的就交换,并跳出,进行下一个i*/
                if(L->list[j] != x)
                {
                    L->list[i] = L->list[j];
                    L->list[j] = x;
                    break; 
                }                     
            }
            /*说明后面已无与X不同的元素且是经过交换的,而不是j自动走到此,表示从i+1,到j,全部为X*/ 
            if (j == L->size) 
            { 
                break;
            }           
        }    
    }
    
    L->size = L->size - (j - i);
    for (i=i; i<=j; i++)
    {
        L->list[i] = 0;    
    }
    return;
}

/* 18.去重,保留第一个*/ 
void deleteRepeateList(struct List *L)
{
    int i;
    for (i=0; i<L->size;i++)
    {
        deleteSameList(L,L->list[i],i+1);  
    } 
}

int main(void)
{
    int i;
    struct List L ;
    int a[] = {5,5,6,7,6,8,1,2,8,5,6,8,8,8};

    initList(&L,10);
    for(i=0; i<sizeof(a)/4; i++)
    {
        insertLastList(&L,a[i]);
    }
    insertFirstList(&L,9);
    
    printf("size = %d\n",L.size);
    travelList(&L);
    
    elemType elem = getElem(&L,5);
    printf("The 5th elem is : %d\n",elem);
    
#if 0
    printf("delete all the number 5\n");    
    deleteSameList(&L,5,0); 
#endif

#if 1
    printf("delete all the same number\n"); 
    deleteRepeateList(&L);
#endif   

    printf("size = %d\n",L.size);
    travelList(&L);
    if(emptyList(&L))
    {
        printf("The List is empty\n");    
    }else{
        printf("The List is not empty\n"); 
    }
    printf("delete the 5th number\n");
    deletePosList(&L,5);
    travelList(&L);
    printf("empty rhe list...\n");
    clearList(&L);
    if(emptyList(&L))
    {
        printf("The List is empty\n");    
    }else{
        printf("The List is not empty\n"); 
    }
    system("pause");
    return 0;        
}

 

********************************************************************************

运行结果:

********************************************************************************

size = 15
9 5 5 6 7 6 8 1 2 8 5 6 8 8 8
The 5th elem is : 7
delete all the same number
size = 7
9 5 6 7 8 1 2
The List is not empty
delete the 5th number
9 5 6 7 1 2
empty rhe list...
The List is empty
请按任意键继续. . .

 

 

posted on 2012-06-29 15:59  尼古拉斯豆  阅读(224)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3