数据结构中的顺序表

9 篇文章 0 订阅
1 篇文章 0 订阅
本程序主要讲解的是,顺序表的创建、插入、修改、查询、判空、判满,等操作。
 /* main.c */
#include <stdio.h>
#include "sqlist.h"

int main(void)
{
    sqlist L = CreateList();  //创建顺序表
    if(L==NULL){
        printf("createlist failed\n");
        return ERROR;
    }
    
    if( TRUE == ListIsEmpty(L) ){

        printf("list is empty\n");
    }

    InsertList(L,0,11);

    InsertList(L,1,22);
    InsertList(L,2,33);

    ShowList(L);

    DelList(L,0);

    DestroyList(L);     // 销毁空间

    ShowList(L);

    return OK;
    }

/* sqlist.c */
#include <stdio.h>
#include <stdlib.h>
#include "sqlist.h"

// 创建一个空表
sqlist CreateList(void) 
{
    sqlist list = (sqlist)malloc(sizeof(Snode));

    if( list == NULL )
    {
        printf("malloc failed");
        return NULL;
    }
    
    list->len = 0; // 创建一个空表 ,长度为0

    return list;
}

// 判断表是否满
int ListIsFull(sqlist L)
{
    return (L->len == MAX ? TRUE : FALSE);
}
// 表的长度
int GetListLenth(sqlist L)
{
    return L->len;
}

// 判断表是否空
int ListIsEmpty(sqlist L)
{
    if ( L->len == 0 )
    {
        return TRUE; // 采用宏进行替换 增加代码的可读性
    }
    else 
    {
        return FALSE;
    }
}

int  InsertList(sqlist L,int pos,int val)
{
    
    int len = L->len;

    // 1.判断表是否满
    if( len == MAX )
    {
        printf("list is full\n");
        return ERROR;    
    }
    // 2.判断位置合法性
    if( pos < 0 || pos > len )
    {
        printf("pos error\n");
        return ERROR;
    }
    // 3.循环的移动
    int i;

    for( i = len - 1; i >= pos; i-- )
    {
        L->a[i+1] = L->a[i];
    }

    // 4.插入值
    L->a[pos] = val;

    L->len++;

    return OK;
}
// 修改
int ChangeList(sqlist L,int pos,int val)
{
    int len = L->len;

    if( pos < 0 || pos > len - 1 )
    {
        printf("pos error\n");
        return ERROR;
    }

    L->a[pos] = val;

    return OK;
}

// 按位置查询
int SearchList(sqlist L,int pos)
{
    int len = L->len;

    if( pos < 0 || pos > len - 1 )
    {
        printf("pos error\n");
        return ERROR;
    }
    printf("find :%d\n",L->a[pos]);

    return L->a[pos];
    
}
// 按值查询
int SearchListByVal(sqlist L,int val)
{
    int len = L->len;
    int i;

    for( i = 0; i < len; i++ )
    {
        if( L->a[i] == val )
        {
            printf("find pos:%d,val:%d\n",i,L->a[i]);
            return OK;
        }
    }
    
    printf("find nothing\n");
    return ERROR;

}
// 清空顺序表
int ClearList(sqlist L)
{
    L->len = 0;
}


void ShowList(sqlist L)
{
    int len = L->len;
    int i;

    for( i = 0; i < len; i++ )
    {
        printf("%d,",L->a[i]);
    }
    printf("\n");
}

// 删除
int DelList(sqlist L,int pos)
{
    int len = L->len;

    //1.判断表是否空
    if( len == 0 )
    {
        printf("list is empty\n");
        return ERROR;
    }
 
    //2.位置是否合法
    if( pos < 0 || pos > len - 1 )
    {
        printf("pos error\n");
        return ERROR;
    }
    
    //3.循环移动(覆盖)
    int i;

    for( i = pos; i < len; i++ )
    {
        L->a[i] = L->a[i+1];
    }
    L->len--;

    return OK;
}

// 销毁
void DestroyList(sqlist L)
{
    while(--L->len)
    {
        free(L);   
    }
}

 /* sqlist.h */
#ifndef _SQLIST_H
#define _SQLIST_H

/* 宏替换 */

#define TRUE 1   
#define FALSE 0
#define OK 1
#define ERROR -1
#define MAX 100


typedef int data_t;

struct snode{
    data_t a[MAX];
    int len;
};
typedef struct snode Snode;
typedef struct snode * sqlist;


sqlist CreateList(void);                         //创建空表
int InsertList(sqlist L,int pos,data_t val);     //增加
int ChangeList(sqlist L,int pos,data_t val);     //修改
data_t SearchList(sqlist L,int pos);             //查询
int SearchListByVal(sqlist L,data_t val);        //按值查询
int ClearList(sqlist L);                         //清空表
void ShowList(sqlist L);                         //输出表
int DelList(sqlist L,int pos);                   //删除
int GetListLenth(sqlist L);                      //求表的长度
int ListIsFull(sqlist L);                        //判断表是否空        
int ListIsEmpty(sqlist L);                       //判断表是否满
void DestroyList(sqlist L);                      //销毁

#endif



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值