Home > Back-end >  Singly linked lists to keep maximum minimum value, there is an error, bosses at emergency
Singly linked lists to keep maximum minimum value, there is an error, bosses at emergency

Time:09-21

#include
#include

using namespace std;

# define OK 1
# define the ERROR 0
# define ElemType int

Typedef int the Status;


Typedef struct LNode
{

ElemType data;

Struct LNode * next;

} LNode * LinkList;

//create a singly linked list

LNode * creat ()

{
LNode * head, * p;

The head=new LNode;

P=the head;

Int x, cycle=1;

While (cycle)
{
Cout & lt; <"Both Please input the data for single would:";
Cin & gt;> x;

If (x!=0)
{

LNode * s=new LNode;
S - & gt; Data=https://bbs.csdn.net/topics/x;
Cout & lt; <"Input data:" & lt; P - & gt; Next=s;
P=s;

}
The else
{
Cycle=0;
Cout & lt; <"Input done! "& lt; }
}

P - & gt; Next=NULL;

//cout & lt; <"\ nFirst data of single would is" & lt;
return head;

}

//print singly linked list

Void printL (LNode * head)
{
LNode * p=head;
P=p - & gt; next;
while (p !=NULL)
{
Cout & lt; <"Single would data is" & lt;

P=p - & gt; next;
}
}

//singly linked lists measuring

Int length (LNode * head)

{
int n=0;
LNode * p=head;

While (p - & gt; Next!=NULL)
{
P=p - & gt; next;
n++;
}
return n;
}

Void del_nolmal (LNode * head)
{

LNode * head1=head;
LNode * p=head1 - & gt; next;

int i;
Int len=length (head1);

Int Max=p - & gt; The data;
Int min=p - & gt; The data;


For (I=0; I & lt; len; I++)
{
P=p - & gt; next;
If (p!=NULL)
{
If (p - & gt; The data & gt; Max)
{
Max=p - & gt; The data;
}
}
}

P=head1;

For (I=0; I & lt; len; I++)
{
P=p - & gt; next;
If (p!=NULL)
{
If (p - & gt; The data & lt; Min)
{
Min=p - & gt; The data;
}

}
}


P=head1;
For (I=0; I & lt;=len; I++)
{
LNode * q=p - & gt; next; A data element before/record/p4 p3
If (q!=NULL)
{
If ((q - & gt; Data Min)
If (q - & gt; Next!=NULL)
{
P - & gt; Next=q - & gt; next;
}
The else
{
P - & gt; Next=NULL;
}
}
}
Free (p);
}


Int main ()
{
Cout & lt; <"To create a single table * * * * * *" & lt; LNode * head=creat ();
Cout & lt;
Cout & lt; <"* * * * * * print singly linked lists" & lt; PrintL (head);
Cout & lt;
Printf (" len=% d ", length (the head));

Del_nolmal (head);
Cout & lt;
Cout & lt; <"* * * * * * print singly linked lists" & lt; PrintL (head);
Cout & lt; }

CodePudding user response:

S - & gt; Data=https://bbs.csdn.net/topics/x;
Cout & lt; <"Input data:" & lt; P - & gt; Next=s;
P=s;

Here the wrong

CodePudding user response:

The
reference 1/f, Simple, Soft reply:
s - & gt; Data=https://bbs.csdn.net/topics/x;
Cout & lt; <"Input data:" & lt; P - & gt; Next=s;
P=s;

Wrong here

Then I should be how to change here

CodePudding user response:

Should s=p, don't know whether your design

CodePudding user response:

The create function allocates memory processing to
If (x!=0)
{//code here to start to change
If (p!={the head)
P - & gt; Next=new LNode;
P=p - & gt; next;
}
P - & gt; data=https://bbs.csdn.net/topics/x;
}
The else//code here to start the same

I don't know del_normal think stem what? Is to delete the Max and min outside of the element? In the final for p hasn't been changed, that is p has been head1, become just p - & gt; Next, so finally free (p) will release the whole list (equivalent to free (head1), head1==head), so in the end the main print printL (head) will go wrong

CodePudding user response:

Del_normal if want to retain maximum minimum value, then modify the code
for(i=0; i{
If (p!=NULL& & P - & gt; next!=NULL)
{
If (p - & gt; Next - & gt; Data Min)
{
LNode * q=p - & gt; next;//record p - & gt; Next
P - & gt; Next=q - & gt; next;//modify p - & gt; Next to p - & gt; Next - & gt; Next
Free (q);//the q node delete
}
}
If (p!=NULL)
P=p - & gt; next;//mobile node
}

CodePudding user response:

reference 5 floor qybao reply:
del_normal if want to retain maximum minimum value, then modify the code
for(i=0; i{
If (p!=NULL& & P - & gt; next!=NULL)
{
If (p - & gt; Next - & gt; Data Min)
{
LNode * q=p - & gt; next;//record p - & gt; Next
P - & gt; Next=q - & gt; next;//modify p - & gt; Next to p - & gt; Next - & gt; Next
Free (q);//the q node delete
}
}
If (p!=NULL)
P=p - & gt; next;//mobile node
}

Thank you, I thought it over carefully
  • Related