473,387 Members | 1,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Simple Linked List

Hi.
I am beginner in a linked list programming and i need some advices.
First of all, what is the diferences between :

Expand|Select|Wrap|Line Numbers
  1. typedef struct node
  2. {
  3.     int data;
  4.     struct node *next;
  5. };
and

Expand|Select|Wrap|Line Numbers
  1. struct node
  2. {
  3.     int data;
  4.     struct node *next;
  5. }*head;
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.

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<malloc.h>
  4.  
  5. typedef struct node
  6. {
  7.     int data;
  8.     struct node *next;
  9. };
  10.  
  11. int main()
  12. {
  13.     struct node *head;
  14.     Create_List(head);
  15.     Print_List(head);
  16.     Number_of_Key(head);
  17. }
  18.  
  19. void Create_List(struct node *head)
  20. {
  21.     int alege = 1;
  22.     do
  23.     {
  24.         struct node *new_node, *current = NULL;
  25.         head = NULL;
  26.         new_node = (struct node *)malloc(sizeof(struct node));
  27.         printf("\n Enter the data : ");
  28.         scanf("%d", &new_node->data);
  29.         if(head == NULL)
  30.         {
  31.             head = new_node;
  32.             current = new_node;
  33.         }
  34.         else
  35.         {
  36.             current->next = new_node;
  37.             current = new_node;
  38.         }
  39.         printf("\n Do you want to creat another element ? (Apasa \"1\" sau \"0\") : ");
  40.         scanf("%d", &alege);
  41.     } while(alege != 0);
  42. }
  43.  
  44. void Print_List(struct node *head)
  45. {
  46.     struct node *temp;
  47.     temp = head;
  48.     printf("\n The Linked List : ");
  49.     while(temp != NULL)
  50.     {
  51.         printf(" %d ->", temp->data);
  52.         temp = temp->next;
  53.     }
  54.     printf(" NULL");
  55.     printf("\n");
  56. }
  57.  
  58. void Number_of_Key(struct node *head)
  59. {
  60.     struct node *current;
  61.     int key, counter = 0;
  62.     printf("\n Enter the key number : ");
  63.     scanf("%d", &key);
  64.     current = head;
  65.     while(current != NULL)
  66.     {
  67.         if(current->data == key)
  68.             counter++;
  69.         current = current->next;
  70.     }
  71.     printf("\n The key number \"%d\" is in the list %d times!\n", key, counter);
  72. }
Feb 19 '14 #1
2 1550
Banfa
9,065 Expert Mod 8TB
Second structure also creates a variable, head, a pointer to node.

Your code doesn't work because in Print_List and Number_of_Key you assume that the end of the list is indicated by a NULL next pointer (which is the standard way to do it) but in Create_List you never set the next pointer to NULL for any node, and especially, not the last one.
Feb 20 '14 #2
donbock
2,426 Expert 2GB
Your first snippet shows an improper use of "typedef". The purpose of typedef is to define a new type that is equivalent to some old type. The syntax is
Expand|Select|Wrap|Line Numbers
  1. typedef oldtype newtype;
In your example, everything from "struct" to "}" is the oldtype. You don't have a newtype. I'm surprised you didn't get a compiler error or warning. Your particular oldtype has the side effect of defining a struct named "node". That's why there is no difference between those first two code snippets (other than the declaration of variable "head").

You should always check if malloc failed (return value is NULL). Line 26.
Feb 21 '14 #3

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

Similar topics

5
by: disco | last post by:
I am working on this example from a book "C Primer Plus" by Prata 4th edition - p. 672. There is no erata on this problem at the publisher's website. 1) Is it a violation of copyright laws to...
13
by: na1paj | last post by:
here's a simple linked list program. the DeleteNode function is producing an infinit loop i think, but i can't figure out where.. #include <stdio.h> typedef struct { char *str; //str is a...
10
by: Ben | last post by:
Hi, I am a newbie with C and am trying to get a simple linked list working for my program. The structure of each linked list stores the char *data and *next referencing to the next link. The...
1
by: Tim | last post by:
I can't seem to figure out why this very simple linked list wont build.. I mean, there is no intelligence, just add to end. Anyway, please let me know if something i can do will make head (the...
1
by: drewy2k12 | last post by:
Heres the story, I have to create a doubly linked list for class, and i have no clue on how to do it, i can barely create a single linked list. It has to have both a head and a tail pointer, and...
4
by: eksamor | last post by:
I have a simple linked list: struct element { struct element *next; int start; }; struct list { struct element *head;
1
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...
1
by: jyothiram | last post by:
write short notes on linked list resentation using arrays,pointers and cursors?
4
by: phe2003 | last post by:
Hi All, I've been testing some extremely simple code about a linked list. What I keep doing is actually writing some parts of a linked list and testing them. Below are my codes:...
0
by: babe20042004 | last post by:
My function round1(boolean qualify, int goals) is supposed to go through a list of nodes comparing every 2 side by side nodes for the highest goals and then has the boolean variable qualify changed...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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...

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.