Rookie--Data structure algorithm 2.1 implementation

Source: Internet
Author: User
Tags function definition

2.1 Suppose that using two linear tables, LA and LB, to represent two sets A and B (that is, the data element in the linear table is a member of the collection), a new collection A=aub is now required. This requires that the linear table be expanded to include the linear table LA, and the data elements that exist in the linear table lb and not in the linear table la are inserted into the linear table LA. As long as each data element is obtained sequentially from the linear table lb and is visited in linear table la by value, it is inserted if it does not exist.

Correct code (O (∩_∩) o~ written with the assistance of the Great God):
#include <stdio.h>

typedef struct list{
int elem[15];
int length;
}list;

int main () {
void Unionlist (List *l,list Lb);

List la={{1,2,3,4,5},5};
List lb={{4,5,6,8,10,12,15,19},8};
Unionlist (&AMP;LA,LB);
return 0;
}

void Unionlist (List *l,list Lb)
{
int Getelem (List, int, int*); Use E to return the first Data element of the table lb;
int Locateelem (List, int); In turn, determine whether e is equal to the data element in LA;
void Listinsert (list*, int, int); If e is not equal to the first Data element in LA, then E is inserted into LA;

int e;

for (int i=1;i<=lb.length;i++)
{
Getelem (lb,i,&e);
if (! Locateelem (*l,e))
{
Listinsert (L, (*l). Length+1,e);
+ + (*l). length;
}
}
for (int m=0;m< (*l). length;m++)
{
printf ("%d", (*l). Elem[m]);
}
printf ("\ n table LA's current length is:%d", (*l). length);
}

int Getelem (List lb,int i,int *eq)//pointer to e variable EQ
{
if (i<=lb.length)
{
*EQ=LB.ELEM[I-1];
return 1;
}
return 0;
}

int Locateelem (List la,int e)
{
for (int k=0;k<la.length;k++)
{
if (E==la.elem[k])
return 1;
}
return 0;
}

void Listinsert (List *l,int p,int e)
{
if (p<=15)
{
(*l). elem[p-1]=e;
}
}

Modify the correct code before (many error tat~):

#include <stdio.h>

typedef struct list{
int elem[15];
int length;
}list;

int La_len;
int Lb_len;

int main () {
void Unionlist (List la,list Lb);
List la={{1,2,3,4,5},5};
List lb={{7,9,13,15,17,19,21},7};
Unionlist (LA,LB);
printf ("\ n table la now has a length of:%d", La_len);
return 0;
}

void Unionlist (List la,list Lb)
{

int Getelem (list,int,int*; e equals the value of the first Data element in the table lb;
int Locateelem (list,int); In turn, determine whether e is equal to the data element in LA;
void Listinsert (List,int); If e is not equal to the first Data element in LA, then E is inserted into LA;

int e;
La_len=la.length;
Lb_len=lb.length;

for (int i=1;i<=lb_len;i++)
{
Getelem (lb,i,e);
if (! Locateelem (La,e))
{
Listinsert (la,e);
++la_len;
}
}
for (int m=0;m<la_len;m++)
{
printf ("%d", la.elem[m]);
}

}

int Getelem (List lb,int i,int e)
{
for (int j=0;j<lb_len;)
{
E=LB.ELEM[J];
++j;
Break
}
}

int Locateelem (List la,int e)
{
for (int k=0;k<la_len;k++)
{
if (E==la.elem[k])
return 1;
Else
return 0;
}
}

void Listinsert (List la,int e)
{
for (int x=la_len;x< (la_len+lb_len);)
{
La.elem[x]=e;
++x;
Break
}
}

A little experience:

1. Original code:

int La_len;

int Lb_len;

La_len= (*l). length;

Lb_len=lb.length;

As a global variable is too redundant, I have defined the structure of the member length, can be used directly (*l). Length, and lb.length, without the need to define more than two global variables.


2. Original code:

void Unionlist (List la,list Lb) {}

if (! Locateelem (La,e))
{
Listinsert (la,la.length+1,e);
++la.length;
}

int Locateelem (List la,int e)
{
for (int k=0;k<la.length;k++)
{
if (E==la.elem[k])
return 1;
}
return 0;
}

void Listinsert (List la,int p,int e)
{
if (p<=15)
{
La.elem[p-1]=e;
}
}

To change the entire LA, it can only be achieved by pointing the pointer variable l to LA.

Otherwise at the time of insertion, la.elem[i]=e; this is actually another LA, with the end of the function call, the formal parameter LA is freed and cannot really change the argument LA.


3. Original code:

Function call:

int e;

for (int i=1;i<=lb_len;i++)
{
Getelem (lb,i,e);
if (! Locateelem (La,e))
{
Listinsert (la,e);
++la_len;
}
}

function definition:

int Getelem (List lb,int i,int e)
{
for (int j=0;j<lb_len;)
{
E=LB.ELEM[J];
++j;
Break
}
}

When this function is called, the argument e is not assigned a value, only an int e is declared, and I initially did not take into account that the argument e does not have the value of the problem, just want to call this function to implement this function.

You can change the value of E by using the pointer variable eq after replacing E in Getelem (lb,i,e) with E in &e,int getelem (list Lb,int i,int e) for *eq.


There is the argument is I, the formal parameter is J, I completely pass the argument I to the parameter J problem to ignore, but superfluous wrote an int j=0, actually should direct I pass over, put Lb.length[j], change to lb.length[i-1];

and ++j question, according to the original idea, I want to wait for E to return to the first J element, j=j+1; and then break exits the loop, and the next time I get the element in lb, I return the first J+1 element with E, but I forget that the Getelem function itself is inside a for loop, This for loop changes the value of I in turn to make the i+1. (╥╯^╰╥) Direct I to the formal parameter allows E to return to the first I+1 element.

4.

void Listinsert (List la,int e)
{
for (int x=la_len;x< (la_len+lb_len);)
{
La.elem[x]=e;
++x;
Break
}
}

--a mess of writing

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.