顺序结构实现线性表的基本操作

<pre name="code" class="cpp">    #include <stdio.h>
    #include <stdlib.h>
    typedef int Elementype;
    #define maxsize 10
    typedef struct {
      Elementype data[maxsize];
      int last;

    }List;
    int find (Elementype x,List *ptr)
    {
        int i=1;
        while (i<ptr->last && ptr->data[i]!=x)   ///从i=1开始不断往后查找
            i++;
        if (i>ptr->last)
        {
            printf ("Not found\n");
        return -1;
        }
        else
           printf("The element you want to find in List is %dth\n",i);
           return 0;
    }
    void Insert (Elementype x,int i,List *ptr)
    {
        int j;
        if (ptr->last==maxsize)
        {
            printf ("List is full!\n");
            return;
        }
        if (i<1 || i>ptr->last+2)
        {
            printf ("Error:i\n");
            return;
        }
        for (j=ptr->last;j>=i;j--)
        ptr->data[j+1]=ptr->data[j];  ///将data[i+1]~data[n]向后移
        ptr->data[i]=x;
        ptr->last++;
        return;
    }
    void Delete  (int i,List *ptr)
    {
        int j;
        if (i<1 || i>ptr->last+1)  ///检查空表及删除位置的合法性
        {
            printf ("Not found %dth element\n",i);
            return;
        }
        for  (j=i+1;j<=ptr->last;j++)
            ptr->data[j-1]=ptr->data[j];   ///将data[i+1]~data[n]向前移
            ptr->last--;
            return;
    }
    void Scanf_List (List *p)          ///输入单链表数据
    {
        int i;
        for (i=1;i<=p->last;i++)
            scanf ("%d",&p->data[i]);
    }
    void Printf_List (List*p)  ///遍历单链表
    {
        int i;
        for (i=1;i<=p->last;i++)
            printf ("%d ",p->data[i]);
        printf ("\n");
    }
    int main  ()
    {
        List n;
    printf ("Please input the lenth of List:\n");
     scanf("%d",&n.last);
    printf ("Please input elements of List:\n");
     Scanf_List(&n);
        Printf_List(&n);
        while (1)
        {
            int m;
            printf ("Please choose the operate\n");
            printf ("1:Find\n2:Insert\n3:Delet\n");
            scanf ("%d",&m);
            switch (m)
            {
            case 1:
                {
                  int data;
                  printf ("Please input the element you want to find\n");
                  scanf ("%d",&data);
        find(data,&n);
                }break;
            case 2:
                {
                   int i,h;
                   printf ("Please input the element and position you want to insert\n");
                   scanf ("%d %d",&h,&i);

            Insert(h,i,&n);
            printf ("Insert element is %d\n",h);
            Printf_List(&n);
                }break;
            case 3:
                {
            int a;
            printf ("Please input  position of the element you want to delete\n ");
            scanf ("%d",&a);
            Delete(a,&n);
            printf ("The element was deleted is %d\n",n.data[a]);
            Printf_List(&n);
                }break;
            }
            }
        return 0;
    }

 


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值