Home > Back-end >  Singly linked lists
Singly linked lists

Time:10-22

#include
#include
#include
//the definition of a singly linked list
Typedef int elemtype;
Typedef struct node
{
Elemtype data;
Struct node * next;
} node, * linklist;
//singly linked lists of initialization
Void initlist (linklist l)
{
L=(linklist) malloc (sizeof (node));
(l) - & gt; Next=NULL;
}
//head interpolation based singly linked list
Void createfromhead linklist (l)
{
Node * s;
char c;
Int flag=1;
While (flag)
{
C=getchar ();
If (c!='0')
{
S=(*) malloc (sizeof (node));
S - & gt; data=https://bbs.csdn.net/topics/c;
S - & gt; Next=l - & gt; Next;
L - & gt; Next=s;
}
The else
Flag=0;
}
}
//merge two orderly singly linked list
Linklist mergelinklist (linklist LA, linklist LB)
//increment orderly singly linked lists of LA and LB merged into an orderly increasing singly linked lists of LC
{
Node * pa * pb;
Linklist LC, r;
R=(linklist) malloc (sizeof (node));


//will LC initial values empty table, pa and pb respectively to two singly linked lists LA, the first node in LB, r initial value for the LC and r always point to the LC footer
Pa=LA - & gt; Next;
Pb=LB - & gt; Next;
LC=LA;
LC - & gt; Next=NULL;
R=LC;
//when are not processed in the two standard is that the more smaller the node is inserted into the new table of LC in
While (pa!=NULL& & Pb!=NULL)
{
If (pa - & gt; Data<=pb - & gt; Data)
{r - & gt; Next=pa; R=pa; Pa=pa - & gt; Next; }
The else
{r - & gt; Next=pb; R=pb; Pb=pb - & gt; Next; }
}
If the if (pa)//table L unfinished, will watch LA chain subsequent elements to the new table of LC footer
R - & gt; Next=pa;
The else//otherwise will LB chain of the subsequent elements to the new table LC footer
R - & gt; Next=pb;
Free (LB);
Return the LC;
}
Void PrintList (linklist l)
{
Linklist p=l - & gt; Next;
While (p)
{
Printf (" % c \ n ", p - & gt; The data);
P=p - & gt; Next;
}
}
Void main ()
{
Linklist LA, LB, LC;
LA=(linklist) malloc (sizeof (node));
LB=(linklist) malloc (sizeof (node));
LC=(linklist) malloc (sizeof (node));
Initlist (LA);
Initlist (LB);
Initlist (LC);
Createfromhead (LA);
PrintList (LA);
Createfromhead (LB);
PrintList (LB);
Mergelinklist (LA and LB);
PrintList (LC);
}
Where is wrong, why couldn't perform

  • Related