Home > Back-end >  For bosses to help solve the problem of the linked list data structure
For bosses to help solve the problem of the linked list data structure

Time:05-03

# include
# include
# define Maxsize 10
Typedef int Elemtype;
Typedef struct node
{
Elemtype data;
Struct node * next;
} Node, * the List;
The List creat (int L)
{
The List of p, r=NULL, List=NULL;
Elemtype I, e;
For (I=1; I & lt;=L; I++)
{
Scanf_s (" % d ", & amp; E);
P=(List) malloc (sizeof (Node));
P - & gt; Data=https://bbs.csdn.net/topics/e;
P - & gt; Next=NULL;
if (! The list)
{
List=p;
}
The else
{
R - & gt; Next=p;
}
R=p;
}
Return the list;
}
Void insert (List * the List, the List p, Elemtype e)
{
The List of q;
Q=(List) malloc (sizeof (Node));
Q - & gt; Data=https://bbs.csdn.net/topics/e;
if (! The list)
{
* the list=q;
Q - & gt; Next=NULL;
}
The else
{
Q - & gt; Next=p - & gt; Next;
P - & gt; Next=p;
}
}
Void the delete (List * the List, the List of q)
{
The List r=NULL;

If (q==* list)
{
* list=q - & gt; Next;
Free (q);
}
The else
{
For (r=* list; R - & gt; Next!=q; R=r - & gt; Next)
;
If (r - & gt; Next!=NULL) {
R - & gt; Next=q - & gt; Next;
Free (q);
}
}
}
Void distory * List (List)
{
The List of q, p;
P=* list;
While (p)
{
Q=p - & gt; Next;
Free (p);
P=q;
}
* the list=NULL;
}
Int main ()
{
Int I, e;
The List of q, l;
Q=l=creat (1);
Printf (" please enter inserted into the list elements: end with 0 ");
Scanf_s (" % d ", & amp; i);
While (I)
{
Insert (& amp; L, q, I);
Q=q - & gt; Next;
Scanf_s (" % d ", & amp; i);
}
Q=l;
Printf (" list of contents be: ");
While (q)
{
Printf (" % d ", q - & gt; The data);
Q=q - & gt; Next;
}
Q=l;
Printf (" delete the fifth element ");
For (I=1; I & lt; 5; I++)
{
Q=q - & gt; Next;
}
Delete (& amp; L, q);
Q=l;
Printf (" delete after the fifth element is: ");
While (q)
{
Printf (" % d ", q - & gt; The data);
Q=q - & gt; Next;
}
Distory (& amp; l);
return 0;
}
Why do I enter 1 2 3 4 5 6 7 it has output 1, solving, want to check for more than an hour, was too careless or look not to come out with the wrong

CodePudding user response:

Resolved 111
  • Related