-
Notifications
You must be signed in to change notification settings - Fork 0
/
numberToLinkedList.cpp
82 lines (73 loc) · 1.95 KB
/
numberToLinkedList.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
OVERVIEW: Given a number convert that to single linked list (each digit as a node).
E.g.: Input: 234, Output: 2->3->4.
INPUTS: A number.
OUTPUT: Create linked list from given number, each digit as a node.
ERROR CASES:
NOTES: For negative numbers ignore negative sign.
*/
#include <stdio.h>
#include <malloc.h>
struct node {
int num;
struct node *next;
};
struct node * numberToLinkedList(int N)
{
// return NULL;
int rem,c=0;int x;
node *p=(struct node*)malloc(sizeof(struct node));//p=NULL;
node *q=(struct node*)malloc(sizeof(struct node));//q=NULL;
//p->next=NULL;
if(N==0)
{
p->next=NULL;p->num=0;
return p;
}
if(N<0)N=-N;
while(N>0)
{
rem=N%10;
if(c==0)
{
//printf("did not enter if:");
node *temp=(struct node*)malloc(sizeof(struct node));//temp=NULL;
temp->num=rem;temp->next=NULL;
p=temp;
//printf("p->num= %d",p->num);//scanf("%d",&x);
c=1;
}
else
{
q=p;
//p->num=rem;
//printf("step1");
/*while(q->next!=NULL)
{
q=q->next;
}
// printf("step2 after loop");
node *temp=(struct node*)malloc(sizeof(struct node));//temp=NULL;
temp->num=rem;temp->next=NULL;
q->next=temp;printf("q->num= %d",q->num); */
node *temp=(struct node*)malloc(sizeof(struct node));
temp->num=rem;temp->next=p;
p=temp;
}N=N/10;//printf("the value of n is %d",N);
}
return p;
}
/*int main()
{
node *p=(struct node*)malloc(sizeof(struct node));
node *q=(struct node*)malloc(sizeof(struct node));p->next=q;p->num=2;q->next=NULL;q->num=3;
p=numberToLinkedList(3458);
q=p;
while(q->next!=NULL)
{
printf("%d ",q->num);q=q->next;
}
//printf("%d",q->num);
//printf("loop exited");
return 0;
}*/