头文件:“test.h”
typedef int Elem_type;
typedef struct Node
{
Elem_type mdata;
struct Node* pnext;
}Node, *Link;
void Init(Link phead);
Node BuyNode(Elem_type val);
//static Link BuyNode();
bool InsertTail(Link phead,Elem_type val);
void print(Link phead,Elem_type val);
Node* Search(Link phead, Elem_type val);
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "test.h"
void Init(Link phead)
{
assert(phead != NULL);
if(phead == NULL)
{
return ;
}
phead->pnext = NULL;
}
Link BuyNode()
{
struct Node* pnewnode = (struct Node*)malloc(sizeof(struct Node));
assert(pnewnode != NULL);
pnewnode->pnext = NULL;
return pnewnode;
}
bool InsertTail(Link phead,Elem_type val)
{
if(phead == NULL)
{
return false;
}
struct Node* pCur = phead;
while(pCur->pnext != NULL)
{
pCur = pCur->pnext;
}
struct Node* pnewnode = BuyNode();
pnewnode->mdata = val;
pCur->pnext = pnewnode;
return true;
}
Node* Search(Link phead, Elem_type val)
{
if(phead == NULL)
{
return 0;
}
Node* pCur = phead->pnext;
Node* pNext = pCur;
while(pCur != NULL)
{
if(pCur->mdata == val)
{
return pCur;
}
pCur = pCur->pnext;
}
}
void Print(Link phead)
{
if(phead == NULL)
{
return ;
}
struct Node* pCur = phead->pnext;
while(pCur != NULL)
{
printf("%3d",pCur->mdata);
pCur = pCur->pnext;
}
printf("\n");
}
Node* find_k(Link phead,int k) //第一题
{
Node* pfast = phead->pnext;
Node* pslow = pfast;
int i;
for(i = 0;i < k-1;i++)
{
pfast = pfast->pnext;
}
while(pfast->pnext != NULL)
{
pfast = pfast->pnext;
pslow = pslow->pnext;
}
return pslow;
}
void Delete_k(Node* pCur) //第二题
{
Node* pNext = pCur->pnext;
pCur->mdata = pNext->mdata;
pCur->pnext = pNext->pnext;
free(pNext);
}
int InsertFront(Node* pCur,Elem_type val) //第三题
{
Node* pnewnode = (Node *)malloc(sizeof(Node));
if(pnewnode == NULL)
{
return 0;
}
pCur->pnext = pnewnode->pnext;
pCur->pnext = pnewnode;
pnewnode->mdata = pCur->mdata;
pCur->mdata = val; //注意赋值顺序
return 1;
}
Node* Middlenode(Link phead) //第五题
{
if(phead == NULL)
{
return 0;
}
Node* pfast = phead;
Node* pslow = pfast;
while(pfast != NULL && pfast->pnext != NULL)
{
pfast = pfast->pnext->pnext; //快指针是慢指针的2倍
pslow = pslow->pnext;
}
return pslow;
}
Node* Intersect(Link phead1,Link phead2) //第四题
{
Node* pCur1 = phead1;
Node* pCur2 = phead2;
int lens1 = 0;
int lens2 = 0;
while(pCur1->pnext != NULL)
{
lens1++;
pCur1 = pCur1->pnext;
}
while(pCur2->pnext != NULL)
{
lens2++;
pCur2 = pCur2->pnext;
}
if(pCur1 == pCur2) //两条链表最后地址相同,说明链表相交
{ //以下为求交点
int k = abs(lens1-lens2);
pCur1 = abs(lens1-lens2)>0 ? phead1:phead2;
pCur2 = abs(lens1-lens2)<=0 ? phead1:phead2;
for(int i = 0;i < k;i++)
{
pCur1 = pCur1->pnext;
}
while(pCur1 != NULL)
{
if(pCur1 == pCur2)
{
return pCur1;
}
pCur1 = pCur1->pnext;
pCur2 = pCur2->pnext;
}
}
return NULL;
}
void PrintReverse(Link phead) //第六题
{
if(phead->pnext == NULL)
{
return ;
}
PrintReverse(phead->pnext); //递归调用
printf("%3d",phead->pnext->mdata);
}
int main()
{
Node head;
Init(&head);
for(int i = 0;i < 9;i++)
{
InsertTail(&head,i+1);
}
Print(&head);
Node* prt1 = find_k(&head,3);
printf("prt1 Address:%x data:%d\n",prt1,prt1->mdata);
// Node* prt2 = Search(&head,7);
// printf("prt2 Address:%x data:%d\n",prt2,prt2->mdata);
//测试search函数
//Node* prt3 = Search(&head,3);
//printf("prt3 Address:%x data:%d\n",prt3,prt3->mdata);
//if(prt3 != NULL)
//{
// Delete_k(prt3);
//}
//Print(&head);
//Node* prt4 = Search(&head,3);
//printf("prt4 Address:%x data:%d\n",prt4,prt4->mdata);
//if(prt4 != NULL)
//{
// InsertFront(prt4,0);
//}
//Print(&head);
//Node* prt5 = Middlenode(&head);
//printf("prt5 Address:%x data:%d\n",prt5,prt5->mdata);
//Node head2;
//Init(&head2);
//for(int i = 2;i<9;i++)
//{
// InsertTail(&head2,i);
//}
//Print(&head2);
//Node* prt6 = Intersect(&head,&head2);
//if(prt6 != NULL)
//{
// printf("prt6 Address:%x data:%d\n",prt6,prt6->mdata);
//}
// PrintReverse(&head);
return 0;
}