//栈中重复元素的策略:1、新项忽略;2、旧项遗忘
struct Node
{
int data;
struct Node* next;
};
typedef struct Node* LinkNode;
void InitalLink();
void PushLink(int);//新项忽略
void PushEx(int item);//旧项遗忘
int PopLink();
void freeNode();
LinkNode tail;
void InitalLink()
{
tail = NULL;
}
static LinkNode New(int item)
{
LinkNode x = malloc(sizeof(*x));
if (!x)
{
printf("malloc falied");
return NULL;
}
x->data = item;
x->next = NULL;
return x;
}
static int Finder(int item)
{
LinkNode temp = tail;
int k = 0;
while (temp)
{
if (temp->data == item)
{
k = 1;
}
temp = temp->next;
}
return k;
}
static LinkNode FinderEx(int item)
{
LinkNode p = NULL,q = tail;
if (q->data == item)
{
return q;
}
while (q)
{
if (q->data == item)
{
return p;
}
p = q;
q = q->next;
}
return NULL;
}
static int IsEmptyEx()
{
if (!tail)
{
return 1;
}
return 0;
}
void PushEx(int item)
{
if (!tail)
{
tail = New(item);
return;
}
LinkNode temp = FinderEx(item);
LinkNode x = New(item);
LinkNode Qfree = NULL;
if (temp == tail)
{
Qfree = tail;
tail = tail->next;
free(Qfree);
}
else if(temp)
{
Qfree = temp->next;
temp->next = temp->next->next;
free(Qfree);
}
x->next = tail;
tail = x;
}
void PushLink(int item)
{
if (!tail)
{
tail = New(item);
return;
}
if (Finder(item))
{
return;
}
LinkNode temp = New(item);
if (!temp)
{
return;
}
temp->next = tail;
tail = temp;
}
int PopLink()
{
if (IsEmptyEx())
{
return -1;
}
int temp = tail->data;
LinkNode Qfree = tail;
tail = tail->next;
free(Qfree);
return temp;
}
void freeNode()
{
while (tail)
{
LinkNode Qfree = tail;
tail = tail->next;
free(Qfree);
}
}