1、链表
1.1、线性表
-
创建线性表:
-
线性表示意图(图片来源于网络):
-
创建线性表
sqlink L;
- sqlink 是结构体的指针
-
申请堆空间
L=(sqlist)malloc(sizeof(sqlist))
-
判断创建是否成功
if(L==0){ printf("malloc erro"); return -1; }
-
初始化线性表
memset(L,0,sizeof(sqlist));
-
给last赋值
last->-1;
-
返回值
return L;
-
-
向线性表中插入数据
-
示意图(图片来源于网络)
-
判断线性表是否为满(full)
if (L->last == N-1) { printf("the sqlist is full,inster erro\n"); return -1; }
定义数组中N为128
#define N 128
-
验证链表中的
last
的位置是不是链表的最后一个位置(是则满不是则空) -
L->last
为最后一个 -
N-1
为链表的最后一个角标
-
-
检查需要插入的位置是否合理(是否超出范围)
if (pos<0||pos>L->last+1) { printf("this pos erro"); return-1; }
-
即现在输入的位置
pos
是否小于0会长是否大于链表的最大长度 -
L->last+1
链表的角标从0开始算,因此此处的+1
表示位置从1开始算 -
pos
表示的是插入的位置
-
-
移动元素、更新last位置
for ( i = L->last; i >=pos; i--) { L->data[i+1]=L->data[i]; } L->data[pos]=vale; L->last++; return 0; }
for
循环的意思为:定位获取当前链表长度i
的值,将pos
之后的数字从后往前依次往后移动1个位置(每次)for
循环截至条件为:pos
定位的位置- 通过移动次数更行
last
的位置
-
-
清空线性表
-
判断是否为空
if(L==NULL){ retun -1; }
若表L为空则表示没有清空的意义
-
清空操作
memset(L,0,sizeof(sqlist));
-
重新设置
last
的位置L ->last ==NULL;
-
返回
return 0;
-
-
判断线性表是否为空
-
通过
last
判断,因为last
始终处于最后一个,因此只需要验证last
的位置即可if (L->last==-1) { printf("this sqlist is NULL") return -1; }
若
last
的值还未发生改变,即last==-1
时该表任然为空,反之不为空。else{ printf("this sqlist not is NULL") return 0; } }
-
-
求线性表的长度
-
判断链表是否为空
if (L==NULL) { printf("L is NULL") return -1; }
-
不为空则根据
last
进行计算 -
return (L->last+1);
-
-
查询指定元素位置
-
判断该线性表是否为空表
if (L == NULL) { printf("the sqlist is NILL") return -1; }
-
通过遍历查找对应的值
for (int i = 0; i <= L->last; i++) { if (L->data[i] == value) { return i; } } return -1; }
- 定位到最后一个值
L->last
并进行遍历和判断
- 定位到最后一个值
-
-
查看遍历线性表
-
判断链表是否创建成功
if (L==NULL) { return -1; }
-
判断是否为空
if (L->last==-1) { printf("the sqlink is null"); }
-
遍历打印
for (size_t i = 0; i < L->last; i++) { printf("%d",L->data[i]); } return 0; }
size_t
在.h
文件中创建结构体时int
的重命名
-
-
释放空间
-
判断是否为空
if(L==NULL){ printf("this is NULL"); return -1; }
-
释放空间
else{ free(L); return 0; }
直接使用
free()
即可
-
-
删除指定位置的数据
-
先判断是否创建成功
if (L == NULL) { printf("this is error"); return -1; }
-
判断线性表是否为空
if (L->last == -1) { printf("this is NULL"); return -1; }
-
pos
的位置是否合理if (pos > L->last || pos < 0) { printf("pos is error"); return -1; }
-
使用
for
循环移动for (int i = pos; i < L->last; i++) { L->data[i - 1] = L->data[i]; }
-
移动
last
的位置L->last--;
-
-
数组合并
-
遍历对比(可以使用上面的查询函数,也可以直接遍历)
for (int i = 0; i < L->last; ++i) { for (int j = 0; j < S->last; ++j)
-
新建一个变量来接受验证是否相同
ret = (L->data[i] == S->data[j]);
-
进行插入
if (ret != 1) { L->data[lcoal + 1] = S->data[j]; L->last++; }
这里验证
ret
,如果相同即返回0ret
-
返回
return L->last;
-
完整代码
int list_merge(sqlink L, sqlink S) { int lcoal = L->last; int ret; for (int i = 0; i < L->last; ++i) { for (int j = 0; j < S->last; ++j) { ret = (L->data[i] == S->data[j]); if (ret != 1) { L->data[lcoal + 1] = S->data[j]; L->last++; } } } return L->last; }
-
-
删除相同元素
-
判断线性表是否为空
if(L->last==0){ return 0; }
-
遍历比较
for (int i = 0; i < L->last; ++i) { for (int j = L->last; j >= 0; --j) { ret=L->data[i]==L->data[j];
-
删除重复元素
if(ret ==1){ for (int k = j; k < L->last; ++k) { L->data[k]=L->data[k+1]; }
-
移动
last
L->last--;
完整代码
sqlist.h
-
#ifndef UNTITLED_SQLIST_H
#define UNTITLED_SQLIST_H
typedef int data_t;
#define N 128
typedef struct sqlist_t {
data_t data[N];
int last;
} sqlist, *sqlink;
sqlink sqlist_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_show(sqlink L);
int list_free(sqlink L);
int list_delete(sqlink L, int pos);
int list_merge(sqlink L,sqlink S);
int list_Delete_Duplicates(sqlink L);
#endif //UNTITLED_SQLIST_H
sqlist.c
#include <stdio.h>
#include <stdlib.h>
#include "sqlist.h"
#include <string.h>
sqlink sqlist_create() {
sqlink L;
L = (sqlink) malloc(sizeof(sqlist));
if (L == NULL) {
printf("malloc ERROR\n");
return L;
}
memset(L, 0, sizeof(sqlist));
L->last = -1;
return L;
}
int list_clear(sqlink L) {
if (L == NULL) {
return -1;
}
memset(L, 0, sizeof(sqlist));
L->last = -1;
return 0;
}
int list_empty(sqlink L) {
if (L == NULL) {
return -1;
} else {
return (L->last == -1);
}
}
int list_length(sqlink L) {
if (L == NULL) {
return -1;
} else {
return (L->last + 1);
}
}
int list_locate(sqlink L, data_t value) {
if (L == NULL) {
return -1;
}
for (int 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) {
if (L == NULL) {
return -1;
}
if (L->last == N - 1) {
printf("The sqlist is full, insert error.\n");
return -1;
}
if (pos < 0 || pos > L->last + 1) {
printf("Invalid position.\n");
return -1;
}
for (int i = L->last; i >= pos; i--) {
L->data[i + 1] = L->data[i];
}
L->data[pos] = value;
L->last++;
return 0;
}
int list_show(sqlink L) {
if (L == NULL) {
return -1;
}
if (L->last == -1) {
printf("The sqlink is empty.\n");
return 0;
}
for (int i = 0; i <= L->last; i++) {
printf("%d ", L->data[i]);
}
printf("\n");
return 0;
}
int list_free(sqlink L) {
if (L == NULL) {
printf("this is NULL");
return -1;
} else {
free(L);
return 0;
}
}
int list_delete(sqlink L, int pos) {
if (L == NULL) {
printf("this erro");
return -1;
}
if (L == -1) {
printf("this is NILL");
return -1;
}
if (pos > L->last || pos < 0) {
printf("pos is erro");
return -1;
}
for (int i = pos; i < L->last; ++i) {
L->data[i - 1] = L->data[i];
L->last--;
}
}
int list_merge(sqlink L, sqlink S) {
int lcoal = L->last;
int ret;
for (int i = 0; i < L->last; ++i) {
for (int j = 0; j < S->last; ++j) {
ret = (L->data[i] == S->data[j]);
if (ret != 1) {
L->data[lcoal + 1] = S->data[j];
L->last++;
}
}
}
return L->last;
}
int list_Delete_Duplicates(sqlink L){
if(L->last==0){
return 0;
}
int ret;
for (int i = 0; i < L->last; ++i) {
for (int j = L->last; j >= 0; --j) {
ret=L->data[i]==L->data[j];
if(ret ==1){
for (int k = j; k < L->last; ++k) {
L->data[k]=L->data[k+1];
}
L->last--;
}
}
}
}
main.c
#include <stdio.h>
#include <stdlib.h>
#include "sqlist.h"
#include "sqlist.c"
#include <string.h>
int main(int argc, const char *argv[]) {
sqlink L;
L = sqlist_create();
if (L == NULL) {
return -1;
}
list_insert(L, 100, 0);
list_insert(L, 100, 1);
list_insert(L, 110, 1);
list_show(L);
return 0;
}