Hi, I've have a problem of printing data from stack, if it's simple words,then stack works just fine,but if I try to push changed data using function(Written by a very intelligent moderator :) ), I get just the same word n times:
Intelect
Intelect
Intelect
....

#include<stdio.h>
#include<stdlib.h>
#include <string.h>
//-------------------Function--------------------------------------------
char AD(char *record, char *record_out)
{
    char *p1 = record;
    char *p2 = record_out;

     while( *p1 )
    {
        // use temp pointer to locate
        // end of digits
        char* p3 = p1;
        while( isdigit(*p3) )
            p3++;
        // is there a space following the digits ?
        if( *p3 == ' ')
            // yes, then skip the space and move on to the next char
            p1 = p3+1;
        else
        {
            // no, then was the last char put into temp buffer
            // a space
            if( *(p2-1) == ' ')
                // yes, then overwrite that space with a new character
                p2--;
            p1 = p3;
        }
        // copy all characters up to the next digit
        while(*p1 && !isdigit(*p1) )
            *p2++ = *p1++;
    }
   
}
//------------------------------------------------------------------------------
struct stack {
       char *d;//data
       struct stack *next;//pointer
              };                

typedef struct stack ELEMENT; 
typedef ELEMENT *POINTER;
 
 
 
void push(POINTER *Top, char *a)
/* put a into the top of the stack */
{
POINTER temp;
temp = malloc(sizeof(ELEMENT));
temp->d= a;  
temp->next = *Top;
*Top = temp;
}

void print_stack(POINTER Top){
POINTER copy= Top;
while(copy!=NULL)
{                 
printf("%s\n",copy->d);
copy=copy->next;
}
}

int main()
{
   POINTER start = NULL;
   char *DELIMITERS = " ";  
	char *str,record[100],*o;
	char line[]="jjj   yyyy dfsfsd sdfsdf sdfsdfsdf sdfsdfsfd sdfasdfafs Intelect";
	
	str = strtok(line, DELIMITERS);
    char recordd[sizeof(record)] = {0};

while (str)
		{
  AD(str, recordd);             //Changing word with function(actually line,but if it works with word,then...good)
                                // printf("%s\n",recordd);   
  o = recordd;                   
            
  
  // str=o;              // But if I just try to push changed word(With function which IS needed) function
                                       // print_stack(start) prints stack for n times ????
  
  push(&start,str); //push the word ... and this part works!!!
  
  
  
  str = strtok(NULL, DELIMITERS);
		}
         
print_stack(start); //print stack..works, print changed words - don't...
  

system("pause");
}

So, should I try to change stack or maybe there is another/easier way?

Recommended Answers

All 5 Replies

thats one of the most godawful implementations of a stack ive ever seen.

a stack is supposed to be simple:

PUSH and POP.

that's it.


.

If you use dynamic allocation because I told you, then I am sorry, because I ment to use dynamic allocation for the random array. Since, every time you allocate a cluster of memory from the heap and is bound to be different form the previous one.

think your creating a circular list there.

<< if I try to push changed data using function, I get just the same word n times:
That happens because you are pushing pointers to one single buffer you have in your whole program, and that buffer is char recordd[].
In other words, you can write to that buffer as many times as you wish, but the data that was last written to it, is the only data you ever can get out of it.

So, you need to allocate dynamic memory into which you copy the AD()'s output, that you eventually push onto the stack. Maybe you get the idea from the following ...

int main()
{
    POINTER start = NULL;
    char *DELIMITERS = " ";  
    char *str,record[100],*o;
    [B]int len;[/B] // holds the length of string that will be pushed
    char line[]="jjj   yyyy dfsfsd sdfsdf sdfsdfsdf sdfsdfsfd sdfasdfafs Intelect";
	
    str = strtok(line, DELIMITERS);
    // Consider recordd as just an intermediate work buffer 
    // for the AD() function
    char recordd[sizeof(record)] = {0};

    while (str)
    {
        // Pass the input to AD()
        AD(str, recordd);
        // Get the length of string and account for a terminating null char
        len = 1 + strlen(recordd);
        if(len > 1) // did AD() give some output?
        {
             // Allocate the memory needed to hold the string to push
             char * temp = (char *)malloc(len * sizeof(char));
             if(temp)
             {
                 // malloc() succeeded, now copy the data from recordd 
                 strcpy(temp, recordd);
                 // and push it
                 push(&start, temp);
             }
        }
        str = strtok(NULL, DELIMITERS);
    }
    print_stack(start);
    system("pause");
}

thx, works great

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.