Home > Back-end >  Binary plus one
Binary plus one

Time:10-28

#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 ");
L=createfromhead (l);
PrintList (l);
Printf (" results ");
Binadd (l);
PrintList (l);
}
Typedef struct Node
{
The int data;
Struct Node * next;
} Node, * LinkList;
Int CreatLinkList LinkList (L)//stern interpolation built table
{
Int ch;
Node * s;
Node * r;
R=L;
While (true)
{
Cin> Ch;
If (ch==999)
break;
The else
{
S=new Node;
S - & gt; data=https://bbs.csdn.net/topics/ch;
R - & gt; Next=s;
R=s;
}
R - & gt; Next=NULL;//this is not less, less would collapse
}
return 0;
}
Int the add (LinkList L)//plus one principle: from the first position to the high of 0, changed the 0 to 1, and then put back all status is changed to 0
{
The Node * p, * r;
R=L;
P=L - & gt; next;
while(p!=NULL)
{
If (p - & gt; data=https://bbs.csdn.net/topics/=0)
R=p;
P=p - & gt; next;
}
If (r!=L)//in a binary number that have 0
{
R - & gt; data=https://bbs.csdn.net/topics/1;
While (r - & gt; Next!=NULL)
{
R - & gt; Next - & gt; data=https://bbs.csdn.net/topics/0;
R=r - & gt; next;
}
R - & gt; data=https://bbs.csdn.net/topics/0;
}
The else
{
Node * temp;
Temp=new Node;
Temp - & gt; Next=L - & gt; next;
R - & gt; Next=temp;
Temp - & gt; data=https://bbs.csdn.net/topics/1;
R=temp - & gt; next;
While (r!=NULL)
{
R - & gt; data=https://bbs.csdn.net/topics/0;
R=r - & gt; next;
}
}
return 0;
}
Int main ()
{
LinkList L1.
L1=new Node;
L1 - & gt; Next=NULL;
CreatLinkList (L1);
Add (L1);
Node * p;
P=L1 - & gt; next;
while(p!=NULL)
{
Cout<& lt; P - & gt; Data<& lt;" ";
P=p - & gt; next;
}
return 0;

}