Home > Back-end >  (guo-hua geng) singly linked list data structure of binary plus one
(guo-hua geng) singly linked list data structure of binary plus one

Time:10-26

//binary add a singly linked list
#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!='2')
{
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;

}
}
//singly linked lists of binary + 1 operation
Void Binadd linklist (l)
//to take the lead node singly linked lists to store a binary number l, add 1 operation
{
Q, the node * * r * s;
Q=l - & gt; next;
R=l;
While (q!=NULL)
//to find the last node of a range of 0
{
If (q - & gt; data=https://bbs.csdn.net/topics/=0)
R=q;
Q=q - & gt; next;
}
If (r!=l)
R - & gt; Data=https://bbs.csdn.net/topics/1;/* will be the final value of the node of a range of 0 assigned to 1 */
//was not found the range of 0 node
The else
{
S=(*) malloc (sizeof (node));/* to apply for a new node storing the highest carry */
S - & gt; Data=https://bbs.csdn.net/topics/1;/* domain assigned to 1 */
S - & gt; Next=l - & gt; next;
L - & gt; Next=s;/* */
after insert an end nodeR=s;
}
R=r - & gt; next;
While (r!=NULL)//to the back of the assignment of the value of the city for all nodes 0
{
R - & gt; data=https://bbs.csdn.net/topics/0;
R=r - & gt; next;
}
}
Void PrintList (linklist l)
{
Linklist p=l - & gt; next;
While (p)
{
Printf (" % c ", p - & gt; The data);
P=p - & gt; next;
printf("\n");
}
}
Void main ()
{
Linklist l;
Initlist (& amp; L);
Printf (" input list element \ n ");
Createfromhead (l);
PrintList (l);
Printf (" results ");
Binadd (l);
PrintList (l);
}







How singly linked list is used to implement binary plus one operation ah, entitled, and establish a leading the linear list of nodes, to keep input binary Numbers, each node in the linked list to store a binary data domain, and on this list for binary number + 1 operation
  • Related