1.合并两个有序链表,合并后依然有序
#pragma once
#define MAX_SIZE 100
#include<stdio.h>
#include<stdio.h>
#include<assert.h>
typedef int DataType;
typedef struct ListNode
{
DataType data;
struct ListNode *next;
}ListNode;
#include"五道作业.h"
ListNode *MergeOrderedList(ListNode *list1, ListNode *list2)
{
ListNode *cur1 = list1;
ListNode *cur2 = list2;
ListNode *result = NULL;//结果链表
ListNode *tail = NULL; //结果链表中的最后一个结点
ListNode *next; //保存遍历过程中的下一个结点
ListNode *node;
while (cur1 != NULL&&cur2 != NULL)
{
if (cur1->data <= cur2->data)
{
node = cur1;
}
else{
node = cur2;
}
//取链表1的结点
next = node->next;
if (result != NULL)
{
tail->next = node;
}
else{
result = node;
}
node->next = NULL;
tail = node;
if (node == cur1)
{
cur1 = next;
}
else{
cur2 = next;
}
}
//若一个链表为空
if (cur1 == NULL)
{
tail->next = cur2;
}
if (cur2 == NULL)
{
tail->next = cur1;
}
return result;
}e *newNode = (ListNode *)malloc(sizeof(ListNode));
assert(newNode);
newNode->data = data;
newNode->next = NULL;
return newNode;
}
void ListPushBack(ListNode **ppfirst, DataType data)//尾插
{
ListNode *newNode = CreateNode(data);
if(*ppfirst == NULL)
{
*ppfirst = newNode;
return;
}
ListNode *cur = *ppfirst;
while (cur->next != NULL)
{
cur = cur->next;
cur->next = newNode;
}
}
void ListPrint(ListNode *first)
{
for (ListNode *cur = first; cur != NULL; cur = cur->next)
{
printf("%d -> ", cur->data);
}
printf("\n");
}
测试函数:
#include"五道作业.h"
void TestMerge()
{
ListNode *list1 = NULL, *list2 = NULL;
ListPushBack(&list1, 1);
ListPushBack(&list1, 1);
ListPushBack(&list1, 3);
ListPushBack(&list1, 5);
ListPushBack(&list1, 6);
ListPushBack(&list2, 1);
ListPushBack(&list2, 2);
ListPushBack(&list2, 5);
ListNode *result = MergeOrderedList(list1, list2);
ListPrint(result);
}
int main()
{
TestMerge();
ListPrint();
system("pause");
return 0;
}
2.单链表实现约瑟夫环(JosephCircle)
ListNode * JosephCycle(ListNode *first,int k)
{
//链表构成环
ListNode *tail = first;
while (tail->next != NULL)
{
tail = tail->next;
}
tail->next = first;
//
ListNode *cur = first;
while (cur->next != cur)
{
ListNode *prev = NULL;
for (int i = 0; i < k - 1; i++)
prev = cur;
cur = cur->next;//cur:要删除的结点
prev->next = cur->next;
cur = prev->next;
}
cur->next = NULL;
return cur;
}
测试函数:
void TestJosephCycle()
{
ListNode *first = NULL;
for (int i = 1; i <= 7; i++)
{
ListPushBack(&first, i);
}
ListNode *sur = JosephCycle();
printf("%d\n", sur->data);
}
3.逆置/反转单链表
void ReverseList(ListNode *first)
{
ListNode *cur = first;
ListNode *node;
ListNode *result=NULL;
result = NULL;
while (first != NULL)
{
node = cur;
cur = cur->next;
node->next = result;
result = node;
}
return result;
}
测试函数:
void TestReverseList()
{
ListNode *first = NULL;
ListPushBack(&first, 1);
ListPushBack(&first, 2);
ListPushBack(&first, 3);
ListPushBack(&first, 4);
ReverseList(first);
}
4.复杂链表的复制。一个链表的每个结点,有一个指向next指针指向下一个结点,还有一个random指针指向这个链表中的一个随机结点或者NULL,现在要求实现复制这个链表,返回复制后的新链表。
struct ComplexNode
{
int data;
struct ComplexNode *next;
struct ComplexNode *random;
}ComplexNode;
ComplexNode* BuyNode(int data)
{
char p;
ComplexNode* p = (ComplexNode*)malloc(sizeof(ComplexNode));
p->data = data;
p->next = NULL;
p->random = NULL;
return p;
}
ComplexNode* CopyComplexNode(ComplexNode* plist)
{
ComplexNode* cur = plist;
if (plist == NULL)
{
return NULL;
}
else if (plist->next == NULL)
{
return BuyNode(plist->data);
}
else
{
while (cur)
{
ComplexNode* tmp = BuyNode(cur->data);
tmp->next = cur->next;
cur->next = tmp;
cur = tmp->next;
}
cur = plist;
while (cur)
{
if (cur->random == NULL)
{
cur->next->random = NULL;
}
else
{
cur->next->random = cur->random->next;
}
cur = cur->next->next;
}
ComplexNode* newlist = plist->next;
cur = plist->next;
while (cur->next)
{
cur->next = cur->next->next;
cur = cur->next;
}
cur->next = NULL;
return newlist;
}
}
void Print(ComplexNod ComplexNode* cur = plist;
e* plist)
{
while (cur)
{
printf("%d->", cur->data);
cur = cur->next;
}
printf("NULL\n");
}
5.元素出栈、入栈的合法性。如入栈的序列(1,2,3,4,5),出栈的序列为(5,4,3,2,1)
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;
}