1 删除线性表某一个位置上的元素——实现
问题描述:
有一个数组存储存储线性表中的数据元素,0-127,给定一个初始状态Last=5,空表为-1(存了6个元素,数组没有满),需要实现list_delete(L, pos),即删除线性表某一位置的元素。
分析:
1 检查现有元素的范围 ;pos∈[0, last]
2 每当删除一个元素时,后面的元素往前移 ;mov
3 元素移动进行更新;last–
实现一个功能函数需要三个文件。
sqlist.h(定义顺序表)
sqlist.c(实现接口函数)
test.c(主函数实现)
代码实现:
sqlist.h
typedef int data_t;
#define N 128
typedef struct {
data_t data[N];
int last;
}sqlist, *sqlink;
sqlink list_create();
int list_clear(sqlink L);
int list_empty(sqlink L);
int list_length(sqlink L);
int list_locate(sqlink L, data_t value);
int list_insert(sqlink L, data_t value, int pos);
int list_delete(sqlink L);
int list_show(sqlink L);
int list_delete_pos(sqlink L, int pos);
int list_merge(sqlink L1, sqlink L2);
int list_purge(sqlink L);
sqlist.c
#include "sqlist.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
sqlink list_create(){
//malloc
sqlink L;
L =(sqlink)malloc(sizeof(sqlist));
if (L == NULL) {
printf("list malloc failed\n");
return L;
}
//initialize
memset(L, 0, sizeof(sqlist));
L->last = -1;
//return
return L;
}
/*
* @ret 0-success -1-failed
**/
int list_clear(sqlink L){
if (L == NULL)
return -1;
memset(L, 0, sizeof(sqlist));
L->last = -1;
return 0;
}
int list_delete(sqlink L){
if (L == NULL)
return -1;
free(L);
L = NULL;
return 0;
}
/*
* list_empty:IS list empty?
*para L:list
*@ret 1--empty 0--not empty
*/
int list_empty(sqlink L){
if (L->last == -1)
return 1;
else
return 0;
}
int list_length(sqlink L){
if (L == NULL)
return -1;
return (L->last+1);
}
int list_locate(sqlink L, data_t value){
return 0;
}
int list_insert(sqlink L, data_t value, int pos){
int i;
//full?
if (L->last==N-1){
printf("list is full\n");
return -1;
}
//check para 0<=pos<=Last+1 [0, last+1]
if (pos < 0 || pos > L->last+1) {
printf("Pos is invalid\n");
return -1;
}
//move
for (i = L->last; i >= pos; i--){
L->data[i+1] = L->data[i];
}
//update value last
L->data[pos] = value;
L->last++;
return 0;
}
int list_show(sqlink L) {
int i;
if (L == NULL)
return -1;
if (L->last == -1)
printf("list is empty\n");
for (i = 0; i <= L->last; i++){
printf("%d ", L->data[i]);
}
puts("");
return 0;
}
int list_delete_pos(sqlink L, int pos)
{
int i;
if (L->last == -1){
printf("list is empty\n");
return -1;
}
//pos [0, last]
if (pos < 0 || pos > L->last){
printf("delete pos is invalid");
return -1;
}
// move [pos+1, last]
for (i = pos+1; i <= L->last; i++){
L->data[i-1] = L->data[i];
}
// update
L->last--;
return 0;
}
int list_merge(sqlink L1, sqlink L2){
return 0;
}
int list_purge(sqlink L){
return 0;
}
test.c
#include <stdio.h>
#include "sqlist.h"
void test_insert();
void test_delete_pos();
int main(int argc, const char *argv[])
{
test_insert();
test_delete_pos();
return 0;
}
void test_insert(){
sqlink L;
L = list_create();
if (L == NULL)
return;
list_insert(L, 10, 0);
list_insert(L, 20, 0);
list_insert(L, 30, 0);
list_insert(L, 40, 0);
list_insert(L, 50, 0);
list_insert(L, 60, 0);
list_show(L);
//list_insert(L, 70, list_length(L));
list_insert(L, 100, -1000);
list_show(L);
list_delete(L);
}
void test_delete_pos(){
sqlink L;
L = list_create();
if (L == NULL)
return;
list_insert(L, 10, 0);
list_insert(L, 20, 0);
list_insert(L, 30, 0);
list_insert(L, 40, 0);
list_insert(L, 50, 0);
list_insert(L, 60, 0);
list_show(L);
list_delete_pos(L, 3);
list_show(L);
}
编译运行:
gcc *.c
./a.out
由运行结果可知,delete_pos(L,0)删除了数组中0位置的元素60。