Sequential table representation set with ascending order, solving intersection of two sets and set difference set (C language implementation) __c language

Source: Internet
Author: User

#include <stdio.h>
#include <stdlib.h>
#define MAX 100
typedef struct {

int Elem[max];
int length;

}list;
void Unionlist ();
void Intersectionlist ();
void Setdifferencelist ();
void Datasort (List &l, int n);
int main () {
Unionlist ();
Intersectionlist ();
Setdifferencelist ();
return 0;

}

and set
void Unionlist () {
int I, j, E;
List La, Lb;
printf ("Set \ n");
printf ("Please enter collection A, number of B elements of the collection: \ n");
scanf ("%d%d", &la.length, &lb.length);
printf ("Please enter the elements of set a: \ n");
for (i = 0; i < la.length; i++) {
scanf ("%d", &la.elem[i]);
}
printf ("Please enter the elements of set B: \ n");
for (i = 0; i < lb.length; i++) {
scanf ("%d", &lb.elem[i]);
}
for (i = 0; i<lb.length; i++)
{
e = Lb.elem[i];
j = 0;
while ((j<la.length) && (La.elem[j]!= e))
j + +;
if (j = = La.length) {
La.elem[la.length] = e;
la.length++;
}
}
Datasort (La, la.length);
for (i = 0; i<la.length; i++) {
printf ("%d", la.elem[i]);
}
printf ("\ n");

}

Intersection
void Intersectionlist () {
int I, j, d = 0;
List La, Lb, Lc;
printf ("intersection \ \");
printf ("Please enter collection A, number of B elements of the collection: \ n");
scanf ("%d%d", &la.length, &lb.length);
printf ("Please enter the elements of set a: \ n");
for (i = 0; i < la.length; i++) {
scanf ("%d", &la.elem[i]);
}
printf ("Please enter the elements of set B: \ n");
for (i = 0; i < lb.length; i++) {
scanf ("%d", &lb.elem[i]);
}
for (i = 0; i<la.length; i++) {
for (j = 0; j<lb.length; j + +) {
if (la.elem[i] = = Lb.elem[j]) {
lc.elem[d++] = La.elem[i];
Break
}
}

}
Datasort (Lc, D);
for (i = 0; i<d; i++) {
printf ("%d", lc.elem[i]);
}
printf ("\ n");
}

Difference Set
void Setdifferencelist () {
int I, j, d = 0, C;
List La, Lb, Lc;
printf ("Bad set \ \");
printf ("Please enter collection A, number of B elements of the collection: \ n");
scanf ("%d%d", &la.length, &lb.length);
printf ("Please enter the elements of set a: \ n");
for (i = 0; i < la.length; i++) {
scanf ("%d", &la.elem[i]);
}
printf ("Please enter the elements of set B: \ n");
for (i = 0; i < lb.length; i++) {
scanf ("%d", &lb.elem[i]);
}

for (i = 0; i<la.length; i++) {
c = 0;
for (j = 0; j<lb.length; j + +) {
if (La.elem[i]!= lb.elem[j]) {
C + +;
}
}
if (c = = lb.length)
lc.elem[d++] = La.elem[i];
}
Datasort (Lc, D);
for (i = 0; i<d; i++) {
printf ("%d", lc.elem[i]);
}
printf ("\ n");
}

Sort
void Datasort (List &l, int n) {
int I, j, temp;
for (i = 0; i<n-1; i++) {
for (j = i + 1; j<n; J + +) {
if (L.elem[j]<l.elem[i]) {
temp = L.elem[j];
L.ELEM[J] = L.elem[i];
L.elem[i] = temp;
}
}
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.