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.
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.
Code:
Inserting a new node at the end of the list.
The new node inserted is the last one in the list.
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct node
{
int numar;
struct node *next;
};
struct node *Create_list()
{
struct node *new_node, *first = NULL, *temp = NULL;
int n;
char choice = 1;
while(choice == 1)
{
new_node = (struct node *)malloc(sizeof(struct node));
if(new_node == NULL)
{
printf("\n Node creation failed!\n");
return NULL;
}
printf("\n Introduceti elementele : ");
scanf("%d", &new_node->numar);
if(first != NULL)
{
temp->next = new_node;
temp = new_node;
}
else
first = temp = new_node;
printf("\n Vreti sa continuati (Apasa \"1\" sau \"0\") ? ");
scanf("%d", &choice);
}
temp->next = NULL;
temp = first;
return temp;
}
void Print_list(struct node *new_node)
{
struct node *temp;
temp = new_node;
printf("\n\n\n Lista liniara simplu inlantuita : \n\n");
while(temp != NULL)
{
printf(" %d ->", temp->numar);
temp = temp->next;
}
printf(" NULL\n\n");
}
int main()
{
struct node *new_node;
new_node = Create_list();
Print_list(new_node);
}
Comment