zoukankan      html  css  js  c++  java
  • 线性表的实现用通用方法实现线性表的初始化、求表长、插入元素、删除元素等

    //线性表的通用程序,c语言实现

    #include <stdio.h>

    #include <stdlib.h>

    #define MaxSize 50

    typedef char DataType;

    typedef struct node

    {

      DataType data[MaxSize]; int last;

    }Lnode,*List;

    //初始化线性表

    void Init_List(List L)

    {

      L->last = 0;

    }

    //线性表的长度

    int Length_List(List L)

    {

      return L->last;

    }

    //取表中元素

    DataType Get_List(List L, int i, DataType x)

    {

      if (i<1 || i>L->last)

        printf("error!!!");

      else

        x = L->data[i-1]; return x;

    }

    //查找表L中值为x的元素,其结果返回在表L中首次出现的值为x元素的序号或地址

    DataType Location_List(List L, DataType x)

    {

      int i = 0;

      while (i < L->last && L->data[i] != x)

        i++;

      if (i == L->last)

        return -1;

      else

        return (i + 1);

    }

    //在线性表的第i个位置插入值为x人元素

    void Insert_List(List L, int i, DataType x)

    {

      int j;

      if (i<1 || i>L->last + 1)

        printf("插入位置错!!!\n");

      else

      {

        for (j = L->last; j >= i; j--)

          L->data[j] = L->data[j - 1];

        L->data[i - 1] = x;

      }

      L->last++;

    }

    //删除线性表第i个位置上的元素

    void Delete_List(List L, int i)

    {

      int j;

      if (i<1 || i>L->last)

        printf("del error");

      else

      {

        for (j = i; j < L->last; j++)

          L->data[j - 1] = L->data[j];

        L->last--;

      }

    }

    //输出线性表

    void Print_List(List L)

    {

      int i;

      for (i = 1; i < L->last; i++)

        printf("%c->",L->data[i-1]);

      printf("%c",L->data[L->last-1]);

    }

    ///////////////主函数////////////////

    void main()

    {

      int i = 1, n;

      Lnode L;

      char ch, x;

      Init_List(&L);

      printf("\n\n\n***************线性表演示程序****************\n");

      printf("请输入您想建立的线性表的元素,以#结束:");

      ch = getchar();

      while (ch != '#')

      {

      Insert_List(&L,i,ch);

      i++;

      ch = getchar();

      }

      printf("你建立的线性表为:");

      Print_List(&L);

      printf("\n线性表的长度为:%d",L.last);

      //fflush(stdin);

      printf("\n输入你想查找的元素:");

      fflush(stdin);

      scanf("%c",&x);

      printf("你查找的元素为%c序位为%d\n",x,Location_List(&L,x));

      printf("输入你想查找的元素序位:");

      scanf("%d",&n);

      printf("\n你查找的元素为:%c\n",Get_List(&L,n,x));

      printf("输入你想插入的元素以及序位:<用逗号隔开>");

      fflush(stdin);

      scanf("%c,%d",&x,&n);

      Insert_List(&L,n,x);

      printf("\n插入后的线性表为:\n");

      Print_List(&L);

      fflush(stdin);

      printf("\n请输入你想删除的元素序位:");

      scanf("%d",&n);

      Delete_List(&L,n);

      printf("\n删除后的线性表为:\n");

      Print_List(&L);

      printf("\n");

      system("pause");

    }

    运行结果:

  • 相关阅读:
    定制化培养:破解企业人才之困
    IT毕业生需要具备的六种能力素质
    JAVA值传递or引用传递
    就业形势严峻 毕业生需练好“内功”
    如何改变mysql auto increment 步长和初始值
    python变量作用域
    关于python的lxml.html 的fromstring 函数
    python string 到date object
    python mysql 连接数据库 latin1 codec错误
    python 使用 mysqldb 批量插入数据
  • 原文地址:https://www.cnblogs.com/duanqibo/p/11098480.html
Copyright © 2011-2022 走看看