User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 396,965 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,967 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser:
Views: 340 | Replies: 5 | Solved
Reply
Join Date: Apr 2008
Posts: 22
Reputation: Onixtender is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
Onixtender Onixtender is offline Offline
Newbie Poster

Stack problem

  #1  
May 12th, 2008
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?
Last edited by Onixtender : May 12th, 2008 at 3:38 pm.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Feb 2008
Location: Seattle
Posts: 713
Reputation: jephthah is a jewel in the rough jephthah is a jewel in the rough jephthah is a jewel in the rough 
Rep Power: 4
Solved Threads: 45
jephthah's Avatar
jephthah jephthah is offline Offline
Master Poster

Re: Stack problem

  #2  
May 13th, 2008
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.


.
Last edited by jephthah : May 13th, 2008 at 3:32 am. Reason: fire your moderator.
Why so serious?
Reply With Quote  
Join Date: May 2008
Location: Infinite Castle, below Beltline
Posts: 282
Reputation: Prabakar is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 22
Prabakar's Avatar
Prabakar Prabakar is offline Offline
Posting Whiz in Training

Re: Stack problem

  #3  
May 13th, 2008
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.
Reply With Quote  
Join Date: Apr 2008
Posts: 14
Reputation: Abzero is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 3
Abzero Abzero is offline Offline
Newbie Poster

Re: Stack problem

  #4  
May 13th, 2008
think your creating a circular list there.
Reply With Quote  
Join Date: Nov 2007
Posts: 842
Reputation: mitrmkar is a jewel in the rough mitrmkar is a jewel in the rough mitrmkar is a jewel in the rough 
Rep Power: 4
Solved Threads: 172
mitrmkar mitrmkar is offline Offline
Practically a Posting Shark

Re: Stack problem

  #5  
May 13th, 2008
<< 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;
    int len; // 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");
}
Reply With Quote  
Join Date: Apr 2008
Posts: 22
Reputation: Onixtender is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
Onixtender Onixtender is offline Offline
Newbie Poster

Re: Stack problem

  #6  
May 14th, 2008
thx, works great
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 8:24 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC