#include <stdlib.h>
#include <stdio.h>
typedef int ElementType;
struct Node
{
ElementType Element;
struct Node *Next;
};
typedef struct Node *Position;
typedef Position List;
void deleteList(Position P, List *pL);
int emptyList(List L);
Position first(List L);
Position endList(List L);
void insertList(ElementType X, Position P, List *pL);
Position locate(ElementType x, List L);
Position myLocate(ElementType x, int i, List L);
void makenullList(List *pL);
Position next(Position P, List L);
Position previous(Position P, List L);
ElementType retrieve(Position P, List L);
void deleteList(Position P, List *pL)
{
Position Temp;
if (P->Next != NULL)
{
Temp = P->Next;
P->Next = Temp->Next;
free(Temp);
}
}
int emptyList(List L)
{
return L->Next == NULL;
}
Position endList(List L)
{
Position P = L;
while (1)
{
if (P->Next == NULL)
return P;
P = P->Next;
}
}
Position first(List L)
{
return L;
}
void insertList(ElementType x, Position P, List *pL)
{
Position T;
T = (Position)malloc(sizeof(struct Node));
T->Element = x;
T->Next = P->Next;
P->Next = T;
}
Position locate(ElementType x, List L)
{
Position P = L;
while (P->Next != NULL)
{
if (P->Next->Element == x)
return P;
P = P->Next;
}
return P;
}
void makenullList(List *pL)
{
*pL = (List)malloc(sizeof(struct Node));
(*pL)->Next = NULL;
}
Position myLocate(ElementType x, int i, List L)
{
Position P = L;
int count = 0;
while (P->Next != NULL)
{
if (P->Next->Element == x)
count++;
if (count == i)
return P->Next;
P = P->Next;
}
return P->Next;
}
Position next(Position P, List L)
{
return P->Next;
}
Position previous(Position P, List L)
{
Position Q = L;
while (Q->Next->Next != NULL)
{
if (Q->Next->Next == P)
Q = Q->Next;
}
return Q->Next;
}
ElementType retrieve(Position P, List L)
{
if (P->Next != NULL)
return P->Next->Element;
}
#define USE_ALL 1
#ifdef USE_ALL
void addFirst(ElementType x, List *pL);
void append(ElementType x, List *pL);
void copyEvenNumbers(List L, List *pL);
void deleteX(ElementType x, List *pL);
List difference(List L1, List L2);
void erase(ElementType x, List *pL);
float getAvg(List L);
List intersection(List L1, List L2);
int member(ElementType x, List L);
void normalize(List *pL);
void printList(List L);
void printOddNumbers(List L);
List readSet();
void removeAll(ElementType x, List *pL);
void readList(List *pL);
void sort(List *pL);
List unionSet(List L1, List L2);
void addFirst(ElementType x, List *pL)
{
insertList(x, first(*pL), pL);
}
void append(ElementType x, List *pL)
{
if (!member(x, *pL))
insertList(x, endList(*pL), pL);
}
void copyEvenNumbers(List L, List *pL)
{
makenullList(pL);
Position P = first(L);
Position End = endList(L);
while (P != End)
{
if (retrieve(P, L) % 2 == 0)
append(retrieve(P, L), pL);
else
P = next(P, L);
}
}
void deleteX(ElementType x, List *pL)
{
while (locate(x, *pL) != endList(*pL))
{
deleteList(locate(x, *pL), pL);
}
}
List difference(List L1, List L2)
{
List L;
Position P = first(L1);
Position EL1 = endList(L1);
Position EL2 = endList(L2);
while (P != EL1)
{
if (locate(retrieve(P, L1), L2) != EL2)
append(retrieve(P, L1), &L);
P = next(P, L1);
}
}
void erase(ElementType x, List *pL)
{
if (locate(x, *pL) != endList(*pL))
deleteList(locate(x, *pL), pL);
}
float getAvg(List L)
{
float sum = 0;
int length = 0;
Position P = first(L);
Position E = endList(L);
while (P != E)
{
sum += retrieve(P, L);
P = next(P, L);
length++;
}
return sum / length;
}
List intersection(List L1, List L2)
{
List L;
makenullList(&L);
Position P2 = first(L2);
Position E2 = endList(L2);
while (P2 != E2)
{
if (member(retrieve(P2, L2), L1))
if (!member(retrieve(P2, L2), L))
addFirst(retrieve(P2, L2), &L);
P2 = next(P2, L2);
}
return L;
}
int member(ElementType x, List L)
{
return locate(x, L) != endList(L);
}
void normalize(List *pL)
{
Position P = *pL, Q;
while (P->Next != NULL)
{
Q = P->Next;
while (Q->Next != NULL)
{
if (P->Next->Element == Q->Next->Element)
{
deleteList(Q, pL);
}
else
{
Q = Q->Next;
}
}
P = P->Next;
}
}
void printList(List L)
{
Position P = first(L);
Position E = endList(L);
while (P != E)
{
printf("%d ", retrieve(P, L));
P = next(P, L);
}
printf("\n");
}
void printOddNumbers(List L)
{
Position P = L;
while (P->Next != NULL)
{
if (P->Next->Element % 2 != 0)
printf("%d ", P->Next->Element);
P = P->Next;
}
printf("\n");
}
List readSet()
{
List L;
makenullList(&L);
int n;
ElementType e;
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
scanf("%d", &e);
if (member(e, L))
continue;
addFirst(e, &L);
}
return L;
}
void removeAll(ElementType x, List *pL)
{
while (locate(x, *pL)->Next != NULL)
deleteList(locate(x, *pL), pL);
}
void readList(List *pL)
{
makenullList(pL);
int n;
scanf("%d", &n);
ElementType e;
for (int i = 0; i < n; i++)
{
scanf("%d", &e);
insertList(e, endList(*pL), pL);
}
}
void sort(List *pL)
{
Position P = *pL,
N;
while (P->Next != NULL)
{
N = P->Next;
while (N->Next != NULL)
{
if (P->Next->Element > N->Next->Element)
{
ElementType Temp = P->Next->Element;
P->Next->Element = N->Next->Element;
N->Next->Element = Temp;
}
N = N->Next;
}
P = P->Next;
}
}
List unionSet(List L1, List L2)
{
List L;
makenullList(&L);
Position P1 = first(L1),
P2 = first(L2),
E1 = endList(L1),
E2 = endList(L2);
while (P1 != E1)
{
insertList(retrieve(P1, L1), endList(L), &L);
P1 = next(P1, L1);
}
while (P2 != E2)
{
if (!member(retrieve(P2, L2), L))
insertList(retrieve(P2, L2), endList(L), &L);
P2 = next(P2, L2);
}
return L;
}
#endif