Home > Back-end >  Singly linked list data structure for help
Singly linked list data structure for help

Time:09-25

# # data structure
Help:
This function is to write what's the problem?
The while loop as if into don't go,
L already have two nodes, after calling the interpolation function, insert the node to be replaced the original node,

CodePudding user response:

Advised to directly post code, and provide more code, including the call,

The function begins to r application space, and then immediately r=L; This can lead to memory leaks, because r just apply for space can't released, can't find the address,

CodePudding user response:

Call the function? Where L is the assignment? IniLinkList (L) code no problem?
In addition, if you want to make r=L; So this code before a code r=(DogNode *) malloc (sizeof (DogNode)) is redundant, also cause a memory leak

CodePudding user response:

IniLinkList (L) the preliminary judgment, this function is to initialize the L, there should be a malloc statement, then r=(DogNode *) malloc (sizeof (DogNode)) this sentence is redundant,

CodePudding user response:

reference 1/f, confident boy reply:
advice directly post code, and then provide more code, including the call,

The function begins to r application space, and then immediately r=L; This can lead to memory leaks, because r just apply for space can't released, can't find the address,

# include
# include
# include
Typedef struct Dog
{
char name[10];
Int id;
} Dog;

Typedef struct DogNode
{
The Dog Dog;
Struct DogNode * next;
} DogNode, * DogLinkList;
/*
Note:
1. Nodes Pointers and two different concepts
2. DogLinkList is structure pointer DogLinkList p<=> DogNode * p;
*/

Void IniLinkList (DogLinkList & amp; L)
/*
Generate a head node
*/
{
L=(DogLinkList) malloc (sizeof (DogNode));
Strcpy (L - & gt; T the name, "this is the first node");
L - & gt; next=NULL;
}

Void CreateList_H (DogLinkList & amp; L, int n)
//from the front insert node values of n elements
{
IniLinkList (L);
DogNode * p;
for(int i=0; i{
P=(DogNode *) malloc (sizeof (DogNode));
Printf (" input the dog's information: ");
The scanf (" % s % d ", p - & gt; T name, & amp; (p - & gt; T id));
//head insert: let p at nodes head again after the first at p
//p into the first node, the original nodes into the second first
p-> Next=L - & gt; Next;
L - & gt; Next=p;
}
}

Void CreateList_R (DogLinkList & amp; L, int n)
//after interpolation
//insert
n nodes from behind{
DogNode * r;
R=L;
While (r - & gt; Next!=NULL)
{
R=r - & gt; Next;
}
DogNode * p;
for(int i=0; i{
P=(DogNode *) malloc (sizeof (DogNode));
Printf (" input the dog's information: ");
The scanf (" % s % d ", p - & gt; T name, & amp; (p - & gt; T id));
p-> next=NULL;
R - & gt; Next=p;
R=p;
}
}

Void GetElem (DogLinkList L, int I, Dog & amp; Dog)
{
//get the value of the ith a node in the dog
DogNode * p;
P=L - & gt; Next;
Int j=1;
While (p& & j{
p=p-> Next;
++j;
}
if(! P | | j> I)
printf("error");
The else
{
T id=p - & gt; T id;
Strcpy (t name, p - & gt; T name);
}
}

DogNode * LocateElem (DogLinkList & amp; L, the Dog Dog)
{
//positioning dog
DogNode * p;
P=L - & gt; Next;
While (p& & (p - & gt; T id!=t id) & amp; & (! STRCMP (p - & gt; T name, t name)))
p=p-> Next;
return p;
}

Void ListInsert (DogLinkList & amp; Int L, I, Dog & amp; Dog)
{
//in the case of a position I insert dog
DogNode * p, * s;
S=(DogNode *) malloc (sizeof (DogNode));
p=L;
int j=0;
While (p& & (j{
p=p-> Next;
j++;
}
If (p!=NULL)
{
S - & gt; T id=t id;
Strcpy (s - & gt; T name, t name);
S - & gt; Next=p - & gt; Next;
p-> Next=s;
}
}

//delete nodes to free ()
Void DelElem (DogLinkList & amp; L, int I)
{
DogNode * p * q;
p=L; int j=0;
While ((p - & gt; Next) & amp; & (j{
p=p-> Next; j++;
}
If (p - & gt; Next!=NULL& & (j==I - 1))
{
Q=p - & gt; Next; p-> Next=q - & gt; Next;
Free (q);
}
}

Void PrintList (DogLinkList & amp; L)
{
DogNode * p;//p temporary variable names, need not malloc
P=L - & gt; Next;
while(p!=NULL)
{
Printf (" % s %

CodePudding user response:

refer to the second floor qybao response:
how function call? Where L is the assignment? IniLinkList (L) code no problem?
In addition, if you want to make r=L; So this code before a code r=(DogNode *) malloc (sizeof (DogNode)) is redundant, also cause memory leaks

Other didn't question should be, I tested, I put the code in the comments section

CodePudding user response:

refer to fifth floor for small north reply:
Quote: refer to the second floor qybao response:
how function call? Where L is the assignment? IniLinkList (L) code no problem?
In addition, if you want to make r=L; So this code before a code r=(DogNode *) malloc (sizeof (DogNode)) is redundant, also cause memory leaks

Other didn't question should be, I tested, I put the code comments

Oh oh, I know the problem, should the initialization deleted, because the initialization before, thank you!
  • Related