473,499 Members | 1,568 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Linked List

4 New Member
Hi.
I am a beginner in linked list programming.
I have made a program, but I am not sure if it is a good implementation.
The code compile and the results are very good, like i expected.
I want to give me some advices.
Thank you.

Expand|Select|Wrap|Line Numbers
  1.  Inserting a new node at the end of the list.
  2.  The new node inserted is the last one in the list.
  3.  
  4. #include<stdio.h>
  5. #include<stdlib.h>
  6. #include<malloc.h>
  7.  
  8. struct node
  9. {
  10.     int numar;
  11.     struct node *next;
  12. };
  13.  
  14. struct node *Create_list()
  15. {
  16.     struct node *new_node, *first = NULL, *temp = NULL;
  17.     int n;
  18.     char choice = 1;
  19.     while(choice == 1)
  20.     {
  21.         new_node  = (struct node *)malloc(sizeof(struct node));
  22.         if(new_node == NULL)
  23.         {
  24.             printf("\n Node creation failed!\n");
  25.             return NULL;
  26.         }
  27.         printf("\n Introduceti elementele : ");
  28.         scanf("%d", &new_node->numar);
  29.         if(first != NULL)
  30.         {
  31.             temp->next = new_node;
  32.             temp = new_node;
  33.         }
  34.         else
  35.             first = temp = new_node;
  36.         printf("\n Vreti sa continuati (Apasa \"1\" sau \"0\") ?  ");
  37.         scanf("%d", &choice);
  38.     }
  39.     temp->next = NULL;
  40.     temp = first;
  41.     return temp;
  42. }
  43.  
  44. void Print_list(struct node *new_node)
  45. {
  46.     struct node *temp;
  47.     temp = new_node;
  48.     printf("\n\n\n Lista liniara simplu inlantuita : \n\n");
  49.     while(temp != NULL)
  50.     {
  51.         printf(" %d ->", temp->numar);
  52.         temp = temp->next;
  53.     }
  54.     printf(" NULL\n\n");
  55. }
  56.  
  57. int main()
  58. {
  59.     struct node *new_node;
  60.     new_node = Create_list();
  61.     Print_list(new_node);
  62. }
  63.  
Feb 27 '14 #1
1 1082
Banfa
9,065 Recognized Expert Moderator Expert
Line 29 test temp not first, temp is what you are about to de-reference.

Line 40 - 41, don't bother copying first to temp, just return first it will be clearer what is going on.

In a more general sense I would split the collection of data (using printf and scanf away from the logic of adding a new node. I would have a function (Add_node(int value)) that allocated a new node with the supplied value and added it to the list. Then Create_list would do all the use interface stuff and call Add_node to add nodes to the list as required.

Spliting your program logic away from your user interface code makes it easier to re-use your program logic in new situations.
Feb 28 '14 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

5
7931
by: Jeffrey Silverman | last post by:
Hi, all. I have a linked list. I need an algorithm to create a tree structure from that list. Basically, I want to turn this: $list = array( array( 'id' => 'A', 'parent_id' => null, 'value'...
11
3074
by: C++fan | last post by:
Suppose that I define the following class: class example_class{ public: example_class(); void funtion_1(); void function_2(); protected:
5
859
by: Dream Catcher | last post by:
1. I don't know once the node is located, how to return that node. Should I return pointer to that node or should I return the struct of that node. 2. Also how to do the fn call in main for that...
10
15098
by: Kent | last post by:
Hi! I want to store data (of enemys in a game) as a linked list, each node will look something like the following: struct node { double x,y; // x and y position coordinates struct enemy...
19
13538
by: RAJASEKHAR KONDABALA | last post by:
Hi, Does anybody know what the fastest way is to "search for a value in a singly-linked list from its tail" as oposed to its head? I am talking about a non-circular singly-linked list, i.e.,...
6
4580
by: Steve Lambert | last post by:
Hi, I've knocked up a number of small routines to create and manipulate a linked list of any structure. If anyone could take a look at this code and give me their opinion and details of any...
12
3927
by: joshd | last post by:
Hello, Im sorry if this question has been asked before, but I did search before posting and couldnt find an answer to my problem. I have two classes each with corresponding linked lists, list1...
1
15496
by: theeverdead | last post by:
Ok I have a file in it is a record of a persons first and last name. Format is like: Trevor Johnson Kevin Smith Allan Harris I need to read that file into program and then turn it into a linked...
0
8606
by: Atos | last post by:
SINGLE-LINKED LIST Let's start with the simplest kind of linked list : the single-linked list which only has one link per node. That node except from the data it contains, which might be...
7
5756
by: QiongZ | last post by:
Hi, I just recently started studying C++ and basically copied an example in the textbook into VS2008, but it doesn't compile. I tried to modify the code by eliminating all the templates then it...
0
7132
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7009
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7178
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7223
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7390
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5475
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4602
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3094
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
302
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.