C语言实现线性表的增删改查

1.线性表的定义

线性表是一种数据结构,一个线性表是n个具有相同特性的数据元素的有限序列。

2.线性表的创建

sq_pnode create_sqlist()//创建线性表
{
    sq_pnode L=(sq_pnode)malloc(sizeof(sq_node));
    if(L==NULL)
    {
        printf("create is error\n");
        return NULL;
    }
    L->len=0;//初始化时,将长度初始化为0
    return L;
}

3.线性表的判空

int empty_sqlist (sq_pnode L)//判空
{
    if(L==NULL)
    {
        printf("L is NULL");
        return 0;
    }
    if(L->len==0)
        return 1;
    else
        return -1;
}

4.线性表的判满

int full_sqlist (sq_pnode L)//判满
{
    if(NULL==L)
    {
        printf("L is NULL");
        return 0;
    }
    if(L->len==N)
        return 1;
    else
        return -1;
}

5.线性表的插入

//根据位置插入相应的元素
int insert_sqlist(sq_pnode L,int pos,data s)
{
    if(L==NULL)
    {
        printf("L is NULL");
        return 0;
    }
    if(full_sqlist(L)==1)
    {
        printf("hhh");
        return -1;
    }
    if(pos<0 || pos>N)
    {
        printf("jjjj");
        return -1;
    }
    for(int i=L->len-1;i>=pos;i--)//从后往前遍历,直到找到要插入元素的位置
    {
        L->buf[i+1]=L->buf[i];//将需要插入位置后面的元素依次往后移,
    }
    L->buf[pos]=s;//将要要插入的元素赋给相应的位置
    L->len++;//插入元素后,线性表长度要增加一个
    return 0;
}

6.线性表的打印

int show_sqlist(sq_pnode L)//打印
{ 
    if(NULL == L)
    {
        printf("L  is NULL\n");
        return 0;
    }
    if(1 == empty_sqlist(L))
    {
        printf("L  is empty\n");
        return -1;
    }

    for(int i=0;i<L->len;i++)//从最开始依次遍历线性表
    {
        printf("%c",L->buf[i]);//将相应位置的值打印出来
    }
    return 0;
}

7.线性表的删除(根据位置删除元素)

int delete1_sqlist(sq_pnode L,int pos)//根据位置删除元素
{
    if(L==NULL)
    {
        printf("L is NULL");
        return 0;
    }
    if(empty_sqlist(L)==1)
    {
        printf("L is empty");
        return 0;
    }
    for(int i=pos;i<L->len;i++)//从要删除位置开始往后遍历
    {
        L->buf[i]=L->buf[i+1];//将要删除位置的后面的元素依次往前赋值
    }
    L->len--;//删除元素后长度减1
    return 0;
}

8.线性表的查找(根据值查找元素位置)

int find_sqlist(sq_pnode L,data s)//根据值查找元素位置
{
    if(L==NULL)
    {
        printf("L is NULL");
        return 0;
    }
    if(empty_sqlist(L)==1)
    {
        printf("L is empty");
        return 0;
    }
    for(int i=0;i<L->len;i++)//从头遍历线性表
    {
        if(L->buf[i]==s)//把线性表每个位置的元素与查找元素比较
        {
            return i;//如果元素相等,则返回元素的相应位置
        }
    }
    return 0;
}

9.线性表的删除(根据元素值删除元素)

//知道元素,删除线性表中相应的元素
int delete_sqlist(sq_pnode L,data s)
{
    if(L==NULL)
    {
        printf("L is NULL");
        return 0;
    }
    if(empty_sqlist(L)==1)
    {
        printf("L is empty");
        return 0;
    }
    int j=find_sqlist(L,s);//先通过查找函数找到对应元素的位置
    for(int i=j;i<=L->len;i++)//从查找到的位置开始往后遍历
    {
        L->buf[i]=L->buf[i+1];//将要删除元素的位置的后面的元素依次往前赋值
    }
    L->len--;//删除元素后长度减1
    return 0;
}

10.根据位置改变相应的值

int change_sqlist(sq_pnode L,int pos,data s)//改变特定位置的值
{
    if(L==NULL)
    {
        printf("L is NULL");
        return 0;
    }
    if(empty_sqlist(L)==1)
    {
        printf("L is empty");
        return 0;
    }
    for(int i=0;i<=pos;i++)//从头依次遍历,直到遍历到要改变元素的位置时
    {
        if(i=pos)
        {
            L->buf[i]=s;//将新元素赋值给相应的位置
        }
    }
    return 0;
    
}

