什么是线性表
线性表的插入元素
线性表的删除元素
线性表顺序存储的缺点
线性表的特点
1.线性表的实例
首先我们创建3个文件,分别如下:
liner_data
–sqlist.c
–sqlist.h
–test.c
sqlist.h
// .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_free(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_show(sqlink L);
int list_merge(sqlink L1, sqlink L2);
int list_purge(sqlink L);
int list_show(sqlink L);
下面编写sqlist.c文件:函数实现的功能
//
// Created by Lenovo on 2023/9/9.
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sqlist.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)); // 向数组中除了最后一个,其他全部初始化为0
L->last = -1;
return L;
}
int list_clear(sqlink L){
/*
* @return: 0-success -1-failed
*/
if(L == NULL){
return -1;
}
memset(L, 0, sizeof(sqlist));
L->last = -1;
return 0;
}
int list_free(sqlink L){
if(L==NULL)
return -1;
free(L); // 删除堆内存
L=NULL;
return 0;
}
/*
* list_empty: Is list empty?
* para L: list
* @return: 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);
}
/*
* @ret -1--not exist pos
* */
int list_locate(sqlink L, data_t value){
int i ;
for (i = 0; i <= L->last; i++) {
if (L->data[i] == value)
return i;
}
return -1;
}
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(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\n");
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) {
int i = 0;
int ret;
while (i <= L2->last){
ret = list_locate(L1, L2->data[i]);
if (ret == -1) {
if (list_insert(L1, L2->data[i], L1->last+1) == -1)
return -1;
}
i++;
}
return 0;
}
int list_purge(sqlink L) {
int i;
int j;
if (L->last == 0)
return 0;
i = 1;
while (i <= L->last) {
j = i-1;
while (j >= 0) {
if (L->data[i] == L->data[j]) {
list_delete(L, i);
break;
} else {
j--;
}
}
if ( j < 0) {
i++;
}
}
return 0;
}
test.c文件:main函数的执行入口
//
// Created by Lenovo on 2023/9/9.
//
#include <stdio.h>
#include "sqlist.h"
void test_insert();
void test_delete();
void test_merge();
void test_purge();
int main(int argc, const char *argv[])
{
//test_insert();
//test_delete();
//test_merge();
test_purge();
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, 100, list_length(L));
list_insert(L, 100, -1000);
list_show(L);
list_free(L);
}
void test_delete() {
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(L, 9);
list_show(L);
list_free(L);
}
void test_merge() {
sqlink L1, L2;
L1 = list_create();
if (L1 == NULL)
return;
L2 = list_create();
if (L2 == NULL)
return;
list_insert(L1, 10, 0);
list_insert(L1, 20, 0);
list_insert(L1, 30, 0);
list_insert(L1, 40, 0);
list_insert(L2, 50, 0);
list_insert(L2, 20, 0);
list_insert(L2, 90, 0);
list_insert(L2, 40, 0);
list_show(L1);
list_show(L2);
printf("********************\n");
list_merge(L1, L2);
list_show(L1);
list_show(L2);
}
void test_purge() {
sqlink L;
L = list_create();
if (L == NULL)
return;
list_insert(L, 10, 0);
list_insert(L, 10, 0);
list_insert(L, 10, 0);
list_insert(L, 10, 0);
list_insert(L, 10, 0);
list_insert(L, 10, 0);
list_show(L);
list_purge(L);
list_show(L);
list_free(L);
}
2.执行步骤
2.1 使用gcc进行编译
c语言程序编译的过程如下:
预编译-编译-汇编-连接
汇编:gcc -c sqlist.c -o sqlist.o
gcc -c test.c -o test.o
连接:可执行文件:gcc sqlist.o test.o -o test
以上3步可直接等价于:gcc *.c -o test
程序运行成功:
1.本站内容仅供参考,不作为任何法律依据。用户在使用本站内容时,应自行判断其真实性、准确性和完整性,并承担相应风险。
2.本站部分内容来源于互联网,仅用于交流学习研究知识,若侵犯了您的合法权益,请及时邮件或站内私信与本站联系,我们将尽快予以处理。
3.本文采用知识共享 署名4.0国际许可协议 [BY-NC-SA] 进行授权
4.根据《计算机软件保护条例》第十七条规定“为了学习和研究软件内含的设计思想和原理,通过安装、显示、传输或者存储软件等方式使用软件的,可以不经软件著作权人许可,不向其支付报酬。”您需知晓本站所有内容资源均来源于网络,仅供用户交流学习与研究使用,版权归属原版权方所有,版权争议与本站无关,用户本人下载后不能用作商业或非法用途,需在24个小时之内从您的电脑中彻底删除上述内容,否则后果均由用户承担责任;如果您访问和下载此文件,表示您同意只将此文件用于参考、学习而非其他用途,否则一切后果请您自行承担,如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。
5.本站是非经营性个人站点,所有软件信息均来自网络,所有资源仅供学习参考研究目的,并不贩卖软件,不存在任何商业目的及用途
暂无评论内容