Hi.
I am beginner in a linked list programming and i need some advices.
First of all, what is the diferences between :
and
Now, I have this problem to count the number of nodes from a linked list.
I have passed the head of linked list every function, i dont know why but it dont works.
I am beginner in a linked list programming and i need some advices.
First of all, what is the diferences between :
Code:
typedef struct node
{
int data;
struct node *next;
};
Code:
struct node
{
int data;
struct node *next;
}*head;
I have passed the head of linked list every function, i dont know why but it dont works.
Code:
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
typedef struct node
{
int data;
struct node *next;
};
int main()
{
struct node *head;
Create_List(head);
Print_List(head);
Number_of_Key(head);
}
void Create_List(struct node *head)
{
int alege = 1;
do
{
struct node *new_node, *current = NULL;
head = NULL;
new_node = (struct node *)malloc(sizeof(struct node));
printf("\n Enter the data : ");
scanf("%d", &new_node->data);
if(head == NULL)
{
head = new_node;
current = new_node;
}
else
{
current->next = new_node;
current = new_node;
}
printf("\n Do you want to creat another element ? (Apasa \"1\" sau \"0\") : ");
scanf("%d", &alege);
} while(alege != 0);
}
void Print_List(struct node *head)
{
struct node *temp;
temp = head;
printf("\n The Linked List : ");
while(temp != NULL)
{
printf(" %d ->", temp->data);
temp = temp->next;
}
printf(" NULL");
printf("\n");
}
void Number_of_Key(struct node *head)
{
struct node *current;
int key, counter = 0;
printf("\n Enter the key number : ");
scanf("%d", &key);
current = head;
while(current != NULL)
{
if(current->data == key)
counter++;
current = current->next;
}
printf("\n The key number \"%d\" is in the list %d times!\n", key, counter);
}
Comment