链表练习题
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef struct SListNode
{
int data;
struct SListNode *pNext;
}SList;
void Init(SList **pphead)
{
*pphead = NULL;
}
void Print(SList *pHead)
{
assert(pHead);
if(pHead == NULL)
{
printf("链表为空\n");
return;
}
while(pHead != NULL)
{
printf("%d -> ",pHead->data);
pHead = pHead->pNext;
}
printf("NULL\n");
}
void SListPushBack(SList **ppHead, int data)
{
SList *pNode;
SList *pNew = (SList *)malloc(sizeof(SList));
if(*ppHead == NULL)
{
*ppHead = pNew;
pNew->data = data;
pNew->pNext = NULL;
return;
}
pNode = *ppHead;
while(pNode->pNext != NULL)
{
pNode = pNode->pNext;
}
pNode->pNext = pNew;
pNew->data = data;
pNew->pNext = NULL;
}
void SListPushFront(SList **ppHead, int data)
{
SList *pNew = (SList *)malloc(sizeof(SList));
if(*ppHead == NULL)
{
*ppHead = pNew;
pNew->data = data;
pNew->pNext = NULL;
return;
}
pNew->pNext = *ppHead;
pNew->data = data;
*ppHead = pNew;
}
void SListPopBack(SList **ppHead)
{
SList *pHead = *ppHead;
SList *pNode;
if(*ppHead == NULL)
{
printf("链表为空\n");
return;
}
if(pHead->pNext == NULL)
{
free(pHead);
*ppHead = NULL;
return;
}
while(pHead->pNext != NULL)
{
pNode = pHead;
pHead = pHead->pNext;
}
free(pHead);
pNode->pNext = NULL;
}
void SListPopFront(SList **ppHead)
{
SList *pHead = *ppHead;
if(pHead->pNext == NULL)
{
free(pHead);
*ppHead = NULL;
return;
}
*ppHead = pHead->pNext;
free(pHead);
}
void SListInsert(SList **ppHead, int x, int data)
{
SList *pHead;
SList *pNew = (SList *)malloc(sizeof(SList));
pHead = *ppHead;
if(pHead->data == x)
{
pNew->pNext = pHead;
pNew->data = data;
*ppHead = pNew;
return;
}
while(pHead->pNext->data != x)
{
pHead = pHead->pNext;
}
pNew->pNext = pHead->pNext;
pHead->pNext = pNew;
pNew->data = data;
}
void SListDel(SList **ppHead, int data)
{
SList *pHead = *ppHead;
SList *pNode;
if(pHead->data == data)
{
SListPopFront(ppHead);
return;
}
while(pHead != NULL)
{
if(pHead->data == data)
{
pNode->pNext = pHead->pNext;
free(pHead);
return;
}
pNode = pHead;
pHead = pHead->pNext;
}
}
void SListRemoveAll(SList **ppHead, int data)
{
SList *pNode = *ppHead;
SList *pNode1;
while(pNode->pNext != NULL)
{
if(pNode->pNext->data == data)
{
pNode1 = pNode->pNext;
pNode->pNext = pNode1->pNext;
free(pNode1);
}else
{
pNode = pNode->pNext;
}
}
if((*ppHead)->data == data)
{
SListPopFront(ppHead);
}
}
SList *SListFind(SList *pHead, int data)
{
SList *pNode = pHead;
while(pNode != NULL)
{
if(pNode->data == data)
{
return pNode;
}
pNode = pNode->pNext;
}
return NULL;
}
void SListDestroy(SList **ppHead)
{
SList *pNode;
assert(ppHead);
while(*ppHead != NULL)
{
pNode = *ppHead;
*ppHead = (*ppHead)->pNext;
free(pNode);
}
}
void DelSList(SList **ppHead,int data)
{
SList *pHead = *ppHead;
assert(ppHead);
while(pHead->data != data)
{
pHead = pHead->pNext;
}
while(pHead->pNext->pNext != NULL)
{
pHead->data = pHead->pNext->data;
pHead = pHead->pNext;
}
pHead->data = pHead->pNext->data;
free(pHead->pNext);
pHead->pNext = NULL;
}
void SListRever(SList **ppHead)
{
SList *p1,*p2,*p3;
assert(ppHead);
p1 = NULL;
p2 = *ppHead;
p3 = p2->pNext;
while(p2 != NULL)
{
p2->pNext = p1;
p1 = p2;
p2 = p3;
if(p3 != NULL)
{
p3 = p3->pNext;
}
}
*ppHead = p1;
}
- 在无头单链表的一个节点前插入一个节点(不能遍历链表)
void InsertNoFirstNode(SList **ppPos, int data)
{
SList *pPos = *ppPos;
SList *pNode;
assert(ppPos);
if(*ppPos == NULL)
{
SListPushBack(ppPos, data);
return;
}
pNode = (SList *)malloc(sizeof(SList));
pNode->data = data;
pNode->pNext = NULL;
pNode->pNext = pPos->pNext;
pPos->pNext = pNode;
pNode->data = pPos->data;
pPos->data = data;
}
void SListJosephCircle(SList **ppHead, int n)
{
SList *pHead = *ppHead;
SList *pNode;
int i;
while(pHead->pNext != NULL)
{
pHead = pHead->pNext;
}
pHead->pNext = *ppHead;
pHead = *ppHead;
while(pHead->pNext != pHead)
{
i = n;
while((--i)>0)
{
pNode = pHead;
pHead = pHead->pNext;
}
pNode->pNext = pHead->pNext;
free(pHead);
pHead = pNode->pNext;
}
pHead->pNext = NULL;
*ppHead = pHead;
}
void PrintFail(SList *pHead)
{
if(pHead == NULL)
{
return;
}
else
{
PrintFail(pHead->pNext);
printf("->%d",pHead->data);
}
}
void SListSort(SList **ppHead)
{
SList *pHead = *ppHead;
SList *pNode = *ppHead;
SList *First = NULL;
int i = 0;
while(pHead != NULL)
{
pNode = pHead;
i = 0;
while(pNode != NULL)
{
if(pNode->data > i)
{
i = pNode->data;
}
pNode = pNode->pNext;
}
SListPushBack(&First, i);
SListDel(&pHead, i);
}
*ppHead = First;
}
SList *SListmerge(SList **ppHead, SList **pptwo)
{
SList *pone = *ppHead;
SList *ptwo = *pptwo;
SList *merge = NULL;
SList *ptmp = NULL;
while(pone != NULL && ptwo != NULL)
{
if(pone->data < ptwo->data)
{
SListPushBack(&merge, pone->data);
pone = pone->pNext;
}else
{
SListPushBack(&merge, ptwo->data);
ptwo = ptwo->pNext;
}
}
if(pone == NULL)
{
ptmp = ptwo;
}else
{
ptmp = pone;
}
while(ptmp != NULL)
{
SListPushBack(&merge, ptmp->data);
ptmp = ptmp->pNext;
}
return merge;
}
SList *SListintersection(SList **ppone, SList **pptwo)
{
SList *pone = *ppone;
SList *ptwo = *pptwo;
SList *pnew = NULL;
while(pone != NULL)
{
ptwo = *pptwo;
while(ptwo != NULL)
{
if(pone->data == ptwo->data)
{
SListPushBack(&pnew, pone->data);
}
ptwo = ptwo->pNext;
}
pone = pone->pNext;
}
return pnew;
}
SList * SListUnion(SList **ppone, SList **pptwo)
{
SList *pone = *ppone;
SList *ptwo = *pptwo;
SList *pnew = pone;
int stract = 0;
int tmp = 0;
if(pone == NULL && ptwo != NULL)
return ptwo;
if(ptwo == NULL && pone != NULL)
return pone;
assert(ppone&&pptwo);
while(ptwo != NULL)
{
pone = *ppone;
while(pone != NULL)
{
if(pone->data != ptwo->data)
{
stract = 1;
tmp = ptwo->data;
}
if(pone->data == ptwo->data)
{
stract = 0;
break;
}
pone = pone->pNext;
}
if(stract == 1)
{
SListPushBack(&pnew, tmp);
stract = 0;
}
ptwo = ptwo->pNext;
}
return pnew;
}
int SListstrlen(SList *pHead)
{
int count = 0;
SList *str = pHead;
while(str != NULL)
{
count++;
str = str->pNext;
}
return count;
}
//链表如果相交,终点肯定是一致的,比较他们的末尾节点的地址,相等则相交。
//如果相交,求出俩链表长度,让长的先走链表长度差的步数,
//然后两链表同时向后走,地址相等时,两个链表相交
SList *SListIsintersect(SList *pHead, SList *ptwo1)
{
SList *pone = pHead;
SList *ptwo = ptwo1;
SList *Long = NULL;
SList *Short = NULL;
int n = 0;
int x = SListstrlen(pone);
int y = SListstrlen(ptwo);
if(x > y)
{
Long = pone;
Short = ptwo;
n = x-y;
}
else
{
Long = ptwo;
Short = pone;
n = y-x;
}
for(n; n>0; n--)
{
Long = Long->pNext;
}
while(Long != Short)
{
Long = Long->pNext;
Short = Short->pNext;
}
return Long;
}
void SListDelK(SList **pptwo, int k)
{
SList *pfast = *pptwo;
SList *pslow = *pptwo;
SList *Del = NULL;
assert(pptwo);
while(k-1)
{
k--;
pfast = pfast->pNext;
}
if(pfast->pNext == NULL)
{
SListPopFront(pptwo);
return;
}
while(pfast->pNext != NULL)
{
Del = pslow;
pfast = pfast->pNext;
pslow = pslow->pNext;
}
Del->pNext = pslow->pNext;
}
SList *SListmeet(SList *ptwo)
{
SList *fast = ptwo;
SList *slow = ptwo;
if(ptwo == NULL)
{
return NULL;
}
while(1)
{
if(fast->pNext == NULL || fast->pNext->pNext == NULL)
{
return NULL;
}
fast = fast->pNext->pNext;
slow = slow->pNext;
if(fast == slow)
{
return slow;
}
}
}
//两个指针,一个从链表开始部分走,一个从相遇的点开始走,当两个指针相等的时候,就是环的入口点
SList *SListCircle(SList *ptwo, SList *meet)
{
SList *first = ptwo;
SList *pMeet = meet;
assert(ptwo && meet);
while(first != pMeet)
{
first = first->pNext;
pMeet = pMeet->pNext;
}
return first;
}
int SListlen(SList *meet)
{
int count = 1;
SList *pMeet = meet->pNext;
while(pMeet != meet)
{
count++;
pMeet = pMeet->pNext;
}
return count;
}
/*
void CListCopy(CList **ppHead, SList **ppCopy)
{
CList *pHead = *ppHead;
CList *Copy = *ppCopy;
CList *pNode = NULL;
Clist *Next = NULL;
CList *pNew = NULL;
assert(ppHead);
while(pHead != NULL)
{
pNode = pHead->pNext;
pNew = (CList *)malloc(sizeof(CList));
pNew->data = pHead->data;
pNew->Rom = NULL;
pNew->pNext = pNode;
pHead->pNext = pNew;
pHead = pHead->pNext-pNext;
}
pHead = *ppHead;
while(pHead != NULL)
{
Next = pHead->Rom;
if(Next != NULL)
{
pHead->pNext->Rom = Next->pNext;
pHead = pHead->pNext->pNext;
}
}
pHead = *ppHead;
Copy = pHead->pNext;
while(pHead != NULL)
{
Next = pHead->pNext->pNext;
if(Next != NULL)
{
Copy->pNext = Next->pNext;
}
else
{
Copy->pNext = NULL;
}
pHead->pNext = Next;
pHead = pHead->pNext;
}
}
*/
int main()
{
SList *pHead;
SList *ptwo = NULL;
SList *pNode = NULL;
SList *pNode2 = NULL;
SList *pCP = NULL;
SList *meet = NULL;
SList *circle = NULL;
int c = 0;
Init(&pHead);
SListPushBack(&pHead, 1);
SListPushBack(&pHead, 2);
SListPushBack(&pHead, 3);
SListPushBack(&pHead, 4);
SListPushBack(&pHead, 6);
SListPushBack(&pHead, 9);
SListPushBack(&pHead, 1);
SListPushBack(&pHead, 8);
SListPushBack(&pHead, 5);
SListPushBack(&ptwo, 2);
SListPushBack(&ptwo, 4);
SListPushBack(&ptwo, 7);
SListPushBack(&ptwo, 5);
SListPushBack(&ptwo, 3);
SListPushBack(&ptwo, 1);
pNode = SListFind(ptwo, 7);
pNode2 = SListFind(ptwo, 1);
pNode2->pNext = pNode;
meet = SListmeet(ptwo);
if(meet != NULL)
{
}
circle = SListCircle(ptwo, meet);
c = SListlen(meet);
return 0;
}