算法思想:单链表有序,可以依次向后查询,设置一个前驱pre指针,一个后继q指针,依次向后遍历,查找后一位链表元素的值是否与前一位相同,如果相同则删除该节点的值
具体代码实现:
#include <stdio.h>
#include <malloc.h>
typedef int ElemType;
typedef struct LNode {
ElemType data;
struct LNode* next;
}LNode, * LinkList;
LinkList TailInsert(LinkList& L) {
int x;
L = (LinkList)malloc(sizeof(LNode));
LNode* s, * r = L;//r为表尾指针
scanf_s("%d", &x);
while (x != -1)
{
s = (LinkList)malloc(sizeof(LNode));
s->data = x;
r->next = s;
r = s;
scanf_s("%d", &x);
}
r->next = NULL;
return L;
}
void Print_LinkList(LinkList L) {
if (L == NULL || L->next == NULL) { // 处理链表为空的情况
printf("cilianbiao wei kong");
}
else {
printf("建立的单链表为:");
L = L->next;
while (L != NULL)
{
printf("%d->", L->data);
L = L->next;
}
}
}
LinkList delete_equeal(LinkList& L) {
LNode* p, * pre,*q;//设置工作指针与其前驱节点
p = L->next;
q = p->next;//设置p的后继指针
pre = L;
LNode* r;//设置另一个工作指针
while (q!=NULL)
{
if (q->data == p->data) {
q = q->next;
r = p->next;
pre->next = p->next;
free(p);
p = r;
}
else
{
q = q->next;
pre = p;
p = p->next;
}
}
return L;
}
int main() {
LinkList L = NULL;
TailInsert(L);
Print_LinkList(L);
printf("\n改变后的单链表为:\n");
delete_equeal(L);
Print_LinkList(L);
}
程序演示结果:
算法思想:依次比较链表A与链表B节点的大小如果谁更小,则利用头插法插入A链表中。
具体代码实现:
#include <stdio.h>
#include <malloc.h>
typedef int ElemType;
typedef struct LNode {
ElemType data;
struct LNode* next;
}LNode, * LinkList;
LinkList TailInsert(LinkList& L) {
int x;
L = (LinkList)malloc(sizeof(LNode));
LNode* s, * r = L;//r为表尾指针
scanf_s("%d", &x);
while (x != -1)
{
s = (LinkList)malloc(sizeof(LNode));
s->data = x;
r->next = s;
r = s;
scanf_s("%d", &x);
}
r->next = NULL;
return L;
}
void Print_LinkList(LinkList L) {
if (L == NULL || L->next == NULL) { // 处理链表为空的情况
printf("cilianbiao wei kong");
}
else {
printf("建立的单链表为:");
L = L->next;
while (L != NULL)
{
printf("%d->", L->data);
L = L->next;
}
}
}
LinkList delete_equeal(LinkList& La,LinkList &Lb) {
LNode* ra = La->next,*pa;
LNode* rb = Lb->next,*pb;
La->next = NULL;//断链
while (ra!=NULL&&rb!=NULL)
{
if (ra->data < rb->data) {
pa = ra->next;
ra->next = La->next;//头插入
La->next = ra;
ra = pa;
}
else
{
pb = rb->next;
rb->next = La->next;
La->next = rb;
rb = pb;
}
}
while (ra!=NULL)
{
pa = ra->next;
ra->next = La->next;//头插入
La->next = ra;
ra = pa;
}while (rb != NULL)
{
pb = rb->next;
rb->next = La->next;
La->next = rb;
rb = pb;
}
return La, Lb;
}
int main() {
LinkList La = NULL;
LinkList Lb = NULL;
TailInsert(La);
Print_LinkList(La);
TailInsert(Lb);
Print_LinkList(Lb);
printf("\n改变后的单链表为:\n");
delete_equeal(La,Lb);
Print_LinkList(La);
}
具体运行结果展示:
具体代码实现:
void Search_common(LinkList& La,LinkList &Lb,LinkList &Lc) {
LNode* ra = La->next,*pa;
LNode* rb = Lb->next,*pb;
LNode* r;
Lc->next = NULL;
r = Lc;
LNode* s = (LNode*)malloc(sizeof(LNode));
while (ra!=NULL&&rb!=NULL)
{
if (ra->data < rb->data) {
ra = ra->next;
}
else if(ra->data>rb->data)
{
rb = rb->next;
}
else
{
s->data = ra->data;
r->next = s;
r = s;
ra = ra->next;
rb = rb->next;
}
}
r->next = NULL;
Print_LinkList(Lc);
}
算法思想:设置指针依次寻找链表A与链表B中的元素,如果一方数据元素小于另一方则向后继续查找,找到相等的点时,将其尾插入A链表中。
具体代码实现:
#include <stdio.h>
#include <malloc.h>
typedef int ElemType;
typedef struct LNode {
ElemType data;
struct LNode* next;
}LNode, * LinkList;
LinkList TailInsert(LinkList& L) {
int x;
L = (LinkList)malloc(sizeof(LNode));
LNode* s, * r = L;//r为表尾指针
scanf_s("%d", &x);
while (x != -1)
{
s = (LinkList)malloc(sizeof(LNode));
s->data = x;
r->next = s;
r = s;
scanf_s("%d", &x);
}
r->next = NULL;
return L;
}
void Print_LinkList(LinkList L) {
if (L == NULL || L->next == NULL) { // 处理链表为空的情况
printf("cilianbiao wei kong");
}
else {
printf("建立的单链表为:");
L = L->next;
while (L != NULL)
{
printf("%d->", L->data);
L = L->next;
}
}
}
void Search_common(LinkList& La,LinkList &Lb) {
LNode* ra = La->next,*pa;
LNode* rb = Lb->next,*pb;
La->next = NULL;
LNode* r=La;
LNode* q, * p;
while (ra!=NULL&&rb!=NULL)
{
if (ra->data < rb->data) {
q = ra;
ra = ra->next;
free(q);
}
else if(ra->data>rb->data)
{
p = rb;
rb = rb->next;
free(p);
}
else{
r->next = ra;
r = ra;
ra = ra->next;
rb = rb->next;
}
}
r->next = NULL;
}
int main() {
LinkList La = NULL;
LinkList Lb = NULL;
TailInsert(La);
Print_LinkList(La);
TailInsert(Lb);
Print_LinkList(Lb);
printf("\n改变后的单链表为:\n");
Search_common(La,Lb);
Print_LinkList(La);
}
具体结果展示:
算法思想:1.如果B链表的长度大于A链表的长度,返回错误,B不是A 的连续子序列。
2.B链表长度小于A链表长度,则进行一一比对,将链表A逐步遍历看是否能有完全与B相对应的连续子序列如果有,则返回TRUE,否则返回FALSE.
具体代码实现:
#include <stdio.h>
#include <malloc.h>
typedef int ElemType;
typedef struct LNode {
ElemType data;
struct LNode* next;
}LNode, * LinkList;
LinkList TailInsert(LinkList& L) {
int x;
L = (LinkList)malloc(sizeof(LNode));
LNode* s, * r = L;//r为表尾指针
scanf_s("%d", &x);
while (x != -1)
{
s = (LinkList)malloc(sizeof(LNode));
s->data = x;
r->next = s;
r = s;
scanf_s("%d", &x);
}
r->next = NULL;
return L;
}
void Print_LinkList(LinkList L) {
if (L == NULL || L->next == NULL) { // 处理链表为空的情况
printf("cilianbiao wei kong");
}
else {
printf("建立的单链表为:");
L = L->next;
while (L != NULL)
{
printf("%d->", L->data);
L = L->next;
}
}
}
bool Is_ture(LinkList La,LinkList Lb) {
LNode* pre = La;
LNode* q = La->next;
LNode* p = Lb->next;
while (q != NULL && p != NULL)
{
if (q->data == p->data) {
q = q->next;
p = p->next;//同时向后移动
}
else
{
pre = q;
q =q->next;//La向后移动
p = Lb->next;
}
}
if (p == NULL) {
printf("B是A的子序列");
return true;
}
else
{
printf("B不是A的子序列");
return false;
}
}
int main() {
LinkList La = NULL;
LinkList Lb = NULL;
TailInsert(La);
Print_LinkList(La);
TailInsert(Lb);
Print_LinkList(Lb);
// printf("\n改变后的单链表为:\n");
Is_ture(La,Lb);
// Print_LinkList(La);
}
程序运行截图:
算法思想:依次找双链表的前驱和后继,判断前驱和后继的值是否相等如果相等就依次向后移动,直到达到循环结束条件为止。
具体代码:
#include <stdio.h>
#include <malloc.h>
typedef int ElemType;
typedef struct LNode {
ElemType data;
struct LNode* next;
struct LNode* prior;
}DNode, * DLinkList;//双链表结构体声明
DLinkList TailInsert(DLinkList& L) {
int x;
L = (DLinkList)malloc(sizeof(DNode));
DNode* s, * r = L;//r为表尾指针
scanf_s("%d", &x);
while (x != -1)
{
s = (DLinkList)malloc(sizeof(DNode));
s->data = x;
r->next = s;
s->prior = r;
r = s;
scanf_s("%d", &x);
}
r->next = L;
L->prior = r;
return L;
}
void Print_LinkList(DLinkList L) {
DNode* r = L->next;
if (L == NULL || L->next == NULL) { // 处理链表为空的情况
printf("cilianbiao wei kong");
}
else {
printf("建立的循环双链表为:");
while (r!=L )
{
printf("%d->", r->data);
r = r->next;
}
}
}
bool Is_duichen(DLinkList L) {
DNode* p = L->next;
DNode* q = L->prior;
while (p->next!=q&& p!=q)
{
if (p->data == q->data)
{
p = p->next;
q = q->prior;
}
else
{
printf("此循环双链表不对称");
return false;
}
}
printf("此循环双链表对称");
return true;
}
int main() {
DLinkList L = NULL;
TailInsert(L);
Print_LinkList(L); // 创建循环双链表
Is_duichen(L);
}
运行结果展示:
算法思想:依次找到h1与h2的尾节点,然后将尾节点链接到下一个链表的头节点上,即可完成链表和合并。
具体代码实现:
#include <stdio.h>
#include <malloc.h>
typedef int ElemType;
typedef struct LNode {
ElemType data;
struct LNode* next;
}LNode, * LinkList;//双链表结构体声明
LinkList TailInsert(LinkList& L) {
int x;
L = (LinkList)malloc(sizeof(LNode));
LNode* s, * r = L;//r为表尾指针
scanf_s("%d", &x);
while (x != -1)
{
s = (LinkList)malloc(sizeof(LNode));
s->data = x;
r->next = s;
r = s;
scanf_s("%d", &x);
}
r->next = L;
return L;
}
void Print_LinkList(LinkList &L) {
LNode* r = L->next;
if (L == NULL || L->next == NULL) { // 处理链表为空的情况
printf("cilianbiao wei kong");
}
else {
printf("建立的循环单链表为:");
while (r!=L )
{
printf("%d->", r->data);
r = r->next;
}
}
}
LinkList Merger_A_B(LinkList &La,LinkList &Lb) {
LNode* r = La->next;
LNode* p = Lb->next;
while (r->next!=La)
{
r = r->next;
}
r->next = p;
while (p->next!=Lb)
{
p = p->next;
}
p->next = La;
return La;
}
int main() {
LinkList La = NULL;
LinkList Lb = NULL;
TailInsert(La);
Print_LinkList(La); // 创建循环双链表
TailInsert(Lb);
Print_LinkList(Lb);
printf("\n改变后的单链表为:\n");
Merger_A_B(La,Lb);
Print_LinkList(La);
// Print_LinkList(La);
}
结果展示:
算法思想:循环遍历单链表,直到删除完所有节点后,循环单链表为空则退出循环
具体代码展示:
#include <stdio.h>
#include <malloc.h>
typedef int ElemType;
typedef struct LNode {
ElemType data;
struct LNode* next;
}LNode, * LinkList;//单链表结构体声明
LinkList TailInsert(LinkList& L) {
int x;
L = (LinkList)malloc(sizeof(LNode));
LNode* s, * r = L;//r为表尾指针
scanf_s("%d", &x);
while (x != -1)
{
s = (LinkList)malloc(sizeof(LNode));
s->data = x;
r->next = s;
r = s;
scanf_s("%d", &x);
}
r->next = L;
return L;
}
void Print_LinkList(LinkList &L) {
LNode* r = L->next;
if (L == NULL || L->next == NULL) { // 处理链表为空的情况
printf("cilianbiao wei kong");
}
else {
printf("建立的循环单链表为:");
while (r!=L )
{
printf("%d->", r->data);
r = r->next;
}
}
}
void Del_min(LinkList &L) {
LNode* min_pre;
LNode* min;
LNode* pre;
LNode* p;
while (L->next != L)
{
p = L->next;
pre = L;
min_pre = pre;
min = p;
while (p != L)
{
if (min->data > p->data) {
min_pre = pre;
min = p;
}
pre = p;
p = p->next;
}
printf("%d->", min->data);
min_pre->next = min->next;
free(min);
}
free(L);
}
int main() {
LinkList L = NULL;
TailInsert(L);
Print_LinkList(L); // 创建循环单链表
printf("\n改变后的单链表为:\n");
Del_min(L);
}
具体结果展示: