# 帮你理解一下线性表

### 顺序存储结构基本操作

``````#include <stdio.h>
#include <stdlib.h>

//*******************************************************************************************************************
//顺序结构线性表

struct sequence_node {     //定义一个结构体
int *data;
int length;
int maxlength;
};
typedef struct sequence_node SList;

void InitSList(SList *slist, int len){
if(len > 0){
slist->maxlength = len;
slist->length = 0;
slist->data = (int *)malloc(sizeof(int) * len);
}
}

void InsertSList(SList *slist, int pos, int val){     //在某位置插入一个元素
if(pos < 0 || pos > slist->length || slist->length == slist->maxlength){
printf("插入失败！\n");
return;
}
for(int i = slist->length; i > pos-1; i--)
slist->data[i+1] = slist->data[i];
slist->data[pos] = val;
slist->length++;
printf("插入成功！\n");
}

void DeleteSList(SList *slist, int val){          //删除一个元素
for(int i = 0; i < slist->length; i++){
if(slist->data[i] == val){
for(int j = i; j < slist->length; j++)
slist->data[j] = slist->data[j+1];
slist->length--;
printf("删除成功！\n");
return;
}
}
printf("删除失败！\n");
}

void ModifySList(SList *slist, int pos, int val){       //将某位置的节点值修改为指定值
if(pos < 0 || pos > slist->length){
printf("修改失败！\n");
return;
}
slist->data[pos] = val;
printf("修改成功！\n");
}

int FindSList(SList *slist, int val) {                  //查找某元素的位置
for(int i = 0; i < slist->length; i++){
if(slist->data[i] == val){
return i;
}
}
return -1;
}

void PrintSList(SList *slist){       //打印所有的节点值
for(int i = 0; i < slist->length; i++)
printf("%d",slist->data[i]);
printf("\n");
}

int main(){
SList *slist = (SList *)malloc(sizeof(SList));
InitSList(slist,5);
InsertSList(slist,0,1);
PrintSList(slist);
InsertSList(slist,0,4);
PrintSList(slist);
InsertSList(slist,0,8);
PrintSList(slist);
ModifySList(slist,1,2);
PrintSList(slist);
DeleteSList(slist,8);
PrintSList(slist);
int pos = FindSList(slist,1);
printf("%d\n",pos);
return 0;
}
``````

### 链式存储结构基本操作

``````#include <stdio.h>
#include <stdlib.h>

//*******************************************************************************************************************
//链式结构线性表
struct chain_list{
int data;
struct chain_list *next;
};
typedef struct chain_list CList;

void InsertCList(CList **head, int val){       //插入一个节点
CList *clist = (CList *)malloc(sizeof(CList));
clist->data = val;
}

void DeleteCList(CList *head, int val){        //删除一个节点
CList *curr;
while(curr->next != NULL){
if(curr->next->data == val){
CList *tmp;
tmp = curr->next;
curr->next = curr->next->next;
free(tmp);
printf("删除成功！\n");
return;
}
curr = curr->next;
}
printf("删除失败！\n");
}

void ModifyCList(CList *head, int pre, int mod){       //修改某节点的值
CList *curr;
while(curr->next != NULL){
if(curr->next->data == pre){
curr->next->data = mod;
printf("修改成功！\n");
return;
}
curr = curr->next;
}
printf("修改失败！\n");
}

int FindCList(CList *head, int val){       //查找某个节点的位置
CList *curr;
int i = 0;
while(curr->next != NULL){
if(curr->next->data == val) {
return i;
}
i++;
curr = curr->next;
}
return -1;
}

void PrintCList(CList *head){      //打印链表
CList *curr = head->next;
while(curr != NULL){
printf("%d,",curr->data);
curr = curr->next;
}
printf("\n");
}

int main(){

CList *head = NULL;
for(int i = 0; i < 5; i++){