#include <stdio.h>
#include <stdlib.h>
#define MAXPOOL 1024
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef int ElemType;
typedef struct Nobe{
ElemType data;
struct Nobe* next;
} Nobe;
typedef Nobe* LinkList;
int getElem(LinkList L, int i, int *e);
int listEmpty(LinkList L);
void clearList(LinkList *L);
int locateElem(LinkList L, int e);
int listInsert(LinkList *L, int i, int e);
int listDelete(LinkList *L, int i, int *e);
int listHAdd(LinkList *L, int e);
int listAdd(LinkList *L, int e);
int listPrint(LinkList L);
int listLength(LinkList L);
int listHAdd(LinkList *L, int e)
{
LinkList p;
p = (LinkList)malloc(sizeof(Nobe));
if (p == NULL)
{
printf("内存申请失败!\n");
return ERROR;
}
p->data = e;
if (*L == NULL)
{
*L = p;
p->next = p;
}
else
{
LinkList cache = (*L)->next;
while (cache->next != *L)
{
cache = cache->next;
}
p->next = *L;
*L = p;
cache->next = p;
}
return OK;
}
int listPrint(LinkList L)
{
LinkList cache = L;
if (L != NULL)
{
printf("%d ", cache->data);
while (cache->next != L)
{
cache = cache->next;
printf("%d ", cache->data);
}
}
putchar('\n');
return OK;
}
int listInsert(LinkList *L, int i, int e)
{
if (i < 0)
{
return ERROR;
}
LinkList p;
p = (LinkList)malloc(sizeof(Nobe));
if (p == NULL)
{
printf("内存申请失败!\n");
return ERROR;
}
p->data = e;
if (i == 1)
{
LinkList cache = (*L)->next;
while (cache->next != *L)
{
cache = cache->next;
}
p->next = *L;
*L = p;
cache->next = p;
}
else
{
LinkList cache = *L;
for (int j = 2; j < i && cache->next != *L; j++)
{
cache = cache->next;
}
p->next = cache->next;
cache->next = p;
}
return OK;
}
int listDelete(LinkList *L, int i, int *e)
{
if (i < 0)
{
return ERROR;
}
LinkList cache = NULL;
LinkList free_nobe = NULL;
if (i == 1)
{
free_nobe = *L;
cache = free_nobe->next;
*e = free_nobe->data;
if (cache == *L)
{
*L = NULL;
}
else
{
while (cache->next != free_nobe)
{
cache = cache->next;
}
cache->next = free_nobe->next;
*L = free_nobe->next;
}
free(free_nobe);
}
else
{
cache = *L;
int j = 2;
for (; j < i && cache->next != *L; j++)
{
cache = cache->next;
}
if (cache->next != *L)
{
*e = cache->next->data;
free_nobe = cache->next;
cache->next = cache->next->next;
free(free_nobe);
}
else
{
return ERROR;
}
}
return OK;
}
int main(void)
{
LinkList head = NULL;
int e;
listHAdd(&head, 1);
listHAdd(&head, 4);
listHAdd(&head, 3);
listHAdd(&head, 3);
listHAdd(&head, 2);
listHAdd(&head, 2);
listHAdd(&head, 3);
listInsert(&head, 2, 666);
listInsert(&head, 1, 314);
listDelete(&head, 9, &e);
printf("删除了%d\n", e);
listPrint(head);
return 0;
}