写在前面:唉,我们学校通信工程大二不学数据结构,实在无奈,只能自己硬补一发了。我看的是耿国华的书,不是很有名,但是还是比较实用的,每章当中的每种数据结构的基本操作我都会用C语言自己实现一遍,但是后面的习题我会挑着做,基本就是把一章中最难的做掉,剩下的随便吧,下学期的课会有算法,自己也会多看一点算法导论,就是这样,开始更新吧,反正我已经开了三个分类了,但是我一定都会更新完的!当然这里我主要以代码为主。
这里我先写顺序表的基本操作。
#define MAXSIZE 1024
typedef struct linelist {
int element[MAXSIZE];
int off;
}Seqlist; //定义基本的数据结构
//以下主要声明函数头
int locate (Seqlist L , int e);
int inslist (Seqlist *L , int i , int e);
int dellist (Seqlist *l , int i , int *e);上面是seqlist.h文件
#include "seqlist.h"
#include <stdio.h>
//定位操作
int locate (Seqlist L , int e) {
int i;
for (i = 0 ; i <= L.off ; i++) {
if (L.element[i] == e)
return (i+1);
}
return -1;
}
//插入操作
int inslist (Seqlist *L , int i , int e) {
int k;
if ((i < 1) || (i > L->off+2)) {
printf ("arguement i error!\n");
return 0;
}
if (L->off == (MAXSIZE-1)) {
printf ("seqlist is full\n");
return 0;
}
if (i <= L->off+1) {
for (k = L->off ; k >= i - 1 ; k--)
L->element[k+1] = L->element[k];
}
L->element[i-1] = e;
L->off++;
return 1;
}
//删除操作
int dellist (Seqlist *L , int i , int *e) {
int k;
if (i < 1 || (i > L->off+1)) {
printf ("arguement i error!\n");
return 0;
}
*e = L->element[i-1];
if (i != L->off+1) {
for (k = i - 1 ; k <= L->off-1 ; k++)
L->element[k] = L->element[k+1];
}
L->off--;
return 1;
}上面是seqlist_op.c
#include <stdio.h>
#include "seqlist.h"
#include <stdlib.h>
int main () {
int array[10] = {0,1,2,3,4,5,6,7,8,9};
int i;
int *e = malloc (sizeof (int));
Seqlist L;
L.off = -1;
for (i = 0 ; i < 10 ; i++)
inslist (&L , i+1 , array[i]);
for (i = 0 ; i < 10 ; i++)
printf ("the number %d is at %d\n" , i , locate (L , i));
for (i = 0 ; i < 10 ; i++)
dellist (&L , 1 , e);
printf ("the L.off is %d\n" , L.off);
}
上面是main.c主要用来测试基本操作的正确性
总结:顺序表主要还是和之前的数组还是有很大的联系的算得上是非常基本的数据结构了,这里在操作方面没有什么好说的,最重要的是注意细节,将在删除和插入的时候要注意对参数i的检查,避免出现错误参数,导致程序崩溃。
本文介绍了在自学数据结构过程中,通过阅读耿国华的书籍,使用C语言实现顺序表的基本操作。作者强调了代码实现的重要性,并指出在处理顺序表时,特别是在插入和删除操作中,需要注意参数检查以防止程序错误或崩溃。
728

被折叠的 条评论
为什么被折叠?



