Mobile wallpaper 1Mobile wallpaper 2Mobile wallpaper 3Mobile wallpaper 4Mobile wallpaper 5Mobile wallpaper 6
202 字
1 分钟
数据结构顺序表复习

code:

//顺序表结构体+main函数
#include<stdio.h>
#include<stdlib.h>
#define OK 1
#define ERROR 2
#define OVERFLOW 3
#define INIT_LIST_SIZE 100
typedef int Status;
typedef int ElemType;
typedef struct{
ElemType *elem;
int length;
int listsize;
}Sqlist;
//初始化
Status Init_sq(Sqlist &L){
L.elem=(ElemType*)malloc(sizeof(ElemType)*INIT_LIST_SIZE);
if(!L.elem)return OVERFLOW;
L.length=0;
L.listsize=INIT_LIST_SIZE;
return OK;
}
//释放占用内存
Status Destroy_Sq(Sqlist &L){
if(!L.elem)return ERROR;
free(L.elem);
L.length=0;
L.listsize=0;
L.elem=NULL;
return OK;
}
//清空线性表
Status Clear_Sq(Sqlist &L){
if(!L.elem)return ERROR;
L.length=0;
return OK;
}
//获取元素
Status GetElem_sq(Sqlist L,int i,ElemType &e){
if(i<1||i>L.length)return ERROR;
e=L.elem[i-1];
return OK;
}
//插入元素
Status Insert_sq(Sqlist &L,int i,ElemType e){
int j;
if(i<1||i>L.length+1)return ERROR;
if(L.length>=L.listsize)return ERROR;
for(j=L.length-1;j>=i-1;j--){
L.elem[j+1]=L.elem[j];
}
L.elem[i-1]=e;
L.length++;
return OK;
}
//删除元素
Status Delete_sq(Sqlist &L,int i,ElemType &e){
int j;
if(i<1||i>L.length)return ERROR;
e=L.elem[i-1];
for(j=i-1;j<=L.length-1;j++){
L.elem[j]=L.elem[j+1];
}
L.length--;
return OK;
}
int main(){
int i;
ElemType e;
Sqlist list;
Init_sq(list);
for(i=1;i<=10;i++){
e=rand()%100;
Insert_sq(list,i,e);
}
for(i=1;i<=10;i++){
GetElem_sq(list,i,e);
printf("%d\n",e);
}
for(i=10;i>=1;i--){
Delete_sq(list,i,e);
printf("%d is deleted\n",e);
}
printf("List Length=%d\n",list.length);
return 1;
}

运行结果如下则无误

运行结果

祝大家数据结构考试顺利!!!

数据结构顺序表复习
https://sldblog.top/posts/markdown_shunxubiao/
作者
十裡冬
发布于
2025-12-11
许可协议
C++

部分信息可能已经过时

封面
Sample Song
Sample Artist
封面
Sample Song
Sample Artist
0:00 / 0:00