函数声明:
typedef int DataType;
typedef struct Node
{
DataType data;
struct Node* next;
}Node,*pNode,*pList;
void InitList(pList *pplist);
void Display(const pList plist);
void PushBack(pList *pplist,DataType d);
void PopBack(pList *pplist);
void PushFront(pList *pplist,DataType d);
void PopFront(pList *pplist);
void DistroyList(pList *pplist);
pNode Find(pList *pplist,DataType d);
void Insert(pList *pplist,pNode pos,DataType d);
void Erase(pList *pplist,pNode pos);
void Remove(pList *pplist,DataType d);
void RemoveAll(pList *pplist,DataType d);
void BubbleSort(pList *pplist);
void Reverse(pList *pplist);
void EraseNotTail(pList *pplist,pNode pos);
void ReversePrint(pList plist);
void InsertFrontNode(pNode pos, DataType x);
void JosephCycle(pList* pplist, int k);
pList Merge(pList* p1, pList* p2);
pNode FindMidNode(pList plist);
pNode FindKNode(pList plist, int k);
pNode CheckCircle(pList plist);
int GetCircleLength(pNode meet);
pNode GetCycleEntryNode(pList plist, pNode meet);
int CheckCross(pList list1, pList list2);
pNode GetCrossNode(pList list1, pList list2);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
函数实现:
void InitList(pList *pplist)
{
assert(pplist);
*pplist=NULL;
}
void Display(const pList plist)
{
pNode cur = plist;
while (cur)
{
printf("%d-->",cur->data);
cur=cur->next;
}
printf("NULL\n");
}
pNode BuyNode(DataType d)
{
pNode newNode = (pNode)malloc(sizeof(Node));
newNode->data=d;
newNode->next=NULL;
return newNode;
}
void PushBack(pList *pplist,DataType d)
{
pNode newNode = BuyNode(d);
pNode cur = *pplist;
assert(pplist);
if (*pplist==NULL)
{
*pplist=newNode;
return;
}
while (cur->next!=NULL)
{
cur=cur->next;
}
cur->next=newNode;
}
void PopBack(pList *pplist)
{
pNode cur = *pplist;
assert(pplist);
if (*pplist==NULL)
{
return;
}
if (cur->next==NULL)
{
free(cur);
*pplist=NULL;
return;
}
while (cur->next->next!=NULL)
{
cur=cur->next;
}
free(cur->next);
cur->next=NULL;
}
void PushFront(pList *pplist,DataType d)
{
pNode newNode=BuyNode(d);
assert(pplist);
newNode->next=*pplist;
*pplist=newNode;
}
void PopFront(pList *pplist)
{
pNode cur = *pplist;
assert(pplist);
if (*pplist==NULL)
{
return;
}
*pplist=(*pplist)->next;
free(cur);
cur=NULL;
}
void DistroyList(pList *pplist)
{
pNode cur = *pplist;
pNode del = NULL;
assert(pplist);
while (cur)
{
del = cur;
cur=cur->next;
free(del);
del=NULL;
}
*pplist=NULL;
}
pNode Find(pList *pplist,DataType d)
{
pNode cur = *pplist;
assert(pplist);
while (cur)
{
if (cur->data==d)
{
return cur;
}
else
{
cur=cur->next;
}
}
return NULL;
}
void Insert(pList *pplist,pNode pos,DataType d)
{
pNode newNode = BuyNode(d);
assert(pplist);
assert(pos);
if (*pplist==NULL)
{
PushBack(pplist,d);
return;
}
newNode->next=pos->next;
pos->next=newNode;
}
void Erase(pList *pplist,pNode pos)
{
pNode cur = *pplist;
assert(pplist&&pos);
while (cur->next!=pos&&cur->next!=NULL)
{
cur=cur->next;
}
if (cur->next==NULL)
{
return;
}
cur->next=pos->next;
free(pos);
pos=NULL;
}
void Remove(pList *pplist,DataType d)
{
pNode pos = NULL;
assert(pplist);
pos = Find(pplist,d);
Erase(pplist,pos);
}
void RemoveAll(pList *pplist,DataType d)
{
assert(pplist);
while ((Find(pplist,d)!=NULL))
{
Erase(pplist,Find(pplist,d));
}
}
void BubbleSort(pList *pplist)
{
DataType tmp = 0;
pNode first = NULL;
pNode second = NULL;
pNode end = NULL;
assert(pplist);
if(NULL==*pplist)
{
return ;
}
while (end!=*pplist)
{
first = *pplist;
second = first->next;
while (second!=end)
{
if (first->data>second->data)
{
tmp=first->data;
first->data=second->data;
second->data=tmp;
}
first=first->next;
second=second->next;
}
end=first;
}
}
void Reverse(pList* pplist)
{
assert(pplist);
if (*pplist==NULL)
{
return;
}
else
{
pNode tail = *pplist;
pNode cur = *pplist;
pNode _next = cur->next;
pNode tmp = NULL;
while (_next!=NULL)
{
tmp = _next->next;
_next->next=cur;
cur = _next;
_next = tmp;
}
tail->next = NULL;
*pplist = cur;
}
}
void EraseNotTail(pList *pplist,pNode pos)
{
pNode cur = NULL;
pNode del = NULL;
assert(pplist);
if (*pplist==NULL)
{
return;
}
pos->data = pos->next->data;
del = pos->next;
cur = pos->next->next;
free(del);
pos->next = cur;
}
void ReversePrint(pList plist)
{
if(plist!=NULL)
{
if (plist->next!=NULL)
{
ReversePrint(plist->next);
}
printf("%d-->",plist->data);
}
}
void InsertFrontNode(pNode pos, DataType x)
{
pNode newnode = BuyNode(x);
DataType tmp = 0;
newnode->next = pos->next;
pos->next = newnode;
tmp = pos->data;
pos->data = newnode->data;
newnode->data = tmp;
}
void JosephCycle(pList* pplist, int k)
{
pNode del = NULL;
pNode cur = NULL;
int i = 0;
assert(pplist);
cur = *pplist;
while (cur->next!=cur)
{
for (i = 0;i<k-1;i++)
{
cur = cur->next;
}
del = cur->next;
printf("%d",cur->data);
cur->data = cur->next->data;
cur->next = cur->next->next;
free(del);
del = NULL;
}
}
pList Merge(pList* p1, pList* p2)
{
pList newlist;
pNode P1 = *p1;
pNode P2 = *p2;
pNode tail = NULL;
if ((*p1==NULL)||(*p2==NULL))
{
return NULL;
}
if (P1->data<P2->data)
{
newlist = P1;
P1 = P1->next;
}
else
{
newlist = P2;
P2 = P2->next;
}
tail = newlist;
while (P1&&P2)
{
if (P1->data<P2->data)
{
tail->next = P1;
P1 = P1->next;
}
else
{
tail->next = P2;
P2 = P2->next;
}
tail = tail->next;
}
if (P1==NULL)
{
tail->next = P2;
}
else
{
tail->next = P1;
}
return newlist;
}
pNode FindMidNode(pList plist)
{
pNode slow = NULL;
pNode fast = NULL;
slow = plist;
fast = plist;
while (fast->next!=NULL)
{
slow = slow->next;
fast = fast->next->next;
}
return slow;
}
pNode FindKNode(pList plist, int k)
{
int i = 0;
pNode fast = plist;
pNode slow = plist;
for (i=0;i<k-1;i++)
{
if (fast!=NULL)
{
fast = fast->next;
}
else
{
return NULL;
}
}
while (fast->next!=NULL)
{
fast = fast->next;
slow = slow->next;
}
return slow;
}
pNode CheckCircle(pList plist)
{
pNode slow = NULL;
pNode fast = NULL;
fast = plist;
slow = plist;
while (1)
{
fast=fast->next->next;
slow=slow->next;
if (fast->next==NULL)
{
return NULL;
}
if (fast==slow)
{
return fast;
}
}
}
int GetCircleLength(pNode meet)
{
pNode cur = meet;
int count = 1;
while(cur->next!=meet)
{
count++;
cur = cur->next;
}
return count;
}
pNode GetCycleEntryNode(pList plist, pNode meet)
{
pNode cur = plist;
pNode tail = meet;
while (cur!=tail)
{
cur=cur->next;
tail = tail->next;
}
return cur;
}
int CheckCross(pList list1, pList list2)
{
pNode cur = list1;
pNode tail = list2;
while (cur->next!=NULL)
{
cur = cur->next;
}
while (tail->next!=NULL)
{
tail = tail->next;
}
if (cur==tail)
{
return 1;
}
else
{
return 0;
}
}
pNode GetCrossNode(pList list1, pList list2)
{
pNode tmp = NULL;
pNode cur = list1;
pNode meet = NULL;
while (cur->next!=NULL)
{
cur = cur->next;
}
cur->next=list1;
meet = CheckCircle(list2);
tmp = GetCycleEntryNode(list2,meet);
}