473,396 Members | 1,814 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,396 software developers and data experts.

Problem with linked list and strings

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>    /* for printf */
  2. #include <stdlib.h>   /* for malloc */
  3. #include <string.h>
  4.  
  5. struct node
  6. {
  7.     char *data;
  8.     struct node *next; /* pointer to next element in list */
  9. };
  10.  
  11.  
  12.  
  13. struct node *list_add(struct node **p, char *i)
  14. {
  15.     struct node *n = (node *)malloc(sizeof(struct node));
  16.     if (n == NULL)
  17.         return NULL;
  18.  
  19.     n->next = *p; /* the previous element (*p) now becomes the "next" element */
  20.     *p = n;       /* add new empty element to the front (head) of the list */
  21.     n->data = i;
  22.  
  23.     return *p;
  24. }
  25.  
  26.  
  27. void list_print(struct node *n)
  28. {
  29.     if (n == NULL)
  30.     {
  31.         printf("list is empty\n");
  32.     }
  33.     while (n != NULL)
  34.     {
  35.         printf("print %s\n", n->data);
  36.         n = n->next;
  37.     }
  38. }
  39.  
  40. int main(void)
  41. {
  42.  
  43.     int i;
  44.     char buf[1024];
  45.     struct node *n = NULL;
  46.  
  47.     for (i=0;i<5;i++){
  48.  
  49.         gets(buf);
  50.  
  51.         list_add(&n, buf);
  52.      }
  53.  
  54.     //list_add(&n, "aa"); 
  55.     //list_add(&n, "bb"); 
  56.     //list_add(&n, "cc"); 
  57.     //list_add(&n, "dd"); 
  58.     //list_add(&n, "ee"); 
  59.     list_print(n);
  60.  
  61.  
  62.  
  63.  
  64.     return 0;
  65.  
  66. }
  67.  
I have this code and it put to all nodes the last input of gets(). However the code in comments(if i don't use gets()) works correctly.

Any idea?
Oct 16 '10 #1
0 909

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

Similar topics

7
by: Chris Ritchey | last post by:
Hmmm I might scare people away from this one just by the title, or draw people in with a chalange :) I'm writting this program in c++, however I'm using char* instead of the string class, I am...
5
by: Darryl B | last post by:
I can not get anywhere on this project I'm tryin to do. I'm not expecting any major help with this but any would be appreciated. The assignment is attached. The problem I'm having is trying to set...
5
by: John N. | last post by:
Hi All, Here I have a linked list each containing a char and is double linked. Then I have a pointer to an item in that list which is the current insertion point. In this funtion, the user...
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...
6
by: Jim Showalter | last post by:
I'm trying to write code that gets fixed-length strings from the user and then stores them in a linked list. Here's the definition of my list node: struct node {char str; struct node* next; };...
4
by: Rhino | last post by:
I have to make little program with linked list and structs. but I'm a little stuck atm. de task is pritty easy, I need to make a tree and in every node there is a question depending on the anwer u...
3
by: bjhecht | last post by:
http://rafb.net/paste/results/tJoB4z75.html After executing the following code I receive the errors: a.c: In function `insert': a.c:59: error: incompatible types in assignment I have no...
1
by: yaarnick | last post by:
Create a linked list data structure library to hold strings. Then library should support the following linked list operations. Create() – Creates the linked list Insert() – Insert a string into the...
8
by: dmp | last post by:
What are Linked list? Please somebody show some ready made programs of linked list
4
by: paranoidandroid | last post by:
I have a txt file with a list of records for example: John Swift 15 2005 20000 Andrew Smith 25 2001 25000 I am trying to read in this data into a linked list of data structures as follows:
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
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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
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,...

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.