11.根据位置查找该位置对应的元素

int find1_sqlist(sq_pnode L,int pos)
{
    if(L==NULL)
    {
        printf("L is NULL");
        return 0;
    }
    if(empty_sqlist(L)==1)
    {
        printf("L is empty");
        return 0;
    }
    for(int i=0;i<L->len;i++)//从头依次遍历整个线性表
    {
        if(i=pos)//当遍历到与查到的位置相同时
        {
            printf("下标为%d的值为:%c",i,L->buf[i]);//打印出该位置对应的元素
            return 0;
        }
    }
    printf("没有该位置\n");
    return 0;
}

12.清空线性表

int clear_sqlist(sq_pnode L)
{
    if(L==NULL)
    {
        printf("L is NULL");
        return 0;
    }
    if(empty_sqlist(L)==1)
    {
        printf("L is empty");
        return 0;
    }
    while(1)
    {
        delete1_sqlist(L,0);
        if(L->len==0)
        {
            printf("L is clearing\n");
            return 0;
        }
    } 
}

13.销毁线性表

int destroy_sqlist(sq_pnode *L)
{
    if(*L==NULL)
        printf("L is NULL\n");
    free(*L);
    *L=NULL;
    return 0;
}

14.总代码

sqlist.h文件

#ifndef  _SQLIST_H
#define _SQLIST_H

#include <stdio.h>
#include <stdlib.h>
#define N 1024 //方便修改大小
typedef char data; //方便书写
typedef struct sqlist
{
    data buf[N]; //操作的数据项
    int len;  //操作的长度
}sq_node,*sq_pnode;//方便修改
//创建
sq_pnode create_sqlist(); //struct sqlist* create_sqlist
//插入
int insert_sqlist(sq_pnode L,int pos,data s);
//判满
int full_sqlist(sq_pnode L);
//判空
int empty_sqlist(sq_pnode L);
//打印
int show_sqlist(sq_pnode L);
//根据下标删除
int delete1_sqlist(sq_pnode L,int pos);
//根据元素值删除
int delete_sqlist(sq_pnode L,data s);
//改
int change_sqlist(sq_pnode L,int pos,data s);
//根据元素值查找
int find_sqlist(sq_pnode L,data s);
//根据下标查找
int find1_sqlist(sq_pnode L,int pos);
//清空
int clear_sqlist(sq_pnode L);
//销毁
int destroy_sqlist(sq_pnode *L);


#endif // !1

功能函数(sqlist.c文件)

#include "sqlist.h"
sq_pnode create_sqlist()//创建线性表
{
    sq_pnode L=(sq_pnode)malloc(sizeof(sq_node));
    if(L==NULL)
    {
        printf("create is error\n");
        return NULL;
    }
    L->len=0;//初始化时,将长度初始化为0
    return L;
}
int empty_sqlist (sq_pnode L)//判空
{
    if(L==NULL)
    {
        printf("L is NULL");
        return 0;
    }
    if(L->len==0)
        return 1;
    else
        return -1;
}
int full_sqlist (sq_pnode L)//判满
{
    if(NULL==L)
    {
        printf("L is NULL");
        return 0;
    }
    if(L->len==N)
        return 1;
    else
        return -1;
}
//根据位置插入相应的元素
int insert_sqlist(sq_pnode L,int pos,data s)
{
    if(L==NULL)
    {
        printf("L is NULL");
        return 0;
    }
    if(full_sqlist(L)==1)
    {
        printf("hhh");
        return -1;
    }
    if(pos<0 || pos>N)
    {
        printf("jjjj");
        return -1;
    }
    for(int i=L->len-1;i>=pos;i--)//从后往前遍历,直到找到要插入元素的位置
    {
        L->buf[i+1]=L->buf[i];//将需要插入位置后面的元素依次往后移,
    }
    L->buf[pos]=s;//将要要插入的元素赋给相应的位置
    L->len++;//插入元素后,线性表长度要增加一个
    return 0;
}
int show_sqlist(sq_pnode L)//打印
{ 
    if(NULL == L)
    {
        printf("L  is NULL\n");
        return 0;
    }
    if(1 == empty_sqlist(L))
    {
        printf("L  is empty\n");
        return -1;
    }

    for(int i=0;i<L->len;i++)//从最开始依次遍历线性表
    {
        printf("%c",L->buf[i]);//将相应位置的值打印出来
    }
    return 0;
}
int delete1_sqlist(sq_pnode L,int pos)//根据位置删除元素
{
    if(L==NULL)
    {
        printf("L is NULL");
        return 0;
    }
    if(empty_sqlist(L)==1)
    {
        printf("L is empty");
        return 0;
    }
    for(int i=pos;i<L->len;i++)//从要删除位置开始往后遍历
    {
        L->buf[i]=L->buf[i+1];//将要删除位置的后面的元素依次往前赋值
    }
    L->len--;//删除元素后长度减1
    return 0;
}
int find_sqlist(sq_pnode L,data s)//根据值查找元素位置
{
    if(L==NULL)
    {
        printf("L is NULL");
        return 0;
    }
    if(empty_sqlist(L)==1)
    {
        printf("L is empty");
        return 0;
    }
    for(int i=0;i<L->len;i++)//从头遍历线性表
    {
        if(L->buf[i]==s)//把线性表每个位置的元素与查找元素比较
        {
            return i;//如果元素相等,则返回元素的相应位置
        }
    }
    return 0;
}
//知道元素,删除线性表中相应的元素
int delete_sqlist(sq_pnode L,data s)
{
    if(L==NULL)
    {
        printf("L is NULL");
        return 0;
    }
    if(empty_sqlist(L)==1)
    {
        printf("L is empty");
        return 0;
    }
    int j=find_sqlist(L,s);//先通过查找函数找到对应元素的位置
    for(int i=j;i<=L->len;i++)//从查找到的位置开始往后遍历
    {
        L->buf[i]=L->buf[i+1];//将要删除元素的位置的后面的元素依次往前赋值
    }
    L->len--;//删除元素后长度减1
    return 0;
}

