//1.合并两个有序链表 合并后仍然有序
int MergeList(SListNode* pHead1, SListNode* pHead2)
{
if (pHead1 == NULL)//链表1为空
{
return pHead2;
}
else if (pHead2 == NULL)//
{
return pHead1;
}
SListNode* pMergeListHead = NULL;
if (pHead1->data < pHead2->data)
{
pMergeListHead = pHead1;
pMergeListHead->pNext = MergeList(pHead1->pNext, pHead2);
}
else
{
pMergeListHead = pHead2;//同上
pMergeListHead->pNext = MergeList(pHead1, pHead2->pNext);
}
return pMergeListHead;//返回新链表的头结点
}
//2.单链表实现约瑟夫环
void Josephcircle(SListNode * pFirst, int k){
SListNode * pNode = pFirst;
SListNode * pPrve = NULL;
int i;
while (pNode->pNext != NULL)
{
pNode = pNode->pNext;
}
pNode->pNext = pFirst;
while (pNode->pNext != pNode)
{
for (i = 0, i < k - 1, i++;)
{
pPrve = pNode;
pNode = pNode->pNext;
}
free(pNode);
pNode = pPrve->pNext;
}
}
//3.链表的逆置
SListNode* ReverseList(SListNode* pHead)
{
SListNode* cur = pHead;
SListNode* newHead = NULL;
while (cur)
{
SListNode* tmp = cur;
cur = cur->pNext;
tmp->pNext = newHead;
newHead = tmp;
}
return newHead;
}
//4.复制复杂链表
typedef struct listNode
{
int data;
struct listNode* random;
struct listNode* next;
} listNode;
static listNode * ComplexCreateNode(int data)
{
listNode *node = (listNode *)malloc(sizeof(Node));
node->data = data;
node->random = NULL;
node->next = NULL;
return node;
}
listNode* CopyCmList(listNode* list)
{
listNode* cur = list;
while (cur != NULL)
{
listNode* newnode = ComplexCreateNode(cur->data);
newnode->next = cur->next;
cur->next = newnode;
cur = cur->next->next;
}
cur = list;
while (cur != NULL)
{
cur->next->random = cur->random->next;
cur = cur->next->next;
}
cur = list;
listNode* newlist = cur->next; //保存新链表
while (cur != NULL)
{
listNode* node = cur->next;
cur->next = node->next;
if (cur->next != NULL)
{
node->next = cur->next->next;
}
else
{
node->next = NULL;
}
cur = cur->next;
}
return newlist;
}
//5.判断出入栈合法性
int IsValid(char in[], char out[], int size)
{
int ii = 0, io = 0;
Stack stack;
StackInit(&stack);
while (ii < size)
{
if (in[ii] == out[io])
{
ii++;
io++;
}
else{
if (!StackEmpty(&stack) && StackTop(&stack) == out[io])
{
StackPop(&stack);
io++;
}
else{
StackPush(&stack, in[ii]);
ii++;
}
}
}
while (!StackEmpty(&stack)
{
if (StackTop(&stack) != out[io])
{
return 0;
}
StackPop(&stack);
io++;
}
return 1;
}