链表、栈面试题

本文详细介绍了几种常见的链表操作方法,包括合并两个有序链表、实现约瑟夫环、链表逆置、复制复杂链表及判断出入栈合法性。通过具体的代码实现,帮助读者深入理解这些算法的工作原理。

//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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值