Home > Back-end >  Should excuse me I this how to change?
Should excuse me I this how to change?

Time:09-16

Results operation processing is always wrong,,
There are errors, crying,,
 # include 
#include
# define OK 1
# define the ERROR 0
# define TRUE 1
# define FALSE 0

Typedef int the Status;
Typedef char ElemType;
Typedef struct LNode
{
ElemType data;
Struct LNode * next;
} LNode * LinkList;

/* your program will be embedded in here */
The Status InitList (LinkList & amp; L)
{
L=(LinkList) malloc (sizeof (LNode));//generated head node
if(! L)
Return the ERROR;//generated head node failure
L - & gt; Next=NULL;
Return OK;



}
The Status ListInsert (LinkList & amp; L, int I ElemType e)
{
LinkList p, s;
P=L;
int j=0;
While (p& & j{
j++;
P=p - & gt; next;
}
if(! P | | j> I - 1)
{
Return the ERROR;
}
//generate a new node
S=(LinkList) malloc (sizeof (LNode));

S - & gt; data=https://bbs.csdn.net/topics/e;
S - & gt; Next=p - & gt; next;//insert node
P - & gt; Next=s;
Return OK;



}

The Status ListLength LinkList (L)
{
//the lead node
LinkList p;
int len=0;
P=L;
While (p)
{
Len++;
P=p - & gt; next;
}
Return OK;

}
Status GetElem (LinkList L, int I ElemType & amp; E)
{
LinkList p;
P=L;
int j=0;
//find the first I - 1 node
While (p& & j{
P=p - & gt; next;
j++;
}
if(! P | | j> I)
Return the ERROR;
E=p - & gt; The data;
Return OK;


}
The Status DestroyList (LinkList & amp; L)
{
LinkList p, temp.
P=L;
While (p)
{
Temp=p;
P=p - & gt; next;
Free (temp);


}
P - & gt; Next=NULL;
Return OK;


}
Void jiaoji (LinkList & amp; La, LinkList & amp; Lb, LinkList & amp; Lc)
{
InitList (Lc);
LinkList pa, pb, PC, p;
Pa=La - & gt; next;
Pb=Lb - & gt; next;

P=PC=Lc;

While (pa!=NULL)
{
While (pb!=NULL)
{
If (pa - & gt; data=https://bbs.csdn.net/topics/=pb-> data)
{
PC=(LinkList) malloc (sizeof (LNode));//new storage space;
PC - & gt; data=https://bbs.csdn.net/topics/pa-> data;
PC - & gt; Next=NULL;

P - & gt; Next=PC;
P=PC;


}
Pb=pb - & gt; next;

}
Pa=pa - & gt; next;
Pb=Lb - & gt; next;

}

}
Int main ()
{
int i,j,len;
ElemType e;
LinkList La,Lb,Lc;
Int m, n;//store two sets the initial length respectively
The scanf (" % d % d ", & amp; M, & amp; N);
Getchar ();
InitList (La);
InitList (Lb);//to build two empty
for(i=1; i<=m; I++)//set up the first set
{
The scanf (" % c ", & amp; E);
ListInsert (La, I, e);
}
Getchar ();
for(i=1; i<=n; I++)//set up the second collection
{
The scanf (" % c ", & amp; E);
ListInsert (Lb, I, e);
}
Jiaoji (La and Lb, Lc);//calculate collection La, Lb intersection of Lc
//output
Len=ListLength (Lc);
for(i=1; i<=len; I++)
{
GetElem (Lc, I, e);
Printf (" % c ", e);
}
//printf (" \ n ");
DestroyList (La);
DestroyList (Lb);
DestroyList (Lc);//destroyed three collection
return 0;
}
//3 5
//ABC
//decbf

CodePudding user response:

Can try to debug or output statements

CodePudding user response:

 Status ListInsert (LinkList & amp; L, int I ElemType e) 
{
LinkList p, s;
P=L;
int j=0;
While (p& & j{
j++;
P=p - & gt; next;
}
if(! P | | j> I - 1)
{
Return the ERROR;
}
//generate a new node
S=(LinkList) malloc (sizeof (LNode));

S - & gt; data=https://bbs.csdn.net/topics/e;
S - & gt; Next=p - & gt; next;//insert node
P - & gt; Next=s;
Return OK;



}


I don't have a detailed look at the function seems to have a problem in temporary variables p=L, with p cycle until after I place are you insert the new data to the p list inside and L list there is no change,
That means you did not create La Lb Lc at this list what do you think?

CodePudding user response:

You this take the length of the return value is wrong, you return to the Status, and not to return to the len, so cause the result not
Change
 Status ListLength (LinkList L, int * len)//here 
{
//the lead node
LinkList p;
//int len=0;
P=L - & gt; next;//here, mainly because your list have an empty element in the beginning, so there is a little too at the beginning of count
While (p)
{
Len (*) + +;//here
P=p - & gt; next;
}
Return OK;

}

Accordingly, the main to modify

 
//output resultsLen=0;//here
ListLength (Lc, & amp; Len);//here
for(i=1; i<=len; I++)
{
GetElem (Lc, I, e);
Printf (" % c ", e);
}


In addition, you have a problem free memory also
 Status DestroyList (LinkList & amp; L) 
{
LinkList p, temp.
P=L;
While (p)//p here is NULL to exit the loop
{
Temp=p;
P=p - & gt; next;
Free (temp);


}
//p - & gt; Next=NULL;//p is NULL will cause error, so here to remove
Return OK;


}
  • Related