本篇文章对链表中的部分题型进行简单的介绍及程序的编写,若是有不对的地方,希望大佬们提点。
1.合并两个有序链表,合并后依然有序,代码如下:
typedef int DataType;
typedef struct ListNode
{
DataType data;
struct ListNode *next;
}ListNode;
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;
}
listNode *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");
}
2.逆置/反转单链表,代码如下:
void ReverseList(ListNode *first)
{
ListNode *cur = first;
ListNode *node;
ListNode *result=NULL;
while (first->next != NULL)
{
node = cur;
cur = cur->next;
node->next = result;
result = node;
}
return result;
}
3.单链表实现约瑟夫环,代码如下:
ListNode * JosephCycle(ListNode *first,int k)
{
//构成单链表
ListNode *node = first;
ListNode *prev = NULL;
int i;
while (node->next != NULL)
{
node = node->next;
}
node->next = first;
while (node->next != node)
{
for (i = 0; i < k - 1; i++)
prev = node;
node = node->next;
prev->next = node->next;
free(node);
node = prev->next;
}
}
4.复杂链表的复制。一个链表的每个结点,有一个指向next指针指向下一个结点,还有一个random指针指向这个链表中的一个随机结点或者NULL,现在要求实现复制这个链表,返回复制后的新链表,代码如下:
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;
}