顺序表C语言代码实现

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<malloc.h>
#define MAXSIZE 100


/****************************************
 *   结构体的声明                *
 ****************************************/
typedef struct std_info
{
 int Num;
 char Name[8];
 float Score;
}ElemType;


/****************************************
 *    头指针的定义    *
 ****************************************/
typedef struct
{
 ElemType elem[MAXSIZE];
 int length;
}SeqList;


/****************************************
 *             创建链表                 *
 ****************************************/
void Create_SeqList(SeqList *L1)
{
 int i,n;
 int num;
 char name[8];
 float score;

 printf("请输入顺序表的长度:/n");
 scanf("%d",&n);
 L1->length=n;
 
 for(i=1;i<=L1->length;i++)
 {
  printf("请输入一个学生的信息:Num,Name,Score/n");
  scanf("%d",&num);
  scanf("%s",name);
  scanf("%f",&score);
  getchar();

  L1->elem[i].Num=num;
  strcpy(L1->elem[i].Name,name);
  L1->elem[i].Score=score;

 }
}

 

/****************************************
 *             链表输出                 *
 ****************************************/
void Printf_SeqList(SeqList *L1)
{
 int i;
 printf("/t/t***Printf_SeqList***/n");
 printf("Num/tName/t/tScore/n");
 for(i=1;i<=L1->length;i++)
 {
  printf("%d/t%s/t/t%f/n",L1->elem[i].Num,L1->elem[i].Name,L1->elem[i].Score);
 }
 printf("/n");
}


/****************************************
 *             链表-插入                *
 ****************************************/
int Insert_SeqList(SeqList *L1,int i,ElemType elemtype)
{
 int j;

 if(L1->length==MAXSIZE-1)
 {
  printf("Surface/n");
  return (-1);
 }

 if(i<1||i>L1->length+2)
 {
  printf("position error/n");
  return (0);
 }

 for(j=L1->length;j>=i-1;j--)
 {
  L1->elem[j+1]=L1->elem[j];
 }
 L1->elem[i-1]=elemtype;
 L1->length++;
 return (1);
}


/****************************************
 *             链表-删除                *
 ****************************************/
int Delete_SeqList(SeqList *L1,int i)
{
 int j;
 if(i<1||i>L1->length+1)
 {
  printf("The elem doesn't i/n");
  return (0);
 }
 for(j=i;j<L1->length;j++)
 {
  L1->elem[j-1]=L1->elem[j];
 }
 L1->length--;
 return (1);
}


/****************************************
 *             链表-逆置                *
 ****************************************/
int Inversion_SeqList(SeqList *L1)
{
 int i,j;
 ElemType temp;
 for(i=1,j=L1->length;i<j;i++,j--)
 {
  temp=L1->elem[i];
  L1->elem[i]=L1->elem[j];
  L1->elem[j]=temp;
 }
 return 1;
}


/****************************************
 *             链表-排序                *
 ****************************************/
int Sort_SeqList(SeqList *L1)
{
 int i,j;
 ElemType temp;
 for(j=1;j<=L1->length-1;j++)
  for(i=1;i<=L1->length-j;i++)
  {
   if(L1->elem[i].Num>L1->elem[i+1].Num)
   {
    temp=L1->elem[i];
    L1->elem[i]=L1->elem[i+1];
    L1->elem[i+1]=temp;
   }
  }
  return 1;
}


/****************************************
 *             链表-顺序插入            *
 ****************************************/
int Insert1_SeqList(SeqList *L1,ElemType elemtype)
{
 int i;
 if(L1->length==MAXSIZE-1)
 {
  printf("表满/n");
 }
 ++L1->length;
 for(i=L1->length-1;L1->elem[i].Num>elemtype.Num;i--)
  L1->elem[i+1]=L1->elem[i];
 L1->elem[i+1]=elemtype;
 return 1;
}

/****************************************
 *         链表-删除i以后k个元素        *
 ****************************************/

int Delete1_SeqList(SeqList *L1,int i,int k)
{
 int j;
 if(i<1||k<0||i+k-1>L1->length-1)
 {
  printf("删除位置错误/n");
  return (0);
 }
 for(j=1;i+j-1<=i+k;j++)
 {
  L1->elem[i+j-1]=L1->elem[i+j];
 }
 return 1;
}

/****************************************
 *             链表操作                 *
 ****************************************/
void Print(SeqList *L1)
{
 int i,j=1,k;

 int num;
 char name[8];
 float score;
 ElemType elemtype;

 printf("/t&&Please select the operation of the table&&/n");
 while(j)
 {
  printf("***********************************************************/n");
  printf("*/t1.The order of the table insert/n");
  printf("*/t2.The order of the table delete/n");
  printf("*/t3.The order of the table Inversion(逆置)/n");
  printf("*/t4.The order of the table sort     /n");
  printf("*/t5.插入一个学生信息放入递增排序的顺序表适当位置/n");
  printf("*/t6.删除第i个元素后的k个元素/n");
  printf("*/t0.Exit/n");
  printf("*************************************************************/n");

  scanf("%d",&j);
  switch(j)
  {
  case 0:
   printf("Bye-Bye ..../n");
   exit(1);
   break;

  case 1:
   printf("Please input one student massege:Num,Name,Score/n");
   scanf("%d",&num);
   scanf("%s",name);
   scanf("%f",&score);
   getchar();

   printf("Please input number to Insert_SeqList/n");
   scanf("%d",&i);

   elemtype.Num=num;
   strcpy(elemtype.Name,name);
   elemtype.Score=score;
   i=Insert_SeqList(L1,i,elemtype);
   Printf_SeqList(L1);        
   printf("OK/n");
   break;

  case 2:
   printf("Please enter the Location you want to delete/n");
   scanf("%d",&i);
   i=Delete_SeqList(L1,i);
      Printf_SeqList(L1);         
   printf("OK/n");
   break;

  case 3:
   Inversion_SeqList(L1);
   Printf_SeqList(L1);
   printf("OK/n");
   break;

  case 4:
   Sort_SeqList(L1);
   Printf_SeqList(L1);
   printf("OK/n");
   break;

  case 5:
   printf("Please you input one student Num Name Score/n");
   scanf("%d",&num);
   scanf("%s",name);
   scanf("%f",&score);

   elemtype.Num=num;
   strcpy(elemtype.Name,name);
   elemtype.Score=score;
   Insert1_SeqList(L1,elemtype);
   Printf_SeqList(L1);
   printf("OK/n");
   break;

  case 6:
   printf("Please input i and delete i last k number/n");
   scanf("%d",&i);
   scanf("%d",&k);
   Delete1_SeqList(L1,i,k);
   Printf_SeqList(L1);
   printf("OK/n");
   break;
  default :printf("Input error,Retype number/n");
  }
 }
}


void main()
{
 SeqList *L=(SeqList *)malloc(sizeof(SeqList));
 Create_SeqList(L);
 Printf_SeqList(L);
 Print(L);
 free(L);
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wbandzlhgod

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值