先遍历链表,遍历的同时将链表的值头插进一个新的链表,在打印新的链表即可。
void SLitsPrintTailToHead(SListNode* pHead)
{
SListNode* _new, *ptr;
_new = NULL;
ptr = pHead;
while (ptr)
{
SListPushFront(&_new, ptr->data);
ptr = ptr->next;
}
while (_new)
{
printf("%d ", _new->data);
_new = _new->next;
}
}
void SListDelNonTailNode(SListNode* pos)
{
SListNode* Ppos;
Ppos = NULL;
Ppos = pos->next;
pos->data = Ppos->data;
pos->next = Ppos->next;
free(Ppos);
}
void SListInsertFrontNode(SListNode* pos, DataType x)
{
SListNode* _new;
_new = NULL;
_new = BuySListNode(pos->data);
_new->next = pos->next;
pos->next = _new;
pos->data = x;
}
SListNode* SListJosephCircle(SListNode* pHead, int k)
{
SListNode* str, *tail;
int i = 0;
str = pHead;
tail = pHead;
while (tail->next)
{
tail = tail->next;
}
tail->next = pHead;
while (str->next!=str)
{
for (;i < k;++i)
{
str = str->next;
}
str->data = str->next->data;
str->next = str->next->next;
free(str->next);
}
return str;
}
SListNode* SListReverse(SListNode* list)
{
SListNode* _new, *str;
_new = NULL;
str = list;
while (str)
{
SListPushFront(&_new, str->data);
str = str->next;
}
return _new;
}
6.单链表冒泡排序
void SListBubbleSort(SListNode* list)
{
int i, j,count;
count = 0;
int mid;
SListNode* str,*ptri,*ptrj;
str = list;
while (str)
{
str = str->next;
count++;
}
for (i = 0;i < count-1;i++)
{
ptri = list;
ptri = ptri->next;
for (j = 0;j < i-1;j++)
{
ptrj = list;
ptrj = ptrj->next;
if (ptri->data > ptrj->data)
{
mid = ptri->data;
ptri->data = ptrj->data;
ptrj->data = mid;
}
}
}
}
SListNode* SListMerge(SListNode* list1, SListNode* list2)
{
SListNode* str1, *str2,*_new;
str1 = list1;
str2 = list2;
_new = NULL;
while (str1&&str2)
{
if (str1->data > str2->data)
{
SListPushBack(&_new,str2->data);
str2 = str2->next;
}
if (str1->data < str2->data)
{
SListPushBack(&_new, str1->data);
str1 = str1->next;
}
else
{
SListPushBack(&_new, str2->data);
}
}
return _new;
}
SListNode* SListFindMidNode(SListNode* list)
{
SListNode* fast, *slow;
fast = list;
slow = list;
while (fast->next)
{
fast = fast->next;
if (fast->next == NULL)
{
break;
}
fast = fast->next;
slow = slow->next;
}
return slow;
}
SListNode* SListFindTailKNode(SListNode* list, size_t k)
{
SListNode* fast, *slow;
fast = list;
slow = list;
int i;
for (i = 0;i < k;i++)
{
fast = fast->next;
}
while (fast)
{
fast = fast->next;
slow = slow->next;
}
return slow;
}
SListNode* SListIsCycle(SListNode* list)
{
SListNode* str,*ptr;
str = list;
ptr = SListFind(list, 0);
while (str)
{
str = str->next;
}
str->next = ptr;
return ptr;
}
int SListCycleLen(SListNode* meetNode)
{
int len,count1,count2;
SListNode*fast, *slow;
fast = meetNode->next;
slow = meetNode;
int count=1;
while (fast!=slow)
{
fast = fast->next->next;
slow = slow->next;
}
slow - slow->next;
while (slow != fast)
{
slow = slow->next;
count++;
}
return count;
}