数据结构 线性表 顺序表的交集
【摘要】 #include <stdio.h>#include <stdlib.h> #define LIST_INIT_SIZE 100#define LISTLNCREMENT 10#define OK 1#define ERROR 0#define OVERFLOW -2 typedef int ElemType; typedef struct{ Elem...
#include <stdio.h>
#include <stdlib.h>
#define LIST_INIT_SIZE 100
#define LISTLNCREMENT 10
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int ElemType;
typedef struct
{
ElemType *elem;
int length;
int listsize;
}SqList;
int InitList_Sq(SqList &L)
{
L.elem = (ElemType *) malloc(LIST_INIT_SIZE * sizeof(ElemType));
if (!L.elem)
{
exit(OVERFLOW);
}
L.length = 0;
L.listsize = LIST_INIT_SIZE;
return OK;
}
void Create_Sq(SqList &L, int n)
{
int i;
for (i=0; i<n; i++)
{
scanf("%d",&L.elem[i]);
}
L.length = n;
}
void Display_Sq(SqList L)
{
int i;
for (i=0; i<L.length ; i++)
{
printf("%d ",L.elem[i]);
}
printf("\n");
}
int Find(SqList L, ElemType e)
{
int i = 0;
while (i<L.length)
{
if (L.elem[i] == e)
{
break;
}
++i;
}
if (i == L.length)
{
return 0;
}
return 1;
}
int ListDel_Sq(SqList &L, int i, ElemType &e)
{
ElemType *p,*q;
if (i<1 || i>L.length+1)
{
return ERROR;
}
p = &(L.elem [i-1]);
e = *p;
q = L.elem + L.length - 1;
for (++p;p<=q;++p)
{
*(p-1) = *p;
}
--L.length ;
return OK;
}
int GetData(SqList L,int i)
{
if (i<0 || i>L.length)
{
return ERROR;
}
else
return L.elem[i-1];
}
void SameinList_Sq(SqList &La, SqList Lb)
{
int i = 1,e;
while (i<=La.length)
{
e = GetData(La,i);
if (!Find(Lb,e))
{
ListDel_Sq(La,i,e);
}
else
{
i++;
}
}
}
int main()
{
int n;
SqList La,Lb;
InitList_Sq(La);
InitList_Sq(Lb);
printf("请输入链表La的长度: ");
scanf("%d",&n);
printf("请输入 %d 个数据: ",n);
Create_Sq(La,n);
printf("请输入链表Lb的长度: ");
scanf("%d",&n);
printf("请输入 %d 个数据: ",n);
Create_Sq(Lb,n);
SameinList_Sq(La,Lb);
if (La.length == 0)
{
printf("两个链表没有相同的元素!\n");
}
else
{
printf("两个链表相同的元素是: ");
Display_Sq(La);
}
return 0;
}
文章来源: blog.csdn.net,作者:悦来客栈的老板,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/qq523176585/article/details/17078381
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)