参考教材:数据结构-用c语言描述(高等教育出版社)
1.将两个递增有序点链表AB合并成一个递减列表C,并要求利用原表的结点
void homework1(linklist &A,linklist &B,linklist &C)
{
node* a = A->next, * b = B->next,*c ;//使a,b分别指向第一个元素,c表示当前正在操作的结点
a = A; b = B;//使a,b分别指向两个链表的头结点
A = C;//将a的头结点用来做c的头结点,就不需要再单独位c分配空间,直接再a上操作
C->next = NULL;
free(B);
while (a->next != NULL && b->next != NULL)
{
if (a->date >= b->date)//使用头插法,将从小到大依次插入,最后会得到一个递减链表
{
c = b; b = b->next;
c->next = C->next;
C->next = c;
}
else
{
c = a; a = a->next;
c->next = C->next;
C->next = c;
}
}
while (a)
{
c = a;
a = a->next;
c->next = C->next;
C->next = c;
}
while (b)
{
c = b;
b = b->next;
c->next = C->next;
C->next = c;
}
}
二:使用尽可能高效的算法查找带有头节点的单链表L的倒数第k个结点并返回date域的值
int homework2(linklist L,int k)
{
node* p = NULL,*q = NULL;//p为当前结点,q用来指向目标结点
p = L->next;
int i;
for (i = 0; i < k; i++)//让p结点先走k步
{
p = p->next;
}
while (p->next)//pq都开始走,当循环结束时即p到达最后结点,此时q也到达倒数第k个
{
p = p->next;
q = q->next;
}
return q->date;
}
三:判断链表L有无环结构
void homework3(linklist L)
{
node* p = NULL, * q = NULL;//一个快指针 ,一个慢指针
p = L; q = L;
while (p->next != NULL && q->next != NULL)
{
p = p->next;//man
q = q->next->next;//kuai
if (p->next == q->next)
{
break;
}
}
if (q->next == p->next)
{
cout << "zhe shi yi ge huan\n";
}
else
{
cout << "bu shi huan\n";
}
}
完整代码如图:
#include <iostream>
using namespace std;
typedef struct node
{
char date ;
struct node* next;
}node,*linklist;
//另一种教材写法,使用指针但是还没搞懂不推荐适合用
//void initlist(linklist* L)
//{
// *L = (linklist)malloc(sizeof(node)); //如果改变一下呢???
// (*L)->next = NULL;
//}
void initlist(linklist& L)//这里的 & 是引用,要掌握
{
L = (linklist)malloc(sizeof(node));
L->next = NULL;
}
//尾插法创建单链表
void creatformback(linklist& L)
{
node* r, * s;//r wei s xin
r = L;
int flag = 1;
char c;
while (flag)
{
c = getchar();
getchar();
if (c != '0')
{
s = (node*)malloc(sizeof(node));
s->date = c;
r->next = s;
r = s;
}
else
{
flag = 0;
r->next = NULL;
}
}
}
void cout_linklist(linklist& L)
{
L = L->next;
if (L == NULL)
{
cout << "链表为空" << endl;
}
while (L != NULL)
{
cout << L->date << " ";
L = L->next;
}
}
void homework1(linklist& A, linklist& B, linklist& C);
int homework2(linklist L, int k);
void homework3(linklist L);
int main()
{
linklist L;
initlist(L);
creatformback(L);
cout_linklist(L);
homework3(L);
return 0;
}
//
void homework1(linklist &A,linklist &B,linklist &C)
{
node* a = A->next, * b = B->next,*c ;//使a,b分别指向第一个元素,c表示当前正在操作的结点
a = A; b = B;//使a,b分别指向两个链表的头结点
A = C;//将a的头结点用来做c的头结点,就不需要再单独位c分配空间,直接再a上操作
C->next = NULL;
free(B);
while (a->next != NULL && b->next != NULL)
{
if (a->date >= b->date)//使用头插法,将从小到大依次插入,最后会得到一个递减链表
{
c = b; b = b->next;
c->next = C->next;
C->next = c;
}
else
{
c = a; a = a->next;
c->next = C->next;
C->next = c;
}
}
while (a)
{
c = a;
a = a->next;
c->next = C->next;
C->next = c;
}
while (b)
{
c = b;
b = b->next;
c->next = C->next;
C->next = c;
}
}
int homework2(linklist L,int k)
{
node* p = NULL,*q = NULL;//p为当前结点,q用来指向目标结点
p = L->next;
int i;
for (i = 0; i < k; i++)//让p结点先走k步
{
p = p->next;
}
while (p->next)//pq都开始走,当循环结束时即p到达最后结点,此时q也到达倒数第k个
{
p = p->next;
q = q->next;
}
return q->date;
}
void homework3(linklist L)
{
node* p = NULL, * q = NULL;//一个快指针 ,一个慢指针
p = L; q = L;
while (p->next != NULL && q->next != NULL)
{
p = p->next;//man
q = q->next->next;//kuai
if (p->next == q->next)
{
break;
}
}
if (q->next == p->next)
{
cout << "zhe shi yi ge huan\n";
}
else
{
cout << "bu shi huan\n";
}
}
(供日后复习使用)