-
Notifications
You must be signed in to change notification settings - Fork 2
/
friend_network.c
185 lines (156 loc) · 2.73 KB
/
friend_network.c
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
//Roll no 287
#include<stdio.h>
int ctr = 0;
typedef struct node
{
int id, comments, date, month, year;
char name[20];
struct node *next;
}node;
typedef struct rootnode
{
int ctr;
struct node *users[100];
}rootnode;
node *getnode()
{
node *p=NULL;
p=(node*)malloc(sizeof(node));
printf("Give ID\n");
scanf("%d",p->id);
printf("Enter name\n");
scanf("%s",p->name);
p->next=NULL;
return(p);
}
node *getRootNode()
{
int i;
rootnode *p=NULL;
p=(rootnode*)malloc(sizeof(rootnode));
p->ctr = 0;
for(i=0;i<100;i++) p->users[i]=NULL;
return(p);
}// getnode
node *create(rootnode *start,int n)
{
node *p=NULL,*t=NULL;
int i;
for(i=0;i<n;i++)
{
p=getnode();
printf("\npuk");
start->users[ctr] = p;
ctr++;
}//for
return(start);
}//create
int display(node *start)
{
node *p=NULL;
printf("\nFacebook console:");
printf("\nStart->");
p=start;
while(p!=NULL)
{
printf("%s->",p->name);
p=p->next;
}//while
printf("NULL");
}// display
int displayNet(rootnode *start)
{
int i;
node *p=NULL;
printf("\nFacebook console:");
printf("\nUsers->");
p=start;
for(i=0;i<ctr;i++){
printf("--%s", start->users[i]->name);
}
printf(".");
}// display
int search(int num, rootnode *start)
{
int i,loc=0;
struct rootnode *p;
p=start;
for(i=0;i<ctr;i++){
if(p->users[i]->id == num){
return i;
}
}
return 0;
}
void addFriend(rootnode *root){
int i,id,fid,res,fres;
node *p=NULL;
printf("\nEnter the id of the user, to which you want to assign this friend");
scanf("%d", id);
res = search(id, root);
if(res==0){
printf("\nID doesn't exist");
}else{
printf("\nEnter friend ID");
scanf("%d", fid);
fres = search(id, root);
if(fres==0){
printf("\nID doesn't exist");
}else{
p = getnode();
p->id = root->users[fres]->id;
strcpy(p->name, root->users[fres]->name);
p->next = NULL;
//Assigning friend
root->users[res]->next = p;
}
}
}
int main()
{
int num,ch;
node * start=NULL;
rootnode *root;
root = getRootNode();
system("cls");
printf("\n\nLet's create a user list first, enter number of users - ");
scanf("%d",&num);
root=create(root,num);
displayNet(root);
while(1)
{
printf("\n -1 Add User\n -2 Add Friends to ID\n -3 Exit");
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
root=create(root,1);
displayNet(root);
break;
case 2:
addFriend(root);
break;
case 4:
display(root->users[0]);
break;
/*
case 4:
start=reverse(start);
display(start);
break;
case 5:
revert(start);
printf("NULL");
break;
case 6:
printf("\nEnter the number of node:");
scanf("%d",&num);
search(num,start);
break; */
case 3:
exit(0);
}//switch
}//while
return(0);
}