什么是線性表
線性表的插入元素
線性表的刪除元素
線性表順序存儲(chǔ)的缺點(diǎn)
線性表的特點(diǎn)
1.線性表的實(shí)例
首先我們創(chuàng)建3個(gè)文件,分別如下:
liner_data
--sqlist.c
--sqlist.h
--test.c
sqlist.h
// .h文件中定位數(shù)據(jù)的結(jié)構(gòu)以及函數(shù)的方法
typedef int data_t;
#define N 128 //定義一個(gè)宏
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文件:函數(shù)實(shí)現(xiàn)的功能
//
// 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)); // 向數(shù)組中除了最后一個(gè),其他全部初始化為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); // 刪除堆內(nèi)存
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(""); // 自動(dòng)換行
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函數(shù)的執(zhí)行入口
//
// 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.執(zhí)行步驟
2.1 使用gcc進(jìn)行編譯
c語言程序編譯的過程如下:
預(yù)編譯-編譯-匯編-連接
匯編:gcc -c sqlist.c -o sqlist.o
gcc -c test.c -o test.o
連接:可執(zhí)行文件:gcc sqlist.o test.o -o test文章來源:http://www.zghlxwxcb.cn/news/detail-702651.html
以上3步可直接等價(jià)于:gcc *.c -o test
程序運(yùn)行成功:文章來源地址http://www.zghlxwxcb.cn/news/detail-702651.html
到了這里,關(guān)于C數(shù)據(jù)結(jié)構(gòu)-線性表之順序表的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!