#include<bits/stdc++.h>
using namespace std;
#define MaxSize 3
typedef struct SNode{
int data;
int next;
}SLinkList[MaxSize];
//初始化
void InitList(SLinkList L){
L[0].data=0; //我这里放的是链表长度
for(int i=0;i<MaxSize;i++){
L[i].next=-1;
}
}
//判断要插入的下标是否已存在data
bool NotExist(SLinkList L,int i){
int k=L[0].next;
while(k!=-1){
if(k==i)
return false;
k=L[k].next;
}
return true;
}
//尾插
void InsertList(SLinkList L,int i,int e){
if(i<1||i>MaxSize-1){
cout<<"Insert Error:Invalid parameter i!"<<endl;
return;
}
if(L[0].data==MaxSize-1){
cout<<"Insert Error:SLinkList Full!"<<endl;
return;
}
if(NotExist(L,i)){
int k=0;
while(L[k].next!=-1)
k=L[k].next;
L[k].next=i;
L[i].data=e;
L[0].data++;
}else{
cout<<"Insert Error:SNode Already Exist!"<<endl;
}
}
//删除
void DeleteList(SLinkList L,int i){
if(i<1||i>MaxSize-1){
cout<<"Delete Error:Invalid parameter i!"<<endl;
return;
}
if(L[0].data==0){
cout<<"Delete Error:No SNode Exist!"<<endl;
return;
}
int k=0;
while(L[k].next!=-1){
if(L[k].next==i){
L[k].next=L[i].next;
L[i].next=-1;
L[0].data--;
return;
}
k=L[k].next;
}
cout<<"Delete Error:SNode Not Found!"<<endl;
}
//打印
void PrintList(SLinkList L){
if(L[0].data){
int k=L[0].next;
while(k!=-1){
cout<<L[k].data<<" ";
k=L[k].next;
}
cout<<endl;
}else{
cout<<"PrintList Error:SLinkList Empty!"<<endl;
}
}
int main(){
SLinkList L;
InitList(L);
PrintList(L);
InsertList(L,2,2);
PrintList(L);
InsertList(L,2,3);
PrintList(L);
InsertList(L,1,4);
PrintList(L);
InsertList(L,1,5);
PrintList(L);
InsertList(L,3,6);
PrintList(L);
DeleteList(L,3);
PrintList(L);
DeleteList(L,2);
PrintList(L);
DeleteList(L,2);
PrintList(L);
DeleteList(L,1);
PrintList(L);
return 0;
}
标签:线性表,int,阉割,链表,MaxSize,SLinkList,data
From: https://www.cnblogs.com/b1ackstar/p/18292378