?
Solved

problem implementing a stack (using character strings as data)

Posted on 2003-03-21
9
Medium Priority
?
211 Views
Last Modified: 2010-04-15
Hi,

I am trying to implement a stack (Last in ,first out). I enter
"To"
"Test"
"Ok"

but when I try to print, instead of printing "Ok", "Test" and "To"
I get "Ok", "Ok", "Ok". It seems word (after calling gets(word)) address remains same for all iterations
Can anyone help me fix the code ?. Attached is the code

#include <stdlib.h>
#include <string.h>
#include <stdio.h>


struct ListNode;
struct ListNode * newList ( );
struct ListNode * cons (char *, struct ListNode *);
void print (struct ListNode *);


struct ListNode  
     {
     char *name;   /* points to the name */
    struct ListNode *next; /*next list */
     };

/* Return an empty linked list. */
struct ListNode * newList ( ) {
     return 0;
     }


/* Return the result of adding the given string to the front of the given list. */
struct ListNode * cons (char *s, struct ListNode * list) {

     struct ListNode *newnode;
     newnode =(struct ListNode *) malloc(sizeof(ListNode)) ; //allocate memory from heap
     
     newnode->name =s; // point the name to the new string
     newnode->next =list; // set next pointer to list;

     return newnode;

     }
/* Print the names in the given linked list, one per line. */


void print (struct ListNode * list) {
     if( list!= 0) {
          printf ("%s\n", list->name);
          print ((list->next));
          }
         
 
// TEST PROGRAM TO TEST STACK

int main ( ) {
     char word[100];
     printf ("Collecting names into a list.\n");
     struct ListNode * list = newList ( );
     while (gets(word)) {
     //printf("%lu\n", word);
      list = cons (word, list);
     }
     printf ("Printing the names in the list.\n");
    print(list);
//     print (&list);
   
     return 0;
}



0
Comment
Question by:cplus2java
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 

Author Comment

by:cplus2java
ID: 8182885
how do I fix the above code to get the right output ?
0
 

Expert Comment

by:posternb
ID: 8182915
Each of you list nodes is POINTING to the char word[100] array, so each and every node will "contain" the string that's in the word buffer, i.e. the last string entered.

When you create a new node, you need to allocate memory and copy the the string, so that each node has a unique copy of it's string.
0
 

Accepted Solution

by:
posternb earned 225 total points
ID: 8182927
Like this:

/* Return the result of adding the given string to the front of the given list. */
struct ListNode * cons (char *s, struct ListNode * list) {

    struct ListNode *newnode;
    newnode =(struct ListNode *) malloc(sizeof(ListNode)) ; //allocate memory from heap
   
    newnode->name = (char *)malloc( sizeof(char) * (strlen(s)+1) );
    strcpy( newnode->name, s );
    newnode->next =list; // set next pointer to list;

    return newnode;

    }


You should remember to free this memory at some later time...
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 6

Expert Comment

by:gj62
ID: 8182940
You don't allocate any string space in your list - all the strings point to only one place - the variable word!

Change your cons function as follows:

struct ListNode * cons (char *s, struct ListNode * list) {

    struct ListNode *newnode;
    newnode =(struct ListNode *) malloc(sizeof(ListNode)) ; //allocate memory from heap


/*MALLOC string space, copy string to that new space using strcpy *?
    newnode->name = (char *)malloc(strlen(s)+1);
    strcpy(newnode->name,s); // point the name to the new string


    newnode->next =list; // set next pointer to list;

    return newnode;

    }
0
 
LVL 6

Expert Comment

by:gj62
ID: 8182954
sorry posternb,

took me too long to type... <grin>

BTW, sizeof(char) is redundant - by definition, it is always 1.  Doesn't hurt, but it is not needed.
0
 

Expert Comment

by:posternb
ID: 8183048
Yes I know, I think it's good to be explicit for clarity though -- especially if, for example, someone copies the code and changes them it 'int'... you might forget it...

0
 

Expert Comment

by:posternb
ID: 8183051
er to 'int'
0
 

Author Comment

by:cplus2java
ID: 8183202
That definitely makes a lot of sense. Thanks for your help.
0
 
LVL 6

Expert Comment

by:gj62
ID: 8186282
You should accept posternb's answer and not leave this question open...
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question