#include <stdio.h>
#include <stdlib.h>
#include "ConsoleApplication1.h"
// Use dynamic array to create list
#define MaxSize 100
#define AddSize 10
#define Elemtype int
typedef struct {
Elemtype *List;
int size;
int length;
}SqList;
void InitList(SqList& L) {
L.size = MaxSize;
L.length = 0;
L.List = (Elemtype*)malloc(MaxSize * sizeof(Elemtype));
}
void DestoryList(SqList& L) {
L.size = 0;
L.length = 0;
free(L.List);
}
void ClearList(SqList& L) {
L.length = 0;
}
int IsEmpty(SqList L) {
if (L.length == 0)
return true;
else
return false;
}
int ListLength(SqList L) {
return L.length;
}
int GetElem(SqList L, int i, Elemtype& e) {
if (i < 0) {
printf("THE ILLEGAL INPUT,YOU CAN'T INPUT NEGATIVE INDEX\n");
return false;
}
if (i >= ListLength(L)) {
printf("YOUR INPUT EXCEEDS THE LIST'S LENGTH\n");
return false;
}
e = L.List[i];
return true;
}
int LocateElem(SqList L, Elemtype e) {
for (int i = 0; i < ListLength(L); i++)
{
if (L.List[i] == e)
return i;
}
return -1;
}
int PriorElem(SqList L, Elemtype cur, Elemtype& prior) {
int PriorIndex = LocateElem(L,cur) - 1;
if (PriorIndex == -2) {
printf("Don't Exist the Element\n");
return false;
}
if (PriorIndex == -1) {
printf("The Element index is ONE,Don't Exist the Prior Element\n");
return false;
}
prior = L.List[PriorIndex];
return true;
}
int NextElem(SqList L, Elemtype cur, Elemtype& next) {
int CurrentIndex = LocateElem(L, cur);
if (CurrentIndex == -1) {
printf("Don't Exist the Element\n");
return false;
}
if (CurrentIndex + 1 == ListLength(L)) {
printf("The Element index is THE LAST,Don't Exist the NEXT Element\n");
return false;
}
next = L.List[CurrentIndex + 1];
return true;
}
int ListInsert(SqList& L, int i, Elemtype e) {
if (i < 0) {
printf("ILLEGAL INPUT,YOU PUT INDEX IS LESS THAN ZERO\n");
return false;
}
if (i > ListLength(L)) {
printf("THE INDEX IS TOO BIG FOR INSERTING NOW\n");
return false;
}
if (L.size == L.length) {
L.List = (Elemtype*)realloc(L.List, AddSize * sizeof(Elemtype));
if (L.List == NULL) {
printf("List's Size is insufficient ,and Reallocate Size Fail\n");
return false;
}
L.size = L.size + AddSize;
}
for (int j = ListLength(L)-1; j >= i; j--)
{
L.List[j + 1] = L.List[j];
}
L.List[i] = e;
L.length++;
return true;
}
int ListDelete(SqList& L, int i, Elemtype& e) {
if (i < 0) {
printf("ILLEGAL INPUT,YOU PUT INDEX IS LESS THAN ZERO\n");
return false;
}
if (i >= ListLength(L)) {
printf("YOUR INDEX CANNOT EXIST IN THIS LIST\n");
return false;
}
if (ListLength(L) == 0) {
printf("THE LIST IS EMPTY, CAN'T DELETE ANY ELEMENT\n");
return false;
}
e = L.List[i];
for (int j = i+1; j < ListLength(L); j++)
{
L.List[j - 1] = L.List[j];
}
L.length--;
}
void Visit(int i) {
printf("%d ",i);
}
int ListTravel(SqList L) {
for (int i = 0; i < ListLength(L); i++)
{
Visit(L.List[i]);
}
printf("\n");
return 0;
}
int InputList(SqList& L, int n) {
Elemtype e;
if (n >= L.size) {
printf("Your inputing numbers more than List'size,Please input less than %d number you will input\n",L.size);
return false;
}
printf("Input List elements:\n");
for (int i = 0; i < n; i++)
{
scanf_s("%d", &e);
ListInsert(L, i, e);
}
return true;
}
// Take out the elements in La one by one and check if they exist in Lb,if exist delete it,otherwise reserve it
void SetMinus(SqList& La, SqList Lb,int n,int m) {
int e;
InputList(La, n);
InputList(Lb, m);
for (int i = 0; i < ListLength(La); i++)
{
GetElem(La, i, e);
if (LocateElem(Lb, e) != -1) {
ListDelete(La, i, e);
--i;
}
}
}
int main()
{
SqList La,Lb;
int m,n;
int e;
InitList(La);
InitList(Lb);
printf("Input La and Lb numbers of element:");
scanf_s("%d %d", &n, &m);
SetMinus(La, Lb, n, m);
ListTravel(La);
DestoryList(La);
DestoryList(Lb);
return 0;
}