#include
#include
#include
#include
using namespace std;
typedef struct LinkNode
{
int data;
struct LinkNode *next;
}LinkNode;
void InitList(LinkNode *&L)
{
L=(LinkNode*)malloc(sizeof(LinkNode));
L->next=NULL;
}
void CreatList(LinkNode *&L,int a[],int n)
{
LinkNode *p;
L=(LinkNode*)malloc(sizeof(LinkNode));
L->next=NULL;
for (int i=0;idata=a[i];
p->next=L->next;
L->next=p;
}
}
void DisList1(LinkNode *L)
{
LinkNode *p=L->next;
printf("L1->");
while(p!=NULL)
{
printf("\t%d",p->data);
p=p->next;
}
printf("\n");
}
void DisList2(LinkNode *L)
{
LinkNode *p=L->next;
printf("L2->");
while(p!=NULL)
{
printf("\t%d",p->data);
p=p->next;
}
printf("\n");
}
void sort(LinkNode *&L)
{
LinkNode *p,*pre,*q;
p=L->next->next;
L->next->next=NULL;
while(p!=NULL)
{
q=p->next;
pre=L;
while(pre->next!=NULL&&pre->next->datadata)
{
pre=pre->next;
}
p->next=pre->next;
pre->next=p;
p=q;
}
}
void connect(LinkNode *L1,LinkNode *L2)
{
LinkNode *L3;
L3=(LinkNode*)malloc(sizeof(LinkNode));
LinkNode *s=L3->next;
LinkNode *p=L1->next;
while(p!=NULL)
{
int flag=0;
LinkNode *q=L2;
while(q!=NULL)
{
if(p->data==q->data)
{
flag=1;
break;
}
q=q->next;
}
if(flag==1)
{
s->data=p->data;
s=s->next;
}
p=p->next;
}
printf("L4->");
LinkNode *pre;
pre=L3->next;
while(pre!=NULL)
{
printf("%d\t",pre->data);
pre=pre->next;
}
}
int main()
{
LinkNode *L1,*L2,*L3;
int a[100],b[100];
int m=0,n=0;
char c=' ';
char d=' ';
while(c!='\n')
{
scanf("%d",&a[m]);
c=getchar();
m++;
}
while(d!='\n')
{
scanf("%d",&b[n]);
d=getchar();
n++;
}
CreatList(L1,a,m);
CreatList(L2,b,n);
sort(*&L1);
sort(*&L2);
DisList1(L1);
DisList2(L2);
connect(L1,L2);
求交集的函数是connect函数,调试了好多遍都还是不行,累觉不爱,请各位神仙帮帮我,到底是哪里出错了?万般感恩。
修改如下,供参考:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
typedef struct LinkNode
{
int data;
struct LinkNode* next;
}LinkNode;
void InitList(LinkNode*& L)
{
L = (LinkNode*)malloc(sizeof(LinkNode));
L->next = NULL;
}
void CreatList(LinkNode*& L, int a[], int n)
{
LinkNode* p;
L = (LinkNode*)malloc(sizeof(LinkNode));
L->next = NULL;
for (int i = 0; i < n; i++)//for (int i = 0; i < n - 1; i++) 修改
{
p = (LinkNode*)malloc(sizeof(LinkNode));
p->data = a[i];
p->next = L->next;
L->next = p;
}
}
void DisList1(LinkNode* L)
{
LinkNode* p = NULL;
if (!L || !L->next) //修改
printf("NULL");
else {
p = L->next;
while (p != NULL)
{
printf(" %d", p->data);
p = p->next;
}
}
printf("\n");
}
void sort(LinkNode*& L)
{
LinkNode* p, * pre, * q;
if (!L || !L->next) //修改
return; //修改
p = L->next->next;
L->next->next = NULL;
while (p != NULL)
{
q = p->next;
pre = L;
while (pre->next != NULL && pre->next->data < p->data)
{
pre = pre->next;
}
p->next = pre->next;
pre->next = p;
p = q;
}
}
void connect(LinkNode* L1, LinkNode* L2)
{
LinkNode* L3;
L3 = (LinkNode*)malloc(sizeof(LinkNode));
L3->next = NULL; // 修改
LinkNode* s = L3;
LinkNode* p = L1->next;
while (p != NULL)
{
int flag = 0;
LinkNode* q = L2->next;//修改
while (q != NULL)
{
if (p->data == q->data)
{
flag = 1;
break;
}
q = q->next;
}
if (flag == 1)
{
LinkNode* pt = (LinkNode*)malloc(sizeof(LinkNode)); //修改
pt->next = NULL; //修改
pt->data = p->data; //修改
s->next = pt;
s = s->next;
}
p = p->next;
}
printf("L3->"); DisList1(L3); // 修改
//LinkNode* pre;
//pre = L3->next;
//while (pre != NULL)
//{
// printf("%d\t", pre->data);
// pre = pre->next;
//}
}
int main()
{
LinkNode* L1, * L2, * L3;
int a[100], b[100], ret;
int m = 0, n = 0;
//char c = ' '; 修改
//char d = ' '; 修改
while (scanf("%d", &a[m]) == 1 && a[m] != -1) m++; // 输入 -1 时,结束输入 修改
while (scanf("%d", &b[n]) == 1 && b[n] != -1) n++; // 输入 -1 时,结束输入 修改
CreatList(L1, a, m);
CreatList(L2, b, n);
sort(*&L1);
sort(*&L2);
printf("L1->"); DisList1(L1); //修改
printf("L2->"); DisList1(L2); //修改
connect(L1, L2);
return 0;
}
//输出函数一个就够了,这个多余了。
void DisList2(LinkNode* L)
{
LinkNode* p = L->next;
printf("L2->");
while (p != NULL)
{
printf(" %d", p->data);
p = p->next;
}
printf("\n");
}
扫描主机尝试(使用三次握手)与目标主机的某个端口建立正规的连接,连接由系统调用connect()开始,如果端口开放,则连接将建立成功,否则,返回-1,则表示端口关闭。全扫描流程图如下:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-BMZ32BlH-1645250441735)(img/007S8ZIlgy1geb1onsvvuj30np0ra74n.jpg)]
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* next;
} Node;
Node* createNode(int data) {
Node* node = (Node*)malloc(sizeof(Node));
node->data = data;
node->next = NULL;
return node;
}
Node* findNode(Node* head, int data) {
Node* current = head;
while (current != NULL) {
if (current->data == data) {
return current;
}
current = current->next;
}
return NULL;
}
Node* connect(Node* head1, Node* head2) {
Node dummy;
Node* current1 = head1, * current2 = head2;
while (current1 != NULL && current2 != NULL) {
if (current1->data < current2->data) {
current1 = current1->next;
} else {
current2 = current2->next;
}
}
if (current1 != NULL) {
current1->next = current2;
} else {
current2->next = current1;
}
return dummy.next;
}
Node* merge(Node* head1, Node* head2) {
Node dummy;
Node* current1 = head1, * current2 = head2;
while (current1 != NULL && current2 != NULL) {
if (current1->data < current2->data) {
current1 = current`createNode()`函数用于创建新节点,`findNode()`函数用于查找节点,`merge()`函数用于合并两个链表