顺序表是黄金年代种最基本、最轻便易行、也是最常用的数据布局,上面包车型地铁次序演示了对顺序表的基本操作。

#include “stdio.h”
#include “stdlib.h” //该头文件包括了realloc(State of Qatar函数、exit
#define List_Size 100
/**
**宏定义
*/
#define ListIncrement 10
#define OK 1
#define ERROR 0
#define OVERFLOW -1
/**
*typedef 注解自定义数据类型
*/
typedef int ElemType;
typedef struct{
 ElemType *elem;
 int length;
 int listsize;
}SqList;
int InitSqList(SqList &L卡塔尔{//顺序表的开首化
 L.elem=new ElemType[List_Size];
 if(!L.elem) exit(OVERFLOW);
 L.length=0;
 L.listsize=List_Size;
 return OK;
}
void CreateSqList(SqList &L,int nState of Qatar{//建设构造顺序表
 int i;
 for(i=0;i<n;i++){
  scanf(“%d”,&L.elem[i]);
  L.length++;
 }
}
void DeastroySqList(SqList &L卡塔尔国{//销毁顺序表,释放内部存储器空间
 delete L.elem;
}
int SqListInsert(SqList &L,int i,ElemType x卡塔尔(قطر‎{//插入操作   
///算法是相比较根本的
 int j;
 ElemType *p;
 if(i<1||i>L.length+1) return ERROR;
 if(L.length>=L.listsize){
 
p=(ElemType*)realloc(L.elem,(L.listsize+ListIncrement)*sizeof(ElemType));
  if(!p) exit(OVERFLOW);
  L.elem=p;
  L.listsize=L.listsize+ListIncrement;
 }
 for(j=L.length-1;j>=i-1;j–)
  L.elem[j+1]=L.elem[j];
 L.elem[i-1]=x;
 ++L.length;
 return OK;
}
int SqListDelete(SqList &L,int i卡塔尔国{//删除操作
 int j;
 if(i<1||i>L.length) return ERROR;
 for(j=i-1;j<L.length-1;j++)
  L.elem[j]=L.elem[j+1];
 L.length–;
 return OK;
 
}

C语言构造顺序表数据读写难题
有以下音讯保管程序,不知怎能力把数量写入文件再读取?这一个经测验应该能够写入,但重启读取时出错或无法读取。手提式有线电话机测量试验快一星期了
#include
#include
#include
#include
#define MAXSIZE 1000
#define OVERFLOW -2
#define OK 1
#define ERROR 0
typedef int Status; // 定义函数重回值类型

#include <stdio.h>
#include <stdlib.h>
#define LIST_INTSIZE 50
typedef char DataType;
typedef struct
{
DataType* elem;    //顺序表的营地址
int length;        //顺序表当前长度
int listsize;      //顺序表当前抽成的积攒容积
}SeqList;
int InitSeqList(SeqList* L卡塔尔国    //最早化顺序表
{
L->elem=(DataType *)malloc((LIST_INTSIZE+1)*sizeof(DataType));
if (L->elem==NULL)
   {
     printf(“ttt内部存储器分配错误n”);
     return 0;
   }
L->length=0;
L->listsize=LIST_INTSIZE;
printf(“ttt内部存款和储蓄器分配成功”卡塔尔国;
return 1;
}
int InsertSeqList(SeqList* L,int i,DataType x卡塔尔国//对顺序表实行扦插操作,插入格式如(1,a卡塔尔国
{
int j;
if (L->length==L->listsize)
    {
      printf(“ttt顺序表已满”卡塔尔国;
      return 0;
    }

int LocateElem(SqList L,ElemType xState of Qatar{//按值查找
 int i=1;
 while(i<=L.length&&L.elem[i-1]!=x) i++;
 if(i<=L.length) return i;
 else return ERROR;
}
void DispSqList(SqList L卡塔尔国{//依次输出线性表中的兼具因素
 int i;
 for(i=0;i<L.length;i++)
  printf(“%dt”,L.elem[i]);
 printf(“n”);
}
void main(){
 SqList L;
 int i;
 ElemType x;
 InitSqList(L);//……
 printf(“please input some messages!!!n”);
 CreateSqList(L,5);
 printf(“Data to be showed!!!n”);
 DispSqList(L);
 printf(“请输入要插入的数:n”);
  scanf(“%d”,&x);
 printf(“请输入要插入位置:n”);
  scanf(“%d”,&i);
 while(!SqListInsert(L,i,x)){
   printf(“插入地点越界!!!请输入准确的插入地方:n”);
   scanf(“%d”,&i);
  }
 printf(“插入成功!!!n”);
 DispSqList(L);
 
 printf(“请输入删除数的任务:n”);
 scanf(“%d”,&x);
 while(!SqListDelete(L,x)){
  printf(“删除地点越界!!!请输入正确的删减数的职位:n”);
  scanf(“%d”,&x);
 }
 printf(“删除成功!!!n”);
 DispSqList(L);
 
 printf(“请输入要探索的数:”卡塔尔国;
 scanf(“%d”,&x);
 if(!LocateElem(L,x)){
  printf(“未有你要索求的数!!!”);
 }else{
  printf(“已查到您要找的数!!!n”);
  printf(“该数在线性表中的岗位:”卡塔尔国;
  printf(“%dn”,LocateElem(L,x));
 }
 DeastroySqList(L);
}
 

struct
{
char uname[22] = “root”;
char upass[11] = “passwd”;
} user;

else
{
   if (i<1||i>L->length+1)
{
       printf(“ttt地点不合规”卡塔尔(قطر‎;
       return 0;
    }

 
我“技能实验室”

int Login()
{
char uname[22], upass[11];
for (int i = 0; i < 4; i++)
{
printf;
scanf(“%s”, &uname);
printf;
scanf(“%s”, &upass);
if (strcmp(user.upass, upass) == 0 && strcmp(user.uname, uname) == 0)
break;
else
{
puts(“客商名或密码输入有误!”卡塔尔国;
if
continue;
}
puts(“尝试过多,请重启程序”卡塔尔(قطر‎;
return 0;
}
}

    else
      {
        for(j=L->length;j>=i;j–)
          {
            L->elem[j+1]=L->elem[j];
          }
   L->elem[i]=x;
   L->length++;
        return 1;
      }
}
}
int DeleteSeqList(SeqList* L,int i卡塔尔国 //对顺序表展开删减操作
{
int j;
if (i<1||i>L->length)
    {
      printf(“ttt荒诞不经第i个元素”卡塔尔(قطر‎;
      return 0;
    }
else
    {
      for (j=i;j<L->length;j++)
        {
          L->elem[j]=L->elem[j+1];
        }
   L->length–;
      return 1;
    }

stdio.h #include stdlib.h
//该头文件蕴涵了realloc(卡塔尔(قطر‎函数、exit #define List_Size 100 /**
**宏定义 */ #define ListIncrement 10 #define OK 1 #define ERROR 0
#def…

typedef struct
{
char num[11]; // 学号
char name[22]; // 姓名
double grade0, grade1, grade2, grade, avg; // 成绩
} student;

}
int LenSeqList(SeqList* L卡塔尔(قطر‎ //求顺序表表长
{
int n=0;
int i=1;
for (i=1;i<=L->length;i++)
    {
      n++;
    }
return n;
}
int SearchSeqList(SeqList* L,DataType x卡塔尔国 //按值查找
{

typedef student ElemType;

int j=1;
while (j<=L->length && L->elem[j]!=x)
   j++;
if(j>L->length)
{
   printf(“ttt找不到你要的值,请重新输入”卡塔尔;
   return (0);
}
else
   return j;
}
DataType GetfromSeqList(SeqList* L,int i卡塔尔 //按地方查找
{
DataType x;
if(i<1||i>L->length)
    {
      printf(“ttt查找地方违法”卡塔尔;
      return 0;
    }
else
    {
      x=L->elem[i];
      return x;
    }
}
void ShowSeqList(SeqList* LState of Qatar //呈现顺序表
{
int i;
printf(“nttt彰显线性表的装有因素: “State of Qatar;
if (L->length==0)
    {
      printf(“nttt链表为空”State of Qatar;
    }
else
    {
      printf(“nttt”);
      for(i=1;i<=L->length;i++)
        {
          printf(“t%c”,L->elem[i]);
        }
    }
}
main()
{
int choice;
int i,n=0;
int j=1;
SeqList L;
DataType x;
while(j)
    {
      printf(“nnnn”);
      printf(“ttt–线性顺序表–n”);
     
printf(“nttt*****************************************”);
      printf(“nttt*      1—–顺序表初步化             
**”);
      printf(“nttt*      2—–插        入             
**”);
      printf(“nttt*      3—–删        除             
**”);
      printf(“nttt*      4—–求   表   长             
**”);
      printf(“nttt*      5—–按 值 查 找             
**”);
      printf(“nttt*      6—–读 取 元素值             
**”);
      printf(“nttt*      7—–显 示 线性表             
**”);
      printf(“nttt*      0—–退        出             
**”);
     
printf(“nttt*****************************************n”);
      printf(“ttt请采纳菜单号(0–7卡塔尔(قطر‎: “State of Qatar;
      scanf(“%d”,&choice);getchar();
      if(choice==1)
        {
          InitSeqList(&L);
        }
      else if(choice==2)
        {
          printf(“nttt请输入地方i和数值x(输入格式: i,x):”卡塔尔(قطر‎;
          scanf(“%d,%c”,&i,&x);
          InsertSeqList(&L,i,x);
        }
      else if(choice==3)
        {
          printf(“nttt请输入要删减成分之处i(输入格式:i卡塔尔”State of Qatar;
          scanf(“%d”,&i);
          DeleteSeqList(&L,i);
    printf(“nttt删除成功”卡塔尔国;
        }
      else if(choice==4)
        {
          n=LenSeqList(&L);
    printf(“nttt线性表的表长为:%d”,n卡塔尔(قطر‎;
        }
    else if(choice==5)
        {
    printf(“nttt请输入要查究的值x(输入格式:x卡塔尔:”State of Qatar;
    scanf(“%c”,&x);
          i=SearchSeqList(&L,x);
    printf(“nttt你查找值的地点为:%d”,i卡塔尔(قطر‎;

typedef struct
{
ElemType *elem; // 存款和储蓄空间集散地址
int length; // 当前长度
} SqList;

        }
   else if(choice==6)
        {
    printf(“nttt请输入要搜索的地点i(输入格式:i卡塔尔:”State of Qatar;
          scanf(“%d”,&i);
          x=GetfromSeqList(&L,i);
    printf(“nttt你找出的值为:%c”,xState of Qatar;
        }
      else if(choice==7)
        {
          ShowSeqList(&L);
        }
      else if(choice==0)
        {
          j=0;
          printf(“ttt程序甘休!n”);
        }
      else printf(“nttt输入错误!请重新输入!n”);
    }
}

Status InitList(SqList * L卡塔尔(قطر‎ // 构造空顺序表
{
L->elem = (ElemType *) malloc(sizeof * MAXSIZE);
if (!L->elem)
exit;
L->length = 0;
return OK;
}

ElemType GetElem(SqList & L, int i卡塔尔(قطر‎ // 访问顺序表,找到i地点,重回给e
{
return L.elem[i];
}

int SearchName(SqList & L, char str[]卡塔尔(قطر‎ //
依照名字查找,重回该同学在挨门逐户表中的编号
{
for (int i = 1; i <= L.length; i++)
{
if (strcmp(L.elem[i].name, str) == 0)
return i;
}
return 0;
}

int SearchNum(SqList & L, char strn[]卡塔尔(قطر‎ //
依照学号查找,重返该同学在大器晚成一表中的数码
{
for (int i = 1; i <= L.length; i++)
{
if (strcmp(L.elem[i].num, strn) == 0)
return i;
}
return 0;
}

void SortList(SqList & L)
{
int i, k;
ElemType temp;
for (i = 1; i < L.length; i++)
{
for (k = i + 1; k < L.length + 1; k++)
{
if (L.elem[i].avg < L.elem[k].avg)
{
temp = L.elem[i];
L.elem[i] = L.elem[k];
L.elem[k] = temp;
}
}
}
puts;
}

Status ListInsert(SqList & L, int i, ElemType eState of Qatar //
在i地点插入有个别学生的音信
{
if ( || (i > L.length + 1))
return ERROR;
if (L.length == MAXSIZE)
return ERROR;
for (int j = L.length; j >= i; j–)
{
L.elem[j + 1] = L.elem[j];
}
L.elem[i] = e;
++L.length;
return OK;
}

Status ListDelete(SqList & L, int i卡塔尔(قطر‎ // 在豆蔻梢头一表中除去i地方的上学的小孩子消息
//
{
if ( || (i > L.length))
return ERROR;
for (int j = i; j <= L.length; j++)
{
L.elem[j] = L.elem[j + 1];
}
–L.length;
return OK;
}

void Input(ElemType * e)
{
printf;
scanf(“%s”, e->name);
printf;
scanf(“%s”, e->num);
printf;
scanf(“%lf”, &e->grade0);
printf;
scanf(“%lf”, &e->grade1);
printf;
scanf(“%lf”, &e->grade2);
e->grade = e->grade0 + e->grade1 + e->grade2;
e->avg = e->grade / 3;
printf(“输入完毕nn”);
}

void Output(ElemType * e)
{
printf(“姓名:%-st学号:%-st英语:%5.2lft数学:%5.2ft计算机:%5.2f平均:%5.2fnn”,
e->name, e->num, e->grade0, e->grade1, e->grade2,
e->avg);
}

void Save(ElemType * e)
{
}

int main()
{
SqList L;
ElemType a,b,c;
int x,choose;
FILE fp;
fp = fopen(“/sdcard/ATEST/info”, “rb+”);
if(fp == NULL)
{
fp = fopen(“/sdcard/ATEST/info”,”wb”);
}
else
{
for (int i = 1; i <= 1000; i++)
{
if break;
fread(&L.elem[i],sizeof, 1, fp);
/
fread(&L.elem[i].name,sizeof(L.elem[i].name), 1, fp);
fread(&L.elem[i].num,sizeof(L.elem[i].num), 1, fp);
fread(&L.elem[i].grade0,sizeof, 1, fp);
fread(&L.elem[i].grade1,sizeof, 1, fp);
fread(&L.elem[i].grade2,sizeof, 1, fp);
fread(&L.elem[i].avg,sizeof, 1, fp);
*/ }
}
// Login();
puts(“***************************学员成绩管理体系**********************************nttt1.早先化学子音信nttt2.查看录入的学子消息nttt3.按姓名查看学子音讯nttt4.按学号查看学子音信nttt5.按平均战表排序nttt6.删除学子音讯nttt7.保存学子新闻nttt8.增添上学的小孩子音信nttt9.校订学子新闻nttt0.退出n*******************************************************************************n”);
while
{
lo:
printf;
scanf(“%d”, &choose);
switch
{
case 0:
char q;
printf(“确认退出?;
scanf(“%s”, &q);
if (q == ‘y’ || q == ‘Y’)
{
fclose;
printf;
return 0;
break;
}
else
goto lo;
break;
case 1:
if (InitList
{
printf(“请输入要录入学子的总人口:”卡塔尔(قطر‎;
scanf(“%d”, &x);
if
break;
else
{
for (int i = 1; i <= x; i++)
{
printf(“第%d个学生:n”, i);
Input(&L.elem[i]);
}
L.length = x;
puts;
}
break;
}
else
printf(“开首化退步nn”);
break;
case 2:
for (int i = 1; i <= x; i++)
{
a = GetElem;
Output;
}
break;
case 3:
char s[22];
printf(“请输入要物色的上学的小孩子姓名:”卡塔尔;
scanf;
if (SearchName
Output(&L.elem[SearchName;
else
puts(“对不起,查无此人”卡塔尔;
puts;
break;
case 4:
char n[11];
printf(“请输入要物色的学号:”卡塔尔;
scanf;
if (SearchNum
Output(&L.elem[SearchNum;
else
puts(“对不起,查无这个人”卡塔尔(قطر‎;
puts;
break;
case 5:
SortList;
break;
case 6:
char r[22];
printf(“请输入要刨除音信的上学的儿童姓名:”卡塔尔国;
scanf;
if (int id = SearchName
{
if (ListDelete
{
x–;
puts;
puts;
}
}
else
puts(“对不起,查无此人”State of Qatar;
puts;
break;
case 7:
fseek;
for (int i = 1; i <= x; i++)
{
fwrite(&L.elem[i],sizeof, 1, fp);
/*fwrite(&L.elem[i].name,sizeof(L.elem[i].name), 1, fp);
fwrite(&L.elem[i].num,sizeof(L.elem[i].num), 1, fp);
fwrite(&L.elem[i].grade0,sizeof, 1, fp);
fwrite(&L.elem[i].grade1,sizeof, 1, fp);
fwrite(&L.elem[i].grade2,sizeof, 1, fp);
fwrite(&L.elem[i].avg,sizeof, 1, fp);*/
}
break;
case 8:
printf(“输入要插入的职位:”卡塔尔(قطر‎;
int iid;
scanf(“%d”, &iid);
if (iid > L.length + 1 || iid < 0)
{
puts;
break;
}
printf(“请输入学子新闻:n”);
Input;
if (ListInsert(L, iid, c))
{
x++;
puts;
puts;
}
else
{
puts;
puts;
}
break;
case 9:
char m[22];
printf(“请输入要修正音讯的上学的小孩子姓名:”卡塔尔(قطر‎;
scanf;
if (SearchName
{
puts;
Output(&L.elem[SearchName;
puts(“n改过消息:”卡塔尔国;
Input(&L.elem[SearchName;
puts;
}
else
puts(“对不起,查无此人”卡塔尔国;
puts;
break;
default:
printf(“选取无效,请重新”卡塔尔(قطر‎;
continue;
}
}
return 0;
}