#ifndef _SlinkList_H_
#include <stdio.h>
#include <stdlib.h>
#define EOE -1
typedef int ElemType
;
typedef struct node
{
ElemType data
;
struct node
*next
;
}node
;
typedef node
* List
;
List initiList
(List
*L
);
List
CreateListF(List L
);
void printfList(List L
);
int GetElem(List L
, int i
);
int Locate(List L
,ElemType e
);
int InsElem(List L
,int i
,ElemType e
);
int DelElem(List L
,int i
);
#endif
#include <stdio.h>
#include "SlinkList.h"
List
initiList(List
* L
)
{
(*L
) = (node
*)malloc(sizeof(node
));
(*L
)->next
= NULL;
return (*L
);
}
List
CreateListF(List L
)
{
node
* p
;
int n
,e
;
printf("输入要插入元素的个数:");
scanf("%d",&n
);
for(int i
=0;i
<n
;i
++)
{
p
= malloc(sizeof(struct node
));
if (!p
)exit(EOE
);
scanf("%d",&e
);
p
->data
= e
;
p
->next
= L
->next
;
L
->next
= p
;
}
return L
;
}
List
CreateListR(List L
)
{
node
*p
;
p
=(node
*)malloc(sizeof(node
));
p
=L
;
node
*q
;
int n
,e
;
printf("请输入要插入元素的个数:");
scanf("%d",&n
);
for (int i
= 0; i
< n
; i
++)
{
q
=(node
*)malloc(sizeof(node
));
scanf("%d",&e
);
q
->data
=e
;
p
->next
=q
;
p
=q
;
}
p
->next
=NULL;
return L
;
}
void printfList(List L
)
{
node
*p
;
p
=L
->next
;
while (p
!=NULL)
{
printf("%d",p
->data
);
p
=p
->next
;
}
printf("\n");
}
int GetElem(List L
, int i
)
{
int j
=1;
node
*p
=L
;
if (i
<=0)
{
return 0;
}
while (p
!=NULL&&j
<=i
)
{
p
=p
->next
;
j
++;
}
if (p
==NULL)
{
return 0;
}
else
{
return p
->data
;
}
}
int Locate(List L
,ElemType e
)
{
int j
=1;
node
*p
=L
;
while (p
!=NULL&&p
->data
!=e
)
{
j
++;
p
=p
->next
;
}
if (p
==NULL)
{
return 0;
}
else
{
return (j
);
}
}
int InsElem(List L
,int i
,ElemType e
)
{
int j
=1;
node
*p
=L
;
node
*q
;
if (i
<=0)
{
return 0;
}
while (p
!=NULL&&j
<i
)
{
j
++;
p
=p
->next
;
}
if (p
==NULL)
{
return 0;
}
else
{
q
=(node
*)malloc(sizeof(node
));
q
->data
=e
;
q
->next
=p
->next
;
p
->next
=q
;
}
}
int DelElem(List L
,int i
)
{
int j
=1;
node
*p
=L
,*q
;
if (i
<=0)
{
return 0;
}
while (p
!=NULL&&j
<i
)
{
j
++;
p
=p
->next
;
}
q
=p
->next
;
if (q
==NULL)
{
return 0;
}
else
{
p
->next
=q
->next
;
free(q
);
return 1;
}
}
转载请注明原文地址: https://lol.8miu.com/read-492.html