#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <math.h>
#include <stdlib.h>
#include <stack>
#include <queue>
#include <random>
#include <set>
using namespace std;
#define N 100
#define Len sizeof(struct Link)
// 删除链表的节点
struct Link
{
int n;
struct Link *next;
} li;
void Print(struct Link *p)
{
for (; p != NULL; p = p->next)
{
cout << p->n << " ";
}
cout << endl;
}
struct Link *del(struct Link *a, struct Link *b)
{
struct Link *p1, *p2, *p;
p = p1 = a;
p2 = b;
while (p1 != NULL)
{
if (p2->n == p1->n)
{
p->next = p1->next;
p1 = p1->next;
p = p1;
p2 = p2->next;
}
else
{
p = p1;
p1 = p1->next;
}
}
return a;
}
struct Link *create(int n)
{
struct Link *head, *p, *ph;
head = NULL;
ph = (struct Link *)malloc(Len);
ph->n = n;
ph->next = NULL;
head = ph;
for (int i = n + n; i <= n * 10; i += n)
{
p = (struct Link *)malloc(Len);
p->n = i;
p->next = NULL;
ph->next = p;
ph = p;
}
return head;
}
int main(int argc, char const *argv[])
{
struct Link *a, *b;
a = create(1);
Print(a);
b = create(2);
Print(b);
a = del(a, b);
Print(a);
return 0;
}
// c9-10合并链表
// struct Link
// {
// int n;
// struct Link *next;
// } li;
// void Print(struct Link *p)
// {
// for (; p != NULL; p = p->next)
// {
// cout << p->n << " ";
// }
// cout << endl;
// }
// struct Link *create(int n)
// {
// struct Link *head, *p, *ph;
// head = NULL;
// ph = (struct Link *)malloc(Len);
// ph->n = n;
// ph->next = NULL;
// head = ph;
// for (int i = n + n; i <= n * 10; i += n)
// {
// p = (struct Link *)malloc(Len);
// p->n = i;
// p->next = NULL;
// ph->next = p;
// ph = p;
// }
// return head;
// }
// struct Link *Addlink(struct Link *a, struct Link *b)
// {
// struct Link *nh, *p, *t;
// nh = NULL;
// while (a && b)
// {
// if (a->n < b->n)
// {
// p = a;
// a = a->next;
// }
// else
// {
// p = b;
// b = b->next;
// }
// if (!nh)
// {
// nh = t = p;
// }
// else
// {
// t = t->next = p;
// }
// }
// if (!a)
// {
// t->next = b;
// }
// else
// {
// t->next = a;
// }
// return nh;
// }
// int main(int argc, char const *argv[])
// {
// int n;
// struct Link *a, *b;
// a = create(1);
// Print(a);
// b = create(2);
// Print(b);
// a = Addlink(a, b);
// Print(a);
// return 0;
// }
// c9-10 链表实现学生信息系统
// struct Link
// {
// int id;
// float score;
// struct Link *next;
// } li;
// void Print(struct Link *p)
// {
// for (; p != NULL; p = p->next)
// {
// cout << p->id << " " << p->score << endl;
// }
// }
// struct Link *insert(struct Link *p, struct Link *newnode)
// {
// struct Link *h, *ph;
// h = p;
// ph = p;
// while (h->next != NULL)
// {
// h = h->next;
// }
// h->next = newnode;
// h = ph;
// return h;
// }
// struct Link *del(struct Link *p, int id)
// {
// struct Link *p1, *p2, *ph;
// p1 = p2 = ph = p;
// while (p1 != NULL)
// {
// if (p1->id == id)
// {
// p2->next = p1->next;
// p1 = p1->next;
// break;
// }
// else
// {
// p2 = p1;
// p1 = p1->next;
// }
// }
// p1 = ph;
// return p1;
// }
// struct Link *create()
// {
// struct Link *p, *p1, *h;
// h = NULL;
// for (int i = 0; i < 3; i++)
// {
// printf("input No.%d student's imformation:\n", i + 1);
// p = (struct Link *)malloc(sizeof(struct Link));
// cin >> p->id >> p->score;
// if (i == 0)
// {
// p->next = NULL;
// h = p;
// p1 = p;
// }
// else
// {
// p->next = NULL;
// h->next = p;
// h = p;
// }
// }
// h = p1;
// return h;
// }
// int main(int argc, char const *argv[])
// {
// struct Link *head, *newnode;
// int di;
// float ds;
// cout << "Please input the imformation of three students" << endl;
// head = create();
// cout << "the imformation of this Link is:" << endl;
// Print(head);
// cout << "Please input the imformation of needing to insert" << endl;
// newnode = (struct Link *)malloc(sizeof(struct Link));
// cout << "id=";
// cin >> newnode->id;
// cout << "score=";
// cin >> newnode->score;
// newnode->next = NULL;
// head = insert(head, newnode);
// cout << "the imformation of inserting a new node in this Link is:" << endl;
// Print(head);
// cout << "Please input the imformation of needing to delete" << endl;
// cout << "id=";
// cin >> di;
// head = del(head, di);
// cout << "the imformation of delete a old node in this Link is:" << endl;
// Print(head);
// return 0;
// }
// c9-8链表插入节点
// struct Link
// {
// int n;
// struct Link *next;
// } li;
// struct Link *insert(struct Link *p, int x, int y, int z)
// {
// if (p == NULL)
// {
// cout << "空表!失败!" << endl;
// return p;
// }
// struct Link *newnode, *p1, *p2;
// p1 = p;
// for (int i = 0; i <= 20; i++)
// {
// if (i == 0)
// {
// newnode = (struct Link *)malloc(sizeof(struct Link));
// newnode->n = x;
// newnode->next = p1;
// p1 = newnode;
// p = newnode;
// }
// else if (i == 10)
// {
// newnode = (struct Link *)malloc(sizeof(struct Link));
// newnode->n = y;
// newnode->next = p1->next;
// p1->next = newnode;
// p1 = newnode;
// }
// else if (i == 20)
// {
// newnode = (struct Link *)malloc(sizeof(struct Link));
// newnode->n = z;
// newnode->next = p1->next;
// p1->next = newnode;
// p1 = newnode;
// }
// p1 = p1->next;
// }
// p1 = p;
// return p1;
// }
// int main(int argc, char const *argv[])
// {
// struct Link *head, *p, *p1;
// p = (struct Link *)malloc(sizeof(struct Link));
// p->n = 1;
// p->next = NULL;
// head = p;
// for (int i = 1; i < 20; i++)
// {
// p1 = (struct Link *)malloc(sizeof(struct Link));
// p1->n = i + 1;
// p1->next = NULL;
// p->next = p1;
// p = p1;
// }
// p = head;
// p = insert(p, 100, 200, 300);
// for (; p != NULL; p = p->next)
// {
// cout << p->n << " ";
// }
// return 0;
// }
// c9-7链表节点删除函数(依旧是13个人报数的题目)
// struct Link
// {
// int n;
// struct Link *next;
// } li;
// struct Link *del(struct Link *p, int n)
// {
// struct Link *p1, *p2;
// int i, count = 0;
// if (p == NULL)
// {
// cout << "空表!失败!" << endl;
// return p;
// }
// p1 = p;// 记录需要删除的当前节点
// p2 = p;// 记录需要删除的当前节点的上一个节点
// while (count < 12)
// {
// i = 0;
// while (i != 3)
// {
// if (p1 != NULL)
// {
// i++;
// }
// if (i != 3)
// {
// p2 = p1;
// p1 = p1->next;
// }
// }
// cout << p1->n << " ";
// p2->next = p1->next;// 前一个结点的next指向需要删除节点的下一节点
// p1 = p1->next;// 往前移动,指向下一个节点
// count++;//淘汰人数+1
// }
// return p1;
// }
// int main(int argc, char const *argv[])
// {
// struct Link *head, *p, *p1;
// head = NULL;
// p = (struct Link *)malloc(sizeof(struct Link));
// p->n = 1;
// head = p;
// p->next = NULL;
// for (int i = 1; i < 13; i++)
// {
// p1 = (struct Link *)malloc(sizeof(struct Link));
// p1->n = i + 1;
// p->next = p1;
// p = p1;
// if (i < 12)
// {
// p->next = NULL;
// }
// else
// {
// p->next = head;
// }
// }
// p = head;
// p = del(p, 3);
// cout << endl;
// cout << "the last one is:" << p->n << endl;
// return 0;
// }
// c9-6 链表方法处理13个人报号问题
// typedef struct
// {
// int number;
// int next;
// } link;
// int main(int argc, char const *argv[])
// {
// link li[14];
// int count = 0;
// int h = 1;
// for (int i = 1; i <= 13; i++)
// {
// li[i].number = i;
// if (i == 13)
// {
// li[i].next = 1; // 最后一个节点的下一个是首节点
// }
// else
// {
// li[i].next = i + 1;
// }
// }
// while (count < 12)
// {
// int i = 0;
// while (i != 3)
// {
// if (li[h].number)
// {
// i++; // 报数自增1
// }
// if (i != 3)
// {
// h = li[h].next;
// }
// }
// cout << li[h].number << " ";
// li[h].number = 0;
// h = li[h].next;
// count++; // 记录淘汰个体数
// }
// cout << endl;
// cout << "the last one is:" << endl;
// for (int i = 1; i <= 13; i++)
// {
// if (li[i].number)
// cout << li[i].number << endl;
// }
// return 0;
// }
// c9-1 计算天数
// typedef struct
// {
// int y, m, d;
// } data;
// int isPrimeyear(int y)
// {
// return (y % 4 == 0 && y % 100 != 0) || (y % 400 == 0);
// }
// void count(data da, int *m)
// {
// int monthdays[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
// for (int i = 1; i < da.m; i++)
// {
// *m += monthdays[i];
// if (i > 2 && isPrimeyear(da.y))
// {
// *m += 1;
// }
// }
// *m += da.d;
// }
// int main(int argc, char const *argv[])
// {
// int x = 0;
// cout << "请输入日期:" << endl;
// data da;
// cin >> da.y >> da.m >> da.d;
// count(da, &x);
// cout << "天数为=" << x << endl;
// return 0;
// }