int change_sqlist(sq_pnode L,int pos,data s)//改变特定位置的值
{
    if(L==NULL)
    {
        printf("L is NULL");
        return 0;
    }
    if(empty_sqlist(L)==1)
    {
        printf("L is empty");
        return 0;
    }
    for(int i=0;i<=pos;i++)//从头依次遍历,直到遍历到要改变元素的位置时
    {
        if(i=pos)
        {
            L->buf[i]=s;//将新元素赋值给相应的位置
        }
    }
    return 0;
    
}
//根据位置查找元素
int find1_sqlist(sq_pnode L,int pos)
{
    if(L==NULL)
    {
        printf("L is NULL");
        return 0;
    }
    if(empty_sqlist(L)==1)
    {
        printf("L is empty");
        return 0;
    }
    for(int i=0;i<L->len;i++)//从头依次遍历整个线性表
    {
        if(i=pos)//当遍历到与查到的位置相同时
        {
            printf("下标为%d的值为:%c",i,L->buf[i]);//打印出该位置对应的元素
            return 0;
        }
    }
    printf("没有该位置\n");
    return 0;
}
int clear_sqlist(sq_pnode L)
{
    if(L==NULL)
    {
        printf("L is NULL");
        return 0;
    }
    if(empty_sqlist(L)==1)
    {
        printf("L is empty");
        return 0;
    }
    while(1)
    {
        delete1_sqlist(L,0);
        if(L->len==0)
        {
            printf("L is clearing\n");
            return 0;
        }
    } 
}
int destroy_sqlist(sq_pnode *L)
{
    if(*L==NULL)
        printf("L is NULL\n");
    free(*L);
    *L=NULL;
    return 0;
}

main.c文件

#include <stdio.h>
#include "sqlist.h"
int main(int argc, char *argv[])
{ 
    int a,i;
    sq_pnode L=create_sqlist();
    insert_sqlist(L,0,'a');
    insert_sqlist(L,1,'b');
    insert_sqlist(L,2,'c');
    insert_sqlist(L,3,'d');
    insert_sqlist(L,4,'e');
    insert_sqlist(L,5,'f');
    insert_sqlist(L,6,'g');
    printf("插入后的顺序:\n");
    show_sqlist(L);
    printf("\n");

    delete1_sqlist(L,2);
    printf("删除下标为2后的排序:\n");
    show_sqlist(L);
    printf("\n");

    printf("e的位置%d\n",find_sqlist(L,'e'));
    printf("\n");

    change_sqlist(L,3,'h');
    printf("将下标为3改为h后的排序:\n");
    show_sqlist(L);
    printf("\n");

    delete_sqlist(L,'h');
    printf("删除h后的排序:\n");
    show_sqlist(L);
    printf("\n");

    find1_sqlist(L,4);
    printf("\n");

  //  clear_sqlist(L);

    destroy_sqlist(&L);
    if(L==NULL)
    {
        printf("销毁成功\n");
    }
    return 0;
} 

  • 7
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值