0

Link list

you have to insert n elements in link list and the value of n is entered by user. how to create such link list.incase of array we can declare a big array ,but in case of link list we have to write that much lines.please anyone help me to solve it.

4th Aug 2018, 12:25 PM
Sachin Bhandari
18 Answers
+ 9
Please show us your attempt.
4th Aug 2018, 1:15 PM
A Fox
A Fox - avatar
+ 10
SL is SoloLearn. No, I just solve problems from different sites like CodeSignal. Just trying to become better at solving problems. We should stop this conversation here since chatting in the Q&A is not allowed.
4th Aug 2018, 6:00 PM
A Fox
A Fox - avatar
+ 9
I will try to answer your problems as well as I can. 🙂 As I'm not comfortable with sharing my facebook profile, why not join the SoloLearn Discord server? You can get more (and maybe better) answers to your questions: https://www.sololearn.com/Discuss/689391/?ref=app
4th Aug 2018, 5:31 PM
A Fox
A Fox - avatar
+ 9
I'm not actually. I prefer C++, but I like playing around with stuff from the SL lessons.
4th Aug 2018, 5:52 PM
A Fox
A Fox - avatar
+ 8
You can do: struct node { int data; node *next; } int main(){ node *head, *ptr; head = new node; ptr = head; for (int i = 1; i <= n; i++){ cin >> ptr->data; ptr = ptr->next; } ptr->next = NULL; ptr = NULL; return 0; } And you should use int main() instead of void main() as it isn't C nor C++, as said in this link: http://www.stroustrup.com/bs_faq2.html#void-main
4th Aug 2018, 3:37 PM
A Fox
A Fox - avatar
+ 8
I did declare new node. You don't need to write struct in declaration. We have a member variable data. On each run of the loop, the user enters a new one and it links to the next node. So if n is 5, we scan 5 elements. After the loop, we link the last node to NULL.
4th Aug 2018, 4:20 PM
A Fox
A Fox - avatar
+ 8
Sorry, but I don't know C. My code generates the list at runtime.
4th Aug 2018, 5:13 PM
A Fox
A Fox - avatar
+ 8
Your welcome 🍪
4th Aug 2018, 5:39 PM
A Fox
A Fox - avatar
+ 8
Codecademy had some good courses about web development. Try searching on SL about web development resources, you can find some useful links.
4th Aug 2018, 5:46 PM
A Fox
A Fox - avatar
+ 1
Anime FanGirl thank you for replying.😊 but still I have some questions.. 1-why didn't you declare new node. I didn't get if the value of n is 5 then ...? because we can only scan 2 elements
4th Aug 2018, 3:49 PM
Sachin Bhandari
+ 1
ok thank. you. i m joining h.hope it will be helpful.thank you once again 😊😊
4th Aug 2018, 5:35 PM
Sachin Bhandari
0
Anime FanGirl I have no idea how to start . would you please help me? this is what I know... struct node{ int data; struct node *ptr; }; struct node *p1,*p2,*p3,*head,*temp; void main() { p1=(struct node *) malloc(sizeof(struct node)); p2=(struct node *) malloc(sizeof(struct node)); p3=(struct node *) malloc(sizeof(struct node)); head=p1; p1->ptr=p2; p2->ptr=p3; p3->ptr=NULL; } /*I have created a link list of 3 elements but what if user will enter more than 3 elements .DO WE have to write that much lines of code*/
4th Aug 2018, 2:45 PM
Sachin Bhandari
0
sorry for again disturbing but can you write your code in c. i know that a[100] we can store 100 elements on this array but in linked list we cannot declare its eyes like array so we don't know. I didn't get the concept where you are using loop by loop how can we create a linked list automatically.
4th Aug 2018, 4:25 PM
Sachin Bhandari
0
we should have enough link list to store n elements and we don't know the value of n.
4th Aug 2018, 4:32 PM
Sachin Bhandari
0
ok thank you for your effort.😊😊 keep replying of my problems please..can I contact you in fb?
4th Aug 2018, 5:18 PM
Sachin Bhandari
0
Anime FanGirl can you tell me how to learn web development. like making. website. Every where in online palteform like YouTube they just teach basic(Data types,loop,function,class etc) they don't teach how to make apps,websites .can you suggest me some channels or anything from where I can learn advance in these language so than I can make websites,apps.
4th Aug 2018, 5:40 PM
Sachin Bhandari
0
ok thank you. BTW you seem like a gud web developer
4th Aug 2018, 5:48 PM
Sachin Bhandari
0
what is SL? oh c++ you do competitive programming? preparation for icpc?
4th Aug 2018, 5:54 PM
Sachin Bhandari