Home > Back-end >  Bosses have a look at me this two-way linked list is wrong (insert)
Bosses have a look at me this two-way linked list is wrong (insert)

Time:11-18

# include
# include
Struct node
{
int num;
struct node *next;
The prior struct node *;
};
Struct node * ins (struct node * head, int k)
{
Struct node * (p1, p2,
The p2=(struct node *) malloc (sizeof (struct node));
The p2 - & gt; Num=k;
P1=head;
While (p1 - & gt; Num & lt; The p2 - & gt; Num & amp; & P1 - & gt; Next!=NULL)
{
P1=p1 - & gt; Next;
}
If (p1 - & gt; Num & gt;=p2 - & gt; Num)
{
If (p1==head)
{
The head=p2;
The p2 - & gt; Next=p;
P1 - & gt; The prior=p2;
}
The else
{
P1 - & gt; The prior - & gt; Next=p2;
The p2 - & gt; The prior=p1 - & gt; The prior;
P1 - & gt; The prior=p2;
The p2 - & gt; Next=p;
}
}
The else
{
The p2 - & gt; Next=NULL;
The p2 - & gt; The prior=p;
P1 - & gt; Next=p2;
}
return head;
}
Struct node * del (struct node * head, int n)
{
Struct node * p1.
P1=head;
While (p1 - & gt; Next!=NULL & amp; & P1 - & gt; Num!=n)
{
P1=p1 - & gt; Next;
}
If (p1 - & gt; Num==n)
{
If (p1==head)
{
The head=p1 - & gt; Next;
P1 - & gt; Next - & gt; The prior=head;
Free (p1);
}
The else
{
P1 - & gt; The prior - & gt; Next=p1 - & gt; Next;
P1 - & gt; Next - & gt; The prior=p1 - & gt; The prior;
Free (p1);
}
}
Else if (p1 - & gt; Num!=n & amp; & P1 - & gt; Next==NULL)
{
Printf (" No Found!" );
}
return head;
}
Struct node * creat ()
{
Struct node * (p1, p2, * head;
head=NULL;
The head - & gt; Next==NULL;
The head - & gt; The prior==NULL;
P1=p2=(struct node *) malloc (sizeof (struct node));
The head=p1;
The scanf (" % d ", & amp; P1 - & gt; Num);
While (p1 - & gt; Num!=0)
{
The p2 - & gt; Next=p;
P1 - & gt; The prior=p2;
P1 p2=;
P1=(struct node *) malloc (sizeof (struct node));
The scanf (" % d ", & amp; P1 - & gt; Num);
}
The p2 - & gt; Next=NULL;
return head;
}
Void print (struct node * head)
{
Struct node * p1.
P1=head;
//int k=10;
While (p1 - & gt; Next!=NULL)
{
Printf (" % d ", p1 - & gt; Num);
P1=p1 - & gt; Next;
}
}
Int main ()
{
Struct node * head;
The head=creat ();
Int n, k;
//the scanf (" % d ", & amp; n);
//head=del (head, n);
The scanf (" % d ", & amp; K);
The head=ins (head, k);
Print (the head);
return 0;
}

CodePudding user response:

 # include 
# include

Struct node
{
int num;
struct node *next;
The prior struct node *;
};


Struct node * ins (struct node * head, int k)
{
Struct node * (p1, p2,

The p2=(struct node *) malloc (sizeof (struct node));
The p2 - & gt; Num=k;
P1=head;
While (p1 - & gt; Num & lt; The p2 - & gt; Num & amp; & P1 - & gt; Next!=NULL)
{
P1=p1 - & gt; Next;
}
If (p1 - & gt; Num & gt;=p2 - & gt; Num)
{
If (p1==head)
{
The p2 - & gt; Next=p;
The p2 - & gt; The prior=NULL;
P1 - & gt; The prior=p2;
The head=p2;
/*
The head=p2;
The p2 - & gt; Next=p;
P1 - & gt; The prior=p2;
*/
}
The else
{
The p2 - & gt; Next=p;
The p2 - & gt; The prior=p1 - & gt; The prior;

P1 - & gt; The prior - & gt; Next=p2;

/*
P1 - & gt; The prior - & gt; Next=p2;
The p2 - & gt; The prior=p1 - & gt; The prior;
P1 - & gt; The prior=p2;
The p2 - & gt; Next=p;
*/
}
}
The else
{
The p2 - & gt; Next=NULL;
The p2 - & gt; The prior=p;
P1 - & gt; Next=p2;
}
return head;
}
Struct node * del (struct node * head, int n)
{
Struct node * p1.
P1=head;
While (p1 - & gt; Next!=NULL & amp; & P1 - & gt; Num!=n)
{
P1=p1 - & gt; Next;
}
If (p1 - & gt; Num==n)
{
If (p1==head)
{
The head=p1 - & gt; Next;
P1 - & gt; Next - & gt; The prior=head;
Free (p1);
}
The else
{
P1 - & gt; The prior - & gt; Next=p1 - & gt; Next;
P1 - & gt; Next - & gt; The prior=p1 - & gt; The prior;
Free (p1);
}
}
Else if (p1 - & gt; Num!=n & amp; & P1 - & gt; Next==NULL)
{
Printf (" No Found!" );
}
return head;
}
Struct node * creat ()
{
Struct node * (p1, p2, * head;
The head=NULL;
//the head - & gt; Next==NULL;//the head is the point to NULL, so can't visit next and members of the prior
//the head - & gt; The prior==NULL;
P1=p2=(struct node *) malloc (sizeof (struct node));
//head=p1;
The scanf (" % d ", & amp; P1 - & gt; Num);
While (p1 - & gt; Num!=0)
{
if (! The head) {
The head=p1;
P1 - & gt; The prior=NULL;
} else {
The p2 - & gt; Next=p;
P1 - & gt; The prior=p2;
}
P1 p2=;
P1=(struct node *) malloc (sizeof (struct node));
The scanf (" % d ", & amp; P1 - & gt; Num);
}
Free (p1);
If (the head)
The p2 - & gt; Next=NULL;

return head;
}

Void print (struct node * head)
{
Struct node * p1.
P1=head;
//int k=10;
//while (p1 - & gt; Next!=NULL)
While (p1)
{
Printf (" % d ", p1 - & gt; Num);
P1=p1 - & gt; Next;
}
Putchar (10);
}
Int main ()
{
Struct node * head;
Int n, k;

The head=creat ();
Print (the head);
//the scanf (" % d ", & amp; n);
//head=del (head, n);
The scanf (" % d ", & amp; K);
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related